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:
026: package com.sun.xml.internal.ws.encoding.jaxb;
027:
028: import com.sun.xml.internal.bind.api.BridgeContext;
029: import com.sun.xml.internal.bind.v2.runtime.MarshallerImpl;
030: import com.sun.xml.internal.ws.encoding.soap.DeserializationException;
031: import com.sun.xml.internal.ws.encoding.soap.SerializationException;
032: import com.sun.xml.internal.ws.streaming.XMLStreamReaderUtil;
033:
034: import javax.xml.bind.JAXBContext;
035: import javax.xml.bind.JAXBException;
036: import javax.xml.bind.Marshaller;
037: import javax.xml.bind.Unmarshaller;
038: import javax.xml.stream.XMLStreamConstants;
039: import javax.xml.stream.XMLStreamReader;
040: import javax.xml.stream.XMLStreamWriter;
041: import javax.xml.transform.Source;
042: import javax.xml.transform.dom.DOMResult;
043: import javax.xml.transform.dom.DOMSource;
044: import java.io.OutputStream;
045:
046: /**
047: * @author Vivek Pandey
048: */
049: public final class JAXBTypeSerializer {
050: private JAXBTypeSerializer() {
051: } // no instanciation please
052:
053: public static void serialize(Object obj, XMLStreamWriter writer,
054: JAXBContext context) {
055:
056: try {
057: Marshaller marshaller = context.createMarshaller();
058: marshaller.setProperty("jaxb.fragment", Boolean.TRUE);
059: marshaller.marshal(obj, writer);
060: } catch (RuntimeException e) {
061: throw e;
062: } catch (Exception e) {
063: throw new SerializationException(e);
064: }
065: }
066:
067: public static void serialize(Object obj, XMLStreamWriter writer,
068: JAXBContext context, Marshaller marshaller) {
069:
070: try {
071: if (marshaller == null)
072: marshaller = context.createMarshaller();
073: marshaller.setProperty("jaxb.fragment", Boolean.TRUE);
074: marshaller.marshal(obj, writer);
075: } catch (RuntimeException e) {
076: throw e;
077: } catch (Exception e) {
078: throw new SerializationException(e);
079: }
080: }
081:
082: /* for FI, it will be a whole document, not fragment
083: * called by setPayload and writeTo methods in XMLMessage class
084: */
085: public static void serializeDocument(Object obj,
086: XMLStreamWriter writer, JAXBContext context) {
087: try {
088: Marshaller marshaller = context.createMarshaller();
089: marshaller.marshal(obj, writer);
090: } catch (RuntimeException e) {
091: throw e;
092: } catch (Exception e) {
093: throw new SerializationException(e);
094: }
095: }
096:
097: public static void serialize(Object obj, OutputStream os,
098: JAXBContext context, Marshaller marshaller) {
099:
100: try {
101: if (marshaller == null)
102: marshaller = context.createMarshaller();
103: marshaller.setProperty("jaxb.fragment", Boolean.TRUE);
104: marshaller.marshal(obj, os);
105: } catch (RuntimeException e) {
106: throw e;
107: } catch (Exception e) {
108: throw new SerializationException(e);
109: }
110: }
111:
112: public static void serialize(Object obj, OutputStream os,
113: JAXBContext context) {
114:
115: try {
116: Marshaller marshaller = context.createMarshaller();
117: marshaller.setProperty("jaxb.fragment", Boolean.TRUE);
118: marshaller.marshal(obj, os);
119: } catch (RuntimeException e) {
120: throw e;
121: } catch (Exception e) {
122: throw new SerializationException(e);
123: }
124: }
125:
126: /*
127: * Marshalls arbitrary type object with the given tag name
128: */
129: public static DOMSource serialize(Object bean, JAXBContext context) {
130: try {
131: Marshaller marshaller = context.createMarshaller();
132: marshaller.setProperty("jaxb.fragment", Boolean.TRUE);
133: DOMResult domResult = new DOMResult();
134: marshaller.marshal(bean, domResult);
135: return new DOMSource(domResult.getNode());
136: } catch (JAXBException e) {
137: throw new SerializationException(e);
138: }
139: }
140:
141: /*
142: * @see JAXBTypeSerializerIf#deserialize(XMLStreamReader,JAXBContext)
143: */
144: public static Object deserialize(XMLStreamReader reader,
145: JAXBContext context) {
146: Object obj = null;
147: try {
148: Unmarshaller unmarshaller = context.createUnmarshaller();
149: if (reader.getEventType() == XMLStreamConstants.START_ELEMENT)
150: obj = unmarshaller.unmarshal(reader);
151:
152: // reader could be left on CHARS token rather than </body>
153: if (reader.getEventType() == XMLStreamConstants.CHARACTERS
154: && reader.isWhiteSpace()) {
155: XMLStreamReaderUtil.nextContent(reader);
156: }
157: return obj;
158:
159: } catch (DeserializationException e) {
160: throw e;
161: } catch (Exception e) {
162: throw new DeserializationException(e);
163: }
164: }
165:
166: public static Object deserialize(XMLStreamReader reader,
167: JAXBContext context, Unmarshaller bc) {
168: Object obj = null;
169: try {
170: Unmarshaller unmarshaller = context.createUnmarshaller();
171: if (bc != null)
172: unmarshaller.setAttachmentUnmarshaller(bc
173: .getAttachmentUnmarshaller());
174:
175: if (reader.getEventType() == XMLStreamConstants.START_ELEMENT)
176: obj = unmarshaller.unmarshal(reader);
177:
178: // reader could be left on CHARS token rather than </body>
179: if (reader.getEventType() == XMLStreamConstants.CHARACTERS
180: && reader.isWhiteSpace()) {
181: XMLStreamReaderUtil.nextContent(reader);
182: }
183: return obj;
184:
185: } catch (DeserializationException e) {
186: throw e;
187: } catch (Exception e) {
188: throw new DeserializationException(e);
189: }
190: }
191:
192: /*
193: * convert JAXB bean as a Source
194: *
195: public static Object toSource(Object obj, JAXBContext context) {
196: try {
197: // Use ctxt to marshall the object
198: ByteArrayOutputStream bos = new ByteArrayOutputStream();
199: Marshaller marshaller = context.createMarshaller();
200: marshaller.setProperty("jaxb.fragment", Boolean.TRUE);
201: marshaller.marshal(obj, bos);
202: ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
203: return new StreamSource(bis);
204: } catch (JAXBException e) {
205: throw new DeserializationException(new LocalizableExceptionAdapter(
206: e));
207: }
208: }
209: */
210:
211: /*
212: * Convert Source object as a JAXB bean
213: */
214: public static Object deserialize(Source source, JAXBContext context) {
215: try {
216: Unmarshaller unmarshaller = context.createUnmarshaller();
217: return unmarshaller.unmarshal(source);
218: } catch (JAXBException e) {
219: throw new DeserializationException(e);
220: }
221: }
222:
223: }
|