src/share/jaxws_classes/javax/xml/bind/util/JAXBSource.java

Fri, 23 Aug 2013 09:57:21 +0100

author
mkos
date
Fri, 23 Aug 2013 09:57:21 +0100
changeset 397
b99d7e355d4b
parent 286
f50545b5e2f1
child 637
9c07ef4934dd
permissions
-rw-r--r--

8022885: Update JAX-WS RI integration to 2.2.9-b14140
8013016: Rebase 8009009 against the latest jdk8/jaxws
Reviewed-by: alanb, chegar

ohair@286 1 /*
mkos@397 2 * Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved.
ohair@286 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
ohair@286 4 *
ohair@286 5 * This code is free software; you can redistribute it and/or modify it
ohair@286 6 * under the terms of the GNU General Public License version 2 only, as
ohair@286 7 * published by the Free Software Foundation. Oracle designates this
ohair@286 8 * particular file as subject to the "Classpath" exception as provided
ohair@286 9 * by Oracle in the LICENSE file that accompanied this code.
ohair@286 10 *
ohair@286 11 * This code is distributed in the hope that it will be useful, but WITHOUT
ohair@286 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
ohair@286 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
ohair@286 14 * version 2 for more details (a copy is included in the LICENSE file that
ohair@286 15 * accompanied this code).
ohair@286 16 *
ohair@286 17 * You should have received a copy of the GNU General Public License version
ohair@286 18 * 2 along with this work; if not, write to the Free Software Foundation,
ohair@286 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
ohair@286 20 *
ohair@286 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ohair@286 22 * or visit www.oracle.com if you need additional information or have any
ohair@286 23 * questions.
ohair@286 24 */
ohair@286 25
ohair@286 26 package javax.xml.bind.util;
ohair@286 27
ohair@286 28 import org.xml.sax.ContentHandler;
ohair@286 29 import org.xml.sax.DTDHandler;
ohair@286 30 import org.xml.sax.EntityResolver;
ohair@286 31 import org.xml.sax.ErrorHandler;
ohair@286 32 import org.xml.sax.InputSource;
ohair@286 33 import org.xml.sax.SAXException;
ohair@286 34 import org.xml.sax.SAXNotRecognizedException;
ohair@286 35 import org.xml.sax.SAXParseException;
ohair@286 36 import org.xml.sax.XMLReader;
ohair@286 37 import org.xml.sax.ext.LexicalHandler;
ohair@286 38 import org.xml.sax.helpers.XMLFilterImpl;
ohair@286 39
ohair@286 40 import javax.xml.bind.JAXBContext;
ohair@286 41 import javax.xml.bind.JAXBException;
ohair@286 42 import javax.xml.bind.Marshaller;
ohair@286 43 import javax.xml.transform.sax.SAXSource;
mkos@397 44 import org.xml.sax.XMLFilter;
ohair@286 45
ohair@286 46 /**
ohair@286 47 * JAXP {@link javax.xml.transform.Source} implementation
ohair@286 48 * that marshals a JAXB-generated object.
ohair@286 49 *
ohair@286 50 * <p>
ohair@286 51 * This utility class is useful to combine JAXB with
ohair@286 52 * other Java/XML technologies.
ohair@286 53 *
ohair@286 54 * <p>
ohair@286 55 * The following example shows how to use JAXB to marshal a document
ohair@286 56 * for transformation by XSLT.
ohair@286 57 *
ohair@286 58 * <blockquote>
ohair@286 59 * <pre>
ohair@286 60 * MyObject o = // get JAXB content tree
ohair@286 61 *
ohair@286 62 * // jaxbContext is a JAXBContext object from which 'o' is created.
ohair@286 63 * JAXBSource source = new JAXBSource( jaxbContext, o );
ohair@286 64 *
ohair@286 65 * // set up XSLT transformation
ohair@286 66 * TransformerFactory tf = TransformerFactory.newInstance();
ohair@286 67 * Transformer t = tf.newTransformer(new StreamSource("test.xsl"));
ohair@286 68 *
ohair@286 69 * // run transformation
ohair@286 70 * t.transform(source,new StreamResult(System.out));
ohair@286 71 * </pre>
ohair@286 72 * </blockquote>
ohair@286 73 *
ohair@286 74 * <p>
ohair@286 75 * The fact that JAXBSource derives from SAXSource is an implementation
ohair@286 76 * detail. Thus in general applications are strongly discouraged from
ohair@286 77 * accessing methods defined on SAXSource. In particular,
ohair@286 78 * the setXMLReader and setInputSource methods shall never be called.
ohair@286 79 * The XMLReader object obtained by the getXMLReader method shall
ohair@286 80 * be used only for parsing the InputSource object returned by
ohair@286 81 * the getInputSource method.
ohair@286 82 *
ohair@286 83 * <p>
ohair@286 84 * Similarly the InputSource object obtained by the getInputSource
ohair@286 85 * method shall be used only for being parsed by the XMLReader object
ohair@286 86 * returned by the getXMLReader.
ohair@286 87 *
ohair@286 88 * @author
ohair@286 89 * Kohsuke Kawaguchi (kohsuke.kawaguchi@sun.com)
ohair@286 90 */
ohair@286 91 public class JAXBSource extends SAXSource {
ohair@286 92
ohair@286 93 /**
ohair@286 94 * Creates a new {@link javax.xml.transform.Source} for the given content object.
ohair@286 95 *
ohair@286 96 * @param context
ohair@286 97 * JAXBContext that was used to create
ohair@286 98 * <code>contentObject</code>. This context is used
ohair@286 99 * to create a new instance of marshaller and must not be null.
ohair@286 100 * @param contentObject
ohair@286 101 * An instance of a JAXB-generated class, which will be
ohair@286 102 * used as a {@link javax.xml.transform.Source} (by marshalling it into XML). It must
ohair@286 103 * not be null.
ohair@286 104 * @throws JAXBException if an error is encountered while creating the
ohair@286 105 * JAXBSource or if either of the parameters are null.
ohair@286 106 */
ohair@286 107 public JAXBSource( JAXBContext context, Object contentObject )
ohair@286 108 throws JAXBException {
ohair@286 109
ohair@286 110 this(
ohair@286 111 ( context == null ) ?
ohair@286 112 assertionFailed( Messages.format( Messages.SOURCE_NULL_CONTEXT ) ) :
ohair@286 113 context.createMarshaller(),
ohair@286 114
ohair@286 115 ( contentObject == null ) ?
ohair@286 116 assertionFailed( Messages.format( Messages.SOURCE_NULL_CONTENT ) ) :
ohair@286 117 contentObject);
ohair@286 118 }
ohair@286 119
ohair@286 120 /**
ohair@286 121 * Creates a new {@link javax.xml.transform.Source} for the given content object.
ohair@286 122 *
ohair@286 123 * @param marshaller
ohair@286 124 * A marshaller instance that will be used to marshal
ohair@286 125 * <code>contentObject</code> into XML. This must be
ohair@286 126 * created from a JAXBContext that was used to build
ohair@286 127 * <code>contentObject</code> and must not be null.
ohair@286 128 * @param contentObject
ohair@286 129 * An instance of a JAXB-generated class, which will be
ohair@286 130 * used as a {@link javax.xml.transform.Source} (by marshalling it into XML). It must
ohair@286 131 * not be null.
ohair@286 132 * @throws JAXBException if an error is encountered while creating the
ohair@286 133 * JAXBSource or if either of the parameters are null.
ohair@286 134 */
ohair@286 135 public JAXBSource( Marshaller marshaller, Object contentObject )
ohair@286 136 throws JAXBException {
ohair@286 137
ohair@286 138 if( marshaller == null )
ohair@286 139 throw new JAXBException(
ohair@286 140 Messages.format( Messages.SOURCE_NULL_MARSHALLER ) );
ohair@286 141
ohair@286 142 if( contentObject == null )
ohair@286 143 throw new JAXBException(
ohair@286 144 Messages.format( Messages.SOURCE_NULL_CONTENT ) );
ohair@286 145
ohair@286 146 this.marshaller = marshaller;
ohair@286 147 this.contentObject = contentObject;
ohair@286 148
ohair@286 149 super.setXMLReader(pseudoParser);
ohair@286 150 // pass a dummy InputSource. We don't care
ohair@286 151 super.setInputSource(new InputSource());
ohair@286 152 }
ohair@286 153
ohair@286 154 private final Marshaller marshaller;
ohair@286 155 private final Object contentObject;
ohair@286 156
ohair@286 157 // this object will pretend as an XMLReader.
ohair@286 158 // no matter what parameter is specified to the parse method,
ohair@286 159 // it just parse the contentObject.
ohair@286 160 private final XMLReader pseudoParser = new XMLReader() {
ohair@286 161 public boolean getFeature(String name) throws SAXNotRecognizedException {
ohair@286 162 if(name.equals("http://xml.org/sax/features/namespaces"))
ohair@286 163 return true;
ohair@286 164 if(name.equals("http://xml.org/sax/features/namespace-prefixes"))
ohair@286 165 return false;
ohair@286 166 throw new SAXNotRecognizedException(name);
ohair@286 167 }
ohair@286 168
ohair@286 169 public void setFeature(String name, boolean value) throws SAXNotRecognizedException {
ohair@286 170 if(name.equals("http://xml.org/sax/features/namespaces") && value)
ohair@286 171 return;
ohair@286 172 if(name.equals("http://xml.org/sax/features/namespace-prefixes") && !value)
ohair@286 173 return;
ohair@286 174 throw new SAXNotRecognizedException(name);
ohair@286 175 }
ohair@286 176
ohair@286 177 public Object getProperty(String name) throws SAXNotRecognizedException {
ohair@286 178 if( "http://xml.org/sax/properties/lexical-handler".equals(name) ) {
ohair@286 179 return lexicalHandler;
ohair@286 180 }
ohair@286 181 throw new SAXNotRecognizedException(name);
ohair@286 182 }
ohair@286 183
ohair@286 184 public void setProperty(String name, Object value) throws SAXNotRecognizedException {
ohair@286 185 if( "http://xml.org/sax/properties/lexical-handler".equals(name) ) {
ohair@286 186 this.lexicalHandler = (LexicalHandler)value;
ohair@286 187 return;
ohair@286 188 }
ohair@286 189 throw new SAXNotRecognizedException(name);
ohair@286 190 }
ohair@286 191
ohair@286 192 private LexicalHandler lexicalHandler;
ohair@286 193
ohair@286 194 // we will store this value but never use it by ourselves.
ohair@286 195 private EntityResolver entityResolver;
ohair@286 196 public void setEntityResolver(EntityResolver resolver) {
ohair@286 197 this.entityResolver = resolver;
ohair@286 198 }
ohair@286 199 public EntityResolver getEntityResolver() {
ohair@286 200 return entityResolver;
ohair@286 201 }
ohair@286 202
ohair@286 203 private DTDHandler dtdHandler;
ohair@286 204 public void setDTDHandler(DTDHandler handler) {
ohair@286 205 this.dtdHandler = handler;
ohair@286 206 }
ohair@286 207 public DTDHandler getDTDHandler() {
ohair@286 208 return dtdHandler;
ohair@286 209 }
ohair@286 210
ohair@286 211 // SAX allows ContentHandler to be changed during the parsing,
ohair@286 212 // but JAXB doesn't. So this repeater will sit between those
ohair@286 213 // two components.
mkos@397 214 private XMLFilter repeater = new XMLFilterImpl();
ohair@286 215
ohair@286 216 public void setContentHandler(ContentHandler handler) {
ohair@286 217 repeater.setContentHandler(handler);
ohair@286 218 }
ohair@286 219 public ContentHandler getContentHandler() {
ohair@286 220 return repeater.getContentHandler();
ohair@286 221 }
ohair@286 222
ohair@286 223 private ErrorHandler errorHandler;
ohair@286 224 public void setErrorHandler(ErrorHandler handler) {
ohair@286 225 this.errorHandler = handler;
ohair@286 226 }
ohair@286 227 public ErrorHandler getErrorHandler() {
ohair@286 228 return errorHandler;
ohair@286 229 }
ohair@286 230
ohair@286 231 public void parse(InputSource input) throws SAXException {
ohair@286 232 parse();
ohair@286 233 }
ohair@286 234
ohair@286 235 public void parse(String systemId) throws SAXException {
ohair@286 236 parse();
ohair@286 237 }
ohair@286 238
ohair@286 239 public void parse() throws SAXException {
ohair@286 240 // parses a content object by using the given marshaller
ohair@286 241 // SAX events will be sent to the repeater, and the repeater
ohair@286 242 // will further forward it to an appropriate component.
ohair@286 243 try {
mkos@397 244 marshaller.marshal( contentObject, (XMLFilterImpl)repeater );
ohair@286 245 } catch( JAXBException e ) {
ohair@286 246 // wrap it to a SAXException
ohair@286 247 SAXParseException se =
ohair@286 248 new SAXParseException( e.getMessage(),
ohair@286 249 null, null, -1, -1, e );
ohair@286 250
ohair@286 251 // if the consumer sets an error handler, it is our responsibility
ohair@286 252 // to notify it.
ohair@286 253 if(errorHandler!=null)
ohair@286 254 errorHandler.fatalError(se);
ohair@286 255
ohair@286 256 // this is a fatal error. Even if the error handler
ohair@286 257 // returns, we will abort anyway.
ohair@286 258 throw se;
ohair@286 259 }
ohair@286 260 }
ohair@286 261 };
ohair@286 262
ohair@286 263 /**
ohair@286 264 * Hook to throw exception from the middle of a contructor chained call
ohair@286 265 * to this
ohair@286 266 */
ohair@286 267 private static Marshaller assertionFailed( String message )
ohair@286 268 throws JAXBException {
ohair@286 269
ohair@286 270 throw new JAXBException( message );
ohair@286 271 }
ohair@286 272 }

mercurial