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