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