aoqi@0: /* aoqi@0: * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. Oracle designates this aoqi@0: * particular file as subject to the "Classpath" exception as provided aoqi@0: * by Oracle in the LICENSE file that accompanied this code. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: */ aoqi@0: aoqi@0: package com.sun.tools.internal.ws.wsdl.parser; aoqi@0: aoqi@0: import com.sun.istack.internal.NotNull; aoqi@0: import com.sun.tools.internal.ws.util.xml.XmlUtil; aoqi@0: import com.sun.tools.internal.ws.wscompile.ErrorReceiver; aoqi@0: import com.sun.tools.internal.ws.wscompile.WsimportOptions; aoqi@0: import com.sun.tools.internal.ws.wsdl.document.schema.SchemaConstants; aoqi@0: import com.sun.tools.internal.xjc.reader.internalizer.LocatorTable; aoqi@0: import com.sun.xml.internal.bind.marshaller.DataWriter; aoqi@0: import org.w3c.dom.Document; aoqi@0: import org.w3c.dom.Element; aoqi@0: import org.w3c.dom.NodeList; aoqi@0: import org.xml.sax.ContentHandler; aoqi@0: import org.xml.sax.*; aoqi@0: import org.xml.sax.helpers.XMLFilterImpl; aoqi@0: aoqi@0: import javax.xml.parsers.DocumentBuilder; aoqi@0: import javax.xml.parsers.DocumentBuilderFactory; aoqi@0: import javax.xml.parsers.ParserConfigurationException; aoqi@0: import javax.xml.parsers.SAXParserFactory; aoqi@0: import javax.xml.transform.Transformer; aoqi@0: import javax.xml.transform.TransformerException; aoqi@0: import javax.xml.transform.TransformerFactory; aoqi@0: import javax.xml.transform.dom.DOMSource; aoqi@0: import javax.xml.transform.sax.SAXResult; aoqi@0: import java.io.IOException; aoqi@0: import java.io.InputStream; aoqi@0: import java.io.OutputStream; aoqi@0: import java.io.OutputStreamWriter; aoqi@0: import java.net.*; aoqi@0: import java.util.*; aoqi@0: aoqi@0: /** aoqi@0: * @author Vivek Pandey aoqi@0: */ aoqi@0: public class DOMForest { aoqi@0: /** aoqi@0: * To correctly feed documents to a schema parser, we need to remember aoqi@0: * which documents (of the forest) were given as the root aoqi@0: * documents, and which of them are read as included/imported aoqi@0: * documents. aoqi@0: *

aoqi@0: *

aoqi@0: * Set of system ids as strings. aoqi@0: */ aoqi@0: protected final Set rootDocuments = new HashSet(); aoqi@0: aoqi@0: /** aoqi@0: * Contains wsdl:import(s) aoqi@0: */ aoqi@0: protected final Set externalReferences = new HashSet(); aoqi@0: aoqi@0: /** aoqi@0: * actual data storage map<SystemId,Document>. aoqi@0: */ aoqi@0: protected final Map core = new HashMap(); aoqi@0: protected final ErrorReceiver errorReceiver; aoqi@0: aoqi@0: private final DocumentBuilder documentBuilder; aoqi@0: private final SAXParserFactory parserFactory; aoqi@0: aoqi@0: /** aoqi@0: * inlined schema elements inside wsdl:type section aoqi@0: */ aoqi@0: protected final List inlinedSchemaElements = new ArrayList(); aoqi@0: aoqi@0: aoqi@0: /** aoqi@0: * Stores location information for all the trees in this forest. aoqi@0: */ aoqi@0: public final LocatorTable locatorTable = new LocatorTable(); aoqi@0: aoqi@0: protected final EntityResolver entityResolver; aoqi@0: /** aoqi@0: * Stores all the outer-most <jaxb:bindings> customizations. aoqi@0: */ aoqi@0: public final Set outerMostBindings = new HashSet(); aoqi@0: aoqi@0: /** aoqi@0: * Schema language dependent part of the processing. aoqi@0: */ aoqi@0: protected final InternalizationLogic logic; aoqi@0: protected final WsimportOptions options; aoqi@0: aoqi@0: public DOMForest(InternalizationLogic logic, @NotNull EntityResolver entityResolver, WsimportOptions options, ErrorReceiver errReceiver) { aoqi@0: this.options = options; aoqi@0: this.entityResolver = entityResolver; aoqi@0: this.errorReceiver = errReceiver; aoqi@0: this.logic = logic; aoqi@0: try { aoqi@0: // secure xml processing can be switched off if input requires it aoqi@0: boolean secureProcessingEnabled = options == null || !options.disableXmlSecurity; aoqi@0: DocumentBuilderFactory dbf = XmlUtil.newDocumentBuilderFactory(secureProcessingEnabled); aoqi@0: dbf.setNamespaceAware(true); aoqi@0: this.documentBuilder = dbf.newDocumentBuilder(); aoqi@0: aoqi@0: this.parserFactory = XmlUtil.newSAXParserFactory(secureProcessingEnabled); aoqi@0: this.parserFactory.setNamespaceAware(true); aoqi@0: } catch (ParserConfigurationException e) { aoqi@0: throw new AssertionError(e); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: public List getInlinedSchemaElement() { aoqi@0: return inlinedSchemaElements; aoqi@0: } aoqi@0: aoqi@0: public @NotNull Document parse(InputSource source, boolean root) throws SAXException, IOException { aoqi@0: if (source.getSystemId() == null) aoqi@0: throw new IllegalArgumentException(); aoqi@0: return parse(source.getSystemId(), source, root); aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Parses an XML at the given location ( aoqi@0: * and XMLs referenced by it) into DOM trees aoqi@0: * and stores them to this forest. aoqi@0: * aoqi@0: * @return the parsed DOM document object. aoqi@0: */ aoqi@0: public Document parse(String systemId, boolean root) throws SAXException, IOException{ aoqi@0: aoqi@0: systemId = normalizeSystemId(systemId); aoqi@0: aoqi@0: InputSource is = null; aoqi@0: aoqi@0: // allow entity resolver to find the actual byte stream. aoqi@0: is = entityResolver.resolveEntity(null, systemId); aoqi@0: if (is == null) aoqi@0: is = new InputSource(systemId); aoqi@0: else { aoqi@0: resolvedCache.put(systemId, is.getSystemId()); aoqi@0: systemId=is.getSystemId(); aoqi@0: } aoqi@0: aoqi@0: if (core.containsKey(systemId)) { aoqi@0: // this document has already been parsed. Just ignore. aoqi@0: return core.get(systemId); aoqi@0: } aoqi@0: aoqi@0: if(!root) aoqi@0: addExternalReferences(systemId); aoqi@0: aoqi@0: // but we still use the original system Id as the key. aoqi@0: return parse(systemId, is, root); aoqi@0: } aoqi@0: protected Map resolvedCache = new HashMap(); aoqi@0: aoqi@0: public Map getReferencedEntityMap() { aoqi@0: return resolvedCache; aoqi@0: } aoqi@0: /** aoqi@0: * Parses the given document and add it to the DOM forest. aoqi@0: * aoqi@0: * @return null if there was a parse error. otherwise non-null. aoqi@0: */ aoqi@0: private @NotNull Document parse(String systemId, InputSource inputSource, boolean root) throws SAXException, IOException{ aoqi@0: Document dom = documentBuilder.newDocument(); aoqi@0: aoqi@0: systemId = normalizeSystemId(systemId); aoqi@0: aoqi@0: // put into the map before growing a tree, to aoqi@0: // prevent recursive reference from causing infinite loop. aoqi@0: core.put(systemId, dom); aoqi@0: aoqi@0: dom.setDocumentURI(systemId); aoqi@0: if (root) aoqi@0: rootDocuments.add(systemId); aoqi@0: aoqi@0: try { aoqi@0: XMLReader reader = createReader(dom); aoqi@0: aoqi@0: InputStream is = null; aoqi@0: if(inputSource.getByteStream() == null){ aoqi@0: inputSource = entityResolver.resolveEntity(null, systemId); aoqi@0: } aoqi@0: reader.parse(inputSource); aoqi@0: Element doc = dom.getDocumentElement(); aoqi@0: if (doc == null) { aoqi@0: return null; aoqi@0: } aoqi@0: NodeList schemas = doc.getElementsByTagNameNS(SchemaConstants.NS_XSD, "schema"); aoqi@0: for (int i = 0; i < schemas.getLength(); i++) { aoqi@0: inlinedSchemaElements.add((Element) schemas.item(i)); aoqi@0: } aoqi@0: } catch (ParserConfigurationException e) { aoqi@0: errorReceiver.error(e); aoqi@0: throw new SAXException(e.getMessage()); aoqi@0: } aoqi@0: resolvedCache.put(systemId, dom.getDocumentURI()); aoqi@0: return dom; aoqi@0: } aoqi@0: aoqi@0: public void addExternalReferences(String ref) { aoqi@0: if (!externalReferences.contains(ref)) aoqi@0: externalReferences.add(ref); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: public Set getExternalReferences() { aoqi@0: return externalReferences; aoqi@0: } aoqi@0: aoqi@0: aoqi@0: aoqi@0: public interface Handler extends ContentHandler { aoqi@0: /** aoqi@0: * Gets the DOM that was built. aoqi@0: */ aoqi@0: public Document getDocument(); aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Returns a {@link org.xml.sax.XMLReader} to parse a document into this DOM forest. aoqi@0: *

aoqi@0: * This version requires that the DOM object to be created and registered aoqi@0: * to the map beforehand. aoqi@0: */ aoqi@0: private XMLReader createReader(Document dom) throws SAXException, ParserConfigurationException { aoqi@0: XMLReader reader = parserFactory.newSAXParser().getXMLReader(); aoqi@0: DOMBuilder dombuilder = new DOMBuilder(dom, locatorTable, outerMostBindings); aoqi@0: try { aoqi@0: reader.setProperty("http://xml.org/sax/properties/lexical-handler", dombuilder); aoqi@0: } catch(SAXException e) { aoqi@0: errorReceiver.debug(e.getMessage()); aoqi@0: } aoqi@0: aoqi@0: ContentHandler handler = new WhitespaceStripper(dombuilder, errorReceiver, entityResolver); aoqi@0: handler = new VersionChecker(handler, errorReceiver, entityResolver); aoqi@0: aoqi@0: // insert the reference finder so that aoqi@0: // included/imported schemas will be also parsed aoqi@0: XMLFilterImpl f = logic.createExternalReferenceFinder(this); aoqi@0: f.setContentHandler(handler); aoqi@0: if (errorReceiver != null) aoqi@0: f.setErrorHandler(errorReceiver); aoqi@0: f.setEntityResolver(entityResolver); aoqi@0: aoqi@0: reader.setContentHandler(f); aoqi@0: if (errorReceiver != null) aoqi@0: reader.setErrorHandler(errorReceiver); aoqi@0: reader.setEntityResolver(entityResolver); aoqi@0: return reader; aoqi@0: } aoqi@0: aoqi@0: private String normalizeSystemId(String systemId) { aoqi@0: try { aoqi@0: systemId = new URI(systemId).normalize().toString(); aoqi@0: } catch (URISyntaxException e) { aoqi@0: // leave the system ID untouched. In my experience URI is often too strict aoqi@0: } aoqi@0: return systemId; aoqi@0: } aoqi@0: aoqi@0: boolean isExtensionMode() { aoqi@0: return options.isExtensionMode(); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: /** aoqi@0: * Gets the DOM tree associated with the specified system ID, aoqi@0: * or null if none is found. aoqi@0: */ aoqi@0: public Document get(String systemId) { aoqi@0: Document doc = core.get(systemId); aoqi@0: aoqi@0: if (doc == null && systemId.startsWith("file:/") && !systemId.startsWith("file://")) { aoqi@0: // As of JDK1.4, java.net.URL.toExternal method returns URLs like aoqi@0: // "file:/abc/def/ghi" which is an incorrect file protocol URL according to RFC1738. aoqi@0: // Some other correctly functioning parts return the correct URLs ("file:///abc/def/ghi"), aoqi@0: // and this descripancy breaks DOM look up by system ID. aoqi@0: aoqi@0: // this extra check solves this problem. aoqi@0: doc = core.get("file://" + systemId.substring(5)); aoqi@0: } aoqi@0: aoqi@0: if (doc == null && systemId.startsWith("file:")) { aoqi@0: // on Windows, filenames are case insensitive. aoqi@0: // perform case-insensitive search for improved user experience aoqi@0: String systemPath = getPath(systemId); aoqi@0: for (String key : core.keySet()) { aoqi@0: if (key.startsWith("file:") && getPath(key).equalsIgnoreCase(systemPath)) { aoqi@0: doc = core.get(key); aoqi@0: break; aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: return doc; aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Strips off the leading 'file:///' portion from an URL. aoqi@0: */ aoqi@0: private String getPath(String key) { aoqi@0: key = key.substring(5); // skip 'file:' aoqi@0: while (key.length() > 0 && key.charAt(0) == '/') aoqi@0: key = key.substring(1); aoqi@0: return key; aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Gets all the system IDs of the documents. aoqi@0: */ aoqi@0: public String[] listSystemIDs() { aoqi@0: return core.keySet().toArray(new String[core.keySet().size()]); aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Gets the system ID from which the given DOM is parsed. aoqi@0: *

aoqi@0: * Poor-man's base URI. aoqi@0: */ aoqi@0: public String getSystemId(Document dom) { aoqi@0: for (Map.Entry e : core.entrySet()) { aoqi@0: if (e.getValue() == dom) aoqi@0: return e.getKey(); aoqi@0: } aoqi@0: return null; aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Gets the first one (which is more or less random) in {@link #rootDocuments}. aoqi@0: */ aoqi@0: public String getFirstRootDocument() { aoqi@0: if(rootDocuments.isEmpty()) return null; aoqi@0: return rootDocuments.iterator().next(); aoqi@0: } aoqi@0: aoqi@0: public Set getRootDocuments() { aoqi@0: return rootDocuments; aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Dumps the contents of the forest to the specified stream. aoqi@0: *

aoqi@0: * This is a debug method. As such, error handling is sloppy. aoqi@0: */ aoqi@0: public void dump(OutputStream out) throws IOException { aoqi@0: try { aoqi@0: // create identity transformer aoqi@0: // secure xml processing can be switched off if input requires it aoqi@0: boolean secureProcessingEnabled = options == null || !options.disableXmlSecurity; aoqi@0: TransformerFactory tf = XmlUtil.newTransformerFactory(secureProcessingEnabled); aoqi@0: Transformer it = tf.newTransformer(); aoqi@0: aoqi@0: for (Map.Entry e : core.entrySet()) { aoqi@0: out.write(("---<< " + e.getKey() + '\n').getBytes()); aoqi@0: aoqi@0: DataWriter dw = new DataWriter(new OutputStreamWriter(out), null); aoqi@0: dw.setIndentStep(" "); aoqi@0: it.transform(new DOMSource(e.getValue()), aoqi@0: new SAXResult(dw)); aoqi@0: aoqi@0: out.write("\n\n\n".getBytes()); aoqi@0: } aoqi@0: } catch (TransformerException e) { aoqi@0: e.printStackTrace(); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: }