aoqi@0: /*
aoqi@0: * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0: *
aoqi@0: * This code is free software; you can redistribute it and/or modify it
aoqi@0: * under the terms of the GNU General Public License version 2 only, as
aoqi@0: * published by the Free Software Foundation. Oracle designates this
aoqi@0: * particular file as subject to the "Classpath" exception as provided
aoqi@0: * by Oracle in the LICENSE file that accompanied this code.
aoqi@0: *
aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0: * accompanied this code).
aoqi@0: *
aoqi@0: * You should have received a copy of the GNU General Public License version
aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0: *
aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0: * or visit www.oracle.com if you need additional information or have any
aoqi@0: * questions.
aoqi@0: */
aoqi@0:
aoqi@0: package com.sun.xml.internal.bind.api;
aoqi@0:
aoqi@0: import java.io.InputStream;
aoqi@0: import java.io.OutputStream;
aoqi@0:
aoqi@0: import javax.xml.bind.JAXBException;
aoqi@0: import javax.xml.bind.Marshaller;
aoqi@0: import javax.xml.bind.Unmarshaller;
aoqi@0: import javax.xml.bind.attachment.AttachmentMarshaller;
aoqi@0: import javax.xml.bind.attachment.AttachmentUnmarshaller;
aoqi@0: import javax.xml.namespace.NamespaceContext;
aoqi@0: import javax.xml.stream.XMLStreamReader;
aoqi@0: import javax.xml.stream.XMLStreamWriter;
aoqi@0: import javax.xml.transform.Result;
aoqi@0: import javax.xml.transform.Source;
aoqi@0:
aoqi@0: import com.sun.istack.internal.NotNull;
aoqi@0: import com.sun.istack.internal.Nullable;
aoqi@0: import com.sun.xml.internal.bind.v2.runtime.BridgeContextImpl;
aoqi@0: import com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl;
aoqi@0:
aoqi@0: import org.w3c.dom.Node;
aoqi@0: import org.xml.sax.ContentHandler;
aoqi@0:
aoqi@0: /**
aoqi@0: * Mini-marshaller/unmarshaller that is specialized for a particular
aoqi@0: * element name and a type.
aoqi@0: *
aoqi@0: *
aoqi@0: * Instances of this class is stateless and multi-thread safe.
aoqi@0: * They are reentrant.
aoqi@0: *
aoqi@0: *
aoqi@0: * All the marshal operation generates fragments.
aoqi@0: *
aoqi@0: *
aoqi@0: * Subject to change without notice.
aoqi@0: *
aoqi@0: * @since JAXB 2.0 EA1
aoqi@0: * @author Kohsuke Kawaguchi
aoqi@0: */
aoqi@0: public abstract class Bridge {
aoqi@0: protected Bridge(JAXBContextImpl context) {
aoqi@0: this.context = context;
aoqi@0: }
aoqi@0:
aoqi@0: protected final JAXBContextImpl context;
aoqi@0:
aoqi@0: /**
aoqi@0: * Gets the {@link JAXBRIContext} to which this object belongs.
aoqi@0: *
aoqi@0: * @since 2.1
aoqi@0: */
aoqi@0: public @NotNull JAXBRIContext getContext() {
aoqi@0: return context;
aoqi@0: }
aoqi@0:
aoqi@0: /**
aoqi@0: *
aoqi@0: * @throws JAXBException
aoqi@0: * if there was an error while marshalling.
aoqi@0: *
aoqi@0: * @since 2.0 EA1
aoqi@0: */
aoqi@0: public final void marshal(T object,XMLStreamWriter output) throws JAXBException {
aoqi@0: marshal(object,output,null);
aoqi@0: }
aoqi@0: public final void marshal(T object,XMLStreamWriter output, AttachmentMarshaller am) throws JAXBException {
aoqi@0: Marshaller m = context.marshallerPool.take();
aoqi@0: m.setAttachmentMarshaller(am);
aoqi@0: marshal(m,object,output);
aoqi@0: m.setAttachmentMarshaller(null);
aoqi@0: context.marshallerPool.recycle(m);
aoqi@0: }
aoqi@0:
aoqi@0: public final void marshal(@NotNull BridgeContext context,T object,XMLStreamWriter output) throws JAXBException {
aoqi@0: marshal( ((BridgeContextImpl)context).marshaller, object, output );
aoqi@0: }
aoqi@0:
aoqi@0: public abstract void marshal(@NotNull Marshaller m,T object,XMLStreamWriter output) throws JAXBException;
aoqi@0:
aoqi@0:
aoqi@0: /**
aoqi@0: * Marshals the specified type object with the implicit element name
aoqi@0: * associated with this instance of {@link Bridge}.
aoqi@0: *
aoqi@0: * @param nsContext
aoqi@0: * if this marshalling is done to marshal a subelement, this {@link NamespaceContext}
aoqi@0: * represents in-scope namespace bindings available for that element. Can be null,
aoqi@0: * in which case JAXB assumes no in-scope namespaces.
aoqi@0: * @throws JAXBException
aoqi@0: * if there was an error while marshalling.
aoqi@0: *
aoqi@0: * @since 2.0 EA1
aoqi@0: */
aoqi@0: public void marshal(T object,OutputStream output, NamespaceContext nsContext) throws JAXBException {
aoqi@0: marshal(object,output,nsContext,null);
aoqi@0: }
aoqi@0: /**
aoqi@0: * @since 2.0.2
aoqi@0: */
aoqi@0: public void marshal(T object,OutputStream output, NamespaceContext nsContext, AttachmentMarshaller am) throws JAXBException {
aoqi@0: Marshaller m = context.marshallerPool.take();
aoqi@0: m.setAttachmentMarshaller(am);
aoqi@0: marshal(m,object,output,nsContext);
aoqi@0: m.setAttachmentMarshaller(null);
aoqi@0: context.marshallerPool.recycle(m);
aoqi@0: }
aoqi@0:
aoqi@0: public final void marshal(@NotNull BridgeContext context,T object,OutputStream output, NamespaceContext nsContext) throws JAXBException {
aoqi@0: marshal( ((BridgeContextImpl)context).marshaller, object, output, nsContext );
aoqi@0: }
aoqi@0:
aoqi@0: public abstract void marshal(@NotNull Marshaller m,T object,OutputStream output, NamespaceContext nsContext) throws JAXBException;
aoqi@0:
aoqi@0:
aoqi@0: public final void marshal(T object,Node output) throws JAXBException {
aoqi@0: Marshaller m = context.marshallerPool.take();
aoqi@0: marshal(m,object,output);
aoqi@0: context.marshallerPool.recycle(m);
aoqi@0: }
aoqi@0:
aoqi@0: public final void marshal(@NotNull BridgeContext context,T object,Node output) throws JAXBException {
aoqi@0: marshal( ((BridgeContextImpl)context).marshaller, object, output );
aoqi@0: }
aoqi@0:
aoqi@0: public abstract void marshal(@NotNull Marshaller m,T object,Node output) throws JAXBException;
aoqi@0:
aoqi@0:
aoqi@0: /**
aoqi@0: * @since 2.0 EA4
aoqi@0: */
aoqi@0: public final void marshal(T object, ContentHandler contentHandler) throws JAXBException {
aoqi@0: marshal(object,contentHandler,null);
aoqi@0: }
aoqi@0: /**
aoqi@0: * @since 2.0.2
aoqi@0: */
aoqi@0: public final void marshal(T object, ContentHandler contentHandler, AttachmentMarshaller am) throws JAXBException {
aoqi@0: Marshaller m = context.marshallerPool.take();
aoqi@0: m.setAttachmentMarshaller(am);
aoqi@0: marshal(m,object,contentHandler);
aoqi@0: m.setAttachmentMarshaller(null);
aoqi@0: context.marshallerPool.recycle(m);
aoqi@0: }
aoqi@0: public final void marshal(@NotNull BridgeContext context,T object, ContentHandler contentHandler) throws JAXBException {
aoqi@0: marshal( ((BridgeContextImpl)context).marshaller, object, contentHandler );
aoqi@0: }
aoqi@0: public abstract void marshal(@NotNull Marshaller m,T object, ContentHandler contentHandler) throws JAXBException;
aoqi@0:
aoqi@0: /**
aoqi@0: * @since 2.0 EA4
aoqi@0: */
aoqi@0: public final void marshal(T object, Result result) throws JAXBException {
aoqi@0: Marshaller m = context.marshallerPool.take();
aoqi@0: marshal(m,object,result);
aoqi@0: context.marshallerPool.recycle(m);
aoqi@0: }
aoqi@0: public final void marshal(@NotNull BridgeContext context,T object, Result result) throws JAXBException {
aoqi@0: marshal( ((BridgeContextImpl)context).marshaller, object, result );
aoqi@0: }
aoqi@0: public abstract void marshal(@NotNull Marshaller m,T object, Result result) throws JAXBException;
aoqi@0:
aoqi@0:
aoqi@0:
aoqi@0: private T exit(T r, Unmarshaller u) {
aoqi@0: u.setAttachmentUnmarshaller(null);
aoqi@0: context.unmarshallerPool.recycle(u);
aoqi@0: return r;
aoqi@0: }
aoqi@0:
aoqi@0: /**
aoqi@0: * Unmarshals the specified type object.
aoqi@0: *
aoqi@0: * @param in
aoqi@0: * the parser must be pointing at a start tag
aoqi@0: * that encloses the XML type that this {@link Bridge} is
aoqi@0: * instanciated for.
aoqi@0: *
aoqi@0: * @return
aoqi@0: * never null.
aoqi@0: *
aoqi@0: * @throws JAXBException
aoqi@0: * if there was an error while unmarshalling.
aoqi@0: *
aoqi@0: * @since 2.0 EA1
aoqi@0: */
aoqi@0: public final @NotNull T unmarshal(@NotNull XMLStreamReader in) throws JAXBException {
aoqi@0: return unmarshal(in,null);
aoqi@0: }
aoqi@0: /**
aoqi@0: * @since 2.0.3
aoqi@0: */
aoqi@0: public final @NotNull T unmarshal(@NotNull XMLStreamReader in, @Nullable AttachmentUnmarshaller au) throws JAXBException {
aoqi@0: Unmarshaller u = context.unmarshallerPool.take();
aoqi@0: u.setAttachmentUnmarshaller(au);
aoqi@0: return exit(unmarshal(u,in),u);
aoqi@0: }
aoqi@0: public final @NotNull T unmarshal(@NotNull BridgeContext context, @NotNull XMLStreamReader in) throws JAXBException {
aoqi@0: return unmarshal( ((BridgeContextImpl)context).unmarshaller, in );
aoqi@0: }
aoqi@0: public abstract @NotNull T unmarshal(@NotNull Unmarshaller u, @NotNull XMLStreamReader in) throws JAXBException;
aoqi@0:
aoqi@0: /**
aoqi@0: * Unmarshals the specified type object.
aoqi@0: *
aoqi@0: * @param in
aoqi@0: * the parser must be pointing at a start tag
aoqi@0: * that encloses the XML type that this {@link Bridge} is
aoqi@0: * instanciated for.
aoqi@0: *
aoqi@0: * @return
aoqi@0: * never null.
aoqi@0: *
aoqi@0: * @throws JAXBException
aoqi@0: * if there was an error while unmarshalling.
aoqi@0: *
aoqi@0: * @since 2.0 EA1
aoqi@0: */
aoqi@0: public final @NotNull T unmarshal(@NotNull Source in) throws JAXBException {
aoqi@0: return unmarshal(in,null);
aoqi@0: }
aoqi@0: /**
aoqi@0: * @since 2.0.3
aoqi@0: */
aoqi@0: public final @NotNull T unmarshal(@NotNull Source in, @Nullable AttachmentUnmarshaller au) throws JAXBException {
aoqi@0: Unmarshaller u = context.unmarshallerPool.take();
aoqi@0: u.setAttachmentUnmarshaller(au);
aoqi@0: return exit(unmarshal(u,in),u);
aoqi@0: }
aoqi@0: public final @NotNull T unmarshal(@NotNull BridgeContext context, @NotNull Source in) throws JAXBException {
aoqi@0: return unmarshal( ((BridgeContextImpl)context).unmarshaller, in );
aoqi@0: }
aoqi@0: public abstract @NotNull T unmarshal(@NotNull Unmarshaller u, @NotNull Source in) throws JAXBException;
aoqi@0:
aoqi@0: /**
aoqi@0: * Unmarshals the specified type object.
aoqi@0: *
aoqi@0: * @param in
aoqi@0: * the parser must be pointing at a start tag
aoqi@0: * that encloses the XML type that this {@link Bridge} is
aoqi@0: * instanciated for.
aoqi@0: *
aoqi@0: * @return
aoqi@0: * never null.
aoqi@0: *
aoqi@0: * @throws JAXBException
aoqi@0: * if there was an error while unmarshalling.
aoqi@0: *
aoqi@0: * @since 2.0 EA1
aoqi@0: */
aoqi@0: public final @NotNull T unmarshal(@NotNull InputStream in) throws JAXBException {
aoqi@0: Unmarshaller u = context.unmarshallerPool.take();
aoqi@0: return exit(unmarshal(u,in),u);
aoqi@0: }
aoqi@0: public final @NotNull T unmarshal(@NotNull BridgeContext context, @NotNull InputStream in) throws JAXBException {
aoqi@0: return unmarshal( ((BridgeContextImpl)context).unmarshaller, in );
aoqi@0: }
aoqi@0: public abstract @NotNull T unmarshal(@NotNull Unmarshaller u, @NotNull InputStream in) throws JAXBException;
aoqi@0:
aoqi@0: /**
aoqi@0: * Unmarshals the specified type object.
aoqi@0: *
aoqi@0: * @param n
aoqi@0: * Node to be unmarshalled.
aoqi@0: *
aoqi@0: * @return
aoqi@0: * never null.
aoqi@0: *
aoqi@0: * @throws JAXBException
aoqi@0: * if there was an error while unmarshalling.
aoqi@0: *
aoqi@0: * @since 2.0 FCS
aoqi@0: */
aoqi@0: public final @NotNull T unmarshal(@NotNull Node n) throws JAXBException {
aoqi@0: return unmarshal(n,null);
aoqi@0: }
aoqi@0: /**
aoqi@0: * @since 2.0.3
aoqi@0: */
aoqi@0: public final @NotNull T unmarshal(@NotNull Node n, @Nullable AttachmentUnmarshaller au) throws JAXBException {
aoqi@0: Unmarshaller u = context.unmarshallerPool.take();
aoqi@0: u.setAttachmentUnmarshaller(au);
aoqi@0: return exit(unmarshal(u,n),u);
aoqi@0: }
aoqi@0: public final @NotNull T unmarshal(@NotNull BridgeContext context, @NotNull Node n) throws JAXBException {
aoqi@0: return unmarshal( ((BridgeContextImpl)context).unmarshaller, n );
aoqi@0: }
aoqi@0: public abstract @NotNull T unmarshal(@NotNull Unmarshaller context, @NotNull Node n) throws JAXBException;
aoqi@0:
aoqi@0: /**
aoqi@0: * Gets the {@link TypeReference} from which this bridge was created.
aoqi@0: */
aoqi@0: public abstract TypeReference getTypeReference();
aoqi@0: }