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

ohair@286 1 /*
ohair@286 2 * Copyright (c) 1997, 2010, 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 com.sun.xml.internal.xsom.parser;
ohair@286 27
ohair@286 28 import java.io.File;
ohair@286 29 import java.io.IOException;
ohair@286 30 import java.io.InputStream;
ohair@286 31 import java.io.Reader;
ohair@286 32 import java.net.URL;
ohair@286 33 import java.util.Set;
ohair@286 34 import java.util.HashSet;
ohair@286 35
ohair@286 36 import javax.xml.parsers.SAXParserFactory;
ohair@286 37
ohair@286 38 import org.xml.sax.ContentHandler;
ohair@286 39 import org.xml.sax.EntityResolver;
ohair@286 40 import org.xml.sax.ErrorHandler;
ohair@286 41 import org.xml.sax.InputSource;
ohair@286 42 import org.xml.sax.SAXException;
ohair@286 43
ohair@286 44 import com.sun.xml.internal.xsom.XSSchemaSet;
ohair@286 45 import com.sun.xml.internal.xsom.impl.parser.NGCCRuntimeEx;
ohair@286 46 import com.sun.xml.internal.xsom.impl.parser.ParserContext;
ohair@286 47 import com.sun.xml.internal.xsom.impl.parser.state.Schema;
ohair@286 48
ohair@286 49 /**
ohair@286 50 * Parses possibly multiple W3C XML Schema files and compose
ohair@286 51 * them into one grammar.
ohair@286 52 *
ohair@286 53 * @author
ohair@286 54 * Kohsuke Kawaguchi (kohsuke.kawaguchi@sun.com)
ohair@286 55 */
ohair@286 56 public final class XSOMParser {
ohair@286 57
ohair@286 58 private EntityResolver entityResolver;
ohair@286 59 private ErrorHandler userErrorHandler;
ohair@286 60
ohair@286 61 private AnnotationParserFactory apFactory;
ohair@286 62
ohair@286 63 private final ParserContext context;
ohair@286 64
ohair@286 65 /**
ohair@286 66 * Creates a new XSOMParser by using a SAX parser from JAXP.
ohair@286 67 */
ohair@286 68 public XSOMParser() {
ohair@286 69 this(new JAXPParser());
ohair@286 70 }
ohair@286 71
ohair@286 72 /**
ohair@286 73 * Creates a new XSOMParser that uses the given SAXParserFactory
ohair@286 74 * for creating new SAX parsers.
ohair@286 75 *
ohair@286 76 * The caller needs to configure
ohair@286 77 * it properly. Don't forget to call <code>setNamespaceAware(true)</code>
ohair@286 78 * or you'll see some strange errors.
ohair@286 79 */
ohair@286 80 public XSOMParser( SAXParserFactory factory ) {
ohair@286 81 this( new JAXPParser(factory) );
ohair@286 82 }
ohair@286 83
ohair@286 84 /**
ohair@286 85 * Creates a new XSOMParser that reads XML Schema from non-standard
ohair@286 86 * inputs.
ohair@286 87 *
ohair@286 88 * By implementing the {@link XMLParser} interface, XML Schema
ohair@286 89 * can be read from something other than XML.
ohair@286 90 *
ohair@286 91 * @param parser
ohair@286 92 * This parser will be called to parse XML Schema documents.
ohair@286 93 */
ohair@286 94 public XSOMParser(XMLParser parser) {
ohair@286 95 context = new ParserContext(this,parser);
ohair@286 96 }
ohair@286 97
ohair@286 98 /**
ohair@286 99 * Parses a new XML Schema document.
ohair@286 100 *
ohair@286 101 * <p>
ohair@286 102 * When using this method, XSOM does not know the system ID of
ohair@286 103 * this document, therefore, when this stream contains relative
ohair@286 104 * references to other schemas, XSOM will fail to resolve them.
ohair@286 105 * To specify an system ID with a stream, use {@link InputSource}
ohair@286 106 */
ohair@286 107 public void parse( InputStream is ) throws SAXException {
ohair@286 108 parse(new InputSource(is));
ohair@286 109 }
ohair@286 110
ohair@286 111 /**
ohair@286 112 * Parses a new XML Schema document.
ohair@286 113 *
ohair@286 114 * <p>
ohair@286 115 * When using this method, XSOM does not know the system ID of
ohair@286 116 * this document, therefore, when this reader contains relative
ohair@286 117 * references to other schemas, XSOM will fail to resolve them.
ohair@286 118 * To specify an system ID with a reader, use {@link InputSource}
ohair@286 119 */
ohair@286 120 public void parse( Reader reader ) throws SAXException {
ohair@286 121 parse(new InputSource(reader));
ohair@286 122 }
ohair@286 123
ohair@286 124 /**
ohair@286 125 * Parses a new XML Schema document.
ohair@286 126 */
ohair@286 127 public void parse( File schema ) throws SAXException, IOException {
ohair@286 128 parse(schema.toURL());
ohair@286 129 }
ohair@286 130
ohair@286 131 /**
ohair@286 132 * Parses a new XML Schema document.
ohair@286 133 */
ohair@286 134 public void parse( URL url ) throws SAXException {
ohair@286 135 parse( url.toExternalForm() );
ohair@286 136 }
ohair@286 137
ohair@286 138 /**
ohair@286 139 * Parses a new XML Schema document.
ohair@286 140 */
ohair@286 141 public void parse( String systemId ) throws SAXException {
ohair@286 142 parse(new InputSource(systemId));
ohair@286 143 }
ohair@286 144
ohair@286 145 /**
ohair@286 146 * Parses a new XML Schema document.
ohair@286 147 *
ohair@286 148 * <p>
ohair@286 149 * Note that if the {@link InputSource} does not have a system ID,
ohair@286 150 * XSOM will fail to resolve them.
ohair@286 151 */
ohair@286 152 public void parse( InputSource source ) throws SAXException {
ohair@286 153 context.parse(source);
ohair@286 154 }
ohair@286 155
ohair@286 156
ohair@286 157
ohair@286 158 /**
ohair@286 159 * Gets the parser implemented as a ContentHandler.
ohair@286 160 *
ohair@286 161 * One can feed XML Schema as SAX events to this interface to
ohair@286 162 * parse a schema. To parse multiple schema files, feed multiple
ohair@286 163 * sets of events.
ohair@286 164 *
ohair@286 165 * <p>
ohair@286 166 * If you don't send a complete event sequence from a startDocument
ohair@286 167 * event to an endDocument event, the state of XSOMParser can become
ohair@286 168 * unstable. This sometimes happen when you encounter an error while
ohair@286 169 * generating SAX events. Don't call the getResult method in that case.
ohair@286 170 *
ohair@286 171 * <p>
ohair@286 172 * This way of reading XML Schema can be useful when XML Schema is
ohair@286 173 * not available as a stand-alone XML document.
ohair@286 174 * For example, one can feed XML Schema inside a WSDL document.
ohair@286 175 */
ohair@286 176 public ContentHandler getParserHandler() {
ohair@286 177 NGCCRuntimeEx runtime = context.newNGCCRuntime();
ohair@286 178 Schema s = new Schema(runtime,false,null);
ohair@286 179 runtime.setRootHandler(s);
ohair@286 180 return runtime;
ohair@286 181 }
ohair@286 182
ohair@286 183 /**
ohair@286 184 * Gets the parsed result. Don't call this method until
ohair@286 185 * you parse all the schemas.
ohair@286 186 *
ohair@286 187 * @return
ohair@286 188 * If there was any parse error, this method returns null.
ohair@286 189 * To receive error information, specify your error handler
ohair@286 190 * through the setErrorHandler method.
ohair@286 191 * @exception SAXException
ohair@286 192 * This exception will never be thrown unless it is thrown
ohair@286 193 * by an error handler.
ohair@286 194 */
ohair@286 195 public XSSchemaSet getResult() throws SAXException {
ohair@286 196 return context.getResult();
ohair@286 197 }
ohair@286 198
ohair@286 199 /**
ohair@286 200 * Gets the set of {@link SchemaDocument} that represents
ohair@286 201 * parsed documents so far.
ohair@286 202 *
ohair@286 203 * @return
ohair@286 204 * can be empty but never null.
ohair@286 205 */
ohair@286 206 public Set<SchemaDocument> getDocuments() {
ohair@286 207 return new HashSet<SchemaDocument>(context.parsedDocuments.keySet());
ohair@286 208 }
ohair@286 209
ohair@286 210 public EntityResolver getEntityResolver() {
ohair@286 211 return entityResolver;
ohair@286 212 }
ohair@286 213 /**
ohair@286 214 * Set an entity resolver that is used to resolve things
ohair@286 215 * like &lt;xsd:import> and &lt;xsd:include>.
ohair@286 216 */
ohair@286 217 public void setEntityResolver( EntityResolver resolver ) {
ohair@286 218 this.entityResolver = resolver;
ohair@286 219 }
ohair@286 220 public ErrorHandler getErrorHandler() {
ohair@286 221 return userErrorHandler;
ohair@286 222 }
ohair@286 223 /**
ohair@286 224 * Set an error handler that receives all the errors encountered
ohair@286 225 * during the parsing.
ohair@286 226 */
ohair@286 227 public void setErrorHandler(ErrorHandler errorHandler) {
ohair@286 228 this.userErrorHandler = errorHandler;
ohair@286 229 }
ohair@286 230
ohair@286 231 /**
ohair@286 232 * Sets the annotation parser.
ohair@286 233 *
ohair@286 234 * Annotation parser can be used to parse application-specific
ohair@286 235 * annotations inside a schema.
ohair@286 236 *
ohair@286 237 * <p>
ohair@286 238 * For each annotation, new instance of this class will be
ohair@286 239 * created and used to parse &lt;xs:annotation>.
ohair@286 240 */
ohair@286 241 public void setAnnotationParser( final Class annParser ) {
ohair@286 242 setAnnotationParser( new AnnotationParserFactory() {
ohair@286 243 public AnnotationParser create() {
ohair@286 244 try {
ohair@286 245 return (AnnotationParser)annParser.newInstance();
ohair@286 246 } catch( InstantiationException e ) {
ohair@286 247 throw new InstantiationError(e.getMessage());
ohair@286 248 } catch( IllegalAccessException e ) {
ohair@286 249 throw new IllegalAccessError(e.getMessage());
ohair@286 250 }
ohair@286 251 }
ohair@286 252 });
ohair@286 253 }
ohair@286 254
ohair@286 255 /**
ohair@286 256 * Sets the annotation parser factory.
ohair@286 257 *
ohair@286 258 * <p>
ohair@286 259 * The specified factory will be used to create AnnotationParsers.
ohair@286 260 */
ohair@286 261 public void setAnnotationParser( AnnotationParserFactory factory ) {
ohair@286 262 this.apFactory = factory;
ohair@286 263 }
ohair@286 264
ohair@286 265 public AnnotationParserFactory getAnnotationParserFactory() {
ohair@286 266 return apFactory;
ohair@286 267 }
ohair@286 268 }

mercurial