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