1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/jaxws_classes/com/sun/tools/internal/xjc/ModelLoader.java Tue Mar 06 16:09:35 2012 -0800 1.3 @@ -0,0 +1,592 @@ 1.4 +/* 1.5 + * Copyright (c) 1997, 2011, 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.tools.internal.xjc; 1.30 + 1.31 +import java.io.IOException; 1.32 +import java.io.StringReader; 1.33 + 1.34 +import com.sun.codemodel.internal.JCodeModel; 1.35 +import com.sun.tools.internal.xjc.model.Model; 1.36 +import com.sun.tools.internal.xjc.reader.Const; 1.37 +import com.sun.tools.internal.xjc.reader.ExtensionBindingChecker; 1.38 +import com.sun.tools.internal.xjc.reader.dtd.TDTDReader; 1.39 +import com.sun.tools.internal.xjc.reader.internalizer.DOMForest; 1.40 +import com.sun.tools.internal.xjc.reader.internalizer.DOMForestScanner; 1.41 +import com.sun.tools.internal.xjc.reader.internalizer.InternalizationLogic; 1.42 +import com.sun.tools.internal.xjc.reader.internalizer.SCDBasedBindingSet; 1.43 +import com.sun.tools.internal.xjc.reader.internalizer.VersionChecker; 1.44 +import com.sun.tools.internal.xjc.reader.relaxng.RELAXNGCompiler; 1.45 +import com.sun.tools.internal.xjc.reader.relaxng.RELAXNGInternalizationLogic; 1.46 +import com.sun.tools.internal.xjc.reader.xmlschema.BGMBuilder; 1.47 +import com.sun.tools.internal.xjc.reader.xmlschema.bindinfo.AnnotationParserFactoryImpl; 1.48 +import com.sun.tools.internal.xjc.reader.xmlschema.parser.CustomizationContextChecker; 1.49 +import com.sun.tools.internal.xjc.reader.xmlschema.parser.IncorrectNamespaceURIChecker; 1.50 +import com.sun.tools.internal.xjc.reader.xmlschema.parser.SchemaConstraintChecker; 1.51 +import com.sun.tools.internal.xjc.reader.xmlschema.parser.XMLSchemaInternalizationLogic; 1.52 +import com.sun.tools.internal.xjc.util.ErrorReceiverFilter; 1.53 +import com.sun.xml.internal.xsom.XSSchemaSet; 1.54 +import com.sun.xml.internal.xsom.parser.JAXPParser; 1.55 +import com.sun.xml.internal.xsom.parser.XMLParser; 1.56 +import com.sun.xml.internal.xsom.parser.XSOMParser; 1.57 +import java.net.URI; 1.58 +import java.net.URISyntaxException; 1.59 +import javax.xml.XMLConstants; 1.60 + 1.61 +import com.sun.xml.internal.rngom.ast.builder.SchemaBuilder; 1.62 +import com.sun.xml.internal.rngom.ast.util.CheckingSchemaBuilder; 1.63 +import com.sun.xml.internal.rngom.digested.DPattern; 1.64 +import com.sun.xml.internal.rngom.digested.DSchemaBuilderImpl; 1.65 +import com.sun.xml.internal.rngom.parse.IllegalSchemaException; 1.66 +import com.sun.xml.internal.rngom.parse.Parseable; 1.67 +import com.sun.xml.internal.rngom.parse.compact.CompactParseable; 1.68 +import com.sun.xml.internal.rngom.parse.xml.SAXParseable; 1.69 +import com.sun.xml.internal.rngom.xml.sax.XMLReaderCreator; 1.70 +import org.w3c.dom.Document; 1.71 +import org.w3c.dom.Element; 1.72 +import org.w3c.dom.NodeList; 1.73 +import org.xml.sax.Attributes; 1.74 +import org.xml.sax.ContentHandler; 1.75 +import org.xml.sax.EntityResolver; 1.76 +import org.xml.sax.ErrorHandler; 1.77 +import org.xml.sax.InputSource; 1.78 +import org.xml.sax.SAXException; 1.79 +import org.xml.sax.SAXParseException; 1.80 +import org.xml.sax.XMLFilter; 1.81 +import org.xml.sax.XMLReader; 1.82 +import org.xml.sax.helpers.XMLFilterImpl; 1.83 + 1.84 +/** 1.85 + * Builds a {@link Model} object. 1.86 + * 1.87 + * This is an utility class that makes it easy to load a grammar object 1.88 + * from various sources. 1.89 + * 1.90 + * @author 1.91 + * Kohsuke Kawaguchi (kohsuke.kawaguchi@sun.com) 1.92 + */ 1.93 +public final class ModelLoader { 1.94 + 1.95 + private final Options opt; 1.96 + private final ErrorReceiverFilter errorReceiver; 1.97 + private final JCodeModel codeModel; 1.98 + /** 1.99 + * {@link DOMForest#transform(boolean)} creates this on the side. 1.100 + */ 1.101 + private SCDBasedBindingSet scdBasedBindingSet; 1.102 + 1.103 + 1.104 + /** 1.105 + * A convenience method to load schemas into a {@link Model}. 1.106 + */ 1.107 + public static Model load( Options opt, JCodeModel codeModel, ErrorReceiver er ) { 1.108 + return new ModelLoader(opt,codeModel,er).load(); 1.109 + } 1.110 + 1.111 + 1.112 + public ModelLoader(Options _opt, JCodeModel _codeModel, ErrorReceiver er) { 1.113 + this.opt = _opt; 1.114 + this.codeModel = _codeModel; 1.115 + this.errorReceiver = new ErrorReceiverFilter(er); 1.116 + } 1.117 + 1.118 + @SuppressWarnings("CallToThreadDumpStack") 1.119 + private Model load() { 1.120 + Model grammar; 1.121 + 1.122 + if(!sanityCheck()) 1.123 + return null; 1.124 + 1.125 + 1.126 + try { 1.127 + switch (opt.getSchemaLanguage()) { 1.128 + case DTD : 1.129 + // TODO: make sure that bindFiles,size()<=1 1.130 + InputSource bindFile = null; 1.131 + if (opt.getBindFiles().length > 0) 1.132 + bindFile = opt.getBindFiles()[0]; 1.133 + // if there is no binding file, make a dummy one. 1.134 + if (bindFile == null) { 1.135 + // if no binding information is specified, provide a default 1.136 + bindFile = 1.137 + new InputSource( 1.138 + new StringReader( 1.139 + "<?xml version='1.0'?><xml-java-binding-schema><options package='" 1.140 + + (opt.defaultPackage==null?"generated":opt.defaultPackage) 1.141 + + "'/></xml-java-binding-schema>")); 1.142 + } 1.143 + 1.144 + checkTooManySchemaErrors(); 1.145 + grammar = loadDTD(opt.getGrammars()[0], bindFile ); 1.146 + break; 1.147 + 1.148 + case RELAXNG : 1.149 + checkTooManySchemaErrors(); 1.150 + grammar = loadRELAXNG(); 1.151 + break; 1.152 + 1.153 + case RELAXNG_COMPACT : 1.154 + checkTooManySchemaErrors(); 1.155 + grammar = loadRELAXNGCompact(); 1.156 + break; 1.157 + 1.158 + case WSDL: 1.159 + grammar = annotateXMLSchema( loadWSDL() ); 1.160 + break; 1.161 + 1.162 + case XMLSCHEMA: 1.163 + grammar = annotateXMLSchema( loadXMLSchema() ); 1.164 + break; 1.165 + 1.166 + default : 1.167 + throw new AssertionError(); // assertion failed 1.168 + } 1.169 + 1.170 + if (errorReceiver.hadError()) { 1.171 + grammar = null; 1.172 + } else { 1.173 + grammar.setPackageLevelAnnotations(opt.packageLevelAnnotations); 1.174 + } 1.175 + 1.176 + return grammar; 1.177 + 1.178 + } catch (SAXException e) { 1.179 + // parsing error in the input document. 1.180 + // this error must have been reported to the user vis error handler 1.181 + // so don't print it again. 1.182 + if (opt.verbose) { 1.183 + // however, a bug in XJC might throw unexpected SAXException. 1.184 + // thus when one is debugging, it is useful to print what went 1.185 + // wrong. 1.186 + if (e.getException() != null) 1.187 + e.getException().printStackTrace(); 1.188 + else 1.189 + e.printStackTrace(); 1.190 + } 1.191 + return null; 1.192 + } catch (AbortException e) { 1.193 + // error should have been reported already, since this is requested by the error receiver 1.194 + return null; 1.195 + } 1.196 + } 1.197 + 1.198 + 1.199 + 1.200 + /** 1.201 + * Do some extra checking and return false if the compilation 1.202 + * should abort. 1.203 + */ 1.204 + private boolean sanityCheck() { 1.205 + if( opt.getSchemaLanguage()==Language.XMLSCHEMA ) { 1.206 + Language guess = opt.guessSchemaLanguage(); 1.207 + 1.208 + String[] msg = null; 1.209 + switch(guess) { 1.210 + case DTD: 1.211 + msg = new String[]{"DTD","-dtd"}; 1.212 + break; 1.213 + case RELAXNG: 1.214 + msg = new String[]{"RELAX NG","-relaxng"}; 1.215 + break; 1.216 + case RELAXNG_COMPACT: 1.217 + msg = new String[]{"RELAX NG compact syntax","-relaxng-compact"}; 1.218 + break; 1.219 + case WSDL: 1.220 + msg = new String[]{"WSDL","-wsdl"}; 1.221 + break; 1.222 + } 1.223 + if( msg!=null ) 1.224 + errorReceiver.warning( null, 1.225 + Messages.format( 1.226 + Messages.EXPERIMENTAL_LANGUAGE_WARNING, 1.227 + msg[0], msg[1] )); 1.228 + } 1.229 + return true; 1.230 + } 1.231 + 1.232 + 1.233 + /** 1.234 + * {@link XMLParser} implementation that adds additional processors into the chain. 1.235 + * 1.236 + * <p> 1.237 + * This parser will parse a DOM forest as: 1.238 + * DOMForestParser --> 1.239 + * ExtensionBindingChecker --> 1.240 + * ProhibitedFeatureFilter --> 1.241 + * XSOMParser 1.242 + */ 1.243 + private class XMLSchemaParser implements XMLParser { 1.244 + private final XMLParser baseParser; 1.245 + 1.246 + private XMLSchemaParser(XMLParser baseParser) { 1.247 + this.baseParser = baseParser; 1.248 + } 1.249 + 1.250 + public void parse(InputSource source, ContentHandler handler, 1.251 + ErrorHandler errorHandler, EntityResolver entityResolver ) throws SAXException, IOException { 1.252 + // set up the chain of handlers. 1.253 + handler = wrapBy( new ExtensionBindingChecker(XMLConstants.W3C_XML_SCHEMA_NS_URI,opt,errorReceiver), handler ); 1.254 + handler = wrapBy( new IncorrectNamespaceURIChecker(errorReceiver), handler ); 1.255 + handler = wrapBy( new CustomizationContextChecker(errorReceiver), handler ); 1.256 +// handler = wrapBy( new VersionChecker(controller), handler ); 1.257 + 1.258 + baseParser.parse( source, handler, errorHandler, entityResolver ); 1.259 + } 1.260 + /** 1.261 + * Wraps the specified content handler by a filter. 1.262 + * It is little awkward to use a helper implementation class like XMLFilterImpl 1.263 + * as the method parameter, but this simplifies the code. 1.264 + */ 1.265 + private ContentHandler wrapBy( XMLFilterImpl filter, ContentHandler handler ) { 1.266 + filter.setContentHandler(handler); 1.267 + return filter; 1.268 + } 1.269 + } 1.270 + 1.271 + private void checkTooManySchemaErrors() { 1.272 + if( opt.getGrammars().length!=1 ) 1.273 + errorReceiver.error(null,Messages.format(Messages.ERR_TOO_MANY_SCHEMA)); 1.274 + } 1.275 + 1.276 + /** 1.277 + * Parses a DTD file into an annotated grammar. 1.278 + * 1.279 + * @param source 1.280 + * DTD file 1.281 + * @param bindFile 1.282 + * External binding file. 1.283 + */ 1.284 + private Model loadDTD( InputSource source, InputSource bindFile) { 1.285 + 1.286 + // parse the schema as a DTD. 1.287 + return TDTDReader.parse( 1.288 + source, 1.289 + bindFile, 1.290 + errorReceiver, 1.291 + opt); 1.292 + } 1.293 + 1.294 + /** 1.295 + * Builds DOMForest and performs the internalization. 1.296 + * 1.297 + * @throws SAXException 1.298 + * when a fatal error happens 1.299 + */ 1.300 + public DOMForest buildDOMForest( InternalizationLogic logic ) 1.301 + throws SAXException { 1.302 + 1.303 + // parse into DOM forest 1.304 + DOMForest forest = new DOMForest(logic); 1.305 + 1.306 + forest.setErrorHandler(errorReceiver); 1.307 + if(opt.entityResolver!=null) 1.308 + forest.setEntityResolver(opt.entityResolver); 1.309 + 1.310 + // parse source grammars 1.311 + for (InputSource value : opt.getGrammars()) { 1.312 + errorReceiver.pollAbort(); 1.313 + forest.parse(value, true); 1.314 + } 1.315 + 1.316 + // parse external binding files 1.317 + for (InputSource value : opt.getBindFiles()) { 1.318 + errorReceiver.pollAbort(); 1.319 + Document dom = forest.parse(value, true); 1.320 + if(dom==null) continue; // error must have been reported 1.321 + Element root = dom.getDocumentElement(); 1.322 + // TODO: it somehow doesn't feel right to do a validation in the Driver class. 1.323 + // think about moving it to somewhere else. 1.324 + if (!fixNull(root.getNamespaceURI()).equals(Const.JAXB_NSURI) 1.325 + || !root.getLocalName().equals("bindings")) 1.326 + errorReceiver.error(new SAXParseException(Messages.format(Messages.ERR_NOT_A_BINDING_FILE, 1.327 + root.getNamespaceURI(), 1.328 + root.getLocalName()), 1.329 + null, 1.330 + value.getSystemId(), 1.331 + -1, -1)); 1.332 + } 1.333 + 1.334 + scdBasedBindingSet = forest.transform(opt.isExtensionMode()); 1.335 + 1.336 + return forest; 1.337 + } 1.338 + 1.339 + private String fixNull(String s) { 1.340 + if(s==null) return ""; 1.341 + else return s; 1.342 + } 1.343 + 1.344 + /** 1.345 + * Parses a set of XML Schema files into an annotated grammar. 1.346 + */ 1.347 + public XSSchemaSet loadXMLSchema() throws SAXException { 1.348 + 1.349 + if( opt.strictCheck && !SchemaConstraintChecker.check(opt.getGrammars(),errorReceiver,opt.entityResolver)) { 1.350 + // schema error. error should have been reported 1.351 + return null; 1.352 + } 1.353 + 1.354 + if(opt.getBindFiles().length==0) { 1.355 + // no external binding. try the speculative no DOMForest execution, 1.356 + // which is faster if the speculation succeeds. 1.357 + try { 1.358 + return createXSOMSpeculative(); 1.359 + } catch( SpeculationFailure _ ) { 1.360 + // failed. go the slow way 1.361 + } 1.362 + } 1.363 + 1.364 + // the default slower way is to parse everything into DOM first. 1.365 + // so that we can take external annotations into account. 1.366 + DOMForest forest = buildDOMForest( new XMLSchemaInternalizationLogic() ); 1.367 + return createXSOM(forest, scdBasedBindingSet); 1.368 + } 1.369 + 1.370 + /** 1.371 + * Parses a set of schemas inside a WSDL file. 1.372 + * 1.373 + * A WSDL file may contain multiple <xsd:schema> elements. 1.374 + */ 1.375 + private XSSchemaSet loadWSDL() 1.376 + throws SAXException { 1.377 + 1.378 + 1.379 + // build DOMForest just like we handle XML Schema 1.380 + DOMForest forest = buildDOMForest( new XMLSchemaInternalizationLogic() ); 1.381 + 1.382 + DOMForestScanner scanner = new DOMForestScanner(forest); 1.383 + 1.384 + XSOMParser xsomParser = createXSOMParser( forest ); 1.385 + 1.386 + // find <xsd:schema>s and parse them individually 1.387 + for( InputSource grammar : opt.getGrammars() ) { 1.388 + Document wsdlDom = forest.get( grammar.getSystemId() ); 1.389 + if (wsdlDom == null) { 1.390 + String systemId = Options.normalizeSystemId(grammar.getSystemId()); 1.391 + if (forest.get(systemId) != null) { 1.392 + grammar.setSystemId(systemId); 1.393 + wsdlDom = forest.get( grammar.getSystemId() ); 1.394 + } 1.395 + } 1.396 + 1.397 + NodeList schemas = wsdlDom.getElementsByTagNameNS(XMLConstants.W3C_XML_SCHEMA_NS_URI,"schema"); 1.398 + for( int i=0; i<schemas.getLength(); i++ ) 1.399 + scanner.scan( (Element)schemas.item(i), xsomParser.getParserHandler() ); 1.400 + } 1.401 + return xsomParser.getResult(); 1.402 + } 1.403 + 1.404 + /** 1.405 + * Annotates the obtained schema set. 1.406 + * 1.407 + * @return 1.408 + * null if an error happens. In that case, the error messages 1.409 + * will be properly reported to the controller by this method. 1.410 + */ 1.411 + public Model annotateXMLSchema(XSSchemaSet xs) { 1.412 + if (xs == null) 1.413 + return null; 1.414 + return BGMBuilder.build(xs, codeModel, errorReceiver, opt); 1.415 + } 1.416 + 1.417 + public XSOMParser createXSOMParser(XMLParser parser) { 1.418 + // set up other parameters to XSOMParser 1.419 + XSOMParser reader = new XSOMParser(new XMLSchemaParser(parser)); 1.420 + reader.setAnnotationParser(new AnnotationParserFactoryImpl(opt)); 1.421 + reader.setErrorHandler(errorReceiver); 1.422 + reader.setEntityResolver(opt.entityResolver); 1.423 + return reader; 1.424 + } 1.425 + 1.426 + public XSOMParser createXSOMParser(final DOMForest forest) { 1.427 + XSOMParser p = createXSOMParser(forest.createParser()); 1.428 + p.setEntityResolver(new EntityResolver() { 1.429 + public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException { 1.430 + // DOMForest only parses documents that are reachable through systemIds, 1.431 + // and it won't pick up references like <xs:import namespace="..." /> without 1.432 + // @schemaLocation. So we still need to use an entity resolver here to resolve 1.433 + // these references, yet we don't want to just run them blindly, since if we do that 1.434 + // DOMForestParser always get the translated system ID when catalog is used 1.435 + // (where DOMForest records trees with their original system IDs.) 1.436 + if(systemId!=null && forest.get(systemId)!=null) 1.437 + return new InputSource(systemId); 1.438 + if(opt.entityResolver!=null) 1.439 + return opt.entityResolver.resolveEntity(publicId,systemId); 1.440 + 1.441 + return null; 1.442 + } 1.443 + }); 1.444 + return p; 1.445 + } 1.446 + 1.447 + 1.448 + private static final class SpeculationFailure extends Error {} 1.449 + 1.450 + private static final class SpeculationChecker extends XMLFilterImpl { 1.451 + @Override 1.452 + public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { 1.453 + if(localName.equals("bindings") && uri.equals(Const.JAXB_NSURI)) 1.454 + throw new SpeculationFailure(); 1.455 + super.startElement(uri,localName,qName,attributes); 1.456 + } 1.457 + } 1.458 + 1.459 + /** 1.460 + * Parses schemas directly into XSOM by assuming that there's 1.461 + * no external annotations. 1.462 + * <p> 1.463 + * When an external annotation is found, a {@link SpeculationFailure} is thrown, 1.464 + * and we will do it all over again by using the slow way. 1.465 + */ 1.466 + private XSSchemaSet createXSOMSpeculative() throws SAXException, SpeculationFailure { 1.467 + 1.468 + // check if the schema contains external binding files. If so, speculation is a failure. 1.469 + 1.470 + XMLParser parser = new XMLParser() { 1.471 + private final JAXPParser base = new JAXPParser(); 1.472 + 1.473 + public void parse(InputSource source, ContentHandler handler, 1.474 + ErrorHandler errorHandler, EntityResolver entityResolver ) throws SAXException, IOException { 1.475 + // set up the chain of handlers. 1.476 + handler = wrapBy( new SpeculationChecker(), handler ); 1.477 + handler = wrapBy( new VersionChecker(null,errorReceiver,entityResolver), handler ); 1.478 + 1.479 + base.parse( source, handler, errorHandler, entityResolver ); 1.480 + } 1.481 + /** 1.482 + * Wraps the specified content handler by a filter. 1.483 + * It is little awkward to use a helper implementation class like XMLFilterImpl 1.484 + * as the method parameter, but this simplifies the code. 1.485 + */ 1.486 + private ContentHandler wrapBy( XMLFilterImpl filter, ContentHandler handler ) { 1.487 + filter.setContentHandler(handler); 1.488 + return filter; 1.489 + } 1.490 + }; 1.491 + 1.492 + XSOMParser reader = createXSOMParser(parser); 1.493 + 1.494 + // parse source grammars 1.495 + for (InputSource value : opt.getGrammars()) 1.496 + reader.parse(value); 1.497 + 1.498 + return reader.getResult(); 1.499 + } 1.500 + 1.501 + /** 1.502 + * Parses a {@link DOMForest} into a {@link XSSchemaSet}. 1.503 + * 1.504 + * @return 1.505 + * null if the parsing failed. 1.506 + */ 1.507 + public XSSchemaSet createXSOM(DOMForest forest, SCDBasedBindingSet scdBasedBindingSet) throws SAXException { 1.508 + // set up other parameters to XSOMParser 1.509 + XSOMParser reader = createXSOMParser(forest); 1.510 + 1.511 + // re-parse the transformed schemas 1.512 + for (String systemId : forest.getRootDocuments()) { 1.513 + errorReceiver.pollAbort(); 1.514 + Document dom = forest.get(systemId); 1.515 + if (!dom.getDocumentElement().getNamespaceURI().equals(Const.JAXB_NSURI)) { 1.516 + reader.parse(systemId); 1.517 + } 1.518 + } 1.519 + 1.520 + XSSchemaSet result = reader.getResult(); 1.521 + 1.522 + if(result!=null) 1.523 + scdBasedBindingSet.apply(result,errorReceiver); 1.524 + 1.525 + return result; 1.526 + } 1.527 + 1.528 + /** 1.529 + * Parses a RELAX NG grammar into an annotated grammar. 1.530 + */ 1.531 + private Model loadRELAXNG() throws SAXException { 1.532 + 1.533 + // build DOM forest 1.534 + final DOMForest forest = buildDOMForest( new RELAXNGInternalizationLogic() ); 1.535 + 1.536 + // use JAXP masquerading to validate the input document. 1.537 + // DOMForest -> ExtensionBindingChecker -> RNGOM 1.538 + 1.539 + XMLReaderCreator xrc = new XMLReaderCreator() { 1.540 + public XMLReader createXMLReader() { 1.541 + 1.542 + // foreset parser cannot change the receivers while it's working, 1.543 + // so we need to have one XMLFilter that works as a buffer 1.544 + XMLFilter buffer = new XMLFilterImpl() { 1.545 + @Override 1.546 + public void parse(InputSource source) throws IOException, SAXException { 1.547 + forest.createParser().parse( source, this, this, this ); 1.548 + } 1.549 + }; 1.550 + 1.551 + XMLFilter f = new ExtensionBindingChecker(Const.RELAXNG_URI,opt,errorReceiver); 1.552 + f.setParent(buffer); 1.553 + 1.554 + f.setEntityResolver(opt.entityResolver); 1.555 + 1.556 + return f; 1.557 + } 1.558 + }; 1.559 + 1.560 + Parseable p = new SAXParseable( opt.getGrammars()[0], errorReceiver, xrc ); 1.561 + 1.562 + return loadRELAXNG(p); 1.563 + 1.564 + } 1.565 + 1.566 + /** 1.567 + * Loads RELAX NG compact syntax 1.568 + */ 1.569 + private Model loadRELAXNGCompact() { 1.570 + if(opt.getBindFiles().length>0) 1.571 + errorReceiver.error(new SAXParseException( 1.572 + Messages.format(Messages.ERR_BINDING_FILE_NOT_SUPPORTED_FOR_RNC),null)); 1.573 + 1.574 + // TODO: entity resolver? 1.575 + Parseable p = new CompactParseable( opt.getGrammars()[0], errorReceiver ); 1.576 + 1.577 + return loadRELAXNG(p); 1.578 + 1.579 + } 1.580 + 1.581 + /** 1.582 + * Common part between the XML syntax and the compact syntax. 1.583 + */ 1.584 + private Model loadRELAXNG(Parseable p) { 1.585 + SchemaBuilder sb = new CheckingSchemaBuilder(new DSchemaBuilderImpl(),errorReceiver); 1.586 + 1.587 + try { 1.588 + DPattern out = (DPattern)p.parse(sb); 1.589 + return RELAXNGCompiler.build(out,codeModel,opt); 1.590 + } catch (IllegalSchemaException e) { 1.591 + errorReceiver.error(e.getMessage(),e); 1.592 + return null; 1.593 + } 1.594 + } 1.595 +}