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

Fri, 04 Oct 2013 16:21:34 +0100

author
mkos
date
Fri, 04 Oct 2013 16:21:34 +0100
changeset 408
b0610cd08440
parent 368
0989ad8c0860
child 637
9c07ef4934dd
child 1386
65d3b0e44551
permissions
-rw-r--r--

8025054: Update JAX-WS RI integration to 2.2.9-b130926.1035
Reviewed-by: chegar

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

mercurial