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.JAXBException;
044: import javax.xml.bind.MarshalException;
045: import javax.xml.bind.Marshaller;
046: import javax.xml.bind.UnmarshalException;
047: import javax.xml.bind.Unmarshaller;
048: import javax.xml.bind.annotation.adapters.XmlAdapter;
049: import javax.xml.namespace.NamespaceContext;
050: import javax.xml.stream.XMLStreamException;
051: import javax.xml.stream.XMLStreamReader;
052: import javax.xml.stream.XMLStreamWriter;
053: import javax.xml.transform.Result;
054: import javax.xml.transform.Source;
055:
056: import com.sun.istack.NotNull;
057: import com.sun.xml.bind.api.Bridge;
058: import com.sun.xml.bind.api.TypeReference;
059: import com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl;
060:
061: import org.w3c.dom.Node;
062: import org.xml.sax.ContentHandler;
063: import org.xml.sax.SAXException;
064:
065: /**
066: * {@link Bridge} decorator for {@link XmlAdapter}.
067: *
068: * @author Kohsuke Kawaguchi
069: */
070: final class BridgeAdapter<OnWire, InMemory> extends
071: InternalBridge<InMemory> {
072: private final InternalBridge<OnWire> core;
073: private final Class<? extends XmlAdapter<OnWire, InMemory>> adapter;
074:
075: public BridgeAdapter(InternalBridge<OnWire> core,
076: Class<? extends XmlAdapter<OnWire, InMemory>> adapter) {
077: super (core.getContext());
078: this .core = core;
079: this .adapter = adapter;
080: }
081:
082: public void marshal(Marshaller m, InMemory inMemory,
083: XMLStreamWriter output) throws JAXBException {
084: core.marshal(m, adaptM(m, inMemory), output);
085: }
086:
087: public void marshal(Marshaller m, InMemory inMemory,
088: OutputStream output, NamespaceContext nsc)
089: throws JAXBException {
090: core.marshal(m, adaptM(m, inMemory), output, nsc);
091: }
092:
093: public void marshal(Marshaller m, InMemory inMemory, Node output)
094: throws JAXBException {
095: core.marshal(m, adaptM(m, inMemory), output);
096: }
097:
098: public void marshal(Marshaller context, InMemory inMemory,
099: ContentHandler contentHandler) throws JAXBException {
100: core
101: .marshal(context, adaptM(context, inMemory),
102: contentHandler);
103: }
104:
105: public void marshal(Marshaller context, InMemory inMemory,
106: Result result) throws JAXBException {
107: core.marshal(context, adaptM(context, inMemory), result);
108: }
109:
110: private OnWire adaptM(Marshaller m, InMemory v)
111: throws JAXBException {
112: XMLSerializer serializer = ((MarshallerImpl) m).serializer;
113: serializer.setThreadAffinity();
114: serializer.pushCoordinator();
115: try {
116: return _adaptM(serializer, v);
117: } finally {
118: serializer.popCoordinator();
119: serializer.resetThreadAffinity();
120: }
121: }
122:
123: private OnWire _adaptM(XMLSerializer serializer, InMemory v)
124: throws MarshalException {
125: XmlAdapter<OnWire, InMemory> a = serializer.getAdapter(adapter);
126: try {
127: return a.marshal(v);
128: } catch (Exception e) {
129: serializer.handleError(e, v, null);
130: throw new MarshalException(e);
131: }
132: }
133:
134: public @NotNull
135: InMemory unmarshal(Unmarshaller u, XMLStreamReader in)
136: throws JAXBException {
137: return adaptU(u, core.unmarshal(u, in));
138: }
139:
140: public @NotNull
141: InMemory unmarshal(Unmarshaller u, Source in) throws JAXBException {
142: return adaptU(u, core.unmarshal(u, in));
143: }
144:
145: public @NotNull
146: InMemory unmarshal(Unmarshaller u, InputStream in)
147: throws JAXBException {
148: return adaptU(u, core.unmarshal(u, in));
149: }
150:
151: public @NotNull
152: InMemory unmarshal(Unmarshaller u, Node n) throws JAXBException {
153: return adaptU(u, core.unmarshal(u, n));
154: }
155:
156: public TypeReference getTypeReference() {
157: return core.getTypeReference();
158: }
159:
160: private @NotNull
161: InMemory adaptU(Unmarshaller _u, OnWire v) throws JAXBException {
162: UnmarshallerImpl u = (UnmarshallerImpl) _u;
163: XmlAdapter<OnWire, InMemory> a = u.coordinator
164: .getAdapter(adapter);
165: u.coordinator.setThreadAffinity();
166: u.coordinator.pushCoordinator();
167: try {
168: return a.unmarshal(v);
169: } catch (Exception e) {
170: throw new UnmarshalException(e);
171: } finally {
172: u.coordinator.popCoordinator();
173: u.coordinator.resetThreadAffinity();
174: }
175: }
176:
177: void marshal(InMemory o, XMLSerializer out) throws IOException,
178: SAXException, XMLStreamException {
179: try {
180: core.marshal(_adaptM(XMLSerializer.getInstance(), o), out);
181: } catch (MarshalException e) {
182: // recover from error by not marshalling this element.
183: }
184: }
185: }
|