1/*
2 * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26package com.sun.xml.internal.ws.spi.db;
27
28import java.io.InputStream;
29import java.io.OutputStream;
30
31import javax.xml.bind.JAXBException;
32import javax.xml.bind.Marshaller;
33import javax.xml.bind.Unmarshaller;
34import javax.xml.bind.attachment.AttachmentMarshaller;
35import javax.xml.bind.attachment.AttachmentUnmarshaller;
36import javax.xml.namespace.NamespaceContext;
37import javax.xml.stream.XMLStreamReader;
38import javax.xml.stream.XMLStreamWriter;
39import javax.xml.transform.Result;
40import javax.xml.transform.Source;
41
42import com.sun.istack.internal.NotNull;
43import com.sun.istack.internal.Nullable;
44import com.sun.xml.internal.bind.api.BridgeContext;
45import com.sun.xml.internal.bind.v2.runtime.BridgeContextImpl;
46import com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl;
47
48import org.w3c.dom.Node;
49import org.xml.sax.ContentHandler;
50
51/**
52 * Mini-marshaller/unmarshaller that is specialized for a particular
53 * element name and a type.
54 *
55 * <p>
56 * Instances of this class is stateless and multi-thread safe.
57 * They are reentrant.
58 *
59 * <p>
60 * All the marshal operation generates fragments.
61 *
62 * <p>
63 * <b>Subject to change without notice</b>.
64 *
65 * @since JAXB 2.0 EA1
66 * @author Kohsuke Kawaguchi
67 */
68public abstract class OldBridge<T> {
69    protected OldBridge(JAXBContextImpl context) {
70        this.context = context;
71    }
72
73    protected final JAXBContextImpl context;
74
75    /**
76     * Gets the {@link BindingContext} to which this object belongs.
77     *
78     * @since 2.1
79     */
80    public @NotNull BindingContext getContext() {
81//        return context;
82        return null;
83    }
84
85    /**
86     *
87     * @throws JAXBException
88     *      if there was an error while marshalling.
89     *
90     * @since 2.0 EA1
91     */
92    public final void marshal(T object,XMLStreamWriter output) throws JAXBException {
93        marshal(object,output,null);
94    }
95    public final void marshal(T object,XMLStreamWriter output, AttachmentMarshaller am) throws JAXBException {
96        Marshaller m = context.marshallerPool.take();
97        m.setAttachmentMarshaller(am);
98        marshal(m,object,output);
99        m.setAttachmentMarshaller(null);
100        context.marshallerPool.recycle(m);
101    }
102
103    public final void marshal(@NotNull BridgeContext context,T object,XMLStreamWriter output) throws JAXBException {
104        marshal( ((BridgeContextImpl)context).marshaller, object, output );
105    }
106
107    public abstract void marshal(@NotNull Marshaller m,T object,XMLStreamWriter output) throws JAXBException;
108
109
110    /**
111     * Marshals the specified type object with the implicit element name
112     * associated with this instance of {@link XMLBridge}.
113     *
114     * @param nsContext
115     *      if this marshalling is done to marshal a subelement, this {@link NamespaceContext}
116     *      represents in-scope namespace bindings available for that element. Can be null,
117     *      in which case JAXB assumes no in-scope namespaces.
118     * @throws JAXBException
119     *      if there was an error while marshalling.
120     *
121     * @since 2.0 EA1
122     */
123    public void marshal(T object,OutputStream output, NamespaceContext nsContext) throws JAXBException {
124        marshal(object,output,nsContext,null);
125    }
126    /**
127     * @since 2.0.2
128     */
129    public void marshal(T object,OutputStream output, NamespaceContext nsContext, AttachmentMarshaller am) 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 BridgeContext context,T object,OutputStream output, NamespaceContext nsContext) throws JAXBException {
138        marshal( ((BridgeContextImpl)context).marshaller, object, output, nsContext );
139    }
140
141    public abstract void marshal(@NotNull Marshaller m,T object,OutputStream output, NamespaceContext nsContext) throws JAXBException;
142
143
144    public final void marshal(T object,Node output) throws JAXBException {
145        Marshaller m = context.marshallerPool.take();
146        marshal(m,object,output);
147        context.marshallerPool.recycle(m);
148    }
149
150    public final void marshal(@NotNull BridgeContext context,T object,Node output) throws JAXBException {
151        marshal( ((BridgeContextImpl)context).marshaller, object, output );
152    }
153
154    public abstract void marshal(@NotNull Marshaller m,T object,Node output) throws JAXBException;
155
156
157    /**
158     * @since 2.0 EA4
159     */
160    public final void marshal(T object, ContentHandler contentHandler) throws JAXBException {
161        marshal(object,contentHandler,null);
162    }
163    /**
164     * @since 2.0.2
165     */
166    public final void marshal(T object, ContentHandler contentHandler, AttachmentMarshaller am) throws JAXBException {
167        Marshaller m = context.marshallerPool.take();
168        m.setAttachmentMarshaller(am);
169        marshal(m,object,contentHandler);
170        m.setAttachmentMarshaller(null);
171        context.marshallerPool.recycle(m);
172    }
173    public final void marshal(@NotNull BridgeContext context,T object, ContentHandler contentHandler) throws JAXBException {
174        marshal( ((BridgeContextImpl)context).marshaller, object, contentHandler );
175    }
176    public abstract void marshal(@NotNull Marshaller m,T object, ContentHandler contentHandler) throws JAXBException;
177
178    /**
179     * @since 2.0 EA4
180     */
181    public final void marshal(T object, Result result) throws JAXBException {
182        Marshaller m = context.marshallerPool.take();
183        marshal(m,object,result);
184        context.marshallerPool.recycle(m);
185    }
186    public final void marshal(@NotNull BridgeContext context,T object, Result result) throws JAXBException {
187        marshal( ((BridgeContextImpl)context).marshaller, object, result );
188    }
189    public abstract void marshal(@NotNull Marshaller m,T object, Result result) throws JAXBException;
190
191
192
193    private T exit(T r, Unmarshaller u) {
194        u.setAttachmentUnmarshaller(null);
195        context.unmarshallerPool.recycle(u);
196        return r;
197    }
198
199    /**
200     * Unmarshals the specified type object.
201     *
202     * @param in
203     *      the parser must be pointing at a start tag
204     *      that encloses the XML type that this {@link XMLBridge} is
205     *      instanciated for.
206     *
207     * @return
208     *      never null.
209     *
210     * @throws JAXBException
211     *      if there was an error while unmarshalling.
212     *
213     * @since 2.0 EA1
214     */
215    public final @NotNull T unmarshal(@NotNull XMLStreamReader in) throws JAXBException {
216        return unmarshal(in,null);
217    }
218    /**
219     * @since 2.0.3
220     */
221    public final @NotNull T unmarshal(@NotNull XMLStreamReader in, @Nullable AttachmentUnmarshaller au) throws JAXBException {
222        Unmarshaller u = context.unmarshallerPool.take();
223        u.setAttachmentUnmarshaller(au);
224        return exit(unmarshal(u,in),u);
225    }
226    public final @NotNull T unmarshal(@NotNull BridgeContext context, @NotNull XMLStreamReader in) throws JAXBException {
227        return unmarshal( ((BridgeContextImpl)context).unmarshaller, in );
228    }
229    public abstract @NotNull T unmarshal(@NotNull Unmarshaller u, @NotNull XMLStreamReader in) throws JAXBException;
230
231    /**
232     * Unmarshals the specified type object.
233     *
234     * @param in
235     *      the parser must be pointing at a start tag
236     *      that encloses the XML type that this {@link XMLBridge} is
237     *      instanciated for.
238     *
239     * @return
240     *      never null.
241     *
242     * @throws JAXBException
243     *      if there was an error while unmarshalling.
244     *
245     * @since 2.0 EA1
246     */
247    public final @NotNull T unmarshal(@NotNull Source in) throws JAXBException {
248        return unmarshal(in,null);
249    }
250    /**
251     * @since 2.0.3
252     */
253    public final @NotNull T unmarshal(@NotNull Source in, @Nullable AttachmentUnmarshaller au) throws JAXBException {
254        Unmarshaller u = context.unmarshallerPool.take();
255        u.setAttachmentUnmarshaller(au);
256        return exit(unmarshal(u,in),u);
257    }
258    public final @NotNull T unmarshal(@NotNull BridgeContext context, @NotNull Source in) throws JAXBException {
259        return unmarshal( ((BridgeContextImpl)context).unmarshaller, in );
260    }
261    public abstract @NotNull T unmarshal(@NotNull Unmarshaller u, @NotNull Source in) throws JAXBException;
262
263    /**
264     * Unmarshals the specified type object.
265     *
266     * @param in
267     *      the parser must be pointing at a start tag
268     *      that encloses the XML type that this {@link XMLBridge} is
269     *      instanciated for.
270     *
271     * @return
272     *      never null.
273     *
274     * @throws JAXBException
275     *      if there was an error while unmarshalling.
276     *
277     * @since 2.0 EA1
278     */
279    public final @NotNull T unmarshal(@NotNull InputStream in) throws JAXBException {
280        Unmarshaller u = context.unmarshallerPool.take();
281        return exit(unmarshal(u,in),u);
282    }
283    public final @NotNull T unmarshal(@NotNull BridgeContext context, @NotNull InputStream in) throws JAXBException {
284        return unmarshal( ((BridgeContextImpl)context).unmarshaller, in );
285    }
286    public abstract @NotNull T unmarshal(@NotNull Unmarshaller u, @NotNull InputStream in) throws JAXBException;
287
288    /**
289     * Unmarshals the specified type object.
290     *
291     * @param n
292     *      Node to be unmarshalled.
293     *
294     * @return
295     *      never null.
296     *
297     * @throws JAXBException
298     *      if there was an error while unmarshalling.
299     *
300     * @since 2.0 FCS
301     */
302    public final @NotNull T unmarshal(@NotNull Node n) throws JAXBException {
303        return unmarshal(n,null);
304    }
305    /**
306     * @since 2.0.3
307     */
308    public final @NotNull T unmarshal(@NotNull Node n, @Nullable AttachmentUnmarshaller au) throws JAXBException {
309        Unmarshaller u = context.unmarshallerPool.take();
310        u.setAttachmentUnmarshaller(au);
311        return exit(unmarshal(u,n),u);
312    }
313    public final @NotNull T unmarshal(@NotNull BridgeContext context, @NotNull Node n) throws JAXBException {
314        return unmarshal( ((BridgeContextImpl)context).unmarshaller, n );
315    }
316    public abstract @NotNull T unmarshal(@NotNull Unmarshaller context, @NotNull Node n) throws JAXBException;
317
318    /**
319     * Gets the {@link TypeInfo} from which this bridge was created.
320     */
321    public abstract TypeInfo getTypeReference();
322}
323