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.api;
027:
028: import java.io.InputStream;
029: import java.io.OutputStream;
030:
031: import javax.xml.bind.JAXBException;
032: import javax.xml.bind.Marshaller;
033: import javax.xml.bind.Unmarshaller;
034: import javax.xml.bind.attachment.AttachmentMarshaller;
035: import javax.xml.bind.attachment.AttachmentUnmarshaller;
036: import javax.xml.namespace.NamespaceContext;
037: import javax.xml.stream.XMLStreamReader;
038: import javax.xml.stream.XMLStreamWriter;
039: import javax.xml.transform.Result;
040: import javax.xml.transform.Source;
041:
042: import com.sun.istack.internal.NotNull;
043: import com.sun.istack.internal.Nullable;
044: import com.sun.xml.internal.bind.v2.runtime.BridgeContextImpl;
045: import com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl;
046:
047: import org.w3c.dom.Node;
048: import org.xml.sax.ContentHandler;
049:
050: /**
051: * Mini-marshaller/unmarshaller that is specialized for a particular
052: * element name and a type.
053: *
054: * <p>
055: * Instances of this class is stateless and multi-thread safe.
056: * They are reentrant.
057: *
058: * <p>
059: * All the marshal operation generates fragments.
060: *
061: * <p>
062: * <b>Subject to change without notice</b>.
063: *
064: * @since JAXB 2.0 EA1
065: * @author Kohsuke Kawaguchi
066: */
067: public abstract class Bridge<T> {
068: protected Bridge(JAXBContextImpl context) {
069: this .context = context;
070: }
071:
072: protected final JAXBContextImpl context;
073:
074: /**
075: *
076: * @throws JAXBException
077: * if there was an error while marshalling.
078: *
079: * @since 2.0 EA1
080: */
081: public final void marshal(T object, XMLStreamWriter output)
082: throws JAXBException {
083: marshal(object, output, null);
084: }
085:
086: public final void marshal(T object, XMLStreamWriter output,
087: AttachmentMarshaller am) throws JAXBException {
088: Marshaller m = context.marshallerPool.take();
089: m.setAttachmentMarshaller(am);
090: marshal(m, object, output);
091: m.setAttachmentMarshaller(null);
092: context.marshallerPool.recycle(m);
093: }
094:
095: public final void marshal(@NotNull
096: BridgeContext context, T object, XMLStreamWriter output)
097: throws JAXBException {
098: marshal(((BridgeContextImpl) context).marshaller, object,
099: output);
100: }
101:
102: public abstract void marshal(@NotNull
103: Marshaller m, T object, XMLStreamWriter output)
104: throws JAXBException;
105:
106: /**
107: * Marshals the specified type object with the implicit element name
108: * associated with this instance of {@link Bridge}.
109: *
110: * @param nsContext
111: * if this marshalling is done to marshal a subelement, this {@link NamespaceContext}
112: * represents in-scope namespace bindings available for that element. Can be null,
113: * in which case JAXB assumes no in-scope namespaces.
114: * @throws JAXBException
115: * if there was an error while marshalling.
116: *
117: * @since 2.0 EA1
118: */
119: public void marshal(T object, OutputStream output,
120: NamespaceContext nsContext) throws JAXBException {
121: marshal(object, output, nsContext, null);
122: }
123:
124: /**
125: * @since 2.0.2
126: */
127: public void marshal(T object, OutputStream output,
128: NamespaceContext nsContext, AttachmentMarshaller am)
129: throws JAXBException {
130: Marshaller m = context.marshallerPool.take();
131: m.setAttachmentMarshaller(am);
132: marshal(m, object, output, nsContext);
133: m.setAttachmentMarshaller(null);
134: context.marshallerPool.recycle(m);
135: }
136:
137: public final void marshal(@NotNull
138: BridgeContext context, T object, OutputStream output,
139: NamespaceContext nsContext) throws JAXBException {
140: marshal(((BridgeContextImpl) context).marshaller, object,
141: output, nsContext);
142: }
143:
144: public abstract void marshal(@NotNull
145: Marshaller m, T object, OutputStream output,
146: NamespaceContext nsContext) throws JAXBException;
147:
148: public final void marshal(T object, Node output)
149: throws JAXBException {
150: Marshaller m = context.marshallerPool.take();
151: marshal(m, object, output);
152: context.marshallerPool.recycle(m);
153: }
154:
155: public final void marshal(@NotNull
156: BridgeContext context, T object, Node output) throws JAXBException {
157: marshal(((BridgeContextImpl) context).marshaller, object,
158: output);
159: }
160:
161: public abstract void marshal(@NotNull
162: Marshaller m, T object, Node output) throws JAXBException;
163:
164: /**
165: * @since 2.0 EA4
166: */
167: public final void marshal(T object, ContentHandler contentHandler)
168: throws JAXBException {
169: marshal(object, contentHandler, null);
170: }
171:
172: /**
173: * @since 2.0.2
174: */
175: public final void marshal(T object, ContentHandler contentHandler,
176: AttachmentMarshaller am) throws JAXBException {
177: Marshaller m = context.marshallerPool.take();
178: m.setAttachmentMarshaller(am);
179: marshal(m, object, contentHandler);
180: m.setAttachmentMarshaller(null);
181: context.marshallerPool.recycle(m);
182: }
183:
184: public final void marshal(@NotNull
185: BridgeContext context, T object, ContentHandler contentHandler)
186: throws JAXBException {
187: marshal(((BridgeContextImpl) context).marshaller, object,
188: contentHandler);
189: }
190:
191: public abstract void marshal(@NotNull
192: Marshaller m, T object, ContentHandler contentHandler)
193: throws JAXBException;
194:
195: /**
196: * @since 2.0 EA4
197: */
198: public final void marshal(T object, Result result)
199: throws JAXBException {
200: Marshaller m = context.marshallerPool.take();
201: marshal(m, object, result);
202: context.marshallerPool.recycle(m);
203: }
204:
205: public final void marshal(@NotNull
206: BridgeContext context, T object, Result result)
207: throws JAXBException {
208: marshal(((BridgeContextImpl) context).marshaller, object,
209: result);
210: }
211:
212: public abstract void marshal(@NotNull
213: Marshaller m, T object, Result result) throws JAXBException;
214:
215: private T exit(T r, Unmarshaller u) {
216: u.setAttachmentUnmarshaller(null);
217: context.unmarshallerPool.recycle(u);
218: return r;
219: }
220:
221: /**
222: * Unmarshals the specified type object.
223: *
224: * @param in
225: * the parser must be pointing at a start tag
226: * that encloses the XML type that this {@link Bridge} is
227: * instanciated for.
228: *
229: * @return
230: * never null.
231: *
232: * @throws JAXBException
233: * if there was an error while unmarshalling.
234: *
235: * @since 2.0 EA1
236: */
237: public final @NotNull
238: T unmarshal(@NotNull
239: XMLStreamReader in) throws JAXBException {
240: return unmarshal(in, null);
241: }
242:
243: /**
244: * @since 2.0.3
245: */
246: public final @NotNull
247: T unmarshal(@NotNull
248: XMLStreamReader in, @Nullable
249: AttachmentUnmarshaller au) throws JAXBException {
250: Unmarshaller u = context.unmarshallerPool.take();
251: u.setAttachmentUnmarshaller(au);
252: return exit(unmarshal(u, in), u);
253: }
254:
255: public final @NotNull
256: T unmarshal(@NotNull
257: BridgeContext context, @NotNull
258: XMLStreamReader in) throws JAXBException {
259: return unmarshal(((BridgeContextImpl) context).unmarshaller, in);
260: }
261:
262: public abstract @NotNull
263: T unmarshal(@NotNull
264: Unmarshaller u, @NotNull
265: XMLStreamReader in) throws JAXBException;
266:
267: /**
268: * Unmarshals the specified type object.
269: *
270: * @param in
271: * the parser must be pointing at a start tag
272: * that encloses the XML type that this {@link Bridge} is
273: * instanciated for.
274: *
275: * @return
276: * never null.
277: *
278: * @throws JAXBException
279: * if there was an error while unmarshalling.
280: *
281: * @since 2.0 EA1
282: */
283: public final @NotNull
284: T unmarshal(@NotNull
285: Source in) throws JAXBException {
286: return unmarshal(in, null);
287: }
288:
289: /**
290: * @since 2.0.3
291: */
292: public final @NotNull
293: T unmarshal(@NotNull
294: Source in, @Nullable
295: AttachmentUnmarshaller au) throws JAXBException {
296: Unmarshaller u = context.unmarshallerPool.take();
297: u.setAttachmentUnmarshaller(au);
298: return exit(unmarshal(u, in), u);
299: }
300:
301: public final @NotNull
302: T unmarshal(@NotNull
303: BridgeContext context, @NotNull
304: Source in) throws JAXBException {
305: return unmarshal(((BridgeContextImpl) context).unmarshaller, in);
306: }
307:
308: public abstract @NotNull
309: T unmarshal(@NotNull
310: Unmarshaller u, @NotNull
311: Source in) throws JAXBException;
312:
313: /**
314: * Unmarshals the specified type object.
315: *
316: * @param in
317: * the parser must be pointing at a start tag
318: * that encloses the XML type that this {@link Bridge} is
319: * instanciated for.
320: *
321: * @return
322: * never null.
323: *
324: * @throws JAXBException
325: * if there was an error while unmarshalling.
326: *
327: * @since 2.0 EA1
328: */
329: public final @NotNull
330: T unmarshal(@NotNull
331: InputStream in) throws JAXBException {
332: Unmarshaller u = context.unmarshallerPool.take();
333: return exit(unmarshal(u, in), u);
334: }
335:
336: public final @NotNull
337: T unmarshal(@NotNull
338: BridgeContext context, @NotNull
339: InputStream in) throws JAXBException {
340: return unmarshal(((BridgeContextImpl) context).unmarshaller, in);
341: }
342:
343: public abstract @NotNull
344: T unmarshal(@NotNull
345: Unmarshaller u, @NotNull
346: InputStream in) throws JAXBException;
347:
348: /**
349: * Unmarshals the specified type object.
350: *
351: * @param n
352: * Node to be unmarshalled.
353: *
354: * @return
355: * never null.
356: *
357: * @throws JAXBException
358: * if there was an error while unmarshalling.
359: *
360: * @since 2.0 FCS
361: */
362: public final @NotNull
363: T unmarshal(@NotNull
364: Node n) throws JAXBException {
365: return unmarshal(n, null);
366: }
367:
368: /**
369: * @since 2.0.3
370: */
371: public final @NotNull
372: T unmarshal(@NotNull
373: Node n, @Nullable
374: AttachmentUnmarshaller au) throws JAXBException {
375: Unmarshaller u = context.unmarshallerPool.take();
376: u.setAttachmentUnmarshaller(au);
377: return exit(unmarshal(u, n), u);
378: }
379:
380: public final @NotNull
381: T unmarshal(@NotNull
382: BridgeContext context, @NotNull
383: Node n) throws JAXBException {
384: return unmarshal(((BridgeContextImpl) context).unmarshaller, n);
385: }
386:
387: public abstract @NotNull
388: T unmarshal(@NotNull
389: Unmarshaller context, @NotNull
390: Node n) throws JAXBException;
391:
392: /**
393: * Gets the {@link TypeReference} from which this bridge was created.
394: */
395: public abstract TypeReference getTypeReference();
396: }
|