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

Thu, 31 Aug 2017 15:18:52 +0800

author
aoqi
date
Thu, 31 Aug 2017 15:18:52 +0800
changeset 637
9c07ef4934dd
parent 408
b0610cd08440
parent 0
373ffda63c9a
child 1435
a90b319bae7a
permissions
-rw-r--r--

merge

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

mercurial