src/share/jaxws_classes/com/sun/xml/internal/bind/api/Bridge.java

Thu, 12 Oct 2017 19:44:07 +0800

author
aoqi
date
Thu, 12 Oct 2017 19:44:07 +0800
changeset 760
e530533619ec
parent 0
373ffda63c9a
permissions
-rw-r--r--

merge

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

mercurial