src/share/jaxws_classes/com/sun/xml/internal/xsom/parser/XSOMParser.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/com/sun/xml/internal/xsom/parser/XSOMParser.java	Wed Apr 27 01:27:09 2016 +0800
     1.3 @@ -0,0 +1,271 @@
     1.4 +/*
     1.5 + * Copyright (c) 1997, 2012, 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 com.sun.xml.internal.xsom.parser;
    1.30 +
    1.31 +import java.io.File;
    1.32 +import java.io.IOException;
    1.33 +import java.io.InputStream;
    1.34 +import java.io.Reader;
    1.35 +import java.net.URL;
    1.36 +import java.util.Set;
    1.37 +import java.util.HashSet;
    1.38 +
    1.39 +import javax.xml.parsers.SAXParserFactory;
    1.40 +
    1.41 +import org.xml.sax.ContentHandler;
    1.42 +import org.xml.sax.EntityResolver;
    1.43 +import org.xml.sax.ErrorHandler;
    1.44 +import org.xml.sax.InputSource;
    1.45 +import org.xml.sax.SAXException;
    1.46 +
    1.47 +import com.sun.xml.internal.xsom.XSSchemaSet;
    1.48 +import com.sun.xml.internal.xsom.impl.parser.NGCCRuntimeEx;
    1.49 +import com.sun.xml.internal.xsom.impl.parser.ParserContext;
    1.50 +import com.sun.xml.internal.xsom.impl.parser.state.Schema;
    1.51 +
    1.52 +/**
    1.53 + * Parses possibly multiple W3C XML Schema files and compose
    1.54 + * them into one grammar.
    1.55 + *
    1.56 + * @author
    1.57 + *     Kohsuke Kawaguchi (kohsuke.kawaguchi@sun.com)
    1.58 + */
    1.59 +public final class XSOMParser {
    1.60 +
    1.61 +    private EntityResolver entityResolver;
    1.62 +    private ErrorHandler userErrorHandler;
    1.63 +
    1.64 +    private AnnotationParserFactory apFactory;
    1.65 +
    1.66 +    private final ParserContext context;
    1.67 +
    1.68 +    /**
    1.69 +    * Creates a new XSOMParser by using a SAX parser from JAXP.
    1.70 +     * @deprecated Unsafe, use XSOMParser(factory) instead with
    1.71 +     * security features initialized by setting
    1.72 +     * XMLConstants.FEATURE_SECURE_PROCESSING feature.
    1.73 +    */
    1.74 +   public XSOMParser() {
    1.75 +       this(new JAXPParser());
    1.76 +   }
    1.77 +
    1.78 +   /**
    1.79 +    * Creates a new XSOMParser that uses the given SAXParserFactory
    1.80 +    * for creating new SAX parsers.
    1.81 +    *
    1.82 +    * The caller needs to configure
    1.83 +    * it properly. Don't forget to call <code>setNamespaceAware(true)</code>
    1.84 +    * or you'll see some strange errors.
    1.85 +    */
    1.86 +   public XSOMParser( SAXParserFactory factory ) {
    1.87 +       this( new JAXPParser(factory) );
    1.88 +   }
    1.89 +
    1.90 +   /**
    1.91 +    * Creates a new XSOMParser that reads XML Schema from non-standard
    1.92 +    * inputs.
    1.93 +    *
    1.94 +    * By implementing the {@link XMLParser} interface, XML Schema
    1.95 +    * can be read from something other than XML.
    1.96 +    *
    1.97 +    * @param   parser
    1.98 +    *      This parser will be called to parse XML Schema documents.
    1.99 +    */
   1.100 +    public XSOMParser(XMLParser parser) {
   1.101 +        context = new ParserContext(this,parser);
   1.102 +    }
   1.103 +
   1.104 +    /**
   1.105 +     * Parses a new XML Schema document.
   1.106 +     *
   1.107 +     * <p>
   1.108 +     * When using this method, XSOM does not know the system ID of
   1.109 +     * this document, therefore, when this stream contains relative
   1.110 +     * references to other schemas, XSOM will fail to resolve them.
   1.111 +     * To specify an system ID with a stream, use {@link InputSource}
   1.112 +     */
   1.113 +    public void parse( InputStream is ) throws SAXException {
   1.114 +        parse(new InputSource(is));
   1.115 +    }
   1.116 +
   1.117 +    /**
   1.118 +     * Parses a new XML Schema document.
   1.119 +     *
   1.120 +     * <p>
   1.121 +     * When using this method, XSOM does not know the system ID of
   1.122 +     * this document, therefore, when this reader contains relative
   1.123 +     * references to other schemas, XSOM will fail to resolve them.
   1.124 +     * To specify an system ID with a reader, use {@link InputSource}
   1.125 +     */
   1.126 +    public void parse( Reader reader ) throws SAXException {
   1.127 +        parse(new InputSource(reader));
   1.128 +    }
   1.129 +
   1.130 +    /**
   1.131 +     * Parses a new XML Schema document.
   1.132 +     */
   1.133 +    public void parse( File schema ) throws SAXException, IOException {
   1.134 +        parse(schema.toURL());
   1.135 +    }
   1.136 +
   1.137 +    /**
   1.138 +     * Parses a new XML Schema document.
   1.139 +     */
   1.140 +    public void parse( URL url ) throws SAXException {
   1.141 +        parse( url.toExternalForm() );
   1.142 +    }
   1.143 +
   1.144 +    /**
   1.145 +     * Parses a new XML Schema document.
   1.146 +     */
   1.147 +    public void parse( String systemId ) throws SAXException {
   1.148 +        parse(new InputSource(systemId));
   1.149 +    }
   1.150 +
   1.151 +    /**
   1.152 +     * Parses a new XML Schema document.
   1.153 +     *
   1.154 +     * <p>
   1.155 +     * Note that if the {@link InputSource} does not have a system ID,
   1.156 +     * XSOM will fail to resolve them.
   1.157 +     */
   1.158 +    public void parse( InputSource source ) throws SAXException {
   1.159 +        context.parse(source);
   1.160 +    }
   1.161 +
   1.162 +
   1.163 +
   1.164 +    /**
   1.165 +     * Gets the parser implemented as a ContentHandler.
   1.166 +     *
   1.167 +     * One can feed XML Schema as SAX events to this interface to
   1.168 +     * parse a schema. To parse multiple schema files, feed multiple
   1.169 +     * sets of events.
   1.170 +     *
   1.171 +     * <p>
   1.172 +     * If you don't send a complete event sequence from a startDocument
   1.173 +     * event to an endDocument event, the state of XSOMParser can become
   1.174 +     * unstable. This sometimes happen when you encounter an error while
   1.175 +     * generating SAX events. Don't call the getResult method in that case.
   1.176 +     *
   1.177 +     * <p>
   1.178 +     * This way of reading XML Schema can be useful when XML Schema is
   1.179 +     * not available as a stand-alone XML document.
   1.180 +     * For example, one can feed XML Schema inside a WSDL document.
   1.181 +     */
   1.182 +    public ContentHandler getParserHandler() {
   1.183 +        NGCCRuntimeEx runtime = context.newNGCCRuntime();
   1.184 +        Schema s = new Schema(runtime,false,null);
   1.185 +        runtime.setRootHandler(s);
   1.186 +        return runtime;
   1.187 +    }
   1.188 +
   1.189 +    /**
   1.190 +     * Gets the parsed result. Don't call this method until
   1.191 +     * you parse all the schemas.
   1.192 +     *
   1.193 +     * @return
   1.194 +     *      If there was any parse error, this method returns null.
   1.195 +     *      To receive error information, specify your error handler
   1.196 +     *      through the setErrorHandler method.
   1.197 +     * @exception SAXException
   1.198 +     *      This exception will never be thrown unless it is thrown
   1.199 +     *      by an error handler.
   1.200 +     */
   1.201 +    public XSSchemaSet getResult() throws SAXException {
   1.202 +        return context.getResult();
   1.203 +    }
   1.204 +
   1.205 +    /**
   1.206 +     * Gets the set of {@link SchemaDocument} that represents
   1.207 +     * parsed documents so far.
   1.208 +     *
   1.209 +     * @return
   1.210 +     *      can be empty but never null.
   1.211 +     */
   1.212 +    public Set<SchemaDocument> getDocuments() {
   1.213 +        return new HashSet<SchemaDocument>(context.parsedDocuments.keySet());
   1.214 +    }
   1.215 +
   1.216 +    public EntityResolver getEntityResolver() {
   1.217 +        return entityResolver;
   1.218 +    }
   1.219 +    /**
   1.220 +     * Set an entity resolver that is used to resolve things
   1.221 +     * like &lt;xsd:import> and &lt;xsd:include>.
   1.222 +     */
   1.223 +    public void setEntityResolver( EntityResolver resolver ) {
   1.224 +        this.entityResolver = resolver;
   1.225 +    }
   1.226 +    public ErrorHandler getErrorHandler() {
   1.227 +        return userErrorHandler;
   1.228 +    }
   1.229 +    /**
   1.230 +     * Set an error handler that receives all the errors encountered
   1.231 +     * during the parsing.
   1.232 +     */
   1.233 +    public void setErrorHandler(ErrorHandler errorHandler) {
   1.234 +        this.userErrorHandler = errorHandler;
   1.235 +    }
   1.236 +
   1.237 +    /**
   1.238 +     * Sets the annotation parser.
   1.239 +     *
   1.240 +     * Annotation parser can be used to parse application-specific
   1.241 +     * annotations inside a schema.
   1.242 +     *
   1.243 +     * <p>
   1.244 +     * For each annotation, new instance of this class will be
   1.245 +     * created and used to parse &lt;xs:annotation>.
   1.246 +     */
   1.247 +    public void setAnnotationParser( final Class annParser ) {
   1.248 +        setAnnotationParser( new AnnotationParserFactory() {
   1.249 +            public AnnotationParser create() {
   1.250 +                try {
   1.251 +                    return (AnnotationParser)annParser.newInstance();
   1.252 +                } catch( InstantiationException e ) {
   1.253 +                    throw new InstantiationError(e.getMessage());
   1.254 +                } catch( IllegalAccessException e ) {
   1.255 +                    throw new IllegalAccessError(e.getMessage());
   1.256 +                }
   1.257 +            }
   1.258 +        });
   1.259 +    }
   1.260 +
   1.261 +    /**
   1.262 +     * Sets the annotation parser factory.
   1.263 +     *
   1.264 +     * <p>
   1.265 +     * The specified factory will be used to create AnnotationParsers.
   1.266 +     */
   1.267 +    public void setAnnotationParser( AnnotationParserFactory factory ) {
   1.268 +        this.apFactory = factory;
   1.269 +    }
   1.270 +
   1.271 +    public AnnotationParserFactory getAnnotationParserFactory() {
   1.272 +        return apFactory;
   1.273 +    }
   1.274 +}

mercurial