src/share/jaxws_classes/com/sun/tools/internal/ws/wsdl/parser/DOMForest.java

Tue, 06 Mar 2012 16:09:35 -0800

author
ohair
date
Tue, 06 Mar 2012 16:09:35 -0800
changeset 286
f50545b5e2f1
child 368
0989ad8c0860
permissions
-rw-r--r--

7150322: Stop using drop source bundles in jaxws
Reviewed-by: darcy, ohrstrom

ohair@286 1 /*
ohair@286 2 * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved.
ohair@286 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
ohair@286 4 *
ohair@286 5 * This code is free software; you can redistribute it and/or modify it
ohair@286 6 * under the terms of the GNU General Public License version 2 only, as
ohair@286 7 * published by the Free Software Foundation. Oracle designates this
ohair@286 8 * particular file as subject to the "Classpath" exception as provided
ohair@286 9 * by Oracle in the LICENSE file that accompanied this code.
ohair@286 10 *
ohair@286 11 * This code is distributed in the hope that it will be useful, but WITHOUT
ohair@286 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
ohair@286 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
ohair@286 14 * version 2 for more details (a copy is included in the LICENSE file that
ohair@286 15 * accompanied this code).
ohair@286 16 *
ohair@286 17 * You should have received a copy of the GNU General Public License version
ohair@286 18 * 2 along with this work; if not, write to the Free Software Foundation,
ohair@286 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
ohair@286 20 *
ohair@286 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ohair@286 22 * or visit www.oracle.com if you need additional information or have any
ohair@286 23 * questions.
ohair@286 24 */
ohair@286 25
ohair@286 26 package com.sun.tools.internal.ws.wsdl.parser;
ohair@286 27
ohair@286 28 import com.sun.istack.internal.NotNull;
ohair@286 29 import com.sun.tools.internal.ws.resources.WscompileMessages;
ohair@286 30 import com.sun.tools.internal.ws.wscompile.AbortException;
ohair@286 31 import com.sun.tools.internal.ws.wscompile.DefaultAuthenticator;
ohair@286 32 import com.sun.tools.internal.ws.wscompile.ErrorReceiver;
ohair@286 33 import com.sun.tools.internal.ws.wscompile.WsimportOptions;
ohair@286 34 import com.sun.tools.internal.ws.wsdl.document.schema.SchemaConstants;
ohair@286 35 import com.sun.tools.internal.xjc.reader.internalizer.LocatorTable;
ohair@286 36 import com.sun.xml.internal.bind.marshaller.DataWriter;
ohair@286 37 import com.sun.xml.internal.ws.util.JAXWSUtils;
ohair@286 38 import org.w3c.dom.Document;
ohair@286 39 import org.w3c.dom.Element;
ohair@286 40 import org.w3c.dom.NodeList;
ohair@286 41 import org.xml.sax.ContentHandler;
ohair@286 42 import org.xml.sax.*;
ohair@286 43 import org.xml.sax.helpers.XMLFilterImpl;
ohair@286 44
ohair@286 45 import javax.xml.parsers.DocumentBuilder;
ohair@286 46 import javax.xml.parsers.DocumentBuilderFactory;
ohair@286 47 import javax.xml.parsers.ParserConfigurationException;
ohair@286 48 import javax.xml.parsers.SAXParserFactory;
ohair@286 49 import javax.xml.transform.Transformer;
ohair@286 50 import javax.xml.transform.TransformerException;
ohair@286 51 import javax.xml.transform.TransformerFactory;
ohair@286 52 import javax.xml.transform.dom.DOMSource;
ohair@286 53 import javax.xml.transform.sax.SAXResult;
ohair@286 54 import javax.net.ssl.HttpsURLConnection;
ohair@286 55 import javax.net.ssl.HostnameVerifier;
ohair@286 56 import javax.net.ssl.SSLSession;
ohair@286 57 import java.io.IOException;
ohair@286 58 import java.io.InputStream;
ohair@286 59 import java.io.OutputStream;
ohair@286 60 import java.io.OutputStreamWriter;
ohair@286 61 import java.net.*;
ohair@286 62 import java.util.*;
ohair@286 63
ohair@286 64 /**
ohair@286 65 * @author Vivek Pandey
ohair@286 66 */
ohair@286 67 public class DOMForest {
ohair@286 68 /**
ohair@286 69 * To correctly feed documents to a schema parser, we need to remember
ohair@286 70 * which documents (of the forest) were given as the root
ohair@286 71 * documents, and which of them are read as included/imported
ohair@286 72 * documents.
ohair@286 73 * <p/>
ohair@286 74 * <p/>
ohair@286 75 * Set of system ids as strings.
ohair@286 76 */
ohair@286 77 protected final Set<String> rootDocuments = new HashSet<String>();
ohair@286 78
ohair@286 79 /**
ohair@286 80 * Contains wsdl:import(s)
ohair@286 81 */
ohair@286 82 protected final Set<String> externalReferences = new HashSet<String>();
ohair@286 83
ohair@286 84 /**
ohair@286 85 * actual data storage map&lt;SystemId,Document>.
ohair@286 86 */
ohair@286 87 protected final Map<String, Document> core = new HashMap<String, Document>();
ohair@286 88 protected final ErrorReceiver errorReceiver;
ohair@286 89
ohair@286 90 private final DocumentBuilder documentBuilder;
ohair@286 91 private final SAXParserFactory parserFactory;
ohair@286 92
ohair@286 93 /**
ohair@286 94 * inlined schema elements inside wsdl:type section
ohair@286 95 */
ohair@286 96 protected final List<Element> inlinedSchemaElements = new ArrayList<Element>();
ohair@286 97
ohair@286 98
ohair@286 99 /**
ohair@286 100 * Stores location information for all the trees in this forest.
ohair@286 101 */
ohair@286 102 public final LocatorTable locatorTable = new LocatorTable();
ohair@286 103
ohair@286 104 protected final EntityResolver entityResolver;
ohair@286 105 /**
ohair@286 106 * Stores all the outer-most &lt;jaxb:bindings> customizations.
ohair@286 107 */
ohair@286 108 public final Set<Element> outerMostBindings = new HashSet<Element>();
ohair@286 109
ohair@286 110 /**
ohair@286 111 * Schema language dependent part of the processing.
ohair@286 112 */
ohair@286 113 protected final InternalizationLogic logic;
ohair@286 114 protected final WsimportOptions options;
ohair@286 115
ohair@286 116 public DOMForest(InternalizationLogic logic, @NotNull EntityResolver entityResolver, WsimportOptions options, ErrorReceiver errReceiver) {
ohair@286 117 this.options = options;
ohair@286 118 this.entityResolver = entityResolver;
ohair@286 119 this.errorReceiver = errReceiver;
ohair@286 120 this.logic = logic;
ohair@286 121 try {
ohair@286 122 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
ohair@286 123 dbf.setNamespaceAware(true);
ohair@286 124 this.documentBuilder = dbf.newDocumentBuilder();
ohair@286 125
ohair@286 126 this.parserFactory = SAXParserFactory.newInstance();
ohair@286 127 this.parserFactory.setNamespaceAware(true);
ohair@286 128 } catch (ParserConfigurationException e) {
ohair@286 129 throw new AssertionError(e);
ohair@286 130 }
ohair@286 131 }
ohair@286 132
ohair@286 133 public List<Element> getInlinedSchemaElement() {
ohair@286 134 return inlinedSchemaElements;
ohair@286 135 }
ohair@286 136
ohair@286 137 public @NotNull Document parse(InputSource source, boolean root) throws SAXException, IOException {
ohair@286 138 if (source.getSystemId() == null)
ohair@286 139 throw new IllegalArgumentException();
ohair@286 140 return parse(source.getSystemId(), source, root);
ohair@286 141 }
ohair@286 142
ohair@286 143 /**
ohair@286 144 * Parses an XML at the given location (
ohair@286 145 * and XMLs referenced by it) into DOM trees
ohair@286 146 * and stores them to this forest.
ohair@286 147 *
ohair@286 148 * @return the parsed DOM document object.
ohair@286 149 */
ohair@286 150 public Document parse(String systemId, boolean root) throws SAXException, IOException{
ohair@286 151
ohair@286 152 systemId = normalizeSystemId(systemId);
ohair@286 153
ohair@286 154 InputSource is = null;
ohair@286 155
ohair@286 156 // allow entity resolver to find the actual byte stream.
ohair@286 157 is = entityResolver.resolveEntity(null, systemId);
ohair@286 158 if (is == null)
ohair@286 159 is = new InputSource(systemId);
ohair@286 160 else {
ohair@286 161 resolvedCache.put(systemId, is.getSystemId());
ohair@286 162 systemId=is.getSystemId();
ohair@286 163 }
ohair@286 164
ohair@286 165 if (core.containsKey(systemId)) {
ohair@286 166 // this document has already been parsed. Just ignore.
ohair@286 167 return core.get(systemId);
ohair@286 168 }
ohair@286 169
ohair@286 170 if(!root)
ohair@286 171 addExternalReferences(systemId);
ohair@286 172
ohair@286 173 // but we still use the original system Id as the key.
ohair@286 174 return parse(systemId, is, root);
ohair@286 175 }
ohair@286 176 protected Map<String,String> resolvedCache = new HashMap<String,String>();
ohair@286 177
ohair@286 178 public Map<String,String> getReferencedEntityMap() {
ohair@286 179 return resolvedCache;
ohair@286 180 }
ohair@286 181 /**
ohair@286 182 * Parses the given document and add it to the DOM forest.
ohair@286 183 *
ohair@286 184 * @return null if there was a parse error. otherwise non-null.
ohair@286 185 */
ohair@286 186 private @NotNull Document parse(String systemId, InputSource inputSource, boolean root) throws SAXException, IOException{
ohair@286 187 Document dom = documentBuilder.newDocument();
ohair@286 188
ohair@286 189 systemId = normalizeSystemId(systemId);
ohair@286 190
ohair@286 191 // put into the map before growing a tree, to
ohair@286 192 // prevent recursive reference from causing infinite loop.
ohair@286 193 core.put(systemId, dom);
ohair@286 194
ohair@286 195 dom.setDocumentURI(systemId);
ohair@286 196 if (root)
ohair@286 197 rootDocuments.add(systemId);
ohair@286 198
ohair@286 199 try {
ohair@286 200 XMLReader reader = createReader(dom);
ohair@286 201
ohair@286 202 InputStream is = null;
ohair@286 203 if(inputSource.getByteStream() == null){
ohair@286 204 inputSource = entityResolver.resolveEntity(null, systemId);
ohair@286 205 }
ohair@286 206 reader.parse(inputSource);
ohair@286 207 Element doc = dom.getDocumentElement();
ohair@286 208 if (doc == null) {
ohair@286 209 return null;
ohair@286 210 }
ohair@286 211 NodeList schemas = doc.getElementsByTagNameNS(SchemaConstants.NS_XSD, "schema");
ohair@286 212 for (int i = 0; i < schemas.getLength(); i++) {
ohair@286 213 inlinedSchemaElements.add((Element) schemas.item(i));
ohair@286 214 }
ohair@286 215 } catch (ParserConfigurationException e) {
ohair@286 216 errorReceiver.error(e);
ohair@286 217 throw new SAXException(e.getMessage());
ohair@286 218 }
ohair@286 219 resolvedCache.put(systemId, dom.getDocumentURI());
ohair@286 220 return dom;
ohair@286 221 }
ohair@286 222
ohair@286 223 public void addExternalReferences(String ref) {
ohair@286 224 if (!externalReferences.contains(ref))
ohair@286 225 externalReferences.add(ref);
ohair@286 226 }
ohair@286 227
ohair@286 228
ohair@286 229 public Set<String> getExternalReferences() {
ohair@286 230 return externalReferences;
ohair@286 231 }
ohair@286 232
ohair@286 233
ohair@286 234
ohair@286 235 public interface Handler extends ContentHandler {
ohair@286 236 /**
ohair@286 237 * Gets the DOM that was built.
ohair@286 238 */
ohair@286 239 public Document getDocument();
ohair@286 240 }
ohair@286 241
ohair@286 242 /**
ohair@286 243 * Returns a {@link org.xml.sax.XMLReader} to parse a document into this DOM forest.
ohair@286 244 * <p/>
ohair@286 245 * This version requires that the DOM object to be created and registered
ohair@286 246 * to the map beforehand.
ohair@286 247 */
ohair@286 248 private XMLReader createReader(Document dom) throws SAXException, ParserConfigurationException {
ohair@286 249 XMLReader reader = parserFactory.newSAXParser().getXMLReader();
ohair@286 250 DOMBuilder dombuilder = new DOMBuilder(dom, locatorTable, outerMostBindings);
ohair@286 251 try {
ohair@286 252 reader.setProperty("http://xml.org/sax/properties/lexical-handler", dombuilder);
ohair@286 253 } catch(SAXException e) {
ohair@286 254 errorReceiver.debug(e.getMessage());
ohair@286 255 }
ohair@286 256
ohair@286 257 ContentHandler handler = new WhitespaceStripper(dombuilder, errorReceiver, entityResolver);
ohair@286 258 handler = new VersionChecker(handler, errorReceiver, entityResolver);
ohair@286 259
ohair@286 260 // insert the reference finder so that
ohair@286 261 // included/imported schemas will be also parsed
ohair@286 262 XMLFilterImpl f = logic.createExternalReferenceFinder(this);
ohair@286 263 f.setContentHandler(handler);
ohair@286 264 if (errorReceiver != null)
ohair@286 265 f.setErrorHandler(errorReceiver);
ohair@286 266 f.setEntityResolver(entityResolver);
ohair@286 267
ohair@286 268 reader.setContentHandler(f);
ohair@286 269 if (errorReceiver != null)
ohair@286 270 reader.setErrorHandler(errorReceiver);
ohair@286 271 reader.setEntityResolver(entityResolver);
ohair@286 272 return reader;
ohair@286 273 }
ohair@286 274
ohair@286 275 private String normalizeSystemId(String systemId) {
ohair@286 276 try {
ohair@286 277 systemId = new URI(systemId).normalize().toString();
ohair@286 278 } catch (URISyntaxException e) {
ohair@286 279 // leave the system ID untouched. In my experience URI is often too strict
ohair@286 280 }
ohair@286 281 return systemId;
ohair@286 282 }
ohair@286 283
ohair@286 284 boolean isExtensionMode() {
ohair@286 285 return options.isExtensionMode();
ohair@286 286 }
ohair@286 287
ohair@286 288
ohair@286 289 /**
ohair@286 290 * Gets the DOM tree associated with the specified system ID,
ohair@286 291 * or null if none is found.
ohair@286 292 */
ohair@286 293 public Document get(String systemId) {
ohair@286 294 Document doc = core.get(systemId);
ohair@286 295
ohair@286 296 if (doc == null && systemId.startsWith("file:/") && !systemId.startsWith("file://")) {
ohair@286 297 // As of JDK1.4, java.net.URL.toExternal method returns URLs like
ohair@286 298 // "file:/abc/def/ghi" which is an incorrect file protocol URL according to RFC1738.
ohair@286 299 // Some other correctly functioning parts return the correct URLs ("file:///abc/def/ghi"),
ohair@286 300 // and this descripancy breaks DOM look up by system ID.
ohair@286 301
ohair@286 302 // this extra check solves this problem.
ohair@286 303 doc = core.get("file://" + systemId.substring(5));
ohair@286 304 }
ohair@286 305
ohair@286 306 if (doc == null && systemId.startsWith("file:")) {
ohair@286 307 // on Windows, filenames are case insensitive.
ohair@286 308 // perform case-insensitive search for improved user experience
ohair@286 309 String systemPath = getPath(systemId);
ohair@286 310 for (String key : core.keySet()) {
ohair@286 311 if (key.startsWith("file:") && getPath(key).equalsIgnoreCase(systemPath)) {
ohair@286 312 doc = core.get(key);
ohair@286 313 break;
ohair@286 314 }
ohair@286 315 }
ohair@286 316 }
ohair@286 317
ohair@286 318 return doc;
ohair@286 319 }
ohair@286 320
ohair@286 321 /**
ohair@286 322 * Strips off the leading 'file:///' portion from an URL.
ohair@286 323 */
ohair@286 324 private String getPath(String key) {
ohair@286 325 key = key.substring(5); // skip 'file:'
ohair@286 326 while (key.length() > 0 && key.charAt(0) == '/')
ohair@286 327 key = key.substring(1);
ohair@286 328 return key;
ohair@286 329 }
ohair@286 330
ohair@286 331 /**
ohair@286 332 * Gets all the system IDs of the documents.
ohair@286 333 */
ohair@286 334 public String[] listSystemIDs() {
ohair@286 335 return core.keySet().toArray(new String[core.keySet().size()]);
ohair@286 336 }
ohair@286 337
ohair@286 338 /**
ohair@286 339 * Gets the system ID from which the given DOM is parsed.
ohair@286 340 * <p/>
ohair@286 341 * Poor-man's base URI.
ohair@286 342 */
ohair@286 343 public String getSystemId(Document dom) {
ohair@286 344 for (Map.Entry<String, Document> e : core.entrySet()) {
ohair@286 345 if (e.getValue() == dom)
ohair@286 346 return e.getKey();
ohair@286 347 }
ohair@286 348 return null;
ohair@286 349 }
ohair@286 350
ohair@286 351 /**
ohair@286 352 * Gets the first one (which is more or less random) in {@link #rootDocuments}.
ohair@286 353 */
ohair@286 354 public String getFirstRootDocument() {
ohair@286 355 if(rootDocuments.isEmpty()) return null;
ohair@286 356 return rootDocuments.iterator().next();
ohair@286 357 }
ohair@286 358
ohair@286 359 public Set<String> getRootDocuments() {
ohair@286 360 return rootDocuments;
ohair@286 361 }
ohair@286 362
ohair@286 363 /**
ohair@286 364 * Dumps the contents of the forest to the specified stream.
ohair@286 365 * <p/>
ohair@286 366 * This is a debug method. As such, error handling is sloppy.
ohair@286 367 */
ohair@286 368 public void dump(OutputStream out) throws IOException {
ohair@286 369 try {
ohair@286 370 // create identity transformer
ohair@286 371 Transformer it = TransformerFactory.newInstance().newTransformer();
ohair@286 372
ohair@286 373 for (Map.Entry<String, Document> e : core.entrySet()) {
ohair@286 374 out.write(("---<< " + e.getKey() + '\n').getBytes());
ohair@286 375
ohair@286 376 DataWriter dw = new DataWriter(new OutputStreamWriter(out), null);
ohair@286 377 dw.setIndentStep(" ");
ohair@286 378 it.transform(new DOMSource(e.getValue()),
ohair@286 379 new SAXResult(dw));
ohair@286 380
ohair@286 381 out.write("\n\n\n".getBytes());
ohair@286 382 }
ohair@286 383 } catch (TransformerException e) {
ohair@286 384 e.printStackTrace();
ohair@286 385 }
ohair@286 386 }
ohair@286 387
ohair@286 388 }

mercurial