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

Thu, 31 Aug 2017 15:18:52 +0800

author
aoqi
date
Thu, 31 Aug 2017 15:18:52 +0800
changeset 637
9c07ef4934dd
parent 397
b99d7e355d4b
parent 0
373ffda63c9a
permissions
-rw-r--r--

merge

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

mercurial