001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common Development
008: * and Distribution License("CDDL") (collectively, the "License"). You
009: * may not use this file except in compliance with the License. You can obtain
010: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
011: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
012: * language governing permissions and limitations under the License.
013: *
014: * When distributing the software, include this License Header Notice in each
015: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
016: * Sun designates this particular file as subject to the "Classpath" exception
017: * as provided by Sun in the GPL Version 2 section of the License file that
018: * accompanied this code. If applicable, add the following below the License
019: * Header, with the fields enclosed by brackets [] replaced by your own
020: * identifying information: "Portions Copyrighted [year]
021: * [name of copyright owner]"
022: *
023: * Contributor(s):
024: *
025: * If you wish your version of this file to be governed by only the CDDL or
026: * only the GPL Version 2, indicate your decision by adding "[Contributor]
027: * elects to include this software in this distribution under the [CDDL or GPL
028: * Version 2] license." If you don't indicate a single choice of license, a
029: * recipient has the option to distribute your version of this file under
030: * either the CDDL, the GPL Version 2 or to extend the choice of license to
031: * its licensees as provided above. However, if you add GPL Version 2 code
032: * and therefore, elected the GPL Version 2 license, then the option applies
033: * only if the new code is made subject to such option by the copyright
034: * holder.
035: */
036:
037: package com.sun.xml.bind.v2.runtime;
038:
039: import java.io.IOException;
040: import java.io.InputStream;
041: import java.io.OutputStream;
042:
043: import javax.xml.bind.JAXBElement;
044: import javax.xml.bind.JAXBException;
045: import javax.xml.bind.Marshaller;
046: import javax.xml.bind.Unmarshaller;
047: import javax.xml.namespace.NamespaceContext;
048: import javax.xml.stream.XMLStreamException;
049: import javax.xml.stream.XMLStreamReader;
050: import javax.xml.stream.XMLStreamWriter;
051: import javax.xml.transform.Result;
052: import javax.xml.transform.Source;
053:
054: import com.sun.istack.NotNull;
055: import com.sun.xml.bind.api.Bridge;
056: import com.sun.xml.bind.api.TypeReference;
057: import com.sun.xml.bind.marshaller.SAX2DOMEx;
058: import com.sun.xml.bind.v2.runtime.output.SAXOutput;
059: import com.sun.xml.bind.v2.runtime.output.XMLStreamWriterOutput;
060: import com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl;
061:
062: import org.w3c.dom.Node;
063: import org.xml.sax.ContentHandler;
064: import org.xml.sax.SAXException;
065:
066: /**
067: * {@link Bridge} implementaiton.
068: *
069: * @author Kohsuke Kawaguchi
070: */
071: final class BridgeImpl<T> extends InternalBridge<T> {
072:
073: /**
074: * Tag name associated with this {@link Bridge}.
075: * Used for marshalling.
076: */
077: private final Name tagName;
078: private final JaxBeanInfo<T> bi;
079: private final TypeReference typeRef;
080:
081: public BridgeImpl(JAXBContextImpl context, Name tagName,
082: JaxBeanInfo<T> bi, TypeReference typeRef) {
083: super (context);
084: this .tagName = tagName;
085: this .bi = bi;
086: this .typeRef = typeRef;
087: }
088:
089: public void marshal(Marshaller _m, T t, XMLStreamWriter output)
090: throws JAXBException {
091: MarshallerImpl m = (MarshallerImpl) _m;
092: m.write(tagName, bi, t, XMLStreamWriterOutput.create(output,
093: context), new StAXPostInitAction(output, m.serializer));
094: }
095:
096: public void marshal(Marshaller _m, T t, OutputStream output,
097: NamespaceContext nsContext) throws JAXBException {
098: MarshallerImpl m = (MarshallerImpl) _m;
099:
100: Runnable pia = null;
101: if (nsContext != null)
102: pia = new StAXPostInitAction(nsContext, m.serializer);
103:
104: m.write(tagName, bi, t, m.createWriter(output), pia);
105: }
106:
107: public void marshal(Marshaller _m, T t, Node output)
108: throws JAXBException {
109: MarshallerImpl m = (MarshallerImpl) _m;
110: m.write(tagName, bi, t, new SAXOutput(new SAX2DOMEx(output)),
111: new DomPostInitAction(output, m.serializer));
112: }
113:
114: public void marshal(Marshaller _m, T t,
115: ContentHandler contentHandler) throws JAXBException {
116: MarshallerImpl m = (MarshallerImpl) _m;
117: m.write(tagName, bi, t, new SAXOutput(contentHandler), null);
118: }
119:
120: public void marshal(Marshaller _m, T t, Result result)
121: throws JAXBException {
122: MarshallerImpl m = (MarshallerImpl) _m;
123: m.write(tagName, bi, t, m.createXmlOutput(result), m
124: .createPostInitAction(result));
125: }
126:
127: public @NotNull
128: T unmarshal(Unmarshaller _u, XMLStreamReader in)
129: throws JAXBException {
130: UnmarshallerImpl u = (UnmarshallerImpl) _u;
131: return ((JAXBElement<T>) u.unmarshal0(in, bi)).getValue();
132: }
133:
134: public @NotNull
135: T unmarshal(Unmarshaller _u, Source in) throws JAXBException {
136: UnmarshallerImpl u = (UnmarshallerImpl) _u;
137: return ((JAXBElement<T>) u.unmarshal0(in, bi)).getValue();
138: }
139:
140: public @NotNull
141: T unmarshal(Unmarshaller _u, InputStream in) throws JAXBException {
142: UnmarshallerImpl u = (UnmarshallerImpl) _u;
143: return ((JAXBElement<T>) u.unmarshal0(in, bi)).getValue();
144: }
145:
146: public @NotNull
147: T unmarshal(Unmarshaller _u, Node n) throws JAXBException {
148: UnmarshallerImpl u = (UnmarshallerImpl) _u;
149: return ((JAXBElement<T>) u.unmarshal0(n, bi)).getValue();
150: }
151:
152: public TypeReference getTypeReference() {
153: return typeRef;
154: }
155:
156: public void marshal(T value, XMLSerializer out) throws IOException,
157: SAXException, XMLStreamException {
158: out.startElement(tagName, null);
159: if (value == null) {
160: out.writeXsiNilTrue();
161: } else {
162: out.childAsXsiType(value, null, bi);
163: }
164: out.endElement();
165: }
166:
167: }
|