src/share/jaxws_classes/com/sun/xml/internal/xsom/parser/XSOMParser.java

Tue, 06 Mar 2012 16:09:35 -0800

author
ohair
date
Tue, 06 Mar 2012 16:09:35 -0800
changeset 286
f50545b5e2f1
child 397
b99d7e355d4b
permissions
-rw-r--r--

7150322: Stop using drop source bundles in jaxws
Reviewed-by: darcy, ohrstrom

     1 /*
     2  * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Oracle designates this
     8  * particular file as subject to the "Classpath" exception as provided
     9  * by Oracle in the LICENSE file that accompanied this code.
    10  *
    11  * This code is distributed in the hope that it will be useful, but WITHOUT
    12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    14  * version 2 for more details (a copy is included in the LICENSE file that
    15  * accompanied this code).
    16  *
    17  * You should have received a copy of the GNU General Public License version
    18  * 2 along with this work; if not, write to the Free Software Foundation,
    19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    20  *
    21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    22  * or visit www.oracle.com if you need additional information or have any
    23  * questions.
    24  */
    26 package com.sun.xml.internal.xsom.parser;
    28 import java.io.File;
    29 import java.io.IOException;
    30 import java.io.InputStream;
    31 import java.io.Reader;
    32 import java.net.URL;
    33 import java.util.Set;
    34 import java.util.HashSet;
    36 import javax.xml.parsers.SAXParserFactory;
    38 import org.xml.sax.ContentHandler;
    39 import org.xml.sax.EntityResolver;
    40 import org.xml.sax.ErrorHandler;
    41 import org.xml.sax.InputSource;
    42 import org.xml.sax.SAXException;
    44 import com.sun.xml.internal.xsom.XSSchemaSet;
    45 import com.sun.xml.internal.xsom.impl.parser.NGCCRuntimeEx;
    46 import com.sun.xml.internal.xsom.impl.parser.ParserContext;
    47 import com.sun.xml.internal.xsom.impl.parser.state.Schema;
    49 /**
    50  * Parses possibly multiple W3C XML Schema files and compose
    51  * them into one grammar.
    52  *
    53  * @author
    54  *     Kohsuke Kawaguchi (kohsuke.kawaguchi@sun.com)
    55  */
    56 public final class XSOMParser {
    58     private EntityResolver entityResolver;
    59     private ErrorHandler userErrorHandler;
    61     private AnnotationParserFactory apFactory;
    63     private final ParserContext context;
    65     /**
    66     * Creates a new XSOMParser by using a SAX parser from JAXP.
    67     */
    68    public XSOMParser() {
    69        this(new JAXPParser());
    70    }
    72    /**
    73     * Creates a new XSOMParser that uses the given SAXParserFactory
    74     * for creating new SAX parsers.
    75     *
    76     * The caller needs to configure
    77     * it properly. Don't forget to call <code>setNamespaceAware(true)</code>
    78     * or you'll see some strange errors.
    79     */
    80    public XSOMParser( SAXParserFactory factory ) {
    81        this( new JAXPParser(factory) );
    82    }
    84    /**
    85     * Creates a new XSOMParser that reads XML Schema from non-standard
    86     * inputs.
    87     *
    88     * By implementing the {@link XMLParser} interface, XML Schema
    89     * can be read from something other than XML.
    90     *
    91     * @param   parser
    92     *      This parser will be called to parse XML Schema documents.
    93     */
    94     public XSOMParser(XMLParser parser) {
    95         context = new ParserContext(this,parser);
    96     }
    98     /**
    99      * Parses a new XML Schema document.
   100      *
   101      * <p>
   102      * When using this method, XSOM does not know the system ID of
   103      * this document, therefore, when this stream contains relative
   104      * references to other schemas, XSOM will fail to resolve them.
   105      * To specify an system ID with a stream, use {@link InputSource}
   106      */
   107     public void parse( InputStream is ) throws SAXException {
   108         parse(new InputSource(is));
   109     }
   111     /**
   112      * Parses a new XML Schema document.
   113      *
   114      * <p>
   115      * When using this method, XSOM does not know the system ID of
   116      * this document, therefore, when this reader contains relative
   117      * references to other schemas, XSOM will fail to resolve them.
   118      * To specify an system ID with a reader, use {@link InputSource}
   119      */
   120     public void parse( Reader reader ) throws SAXException {
   121         parse(new InputSource(reader));
   122     }
   124     /**
   125      * Parses a new XML Schema document.
   126      */
   127     public void parse( File schema ) throws SAXException, IOException {
   128         parse(schema.toURL());
   129     }
   131     /**
   132      * Parses a new XML Schema document.
   133      */
   134     public void parse( URL url ) throws SAXException {
   135         parse( url.toExternalForm() );
   136     }
   138     /**
   139      * Parses a new XML Schema document.
   140      */
   141     public void parse( String systemId ) throws SAXException {
   142         parse(new InputSource(systemId));
   143     }
   145     /**
   146      * Parses a new XML Schema document.
   147      *
   148      * <p>
   149      * Note that if the {@link InputSource} does not have a system ID,
   150      * XSOM will fail to resolve them.
   151      */
   152     public void parse( InputSource source ) throws SAXException {
   153         context.parse(source);
   154     }
   158     /**
   159      * Gets the parser implemented as a ContentHandler.
   160      *
   161      * One can feed XML Schema as SAX events to this interface to
   162      * parse a schema. To parse multiple schema files, feed multiple
   163      * sets of events.
   164      *
   165      * <p>
   166      * If you don't send a complete event sequence from a startDocument
   167      * event to an endDocument event, the state of XSOMParser can become
   168      * unstable. This sometimes happen when you encounter an error while
   169      * generating SAX events. Don't call the getResult method in that case.
   170      *
   171      * <p>
   172      * This way of reading XML Schema can be useful when XML Schema is
   173      * not available as a stand-alone XML document.
   174      * For example, one can feed XML Schema inside a WSDL document.
   175      */
   176     public ContentHandler getParserHandler() {
   177         NGCCRuntimeEx runtime = context.newNGCCRuntime();
   178         Schema s = new Schema(runtime,false,null);
   179         runtime.setRootHandler(s);
   180         return runtime;
   181     }
   183     /**
   184      * Gets the parsed result. Don't call this method until
   185      * you parse all the schemas.
   186      *
   187      * @return
   188      *      If there was any parse error, this method returns null.
   189      *      To receive error information, specify your error handler
   190      *      through the setErrorHandler method.
   191      * @exception SAXException
   192      *      This exception will never be thrown unless it is thrown
   193      *      by an error handler.
   194      */
   195     public XSSchemaSet getResult() throws SAXException {
   196         return context.getResult();
   197     }
   199     /**
   200      * Gets the set of {@link SchemaDocument} that represents
   201      * parsed documents so far.
   202      *
   203      * @return
   204      *      can be empty but never null.
   205      */
   206     public Set<SchemaDocument> getDocuments() {
   207         return new HashSet<SchemaDocument>(context.parsedDocuments.keySet());
   208     }
   210     public EntityResolver getEntityResolver() {
   211         return entityResolver;
   212     }
   213     /**
   214      * Set an entity resolver that is used to resolve things
   215      * like &lt;xsd:import> and &lt;xsd:include>.
   216      */
   217     public void setEntityResolver( EntityResolver resolver ) {
   218         this.entityResolver = resolver;
   219     }
   220     public ErrorHandler getErrorHandler() {
   221         return userErrorHandler;
   222     }
   223     /**
   224      * Set an error handler that receives all the errors encountered
   225      * during the parsing.
   226      */
   227     public void setErrorHandler(ErrorHandler errorHandler) {
   228         this.userErrorHandler = errorHandler;
   229     }
   231     /**
   232      * Sets the annotation parser.
   233      *
   234      * Annotation parser can be used to parse application-specific
   235      * annotations inside a schema.
   236      *
   237      * <p>
   238      * For each annotation, new instance of this class will be
   239      * created and used to parse &lt;xs:annotation>.
   240      */
   241     public void setAnnotationParser( final Class annParser ) {
   242         setAnnotationParser( new AnnotationParserFactory() {
   243             public AnnotationParser create() {
   244                 try {
   245                     return (AnnotationParser)annParser.newInstance();
   246                 } catch( InstantiationException e ) {
   247                     throw new InstantiationError(e.getMessage());
   248                 } catch( IllegalAccessException e ) {
   249                     throw new IllegalAccessError(e.getMessage());
   250                 }
   251             }
   252         });
   253     }
   255     /**
   256      * Sets the annotation parser factory.
   257      *
   258      * <p>
   259      * The specified factory will be used to create AnnotationParsers.
   260      */
   261     public void setAnnotationParser( AnnotationParserFactory factory ) {
   262         this.apFactory = factory;
   263     }
   265     public AnnotationParserFactory getAnnotationParserFactory() {
   266         return apFactory;
   267     }
   268 }

mercurial