1 /*
2 * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package com.sun.tools.internal.ws.wsdl.parser;
27
28 import com.sun.istack.internal.NotNull;
29 import com.sun.tools.internal.ws.resources.WscompileMessages;
30 import com.sun.tools.internal.ws.wscompile.AbortException;
31 import com.sun.tools.internal.ws.wscompile.DefaultAuthenticator;
32 import com.sun.tools.internal.ws.wscompile.ErrorReceiver;
33 import com.sun.tools.internal.ws.wscompile.WsimportOptions;
34 import com.sun.tools.internal.ws.wsdl.document.schema.SchemaConstants;
35 import com.sun.tools.internal.xjc.reader.internalizer.LocatorTable;
36 import com.sun.xml.internal.bind.marshaller.DataWriter;
37 import com.sun.xml.internal.ws.util.JAXWSUtils;
38 import org.w3c.dom.Document;
39 import org.w3c.dom.Element;
40 import org.w3c.dom.NodeList;
41 import org.xml.sax.ContentHandler;
42 import org.xml.sax.*;
43 import org.xml.sax.helpers.XMLFilterImpl;
44
45 import javax.xml.parsers.DocumentBuilder;
46 import javax.xml.parsers.DocumentBuilderFactory;
47 import javax.xml.parsers.ParserConfigurationException;
48 import javax.xml.parsers.SAXParserFactory;
49 import javax.xml.transform.Transformer;
50 import javax.xml.transform.TransformerException;
51 import javax.xml.transform.TransformerFactory;
52 import javax.xml.transform.dom.DOMSource;
53 import javax.xml.transform.sax.SAXResult;
54 import javax.net.ssl.HttpsURLConnection;
55 import javax.net.ssl.HostnameVerifier;
56 import javax.net.ssl.SSLSession;
57 import java.io.IOException;
58 import java.io.InputStream;
59 import java.io.OutputStream;
60 import java.io.OutputStreamWriter;
61 import java.net.*;
62 import java.util.*;
63
64 /**
65 * @author Vivek Pandey
66 */
67 public class DOMForest {
68 /**
69 * To correctly feed documents to a schema parser, we need to remember
70 * which documents (of the forest) were given as the root
71 * documents, and which of them are read as included/imported
72 * documents.
73 * <p/>
74 * <p/>
75 * Set of system ids as strings.
76 */
101 */
102 public final LocatorTable locatorTable = new LocatorTable();
103
104 protected final EntityResolver entityResolver;
105 /**
106 * Stores all the outer-most <jaxb:bindings> customizations.
107 */
108 public final Set<Element> outerMostBindings = new HashSet<Element>();
109
110 /**
111 * Schema language dependent part of the processing.
112 */
113 protected final InternalizationLogic logic;
114 protected final WsimportOptions options;
115
116 public DOMForest(InternalizationLogic logic, @NotNull EntityResolver entityResolver, WsimportOptions options, ErrorReceiver errReceiver) {
117 this.options = options;
118 this.entityResolver = entityResolver;
119 this.errorReceiver = errReceiver;
120 this.logic = logic;
121 try {
122 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
123 dbf.setNamespaceAware(true);
124 this.documentBuilder = dbf.newDocumentBuilder();
125
126 this.parserFactory = SAXParserFactory.newInstance();
127 this.parserFactory.setNamespaceAware(true);
128 } catch (ParserConfigurationException e) {
129 throw new AssertionError(e);
130 }
131 }
132
133 public List<Element> getInlinedSchemaElement() {
134 return inlinedSchemaElements;
135 }
136
137 public @NotNull Document parse(InputSource source, boolean root) throws SAXException, IOException {
138 if (source.getSystemId() == null)
139 throw new IllegalArgumentException();
140 return parse(source.getSystemId(), source, root);
141 }
142
143 /**
144 * Parses an XML at the given location (
145 * and XMLs referenced by it) into DOM trees
146 * and stores them to this forest.
147 *
351 /**
352 * Gets the first one (which is more or less random) in {@link #rootDocuments}.
353 */
354 public String getFirstRootDocument() {
355 if(rootDocuments.isEmpty()) return null;
356 return rootDocuments.iterator().next();
357 }
358
359 public Set<String> getRootDocuments() {
360 return rootDocuments;
361 }
362
363 /**
364 * Dumps the contents of the forest to the specified stream.
365 * <p/>
366 * This is a debug method. As such, error handling is sloppy.
367 */
368 public void dump(OutputStream out) throws IOException {
369 try {
370 // create identity transformer
371 Transformer it = TransformerFactory.newInstance().newTransformer();
372
373 for (Map.Entry<String, Document> e : core.entrySet()) {
374 out.write(("---<< " + e.getKey() + '\n').getBytes());
375
376 DataWriter dw = new DataWriter(new OutputStreamWriter(out), null);
377 dw.setIndentStep(" ");
378 it.transform(new DOMSource(e.getValue()),
379 new SAXResult(dw));
380
381 out.write("\n\n\n".getBytes());
382 }
383 } catch (TransformerException e) {
384 e.printStackTrace();
385 }
386 }
387
388 }
|
1 /*
2 * Copyright (c) 1997, 2017, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package com.sun.tools.internal.ws.wsdl.parser;
27
28 import com.sun.istack.internal.NotNull;
29 import com.sun.tools.internal.ws.util.xml.XmlUtil;
30 import com.sun.tools.internal.ws.wscompile.ErrorReceiver;
31 import com.sun.tools.internal.ws.wscompile.WsimportOptions;
32 import com.sun.tools.internal.ws.wsdl.document.schema.SchemaConstants;
33 import com.sun.tools.internal.xjc.reader.internalizer.LocatorTable;
34 import com.sun.xml.internal.bind.marshaller.DataWriter;
35 import org.w3c.dom.Document;
36 import org.w3c.dom.Element;
37 import org.w3c.dom.NodeList;
38 import org.xml.sax.ContentHandler;
39 import org.xml.sax.*;
40 import org.xml.sax.helpers.XMLFilterImpl;
41
42 import javax.xml.parsers.DocumentBuilder;
43 import javax.xml.parsers.DocumentBuilderFactory;
44 import javax.xml.parsers.ParserConfigurationException;
45 import javax.xml.parsers.SAXParserFactory;
46 import javax.xml.transform.Transformer;
47 import javax.xml.transform.TransformerException;
48 import javax.xml.transform.TransformerFactory;
49 import javax.xml.transform.dom.DOMSource;
50 import javax.xml.transform.sax.SAXResult;
51 import java.io.IOException;
52 import java.io.InputStream;
53 import java.io.OutputStream;
54 import java.io.OutputStreamWriter;
55 import java.net.*;
56 import java.util.*;
57
58 /**
59 * @author Vivek Pandey
60 */
61 public class DOMForest {
62 /**
63 * To correctly feed documents to a schema parser, we need to remember
64 * which documents (of the forest) were given as the root
65 * documents, and which of them are read as included/imported
66 * documents.
67 * <p/>
68 * <p/>
69 * Set of system ids as strings.
70 */
95 */
96 public final LocatorTable locatorTable = new LocatorTable();
97
98 protected final EntityResolver entityResolver;
99 /**
100 * Stores all the outer-most <jaxb:bindings> customizations.
101 */
102 public final Set<Element> outerMostBindings = new HashSet<Element>();
103
104 /**
105 * Schema language dependent part of the processing.
106 */
107 protected final InternalizationLogic logic;
108 protected final WsimportOptions options;
109
110 public DOMForest(InternalizationLogic logic, @NotNull EntityResolver entityResolver, WsimportOptions options, ErrorReceiver errReceiver) {
111 this.options = options;
112 this.entityResolver = entityResolver;
113 this.errorReceiver = errReceiver;
114 this.logic = logic;
115 // secure xml processing can be switched off if input requires it
116 boolean disableXmlSecurity = options == null ? false : options.disableXmlSecurity;
117
118 DocumentBuilderFactory dbf = XmlUtil.newDocumentBuilderFactory(disableXmlSecurity);
119 this.parserFactory = XmlUtil.newSAXParserFactory(disableXmlSecurity);
120 try {
121 this.documentBuilder = dbf.newDocumentBuilder();
122 } catch (ParserConfigurationException e) {
123 throw new AssertionError(e);
124 }
125 }
126
127 public List<Element> getInlinedSchemaElement() {
128 return inlinedSchemaElements;
129 }
130
131 public @NotNull Document parse(InputSource source, boolean root) throws SAXException, IOException {
132 if (source.getSystemId() == null)
133 throw new IllegalArgumentException();
134 return parse(source.getSystemId(), source, root);
135 }
136
137 /**
138 * Parses an XML at the given location (
139 * and XMLs referenced by it) into DOM trees
140 * and stores them to this forest.
141 *
345 /**
346 * Gets the first one (which is more or less random) in {@link #rootDocuments}.
347 */
348 public String getFirstRootDocument() {
349 if(rootDocuments.isEmpty()) return null;
350 return rootDocuments.iterator().next();
351 }
352
353 public Set<String> getRootDocuments() {
354 return rootDocuments;
355 }
356
357 /**
358 * Dumps the contents of the forest to the specified stream.
359 * <p/>
360 * This is a debug method. As such, error handling is sloppy.
361 */
362 public void dump(OutputStream out) throws IOException {
363 try {
364 // create identity transformer
365 // secure xml processing can be switched off if input requires it
366 boolean secureProcessingEnabled = options == null || !options.disableXmlSecurity;
367 TransformerFactory tf = XmlUtil.newTransformerFactory(secureProcessingEnabled);
368 Transformer it = tf.newTransformer();
369
370 for (Map.Entry<String, Document> e : core.entrySet()) {
371 out.write(("---<< " + e.getKey() + '\n').getBytes());
372
373 DataWriter dw = new DataWriter(new OutputStreamWriter(out), null);
374 dw.setIndentStep(" ");
375 it.transform(new DOMSource(e.getValue()),
376 new SAXResult(dw));
377
378 out.write("\n\n\n".getBytes());
379 }
380 } catch (TransformerException e) {
381 e.printStackTrace();
382 }
383 }
384
385 }
|