001: /*
002: * Portions 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: package com.sun.xml.internal.ws.encoding.jaxb;
026:
027: import com.sun.xml.internal.bind.api.Bridge;
028: import com.sun.xml.internal.bind.api.TypeReference;
029: import com.sun.xml.internal.bind.api.BridgeContext;
030: import com.sun.xml.internal.ws.encoding.soap.SerializationException;
031: import com.sun.xml.internal.ws.encoding.soap.DeserializationException;
032: import com.sun.xml.internal.ws.streaming.XMLStreamReaderUtil;
033:
034: import javax.xml.namespace.QName;
035: import javax.xml.namespace.NamespaceContext;
036: import javax.xml.bind.JAXBException;
037: import javax.xml.stream.XMLStreamWriter;
038: import javax.xml.stream.XMLStreamReader;
039: import javax.xml.stream.XMLStreamConstants;
040: import javax.xml.transform.Source;
041: import java.io.OutputStream;
042: import java.io.InputStream;
043:
044: import org.w3c.dom.Node;
045:
046: /**
047: * XML infoset represented as a JAXB object and {@link Bridge}.
048: *
049: * @author WS Development Team
050: */
051: public final class JAXBBridgeInfo {
052: private final Bridge bridge;
053: private Object value;
054:
055: public JAXBBridgeInfo(Bridge bridge) {
056: this .bridge = bridge;
057: }
058:
059: public JAXBBridgeInfo(Bridge bridge, Object value) {
060: this (bridge);
061: this .value = value;
062: }
063:
064: public QName getName() {
065: return bridge.getTypeReference().tagName;
066: }
067:
068: public TypeReference getType() {
069: return bridge.getTypeReference();
070: }
071:
072: public Bridge getBridge() {
073: return bridge;
074: }
075:
076: public Object getValue() {
077: return value;
078: }
079:
080: public static JAXBBridgeInfo copy(JAXBBridgeInfo payload) {
081: return new JAXBBridgeInfo(payload.getBridge(), payload
082: .getValue());
083: }
084:
085: /**
086: * JAXB object is serialized. Note that the BridgeContext is cached per
087: * thread, and JAXBBridgeInfo should contain correct BridgeContext for the
088: * current thread.
089: */
090: public void serialize(BridgeContext bridgeContext, OutputStream os,
091: NamespaceContext nsContext) {
092: try {
093: bridge.marshal(bridgeContext, value, os, nsContext);
094: } catch (JAXBException e) {
095: throw new SerializationException(e);
096: }
097: }
098:
099: /**
100: * Serialize to StAX.
101: */
102: public void serialize(BridgeContext bridgeContext,
103: XMLStreamWriter writer) {
104: try {
105: bridge.marshal(bridgeContext, value, writer);
106: } catch (JAXBException e) {
107: throw new SerializationException(e);
108: }
109: }
110:
111: /**
112: * Serialize to DOM.
113: */
114: public void serialize(BridgeContext bridgeContext, Node node) {
115: try {
116: bridge.marshal(bridgeContext, value, node);
117: } catch (JAXBException e) {
118: throw new SerializationException(e);
119: }
120: }
121:
122: public void deserialize(Source source, BridgeContext bridgeContext) {
123: try {
124: value = bridge.unmarshal(bridgeContext, source);
125: } catch (JAXBException e) {
126: throw new DeserializationException(e);
127: }
128: }
129:
130: public void deserialize(InputStream stream,
131: BridgeContext bridgeContext) {
132: try {
133: value = bridge.unmarshal(bridgeContext, stream);
134: } catch (JAXBException e) {
135: throw new DeserializationException(e);
136: }
137: }
138:
139: /*
140: * JAXB object is deserialized and is set in JAXBBridgeInfo. Note that
141: * the BridgeContext is cached per thread, and JAXBBridgeInfo should contain
142: * correct BridgeContext for the current thread.
143: */
144: public void deserialize(XMLStreamReader reader,
145: BridgeContext bridgeContext) {
146: try {
147: value = bridge.unmarshal(bridgeContext, reader);
148:
149: // reader could be left on CHARS token rather than </body>
150: if (reader.getEventType() == XMLStreamConstants.CHARACTERS
151: && reader.isWhiteSpace()) {
152: XMLStreamReaderUtil.nextContent(reader);
153: }
154: } catch (JAXBException e) {
155: throw new DeserializationException(e);
156: }
157: }
158: }
|