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

changeset 0
373ffda63c9a
child 637
9c07ef4934dd
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/jaxws_classes/javax/xml/bind/util/JAXBSource.java	Wed Apr 27 01:27:09 2016 +0800
     1.3 @@ -0,0 +1,272 @@
     1.4 +/*
     1.5 + * Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.  Oracle designates this
    1.11 + * particular file as subject to the "Classpath" exception as provided
    1.12 + * by Oracle in the LICENSE file that accompanied this code.
    1.13 + *
    1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.17 + * version 2 for more details (a copy is included in the LICENSE file that
    1.18 + * accompanied this code).
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License version
    1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.23 + *
    1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.25 + * or visit www.oracle.com if you need additional information or have any
    1.26 + * questions.
    1.27 + */
    1.28 +
    1.29 +package javax.xml.bind.util;
    1.30 +
    1.31 +import org.xml.sax.ContentHandler;
    1.32 +import org.xml.sax.DTDHandler;
    1.33 +import org.xml.sax.EntityResolver;
    1.34 +import org.xml.sax.ErrorHandler;
    1.35 +import org.xml.sax.InputSource;
    1.36 +import org.xml.sax.SAXException;
    1.37 +import org.xml.sax.SAXNotRecognizedException;
    1.38 +import org.xml.sax.SAXParseException;
    1.39 +import org.xml.sax.XMLReader;
    1.40 +import org.xml.sax.ext.LexicalHandler;
    1.41 +import org.xml.sax.helpers.XMLFilterImpl;
    1.42 +
    1.43 +import javax.xml.bind.JAXBContext;
    1.44 +import javax.xml.bind.JAXBException;
    1.45 +import javax.xml.bind.Marshaller;
    1.46 +import javax.xml.transform.sax.SAXSource;
    1.47 +import org.xml.sax.XMLFilter;
    1.48 +
    1.49 +/**
    1.50 + * JAXP {@link javax.xml.transform.Source} implementation
    1.51 + * that marshals a JAXB-generated object.
    1.52 + *
    1.53 + * <p>
    1.54 + * This utility class is useful to combine JAXB with
    1.55 + * other Java/XML technologies.
    1.56 + *
    1.57 + * <p>
    1.58 + * The following example shows how to use JAXB to marshal a document
    1.59 + * for transformation by XSLT.
    1.60 + *
    1.61 + * <blockquote>
    1.62 + *    <pre>
    1.63 + *       MyObject o = // get JAXB content tree
    1.64 + *
    1.65 + *       // jaxbContext is a JAXBContext object from which 'o' is created.
    1.66 + *       JAXBSource source = new JAXBSource( jaxbContext, o );
    1.67 + *
    1.68 + *       // set up XSLT transformation
    1.69 + *       TransformerFactory tf = TransformerFactory.newInstance();
    1.70 + *       Transformer t = tf.newTransformer(new StreamSource("test.xsl"));
    1.71 + *
    1.72 + *       // run transformation
    1.73 + *       t.transform(source,new StreamResult(System.out));
    1.74 + *    </pre>
    1.75 + * </blockquote>
    1.76 + *
    1.77 + * <p>
    1.78 + * The fact that JAXBSource derives from SAXSource is an implementation
    1.79 + * detail. Thus in general applications are strongly discouraged from
    1.80 + * accessing methods defined on SAXSource. In particular,
    1.81 + * the setXMLReader and setInputSource methods shall never be called.
    1.82 + * The XMLReader object obtained by the getXMLReader method shall
    1.83 + * be used only for parsing the InputSource object returned by
    1.84 + * the getInputSource method.
    1.85 + *
    1.86 + * <p>
    1.87 + * Similarly the InputSource object obtained by the getInputSource
    1.88 + * method shall be used only for being parsed by the XMLReader object
    1.89 + * returned by the getXMLReader.
    1.90 + *
    1.91 + * @author
    1.92 + *      Kohsuke Kawaguchi (kohsuke.kawaguchi@sun.com)
    1.93 + */
    1.94 +public class JAXBSource extends SAXSource {
    1.95 +
    1.96 +    /**
    1.97 +     * Creates a new {@link javax.xml.transform.Source} for the given content object.
    1.98 +     *
    1.99 +     * @param   context
   1.100 +     *      JAXBContext that was used to create
   1.101 +     *      <code>contentObject</code>. This context is used
   1.102 +     *      to create a new instance of marshaller and must not be null.
   1.103 +     * @param   contentObject
   1.104 +     *      An instance of a JAXB-generated class, which will be
   1.105 +     *      used as a {@link javax.xml.transform.Source} (by marshalling it into XML).  It must
   1.106 +     *      not be null.
   1.107 +     * @throws JAXBException if an error is encountered while creating the
   1.108 +     * JAXBSource or if either of the parameters are null.
   1.109 +     */
   1.110 +    public JAXBSource( JAXBContext context, Object contentObject )
   1.111 +        throws JAXBException {
   1.112 +
   1.113 +        this(
   1.114 +            ( context == null ) ?
   1.115 +                assertionFailed( Messages.format( Messages.SOURCE_NULL_CONTEXT ) ) :
   1.116 +                context.createMarshaller(),
   1.117 +
   1.118 +            ( contentObject == null ) ?
   1.119 +                assertionFailed( Messages.format( Messages.SOURCE_NULL_CONTENT ) ) :
   1.120 +                contentObject);
   1.121 +    }
   1.122 +
   1.123 +    /**
   1.124 +     * Creates a new {@link javax.xml.transform.Source} for the given content object.
   1.125 +     *
   1.126 +     * @param   marshaller
   1.127 +     *      A marshaller instance that will be used to marshal
   1.128 +     *      <code>contentObject</code> into XML. This must be
   1.129 +     *      created from a JAXBContext that was used to build
   1.130 +     *      <code>contentObject</code> and must not be null.
   1.131 +     * @param   contentObject
   1.132 +     *      An instance of a JAXB-generated class, which will be
   1.133 +     *      used as a {@link javax.xml.transform.Source} (by marshalling it into XML).  It must
   1.134 +     *      not be null.
   1.135 +     * @throws JAXBException if an error is encountered while creating the
   1.136 +     * JAXBSource or if either of the parameters are null.
   1.137 +     */
   1.138 +    public JAXBSource( Marshaller marshaller, Object contentObject )
   1.139 +        throws JAXBException {
   1.140 +
   1.141 +        if( marshaller == null )
   1.142 +            throw new JAXBException(
   1.143 +                Messages.format( Messages.SOURCE_NULL_MARSHALLER ) );
   1.144 +
   1.145 +        if( contentObject == null )
   1.146 +            throw new JAXBException(
   1.147 +                Messages.format( Messages.SOURCE_NULL_CONTENT ) );
   1.148 +
   1.149 +        this.marshaller = marshaller;
   1.150 +        this.contentObject = contentObject;
   1.151 +
   1.152 +        super.setXMLReader(pseudoParser);
   1.153 +        // pass a dummy InputSource. We don't care
   1.154 +        super.setInputSource(new InputSource());
   1.155 +    }
   1.156 +
   1.157 +    private final Marshaller marshaller;
   1.158 +    private final Object contentObject;
   1.159 +
   1.160 +    // this object will pretend as an XMLReader.
   1.161 +    // no matter what parameter is specified to the parse method,
   1.162 +    // it just parse the contentObject.
   1.163 +    private final XMLReader pseudoParser = new XMLReader() {
   1.164 +        public boolean getFeature(String name) throws SAXNotRecognizedException {
   1.165 +            if(name.equals("http://xml.org/sax/features/namespaces"))
   1.166 +                return true;
   1.167 +            if(name.equals("http://xml.org/sax/features/namespace-prefixes"))
   1.168 +                return false;
   1.169 +            throw new SAXNotRecognizedException(name);
   1.170 +        }
   1.171 +
   1.172 +        public void setFeature(String name, boolean value) throws SAXNotRecognizedException {
   1.173 +            if(name.equals("http://xml.org/sax/features/namespaces") && value)
   1.174 +                return;
   1.175 +            if(name.equals("http://xml.org/sax/features/namespace-prefixes") && !value)
   1.176 +                return;
   1.177 +            throw new SAXNotRecognizedException(name);
   1.178 +        }
   1.179 +
   1.180 +        public Object getProperty(String name) throws SAXNotRecognizedException {
   1.181 +            if( "http://xml.org/sax/properties/lexical-handler".equals(name) ) {
   1.182 +                return lexicalHandler;
   1.183 +            }
   1.184 +            throw new SAXNotRecognizedException(name);
   1.185 +        }
   1.186 +
   1.187 +        public void setProperty(String name, Object value) throws SAXNotRecognizedException {
   1.188 +            if( "http://xml.org/sax/properties/lexical-handler".equals(name) ) {
   1.189 +                this.lexicalHandler = (LexicalHandler)value;
   1.190 +                return;
   1.191 +            }
   1.192 +            throw new SAXNotRecognizedException(name);
   1.193 +        }
   1.194 +
   1.195 +        private LexicalHandler lexicalHandler;
   1.196 +
   1.197 +        // we will store this value but never use it by ourselves.
   1.198 +        private EntityResolver entityResolver;
   1.199 +        public void setEntityResolver(EntityResolver resolver) {
   1.200 +            this.entityResolver = resolver;
   1.201 +        }
   1.202 +        public EntityResolver getEntityResolver() {
   1.203 +            return entityResolver;
   1.204 +        }
   1.205 +
   1.206 +        private DTDHandler dtdHandler;
   1.207 +        public void setDTDHandler(DTDHandler handler) {
   1.208 +            this.dtdHandler = handler;
   1.209 +        }
   1.210 +        public DTDHandler getDTDHandler() {
   1.211 +            return dtdHandler;
   1.212 +        }
   1.213 +
   1.214 +        // SAX allows ContentHandler to be changed during the parsing,
   1.215 +        // but JAXB doesn't. So this repeater will sit between those
   1.216 +        // two components.
   1.217 +        private XMLFilter repeater = new XMLFilterImpl();
   1.218 +
   1.219 +        public void setContentHandler(ContentHandler handler) {
   1.220 +            repeater.setContentHandler(handler);
   1.221 +        }
   1.222 +        public ContentHandler getContentHandler() {
   1.223 +            return repeater.getContentHandler();
   1.224 +        }
   1.225 +
   1.226 +        private ErrorHandler errorHandler;
   1.227 +        public void setErrorHandler(ErrorHandler handler) {
   1.228 +            this.errorHandler = handler;
   1.229 +        }
   1.230 +        public ErrorHandler getErrorHandler() {
   1.231 +            return errorHandler;
   1.232 +        }
   1.233 +
   1.234 +        public void parse(InputSource input) throws SAXException {
   1.235 +            parse();
   1.236 +        }
   1.237 +
   1.238 +        public void parse(String systemId) throws SAXException {
   1.239 +            parse();
   1.240 +        }
   1.241 +
   1.242 +        public void parse() throws SAXException {
   1.243 +            // parses a content object by using the given marshaller
   1.244 +            // SAX events will be sent to the repeater, and the repeater
   1.245 +            // will further forward it to an appropriate component.
   1.246 +            try {
   1.247 +                marshaller.marshal( contentObject, (XMLFilterImpl)repeater );
   1.248 +            } catch( JAXBException e ) {
   1.249 +                // wrap it to a SAXException
   1.250 +                SAXParseException se =
   1.251 +                    new SAXParseException( e.getMessage(),
   1.252 +                        null, null, -1, -1, e );
   1.253 +
   1.254 +                // if the consumer sets an error handler, it is our responsibility
   1.255 +                // to notify it.
   1.256 +                if(errorHandler!=null)
   1.257 +                    errorHandler.fatalError(se);
   1.258 +
   1.259 +                // this is a fatal error. Even if the error handler
   1.260 +                // returns, we will abort anyway.
   1.261 +                throw se;
   1.262 +            }
   1.263 +        }
   1.264 +    };
   1.265 +
   1.266 +    /**
   1.267 +     * Hook to throw exception from the middle of a contructor chained call
   1.268 +     * to this
   1.269 +     */
   1.270 +    private static Marshaller assertionFailed( String message )
   1.271 +        throws JAXBException {
   1.272 +
   1.273 +        throw new JAXBException( message );
   1.274 +    }
   1.275 +}

mercurial