src/share/jaxws_classes/com/sun/tools/internal/ws/wsdl/parser/DOMForest.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/ws/wsdl/parser/DOMForest.java	Wed Apr 27 01:27:09 2016 +0800
     1.3 @@ -0,0 +1,387 @@
     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.ws.wsdl.parser;
    1.30 +
    1.31 +import com.sun.istack.internal.NotNull;
    1.32 +import com.sun.tools.internal.ws.util.xml.XmlUtil;
    1.33 +import com.sun.tools.internal.ws.wscompile.ErrorReceiver;
    1.34 +import com.sun.tools.internal.ws.wscompile.WsimportOptions;
    1.35 +import com.sun.tools.internal.ws.wsdl.document.schema.SchemaConstants;
    1.36 +import com.sun.tools.internal.xjc.reader.internalizer.LocatorTable;
    1.37 +import com.sun.xml.internal.bind.marshaller.DataWriter;
    1.38 +import org.w3c.dom.Document;
    1.39 +import org.w3c.dom.Element;
    1.40 +import org.w3c.dom.NodeList;
    1.41 +import org.xml.sax.ContentHandler;
    1.42 +import org.xml.sax.*;
    1.43 +import org.xml.sax.helpers.XMLFilterImpl;
    1.44 +
    1.45 +import javax.xml.parsers.DocumentBuilder;
    1.46 +import javax.xml.parsers.DocumentBuilderFactory;
    1.47 +import javax.xml.parsers.ParserConfigurationException;
    1.48 +import javax.xml.parsers.SAXParserFactory;
    1.49 +import javax.xml.transform.Transformer;
    1.50 +import javax.xml.transform.TransformerException;
    1.51 +import javax.xml.transform.TransformerFactory;
    1.52 +import javax.xml.transform.dom.DOMSource;
    1.53 +import javax.xml.transform.sax.SAXResult;
    1.54 +import java.io.IOException;
    1.55 +import java.io.InputStream;
    1.56 +import java.io.OutputStream;
    1.57 +import java.io.OutputStreamWriter;
    1.58 +import java.net.*;
    1.59 +import java.util.*;
    1.60 +
    1.61 +/**
    1.62 + * @author Vivek Pandey
    1.63 + */
    1.64 +public class DOMForest {
    1.65 +    /**
    1.66 +     * To correctly feed documents to a schema parser, we need to remember
    1.67 +     * which documents (of the forest) were given as the root
    1.68 +     * documents, and which of them are read as included/imported
    1.69 +     * documents.
    1.70 +     * <p/>
    1.71 +     * <p/>
    1.72 +     * Set of system ids as strings.
    1.73 +     */
    1.74 +    protected final Set<String> rootDocuments = new HashSet<String>();
    1.75 +
    1.76 +    /**
    1.77 +     * Contains wsdl:import(s)
    1.78 +     */
    1.79 +    protected final Set<String> externalReferences = new HashSet<String>();
    1.80 +
    1.81 +    /**
    1.82 +     * actual data storage map&lt;SystemId,Document>.
    1.83 +     */
    1.84 +    protected final Map<String, Document> core = new HashMap<String, Document>();
    1.85 +    protected final ErrorReceiver errorReceiver;
    1.86 +
    1.87 +    private final DocumentBuilder documentBuilder;
    1.88 +    private final SAXParserFactory parserFactory;
    1.89 +
    1.90 +    /**
    1.91 +     * inlined schema elements inside wsdl:type section
    1.92 +     */
    1.93 +    protected final List<Element> inlinedSchemaElements = new ArrayList<Element>();
    1.94 +
    1.95 +
    1.96 +    /**
    1.97 +     * Stores location information for all the trees in this forest.
    1.98 +     */
    1.99 +    public final LocatorTable locatorTable = new LocatorTable();
   1.100 +
   1.101 +    protected final EntityResolver entityResolver;
   1.102 +    /**
   1.103 +     * Stores all the outer-most &lt;jaxb:bindings> customizations.
   1.104 +     */
   1.105 +    public final Set<Element> outerMostBindings = new HashSet<Element>();
   1.106 +
   1.107 +    /**
   1.108 +     * Schema language dependent part of the processing.
   1.109 +     */
   1.110 +    protected final InternalizationLogic logic;
   1.111 +    protected final WsimportOptions options;
   1.112 +
   1.113 +    public DOMForest(InternalizationLogic logic, @NotNull EntityResolver entityResolver, WsimportOptions options, ErrorReceiver errReceiver) {
   1.114 +        this.options = options;
   1.115 +        this.entityResolver = entityResolver;
   1.116 +        this.errorReceiver = errReceiver;
   1.117 +        this.logic = logic;
   1.118 +        try {
   1.119 +            // secure xml processing can be switched off if input requires it
   1.120 +            boolean secureProcessingEnabled = options == null || !options.disableXmlSecurity;
   1.121 +            DocumentBuilderFactory dbf = XmlUtil.newDocumentBuilderFactory(secureProcessingEnabled);
   1.122 +            dbf.setNamespaceAware(true);
   1.123 +            this.documentBuilder = dbf.newDocumentBuilder();
   1.124 +
   1.125 +            this.parserFactory = XmlUtil.newSAXParserFactory(secureProcessingEnabled);
   1.126 +            this.parserFactory.setNamespaceAware(true);
   1.127 +        } catch (ParserConfigurationException e) {
   1.128 +            throw new AssertionError(e);
   1.129 +        }
   1.130 +    }
   1.131 +
   1.132 +    public List<Element> getInlinedSchemaElement() {
   1.133 +        return inlinedSchemaElements;
   1.134 +    }
   1.135 +
   1.136 +    public @NotNull Document parse(InputSource source, boolean root) throws SAXException, IOException {
   1.137 +        if (source.getSystemId() == null)
   1.138 +            throw new IllegalArgumentException();
   1.139 +        return parse(source.getSystemId(), source, root);
   1.140 +    }
   1.141 +
   1.142 +    /**
   1.143 +     * Parses an XML at the given location (
   1.144 +     * and XMLs referenced by it) into DOM trees
   1.145 +     * and stores them to this forest.
   1.146 +     *
   1.147 +     * @return the parsed DOM document object.
   1.148 +     */
   1.149 +    public Document parse(String systemId, boolean root) throws SAXException, IOException{
   1.150 +
   1.151 +        systemId = normalizeSystemId(systemId);
   1.152 +
   1.153 +        InputSource is = null;
   1.154 +
   1.155 +        // allow entity resolver to find the actual byte stream.
   1.156 +        is = entityResolver.resolveEntity(null, systemId);
   1.157 +        if (is == null)
   1.158 +            is = new InputSource(systemId);
   1.159 +        else {
   1.160 +            resolvedCache.put(systemId, is.getSystemId());
   1.161 +            systemId=is.getSystemId();
   1.162 +        }
   1.163 +
   1.164 +        if (core.containsKey(systemId)) {
   1.165 +            // this document has already been parsed. Just ignore.
   1.166 +            return core.get(systemId);
   1.167 +        }
   1.168 +
   1.169 +        if(!root)
   1.170 +            addExternalReferences(systemId);
   1.171 +
   1.172 +        // but we still use the original system Id as the key.
   1.173 +        return parse(systemId, is, root);
   1.174 +    }
   1.175 +    protected Map<String,String> resolvedCache = new HashMap<String,String>();
   1.176 +
   1.177 +    public Map<String,String> getReferencedEntityMap() {
   1.178 +        return resolvedCache;
   1.179 +    }
   1.180 +    /**
   1.181 +     * Parses the given document and add it to the DOM forest.
   1.182 +     *
   1.183 +     * @return null if there was a parse error. otherwise non-null.
   1.184 +     */
   1.185 +    private @NotNull Document parse(String systemId, InputSource inputSource, boolean root) throws SAXException, IOException{
   1.186 +        Document dom = documentBuilder.newDocument();
   1.187 +
   1.188 +        systemId = normalizeSystemId(systemId);
   1.189 +
   1.190 +        // put into the map before growing a tree, to
   1.191 +        // prevent recursive reference from causing infinite loop.
   1.192 +        core.put(systemId, dom);
   1.193 +
   1.194 +        dom.setDocumentURI(systemId);
   1.195 +        if (root)
   1.196 +            rootDocuments.add(systemId);
   1.197 +
   1.198 +        try {
   1.199 +            XMLReader reader = createReader(dom);
   1.200 +
   1.201 +            InputStream is = null;
   1.202 +            if(inputSource.getByteStream() == null){
   1.203 +                inputSource = entityResolver.resolveEntity(null, systemId);
   1.204 +            }
   1.205 +            reader.parse(inputSource);
   1.206 +            Element doc = dom.getDocumentElement();
   1.207 +            if (doc == null) {
   1.208 +                return null;
   1.209 +            }
   1.210 +            NodeList schemas = doc.getElementsByTagNameNS(SchemaConstants.NS_XSD, "schema");
   1.211 +            for (int i = 0; i < schemas.getLength(); i++) {
   1.212 +                inlinedSchemaElements.add((Element) schemas.item(i));
   1.213 +            }
   1.214 +        } catch (ParserConfigurationException e) {
   1.215 +            errorReceiver.error(e);
   1.216 +            throw new SAXException(e.getMessage());
   1.217 +        }
   1.218 +        resolvedCache.put(systemId, dom.getDocumentURI());
   1.219 +        return dom;
   1.220 +    }
   1.221 +
   1.222 +    public void addExternalReferences(String ref) {
   1.223 +        if (!externalReferences.contains(ref))
   1.224 +            externalReferences.add(ref);
   1.225 +    }
   1.226 +
   1.227 +
   1.228 +    public Set<String> getExternalReferences() {
   1.229 +        return externalReferences;
   1.230 +    }
   1.231 +
   1.232 +
   1.233 +
   1.234 +    public interface Handler extends ContentHandler {
   1.235 +        /**
   1.236 +         * Gets the DOM that was built.
   1.237 +         */
   1.238 +        public Document getDocument();
   1.239 +    }
   1.240 +
   1.241 +    /**
   1.242 +         * Returns a {@link org.xml.sax.XMLReader} to parse a document into this DOM forest.
   1.243 +         * <p/>
   1.244 +         * This version requires that the DOM object to be created and registered
   1.245 +         * to the map beforehand.
   1.246 +         */
   1.247 +    private XMLReader createReader(Document dom) throws SAXException, ParserConfigurationException {
   1.248 +        XMLReader reader = parserFactory.newSAXParser().getXMLReader();
   1.249 +        DOMBuilder dombuilder = new DOMBuilder(dom, locatorTable, outerMostBindings);
   1.250 +        try {
   1.251 +            reader.setProperty("http://xml.org/sax/properties/lexical-handler", dombuilder);
   1.252 +        } catch(SAXException e) {
   1.253 +            errorReceiver.debug(e.getMessage());
   1.254 +        }
   1.255 +
   1.256 +        ContentHandler handler = new WhitespaceStripper(dombuilder, errorReceiver, entityResolver);
   1.257 +        handler = new VersionChecker(handler, errorReceiver, entityResolver);
   1.258 +
   1.259 +        // insert the reference finder so that
   1.260 +        // included/imported schemas will be also parsed
   1.261 +        XMLFilterImpl f = logic.createExternalReferenceFinder(this);
   1.262 +        f.setContentHandler(handler);
   1.263 +        if (errorReceiver != null)
   1.264 +            f.setErrorHandler(errorReceiver);
   1.265 +        f.setEntityResolver(entityResolver);
   1.266 +
   1.267 +        reader.setContentHandler(f);
   1.268 +        if (errorReceiver != null)
   1.269 +            reader.setErrorHandler(errorReceiver);
   1.270 +        reader.setEntityResolver(entityResolver);
   1.271 +        return reader;
   1.272 +    }
   1.273 +
   1.274 +    private String normalizeSystemId(String systemId) {
   1.275 +        try {
   1.276 +            systemId = new URI(systemId).normalize().toString();
   1.277 +        } catch (URISyntaxException e) {
   1.278 +            // leave the system ID untouched. In my experience URI is often too strict
   1.279 +        }
   1.280 +        return systemId;
   1.281 +    }
   1.282 +
   1.283 +    boolean isExtensionMode() {
   1.284 +        return options.isExtensionMode();
   1.285 +    }
   1.286 +
   1.287 +
   1.288 +    /**
   1.289 +     * Gets the DOM tree associated with the specified system ID,
   1.290 +     * or null if none is found.
   1.291 +     */
   1.292 +    public Document get(String systemId) {
   1.293 +        Document doc = core.get(systemId);
   1.294 +
   1.295 +        if (doc == null && systemId.startsWith("file:/") && !systemId.startsWith("file://")) {
   1.296 +            // As of JDK1.4, java.net.URL.toExternal method returns URLs like
   1.297 +            // "file:/abc/def/ghi" which is an incorrect file protocol URL according to RFC1738.
   1.298 +            // Some other correctly functioning parts return the correct URLs ("file:///abc/def/ghi"),
   1.299 +            // and this descripancy breaks DOM look up by system ID.
   1.300 +
   1.301 +            // this extra check solves this problem.
   1.302 +            doc = core.get("file://" + systemId.substring(5));
   1.303 +        }
   1.304 +
   1.305 +        if (doc == null && systemId.startsWith("file:")) {
   1.306 +            // on Windows, filenames are case insensitive.
   1.307 +            // perform case-insensitive search for improved user experience
   1.308 +            String systemPath = getPath(systemId);
   1.309 +            for (String key : core.keySet()) {
   1.310 +                if (key.startsWith("file:") && getPath(key).equalsIgnoreCase(systemPath)) {
   1.311 +                    doc = core.get(key);
   1.312 +                    break;
   1.313 +                }
   1.314 +            }
   1.315 +        }
   1.316 +
   1.317 +        return doc;
   1.318 +    }
   1.319 +
   1.320 +    /**
   1.321 +     * Strips off the leading 'file:///' portion from an URL.
   1.322 +     */
   1.323 +    private String getPath(String key) {
   1.324 +        key = key.substring(5); // skip 'file:'
   1.325 +        while (key.length() > 0 && key.charAt(0) == '/')
   1.326 +            key = key.substring(1);
   1.327 +        return key;
   1.328 +    }
   1.329 +
   1.330 +    /**
   1.331 +     * Gets all the system IDs of the documents.
   1.332 +     */
   1.333 +    public String[] listSystemIDs() {
   1.334 +        return core.keySet().toArray(new String[core.keySet().size()]);
   1.335 +    }
   1.336 +
   1.337 +    /**
   1.338 +     * Gets the system ID from which the given DOM is parsed.
   1.339 +     * <p/>
   1.340 +     * Poor-man's base URI.
   1.341 +     */
   1.342 +    public String getSystemId(Document dom) {
   1.343 +        for (Map.Entry<String, Document> e : core.entrySet()) {
   1.344 +            if (e.getValue() == dom)
   1.345 +                return e.getKey();
   1.346 +        }
   1.347 +        return null;
   1.348 +    }
   1.349 +
   1.350 +    /**
   1.351 +     * Gets the first one (which is more or less random) in {@link #rootDocuments}.
   1.352 +     */
   1.353 +    public String getFirstRootDocument() {
   1.354 +        if(rootDocuments.isEmpty()) return null;
   1.355 +        return rootDocuments.iterator().next();
   1.356 +    }
   1.357 +
   1.358 +    public Set<String> getRootDocuments() {
   1.359 +        return rootDocuments;
   1.360 +    }
   1.361 +
   1.362 +    /**
   1.363 +     * Dumps the contents of the forest to the specified stream.
   1.364 +     * <p/>
   1.365 +     * This is a debug method. As such, error handling is sloppy.
   1.366 +     */
   1.367 +    public void dump(OutputStream out) throws IOException {
   1.368 +        try {
   1.369 +            // create identity transformer
   1.370 +            // secure xml processing can be switched off if input requires it
   1.371 +            boolean secureProcessingEnabled = options == null || !options.disableXmlSecurity;
   1.372 +            TransformerFactory tf = XmlUtil.newTransformerFactory(secureProcessingEnabled);
   1.373 +            Transformer it = tf.newTransformer();
   1.374 +
   1.375 +            for (Map.Entry<String, Document> e : core.entrySet()) {
   1.376 +                out.write(("---<< " + e.getKey() + '\n').getBytes());
   1.377 +
   1.378 +                DataWriter dw = new DataWriter(new OutputStreamWriter(out), null);
   1.379 +                dw.setIndentStep("  ");
   1.380 +                it.transform(new DOMSource(e.getValue()),
   1.381 +                        new SAXResult(dw));
   1.382 +
   1.383 +                out.write("\n\n\n".getBytes());
   1.384 +            }
   1.385 +        } catch (TransformerException e) {
   1.386 +            e.printStackTrace();
   1.387 +        }
   1.388 +    }
   1.389 +
   1.390 +}

mercurial