ohair@286: /*
alanb@368: * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
ohair@286: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
ohair@286: *
ohair@286: * This code is free software; you can redistribute it and/or modify it
ohair@286: * under the terms of the GNU General Public License version 2 only, as
ohair@286: * published by the Free Software Foundation. Oracle designates this
ohair@286: * particular file as subject to the "Classpath" exception as provided
ohair@286: * by Oracle in the LICENSE file that accompanied this code.
ohair@286: *
ohair@286: * This code is distributed in the hope that it will be useful, but WITHOUT
ohair@286: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
ohair@286: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
ohair@286: * version 2 for more details (a copy is included in the LICENSE file that
ohair@286: * accompanied this code).
ohair@286: *
ohair@286: * You should have received a copy of the GNU General Public License version
ohair@286: * 2 along with this work; if not, write to the Free Software Foundation,
ohair@286: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
ohair@286: *
ohair@286: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ohair@286: * or visit www.oracle.com if you need additional information or have any
ohair@286: * questions.
ohair@286: */
ohair@286:
ohair@286: package com.sun.tools.internal.ws.wsdl.parser;
ohair@286:
ohair@286: import com.sun.istack.internal.NotNull;
alanb@368: import com.sun.tools.internal.ws.util.xml.XmlUtil;
ohair@286: import com.sun.tools.internal.ws.wscompile.ErrorReceiver;
ohair@286: import com.sun.tools.internal.ws.wscompile.WsimportOptions;
ohair@286: import com.sun.tools.internal.ws.wsdl.document.schema.SchemaConstants;
ohair@286: import com.sun.tools.internal.xjc.reader.internalizer.LocatorTable;
ohair@286: import com.sun.xml.internal.bind.marshaller.DataWriter;
ohair@286: import org.w3c.dom.Document;
ohair@286: import org.w3c.dom.Element;
ohair@286: import org.w3c.dom.NodeList;
ohair@286: import org.xml.sax.ContentHandler;
ohair@286: import org.xml.sax.*;
ohair@286: import org.xml.sax.helpers.XMLFilterImpl;
ohair@286:
ohair@286: import javax.xml.parsers.DocumentBuilder;
ohair@286: import javax.xml.parsers.DocumentBuilderFactory;
ohair@286: import javax.xml.parsers.ParserConfigurationException;
ohair@286: import javax.xml.parsers.SAXParserFactory;
ohair@286: import javax.xml.transform.Transformer;
ohair@286: import javax.xml.transform.TransformerException;
ohair@286: import javax.xml.transform.TransformerFactory;
ohair@286: import javax.xml.transform.dom.DOMSource;
ohair@286: import javax.xml.transform.sax.SAXResult;
ohair@286: import java.io.IOException;
ohair@286: import java.io.InputStream;
ohair@286: import java.io.OutputStream;
ohair@286: import java.io.OutputStreamWriter;
ohair@286: import java.net.*;
ohair@286: import java.util.*;
ohair@286:
ohair@286: /**
ohair@286: * @author Vivek Pandey
ohair@286: */
ohair@286: public class DOMForest {
ohair@286: /**
ohair@286: * To correctly feed documents to a schema parser, we need to remember
ohair@286: * which documents (of the forest) were given as the root
ohair@286: * documents, and which of them are read as included/imported
ohair@286: * documents.
ohair@286: *
ohair@286: *
ohair@286: * Set of system ids as strings.
ohair@286: */
ohair@286: protected final Set rootDocuments = new HashSet();
ohair@286:
ohair@286: /**
ohair@286: * Contains wsdl:import(s)
ohair@286: */
ohair@286: protected final Set externalReferences = new HashSet();
ohair@286:
ohair@286: /**
ohair@286: * actual data storage map<SystemId,Document>.
ohair@286: */
ohair@286: protected final Map core = new HashMap();
ohair@286: protected final ErrorReceiver errorReceiver;
ohair@286:
ohair@286: private final DocumentBuilder documentBuilder;
ohair@286: private final SAXParserFactory parserFactory;
ohair@286:
ohair@286: /**
ohair@286: * inlined schema elements inside wsdl:type section
ohair@286: */
ohair@286: protected final List inlinedSchemaElements = new ArrayList();
ohair@286:
ohair@286:
ohair@286: /**
ohair@286: * Stores location information for all the trees in this forest.
ohair@286: */
ohair@286: public final LocatorTable locatorTable = new LocatorTable();
ohair@286:
ohair@286: protected final EntityResolver entityResolver;
ohair@286: /**
ohair@286: * Stores all the outer-most <jaxb:bindings> customizations.
ohair@286: */
ohair@286: public final Set outerMostBindings = new HashSet();
ohair@286:
ohair@286: /**
ohair@286: * Schema language dependent part of the processing.
ohair@286: */
ohair@286: protected final InternalizationLogic logic;
ohair@286: protected final WsimportOptions options;
ohair@286:
ohair@286: public DOMForest(InternalizationLogic logic, @NotNull EntityResolver entityResolver, WsimportOptions options, ErrorReceiver errReceiver) {
ohair@286: this.options = options;
ohair@286: this.entityResolver = entityResolver;
ohair@286: this.errorReceiver = errReceiver;
ohair@286: this.logic = logic;
ohair@286: try {
alanb@368: // secure xml processing can be switched off if input requires it
mkos@408: boolean secureProcessingEnabled = options == null || !options.disableXmlSecurity;
alanb@368: DocumentBuilderFactory dbf = XmlUtil.newDocumentBuilderFactory(secureProcessingEnabled);
ohair@286: dbf.setNamespaceAware(true);
ohair@286: this.documentBuilder = dbf.newDocumentBuilder();
ohair@286:
alanb@368: this.parserFactory = XmlUtil.newSAXParserFactory(secureProcessingEnabled);
ohair@286: this.parserFactory.setNamespaceAware(true);
ohair@286: } catch (ParserConfigurationException e) {
ohair@286: throw new AssertionError(e);
ohair@286: }
ohair@286: }
ohair@286:
ohair@286: public List getInlinedSchemaElement() {
ohair@286: return inlinedSchemaElements;
ohair@286: }
ohair@286:
ohair@286: public @NotNull Document parse(InputSource source, boolean root) throws SAXException, IOException {
ohair@286: if (source.getSystemId() == null)
ohair@286: throw new IllegalArgumentException();
ohair@286: return parse(source.getSystemId(), source, root);
ohair@286: }
ohair@286:
ohair@286: /**
ohair@286: * Parses an XML at the given location (
ohair@286: * and XMLs referenced by it) into DOM trees
ohair@286: * and stores them to this forest.
ohair@286: *
ohair@286: * @return the parsed DOM document object.
ohair@286: */
ohair@286: public Document parse(String systemId, boolean root) throws SAXException, IOException{
ohair@286:
ohair@286: systemId = normalizeSystemId(systemId);
ohair@286:
ohair@286: InputSource is = null;
ohair@286:
ohair@286: // allow entity resolver to find the actual byte stream.
ohair@286: is = entityResolver.resolveEntity(null, systemId);
ohair@286: if (is == null)
ohair@286: is = new InputSource(systemId);
ohair@286: else {
ohair@286: resolvedCache.put(systemId, is.getSystemId());
ohair@286: systemId=is.getSystemId();
ohair@286: }
ohair@286:
ohair@286: if (core.containsKey(systemId)) {
ohair@286: // this document has already been parsed. Just ignore.
ohair@286: return core.get(systemId);
ohair@286: }
ohair@286:
ohair@286: if(!root)
ohair@286: addExternalReferences(systemId);
ohair@286:
ohair@286: // but we still use the original system Id as the key.
ohair@286: return parse(systemId, is, root);
ohair@286: }
ohair@286: protected Map resolvedCache = new HashMap();
ohair@286:
ohair@286: public Map getReferencedEntityMap() {
ohair@286: return resolvedCache;
ohair@286: }
ohair@286: /**
ohair@286: * Parses the given document and add it to the DOM forest.
ohair@286: *
ohair@286: * @return null if there was a parse error. otherwise non-null.
ohair@286: */
ohair@286: private @NotNull Document parse(String systemId, InputSource inputSource, boolean root) throws SAXException, IOException{
ohair@286: Document dom = documentBuilder.newDocument();
ohair@286:
ohair@286: systemId = normalizeSystemId(systemId);
ohair@286:
ohair@286: // put into the map before growing a tree, to
ohair@286: // prevent recursive reference from causing infinite loop.
ohair@286: core.put(systemId, dom);
ohair@286:
ohair@286: dom.setDocumentURI(systemId);
ohair@286: if (root)
ohair@286: rootDocuments.add(systemId);
ohair@286:
ohair@286: try {
ohair@286: XMLReader reader = createReader(dom);
ohair@286:
ohair@286: InputStream is = null;
ohair@286: if(inputSource.getByteStream() == null){
ohair@286: inputSource = entityResolver.resolveEntity(null, systemId);
ohair@286: }
ohair@286: reader.parse(inputSource);
ohair@286: Element doc = dom.getDocumentElement();
ohair@286: if (doc == null) {
ohair@286: return null;
ohair@286: }
ohair@286: NodeList schemas = doc.getElementsByTagNameNS(SchemaConstants.NS_XSD, "schema");
ohair@286: for (int i = 0; i < schemas.getLength(); i++) {
ohair@286: inlinedSchemaElements.add((Element) schemas.item(i));
ohair@286: }
ohair@286: } catch (ParserConfigurationException e) {
ohair@286: errorReceiver.error(e);
ohair@286: throw new SAXException(e.getMessage());
ohair@286: }
ohair@286: resolvedCache.put(systemId, dom.getDocumentURI());
ohair@286: return dom;
ohair@286: }
ohair@286:
ohair@286: public void addExternalReferences(String ref) {
ohair@286: if (!externalReferences.contains(ref))
ohair@286: externalReferences.add(ref);
ohair@286: }
ohair@286:
ohair@286:
ohair@286: public Set getExternalReferences() {
ohair@286: return externalReferences;
ohair@286: }
ohair@286:
ohair@286:
ohair@286:
ohair@286: public interface Handler extends ContentHandler {
ohair@286: /**
ohair@286: * Gets the DOM that was built.
ohair@286: */
ohair@286: public Document getDocument();
ohair@286: }
ohair@286:
ohair@286: /**
ohair@286: * Returns a {@link org.xml.sax.XMLReader} to parse a document into this DOM forest.
ohair@286: *
ohair@286: * This version requires that the DOM object to be created and registered
ohair@286: * to the map beforehand.
ohair@286: */
ohair@286: private XMLReader createReader(Document dom) throws SAXException, ParserConfigurationException {
ohair@286: XMLReader reader = parserFactory.newSAXParser().getXMLReader();
ohair@286: DOMBuilder dombuilder = new DOMBuilder(dom, locatorTable, outerMostBindings);
ohair@286: try {
ohair@286: reader.setProperty("http://xml.org/sax/properties/lexical-handler", dombuilder);
ohair@286: } catch(SAXException e) {
ohair@286: errorReceiver.debug(e.getMessage());
ohair@286: }
ohair@286:
ohair@286: ContentHandler handler = new WhitespaceStripper(dombuilder, errorReceiver, entityResolver);
ohair@286: handler = new VersionChecker(handler, errorReceiver, entityResolver);
ohair@286:
ohair@286: // insert the reference finder so that
ohair@286: // included/imported schemas will be also parsed
ohair@286: XMLFilterImpl f = logic.createExternalReferenceFinder(this);
ohair@286: f.setContentHandler(handler);
ohair@286: if (errorReceiver != null)
ohair@286: f.setErrorHandler(errorReceiver);
ohair@286: f.setEntityResolver(entityResolver);
ohair@286:
ohair@286: reader.setContentHandler(f);
ohair@286: if (errorReceiver != null)
ohair@286: reader.setErrorHandler(errorReceiver);
ohair@286: reader.setEntityResolver(entityResolver);
ohair@286: return reader;
ohair@286: }
ohair@286:
ohair@286: private String normalizeSystemId(String systemId) {
ohair@286: try {
ohair@286: systemId = new URI(systemId).normalize().toString();
ohair@286: } catch (URISyntaxException e) {
ohair@286: // leave the system ID untouched. In my experience URI is often too strict
ohair@286: }
ohair@286: return systemId;
ohair@286: }
ohair@286:
ohair@286: boolean isExtensionMode() {
ohair@286: return options.isExtensionMode();
ohair@286: }
ohair@286:
ohair@286:
ohair@286: /**
ohair@286: * Gets the DOM tree associated with the specified system ID,
ohair@286: * or null if none is found.
ohair@286: */
ohair@286: public Document get(String systemId) {
ohair@286: Document doc = core.get(systemId);
ohair@286:
ohair@286: if (doc == null && systemId.startsWith("file:/") && !systemId.startsWith("file://")) {
ohair@286: // As of JDK1.4, java.net.URL.toExternal method returns URLs like
ohair@286: // "file:/abc/def/ghi" which is an incorrect file protocol URL according to RFC1738.
ohair@286: // Some other correctly functioning parts return the correct URLs ("file:///abc/def/ghi"),
ohair@286: // and this descripancy breaks DOM look up by system ID.
ohair@286:
ohair@286: // this extra check solves this problem.
ohair@286: doc = core.get("file://" + systemId.substring(5));
ohair@286: }
ohair@286:
ohair@286: if (doc == null && systemId.startsWith("file:")) {
ohair@286: // on Windows, filenames are case insensitive.
ohair@286: // perform case-insensitive search for improved user experience
ohair@286: String systemPath = getPath(systemId);
ohair@286: for (String key : core.keySet()) {
ohair@286: if (key.startsWith("file:") && getPath(key).equalsIgnoreCase(systemPath)) {
ohair@286: doc = core.get(key);
ohair@286: break;
ohair@286: }
ohair@286: }
ohair@286: }
ohair@286:
ohair@286: return doc;
ohair@286: }
ohair@286:
ohair@286: /**
ohair@286: * Strips off the leading 'file:///' portion from an URL.
ohair@286: */
ohair@286: private String getPath(String key) {
ohair@286: key = key.substring(5); // skip 'file:'
ohair@286: while (key.length() > 0 && key.charAt(0) == '/')
ohair@286: key = key.substring(1);
ohair@286: return key;
ohair@286: }
ohair@286:
ohair@286: /**
ohair@286: * Gets all the system IDs of the documents.
ohair@286: */
ohair@286: public String[] listSystemIDs() {
ohair@286: return core.keySet().toArray(new String[core.keySet().size()]);
ohair@286: }
ohair@286:
ohair@286: /**
ohair@286: * Gets the system ID from which the given DOM is parsed.
ohair@286: *
ohair@286: * Poor-man's base URI.
ohair@286: */
ohair@286: public String getSystemId(Document dom) {
ohair@286: for (Map.Entry e : core.entrySet()) {
ohair@286: if (e.getValue() == dom)
ohair@286: return e.getKey();
ohair@286: }
ohair@286: return null;
ohair@286: }
ohair@286:
ohair@286: /**
ohair@286: * Gets the first one (which is more or less random) in {@link #rootDocuments}.
ohair@286: */
ohair@286: public String getFirstRootDocument() {
ohair@286: if(rootDocuments.isEmpty()) return null;
ohair@286: return rootDocuments.iterator().next();
ohair@286: }
ohair@286:
ohair@286: public Set getRootDocuments() {
ohair@286: return rootDocuments;
ohair@286: }
ohair@286:
ohair@286: /**
ohair@286: * Dumps the contents of the forest to the specified stream.
ohair@286: *
ohair@286: * This is a debug method. As such, error handling is sloppy.
ohair@286: */
ohair@286: public void dump(OutputStream out) throws IOException {
ohair@286: try {
ohair@286: // create identity transformer
alanb@368: // secure xml processing can be switched off if input requires it
mkos@408: boolean secureProcessingEnabled = options == null || !options.disableXmlSecurity;
alanb@368: TransformerFactory tf = XmlUtil.newTransformerFactory(secureProcessingEnabled);
alanb@368: Transformer it = tf.newTransformer();
ohair@286:
ohair@286: for (Map.Entry e : core.entrySet()) {
ohair@286: out.write(("---<< " + e.getKey() + '\n').getBytes());
ohair@286:
ohair@286: DataWriter dw = new DataWriter(new OutputStreamWriter(out), null);
ohair@286: dw.setIndentStep(" ");
ohair@286: it.transform(new DOMSource(e.getValue()),
ohair@286: new SAXResult(dw));
ohair@286:
ohair@286: out.write("\n\n\n".getBytes());
ohair@286: }
ohair@286: } catch (TransformerException e) {
ohair@286: e.printStackTrace();
ohair@286: }
ohair@286: }
ohair@286:
ohair@286: }