001: /*
002: * Copyright 2006 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package com.sun.xml.internal.bind.v2.runtime;
027:
028: import java.io.IOException;
029: import java.io.InputStream;
030: import java.io.OutputStream;
031:
032: import javax.xml.bind.JAXBElement;
033: import javax.xml.bind.JAXBException;
034: import javax.xml.bind.Marshaller;
035: import javax.xml.bind.Unmarshaller;
036: import javax.xml.namespace.NamespaceContext;
037: import javax.xml.stream.XMLStreamException;
038: import javax.xml.stream.XMLStreamReader;
039: import javax.xml.stream.XMLStreamWriter;
040: import javax.xml.transform.Result;
041: import javax.xml.transform.Source;
042:
043: import com.sun.istack.internal.NotNull;
044: import com.sun.xml.internal.bind.api.Bridge;
045: import com.sun.xml.internal.bind.api.TypeReference;
046: import com.sun.xml.internal.bind.marshaller.SAX2DOMEx;
047: import com.sun.xml.internal.bind.v2.runtime.output.SAXOutput;
048: import com.sun.xml.internal.bind.v2.runtime.output.XMLStreamWriterOutput;
049: import com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl;
050:
051: import org.w3c.dom.Node;
052: import org.xml.sax.ContentHandler;
053: import org.xml.sax.SAXException;
054:
055: /**
056: * {@link Bridge} implementaiton.
057: *
058: * @author Kohsuke Kawaguchi
059: */
060: final class BridgeImpl<T> extends InternalBridge<T> {
061:
062: /**
063: * Tag name associated with this {@link Bridge}.
064: * Used for marshalling.
065: */
066: private final Name tagName;
067: private final JaxBeanInfo<T> bi;
068: private final TypeReference typeRef;
069:
070: public BridgeImpl(JAXBContextImpl context, Name tagName,
071: JaxBeanInfo<T> bi, TypeReference typeRef) {
072: super (context);
073: this .tagName = tagName;
074: this .bi = bi;
075: this .typeRef = typeRef;
076: }
077:
078: public void marshal(Marshaller _m, T t, XMLStreamWriter output)
079: throws JAXBException {
080: MarshallerImpl m = (MarshallerImpl) _m;
081: m.write(tagName, bi, t, XMLStreamWriterOutput.create(output),
082: new StAXPostInitAction(output, m.serializer));
083: }
084:
085: public void marshal(Marshaller _m, T t, OutputStream output,
086: NamespaceContext nsContext) throws JAXBException {
087: MarshallerImpl m = (MarshallerImpl) _m;
088:
089: Runnable pia = null;
090: if (nsContext != null)
091: pia = new StAXPostInitAction(nsContext, m.serializer);
092:
093: m.write(tagName, bi, t, m.createWriter(output), pia);
094: }
095:
096: public void marshal(Marshaller _m, T t, Node output)
097: throws JAXBException {
098: MarshallerImpl m = (MarshallerImpl) _m;
099: m.write(tagName, bi, t, new SAXOutput(new SAX2DOMEx(output)),
100: new DomPostInitAction(output, m.serializer));
101: }
102:
103: public void marshal(Marshaller _m, T t,
104: ContentHandler contentHandler) throws JAXBException {
105: MarshallerImpl m = (MarshallerImpl) _m;
106: m.write(tagName, bi, t, new SAXOutput(contentHandler), null);
107: }
108:
109: public void marshal(Marshaller _m, T t, Result result)
110: throws JAXBException {
111: MarshallerImpl m = (MarshallerImpl) _m;
112: m.write(tagName, bi, t, m.createXmlOutput(result), m
113: .createPostInitAction(result));
114: }
115:
116: public @NotNull
117: T unmarshal(Unmarshaller _u, XMLStreamReader in)
118: throws JAXBException {
119: UnmarshallerImpl u = (UnmarshallerImpl) _u;
120: return ((JAXBElement<T>) u.unmarshal0(in, bi)).getValue();
121: }
122:
123: public @NotNull
124: T unmarshal(Unmarshaller _u, Source in) throws JAXBException {
125: UnmarshallerImpl u = (UnmarshallerImpl) _u;
126: return ((JAXBElement<T>) u.unmarshal0(in, bi)).getValue();
127: }
128:
129: public @NotNull
130: T unmarshal(Unmarshaller _u, InputStream in) throws JAXBException {
131: UnmarshallerImpl u = (UnmarshallerImpl) _u;
132: return ((JAXBElement<T>) u.unmarshal0(in, bi)).getValue();
133: }
134:
135: public @NotNull
136: T unmarshal(Unmarshaller _u, Node n) throws JAXBException {
137: UnmarshallerImpl u = (UnmarshallerImpl) _u;
138: return ((JAXBElement<T>) u.unmarshal0(n, bi)).getValue();
139: }
140:
141: public TypeReference getTypeReference() {
142: return typeRef;
143: }
144:
145: public void marshal(T value, XMLSerializer out) throws IOException,
146: SAXException, XMLStreamException {
147: out.startElement(tagName, null);
148: if (value == null) {
149: out.writeXsiNilTrue();
150: } else {
151: out.childAsXsiType(value, null, bi);
152: }
153: out.endElement();
154: }
155:
156: }
|