Sun, 31 Aug 2014 16:14:36 +0400
8036981: JAXB not preserving formatting for xsd:any Mixed content
Reviewed-by: lancea, mkos
1 /*
2 * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
26 package com.sun.xml.internal.bind.v2.runtime;
28 import java.io.IOException;
29 import java.io.InputStream;
30 import java.io.OutputStream;
32 import javax.xml.bind.JAXBElement;
33 import javax.xml.bind.JAXBException;
34 import javax.xml.bind.Marshaller;
35 import javax.xml.bind.Unmarshaller;
36 import javax.xml.namespace.NamespaceContext;
37 import javax.xml.stream.XMLStreamException;
38 import javax.xml.stream.XMLStreamReader;
39 import javax.xml.stream.XMLStreamWriter;
40 import javax.xml.transform.Result;
41 import javax.xml.transform.Source;
43 import com.sun.istack.internal.NotNull;
44 import com.sun.xml.internal.bind.api.Bridge;
45 import com.sun.xml.internal.bind.api.TypeReference;
46 import com.sun.xml.internal.bind.marshaller.SAX2DOMEx;
47 import com.sun.xml.internal.bind.v2.runtime.output.SAXOutput;
48 import com.sun.xml.internal.bind.v2.runtime.output.XMLStreamWriterOutput;
49 import com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl;
51 import org.w3c.dom.Node;
52 import org.xml.sax.ContentHandler;
53 import org.xml.sax.SAXException;
55 /**
56 * {@link Bridge} implementaiton.
57 *
58 * @author Kohsuke Kawaguchi
59 */
60 final class BridgeImpl<T> extends InternalBridge<T> {
62 /**
63 * Tag name associated with this {@link Bridge}.
64 * Used for marshalling.
65 */
66 private final Name tagName;
67 private final JaxBeanInfo<T> bi;
68 private final TypeReference typeRef;
70 public BridgeImpl(JAXBContextImpl context, Name tagName, JaxBeanInfo<T> bi,TypeReference typeRef) {
71 super(context);
72 this.tagName = tagName;
73 this.bi = bi;
74 this.typeRef = typeRef;
75 }
77 public void marshal(Marshaller _m, T t, XMLStreamWriter output) throws JAXBException {
78 MarshallerImpl m = (MarshallerImpl)_m;
79 m.write(tagName,bi,t,XMLStreamWriterOutput.create(output,context),new StAXPostInitAction(output,m.serializer));
80 }
82 public void marshal(Marshaller _m, T t, OutputStream output, NamespaceContext nsContext) throws JAXBException {
83 MarshallerImpl m = (MarshallerImpl)_m;
85 Runnable pia = null;
86 if(nsContext!=null)
87 pia = new StAXPostInitAction(nsContext,m.serializer);
89 m.write(tagName,bi,t,m.createWriter(output),pia);
90 }
92 public void marshal(Marshaller _m, T t, Node output) throws JAXBException {
93 MarshallerImpl m = (MarshallerImpl)_m;
94 m.write(tagName,bi,t,new SAXOutput(new SAX2DOMEx(output)),new DomPostInitAction(output,m.serializer));
95 }
97 public void marshal(Marshaller _m, T t, ContentHandler contentHandler) throws JAXBException {
98 MarshallerImpl m = (MarshallerImpl)_m;
99 m.write(tagName,bi,t,new SAXOutput(contentHandler),null);
100 }
102 public void marshal(Marshaller _m, T t, Result result) throws JAXBException {
103 MarshallerImpl m = (MarshallerImpl)_m;
104 m.write(tagName,bi,t, m.createXmlOutput(result),m.createPostInitAction(result));
105 }
107 public @NotNull T unmarshal(Unmarshaller _u, XMLStreamReader in) throws JAXBException {
108 UnmarshallerImpl u = (UnmarshallerImpl)_u;
109 return ((JAXBElement<T>)u.unmarshal0(in,bi)).getValue();
110 }
112 public @NotNull T unmarshal(Unmarshaller _u, Source in) throws JAXBException {
113 UnmarshallerImpl u = (UnmarshallerImpl)_u;
114 return ((JAXBElement<T>)u.unmarshal0(in,bi)).getValue();
115 }
117 public @NotNull T unmarshal(Unmarshaller _u, InputStream in) throws JAXBException {
118 UnmarshallerImpl u = (UnmarshallerImpl)_u;
119 return ((JAXBElement<T>)u.unmarshal0(in,bi)).getValue();
120 }
122 public @NotNull T unmarshal(Unmarshaller _u, Node n) throws JAXBException {
123 UnmarshallerImpl u = (UnmarshallerImpl)_u;
124 return ((JAXBElement<T>)u.unmarshal0(n,bi)).getValue();
125 }
127 public TypeReference getTypeReference() {
128 return typeRef;
129 }
131 public void marshal(T value, XMLSerializer out) throws IOException, SAXException, XMLStreamException {
132 out.startElement(tagName,null);
133 if(value==null) {
134 out.writeXsiNilTrue();
135 } else {
136 out.childAsXsiType(value,null,bi,false);
137 }
138 out.endElement();
139 }
141 }