src/share/jaxws_classes/com/sun/tools/internal/xjc/ModelLoader.java

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

mercurial