ohair@286: /* alanb@368: * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved. ohair@286: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. ohair@286: * ohair@286: * This code is free software; you can redistribute it and/or modify it ohair@286: * under the terms of the GNU General Public License version 2 only, as ohair@286: * published by the Free Software Foundation. Oracle designates this ohair@286: * particular file as subject to the "Classpath" exception as provided ohair@286: * by Oracle in the LICENSE file that accompanied this code. ohair@286: * ohair@286: * This code is distributed in the hope that it will be useful, but WITHOUT ohair@286: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or ohair@286: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License ohair@286: * version 2 for more details (a copy is included in the LICENSE file that ohair@286: * accompanied this code). ohair@286: * ohair@286: * You should have received a copy of the GNU General Public License version ohair@286: * 2 along with this work; if not, write to the Free Software Foundation, ohair@286: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. ohair@286: * ohair@286: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA ohair@286: * or visit www.oracle.com if you need additional information or have any ohair@286: * questions. ohair@286: */ ohair@286: ohair@286: package com.sun.xml.internal.ws.spi.db; ohair@286: ohair@286: import java.io.InputStream; ohair@286: import java.io.OutputStream; ohair@286: ohair@286: import javax.xml.bind.JAXBException; ohair@286: import javax.xml.bind.Marshaller; ohair@286: import javax.xml.bind.Unmarshaller; ohair@286: import javax.xml.bind.attachment.AttachmentMarshaller; ohair@286: import javax.xml.bind.attachment.AttachmentUnmarshaller; ohair@286: import javax.xml.namespace.NamespaceContext; ohair@286: import javax.xml.stream.XMLStreamReader; ohair@286: import javax.xml.stream.XMLStreamWriter; ohair@286: import javax.xml.transform.Result; ohair@286: import javax.xml.transform.Source; ohair@286: ohair@286: import com.sun.istack.internal.NotNull; ohair@286: import com.sun.istack.internal.Nullable; ohair@286: import com.sun.xml.internal.bind.api.BridgeContext; ohair@286: import com.sun.xml.internal.bind.v2.runtime.BridgeContextImpl; ohair@286: import com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl; ohair@286: ohair@286: import org.w3c.dom.Node; ohair@286: import org.xml.sax.ContentHandler; ohair@286: ohair@286: /** ohair@286: * Mini-marshaller/unmarshaller that is specialized for a particular ohair@286: * element name and a type. ohair@286: * ohair@286: *

ohair@286: * Instances of this class is stateless and multi-thread safe. ohair@286: * They are reentrant. ohair@286: * ohair@286: *

ohair@286: * All the marshal operation generates fragments. ohair@286: * ohair@286: *

ohair@286: * Subject to change without notice. ohair@286: * ohair@286: * @since JAXB 2.0 EA1 ohair@286: * @author Kohsuke Kawaguchi ohair@286: */ ohair@286: public abstract class OldBridge { ohair@286: protected OldBridge(JAXBContextImpl context) { ohair@286: this.context = context; ohair@286: } ohair@286: ohair@286: protected final JAXBContextImpl context; ohair@286: ohair@286: /** ohair@286: * Gets the {@link BindingContext} to which this object belongs. ohair@286: * ohair@286: * @since 2.1 ohair@286: */ ohair@286: public @NotNull BindingContext getContext() { ohair@286: // return context; ohair@286: return null; ohair@286: } ohair@286: ohair@286: /** ohair@286: * ohair@286: * @throws JAXBException ohair@286: * if there was an error while marshalling. ohair@286: * ohair@286: * @since 2.0 EA1 ohair@286: */ ohair@286: public final void marshal(T object,XMLStreamWriter output) throws JAXBException { ohair@286: marshal(object,output,null); ohair@286: } ohair@286: public final void marshal(T object,XMLStreamWriter output, AttachmentMarshaller am) throws JAXBException { ohair@286: Marshaller m = context.marshallerPool.take(); ohair@286: m.setAttachmentMarshaller(am); ohair@286: marshal(m,object,output); ohair@286: m.setAttachmentMarshaller(null); ohair@286: context.marshallerPool.recycle(m); ohair@286: } ohair@286: ohair@286: public final void marshal(@NotNull BridgeContext context,T object,XMLStreamWriter output) throws JAXBException { ohair@286: marshal( ((BridgeContextImpl)context).marshaller, object, output ); ohair@286: } ohair@286: ohair@286: public abstract void marshal(@NotNull Marshaller m,T object,XMLStreamWriter output) throws JAXBException; ohair@286: ohair@286: ohair@286: /** ohair@286: * Marshals the specified type object with the implicit element name ohair@286: * associated with this instance of {@link XMLBridge}. ohair@286: * ohair@286: * @param nsContext ohair@286: * if this marshalling is done to marshal a subelement, this {@link NamespaceContext} ohair@286: * represents in-scope namespace bindings available for that element. Can be null, ohair@286: * in which case JAXB assumes no in-scope namespaces. ohair@286: * @throws JAXBException ohair@286: * if there was an error while marshalling. ohair@286: * ohair@286: * @since 2.0 EA1 ohair@286: */ ohair@286: public void marshal(T object,OutputStream output, NamespaceContext nsContext) throws JAXBException { ohair@286: marshal(object,output,nsContext,null); ohair@286: } ohair@286: /** ohair@286: * @since 2.0.2 ohair@286: */ ohair@286: public void marshal(T object,OutputStream output, NamespaceContext nsContext, AttachmentMarshaller am) throws JAXBException { ohair@286: Marshaller m = context.marshallerPool.take(); ohair@286: m.setAttachmentMarshaller(am); ohair@286: marshal(m,object,output,nsContext); ohair@286: m.setAttachmentMarshaller(null); ohair@286: context.marshallerPool.recycle(m); ohair@286: } ohair@286: ohair@286: public final void marshal(@NotNull BridgeContext context,T object,OutputStream output, NamespaceContext nsContext) throws JAXBException { ohair@286: marshal( ((BridgeContextImpl)context).marshaller, object, output, nsContext ); ohair@286: } ohair@286: ohair@286: public abstract void marshal(@NotNull Marshaller m,T object,OutputStream output, NamespaceContext nsContext) throws JAXBException; ohair@286: ohair@286: ohair@286: public final void marshal(T object,Node output) throws JAXBException { ohair@286: Marshaller m = context.marshallerPool.take(); ohair@286: marshal(m,object,output); ohair@286: context.marshallerPool.recycle(m); ohair@286: } ohair@286: ohair@286: public final void marshal(@NotNull BridgeContext context,T object,Node output) throws JAXBException { ohair@286: marshal( ((BridgeContextImpl)context).marshaller, object, output ); ohair@286: } ohair@286: ohair@286: public abstract void marshal(@NotNull Marshaller m,T object,Node output) throws JAXBException; ohair@286: ohair@286: ohair@286: /** ohair@286: * @since 2.0 EA4 ohair@286: */ ohair@286: public final void marshal(T object, ContentHandler contentHandler) throws JAXBException { ohair@286: marshal(object,contentHandler,null); ohair@286: } ohair@286: /** ohair@286: * @since 2.0.2 ohair@286: */ ohair@286: public final void marshal(T object, ContentHandler contentHandler, AttachmentMarshaller am) throws JAXBException { ohair@286: Marshaller m = context.marshallerPool.take(); ohair@286: m.setAttachmentMarshaller(am); ohair@286: marshal(m,object,contentHandler); ohair@286: m.setAttachmentMarshaller(null); ohair@286: context.marshallerPool.recycle(m); ohair@286: } ohair@286: public final void marshal(@NotNull BridgeContext context,T object, ContentHandler contentHandler) throws JAXBException { ohair@286: marshal( ((BridgeContextImpl)context).marshaller, object, contentHandler ); ohair@286: } ohair@286: public abstract void marshal(@NotNull Marshaller m,T object, ContentHandler contentHandler) throws JAXBException; ohair@286: ohair@286: /** ohair@286: * @since 2.0 EA4 ohair@286: */ ohair@286: public final void marshal(T object, Result result) throws JAXBException { ohair@286: Marshaller m = context.marshallerPool.take(); ohair@286: marshal(m,object,result); ohair@286: context.marshallerPool.recycle(m); ohair@286: } ohair@286: public final void marshal(@NotNull BridgeContext context,T object, Result result) throws JAXBException { ohair@286: marshal( ((BridgeContextImpl)context).marshaller, object, result ); ohair@286: } ohair@286: public abstract void marshal(@NotNull Marshaller m,T object, Result result) throws JAXBException; ohair@286: ohair@286: ohair@286: ohair@286: private T exit(T r, Unmarshaller u) { ohair@286: u.setAttachmentUnmarshaller(null); ohair@286: context.unmarshallerPool.recycle(u); ohair@286: return r; ohair@286: } ohair@286: ohair@286: /** ohair@286: * Unmarshals the specified type object. ohair@286: * ohair@286: * @param in ohair@286: * the parser must be pointing at a start tag ohair@286: * that encloses the XML type that this {@link XMLBridge} is ohair@286: * instanciated for. ohair@286: * ohair@286: * @return ohair@286: * never null. ohair@286: * ohair@286: * @throws JAXBException ohair@286: * if there was an error while unmarshalling. ohair@286: * ohair@286: * @since 2.0 EA1 ohair@286: */ ohair@286: public final @NotNull T unmarshal(@NotNull XMLStreamReader in) throws JAXBException { ohair@286: return unmarshal(in,null); ohair@286: } ohair@286: /** ohair@286: * @since 2.0.3 ohair@286: */ ohair@286: public final @NotNull T unmarshal(@NotNull XMLStreamReader in, @Nullable AttachmentUnmarshaller au) throws JAXBException { ohair@286: Unmarshaller u = context.unmarshallerPool.take(); ohair@286: u.setAttachmentUnmarshaller(au); ohair@286: return exit(unmarshal(u,in),u); ohair@286: } ohair@286: public final @NotNull T unmarshal(@NotNull BridgeContext context, @NotNull XMLStreamReader in) throws JAXBException { ohair@286: return unmarshal( ((BridgeContextImpl)context).unmarshaller, in ); ohair@286: } ohair@286: public abstract @NotNull T unmarshal(@NotNull Unmarshaller u, @NotNull XMLStreamReader in) throws JAXBException; ohair@286: ohair@286: /** ohair@286: * Unmarshals the specified type object. ohair@286: * ohair@286: * @param in ohair@286: * the parser must be pointing at a start tag ohair@286: * that encloses the XML type that this {@link XMLBridge} is ohair@286: * instanciated for. ohair@286: * ohair@286: * @return ohair@286: * never null. ohair@286: * ohair@286: * @throws JAXBException ohair@286: * if there was an error while unmarshalling. ohair@286: * ohair@286: * @since 2.0 EA1 ohair@286: */ ohair@286: public final @NotNull T unmarshal(@NotNull Source in) throws JAXBException { ohair@286: return unmarshal(in,null); ohair@286: } ohair@286: /** ohair@286: * @since 2.0.3 ohair@286: */ ohair@286: public final @NotNull T unmarshal(@NotNull Source in, @Nullable AttachmentUnmarshaller au) throws JAXBException { ohair@286: Unmarshaller u = context.unmarshallerPool.take(); ohair@286: u.setAttachmentUnmarshaller(au); ohair@286: return exit(unmarshal(u,in),u); ohair@286: } ohair@286: public final @NotNull T unmarshal(@NotNull BridgeContext context, @NotNull Source in) throws JAXBException { ohair@286: return unmarshal( ((BridgeContextImpl)context).unmarshaller, in ); ohair@286: } ohair@286: public abstract @NotNull T unmarshal(@NotNull Unmarshaller u, @NotNull Source in) throws JAXBException; ohair@286: ohair@286: /** ohair@286: * Unmarshals the specified type object. ohair@286: * ohair@286: * @param in ohair@286: * the parser must be pointing at a start tag ohair@286: * that encloses the XML type that this {@link XMLBridge} is ohair@286: * instanciated for. ohair@286: * ohair@286: * @return ohair@286: * never null. ohair@286: * ohair@286: * @throws JAXBException ohair@286: * if there was an error while unmarshalling. ohair@286: * ohair@286: * @since 2.0 EA1 ohair@286: */ ohair@286: public final @NotNull T unmarshal(@NotNull InputStream in) throws JAXBException { ohair@286: Unmarshaller u = context.unmarshallerPool.take(); ohair@286: return exit(unmarshal(u,in),u); ohair@286: } ohair@286: public final @NotNull T unmarshal(@NotNull BridgeContext context, @NotNull InputStream in) throws JAXBException { ohair@286: return unmarshal( ((BridgeContextImpl)context).unmarshaller, in ); ohair@286: } ohair@286: public abstract @NotNull T unmarshal(@NotNull Unmarshaller u, @NotNull InputStream in) throws JAXBException; ohair@286: ohair@286: /** ohair@286: * Unmarshals the specified type object. ohair@286: * ohair@286: * @param n ohair@286: * Node to be unmarshalled. ohair@286: * ohair@286: * @return ohair@286: * never null. ohair@286: * ohair@286: * @throws JAXBException ohair@286: * if there was an error while unmarshalling. ohair@286: * ohair@286: * @since 2.0 FCS ohair@286: */ ohair@286: public final @NotNull T unmarshal(@NotNull Node n) throws JAXBException { ohair@286: return unmarshal(n,null); ohair@286: } ohair@286: /** ohair@286: * @since 2.0.3 ohair@286: */ ohair@286: public final @NotNull T unmarshal(@NotNull Node n, @Nullable AttachmentUnmarshaller au) throws JAXBException { ohair@286: Unmarshaller u = context.unmarshallerPool.take(); ohair@286: u.setAttachmentUnmarshaller(au); ohair@286: return exit(unmarshal(u,n),u); ohair@286: } ohair@286: public final @NotNull T unmarshal(@NotNull BridgeContext context, @NotNull Node n) throws JAXBException { ohair@286: return unmarshal( ((BridgeContextImpl)context).unmarshaller, n ); ohair@286: } ohair@286: public abstract @NotNull T unmarshal(@NotNull Unmarshaller context, @NotNull Node n) throws JAXBException; ohair@286: ohair@286: /** ohair@286: * Gets the {@link TypeInfo} from which this bridge was created. ohair@286: */ ohair@286: public abstract TypeInfo getTypeReference(); ohair@286: }