1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.ws.commons.schema.tools;
21
22 import java.io.IOException;
23 import java.util.ArrayList;
24 import java.util.Iterator;
25 import java.util.List;
26
27 import javax.xml.namespace.NamespaceContext;
28 import javax.xml.parsers.DocumentBuilder;
29 import javax.xml.parsers.DocumentBuilderFactory;
30 import javax.xml.parsers.ParserConfigurationException;
31 import javax.xml.xpath.XPath;
32 import javax.xml.xpath.XPathConstants;
33 import javax.xml.xpath.XPathExpressionException;
34 import javax.xml.xpath.XPathFactory;
35
36 import org.w3c.dom.Document;
37 import org.w3c.dom.Element;
38 import org.w3c.dom.NodeList;
39
40 import org.xml.sax.SAXException;
41
42 import org.apache.ws.commons.schema.XmlSchemaCollection;
43
44
45
46
47 public class ReadSchemaFromURL {
48
49
50 private final static String SCHEMA_URI = "http://www.w3.org/2001/XMLSchema";
51
52
53
54
55
56
57
58
59
60 public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException, XPathExpressionException {
61 if (args.length != 1 && args.length != 2) {
62 System.err.println("Usage: ReadSchemaFromURL URL -wsdl");
63 return;
64 }
65 boolean wsdl = false;
66 if (args.length == 2) {
67 wsdl = true;
68 }
69 XmlSchemaCollection collection = new XmlSchemaCollection();
70 DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
71 documentBuilderFactory.setNamespaceAware(true);
72 DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
73 Document document = documentBuilder.parse(args[0]);
74 Element schema = document.getDocumentElement();
75 if (wsdl) {
76 NodeList schemas;
77 XPath xpath = XPathFactory.newInstance().newXPath();
78 xpath.setNamespaceContext(new NamespaceContext() {
79
80 public String getNamespaceURI(String prefix) {
81 if ("xsd".equals(prefix)) {
82 return SCHEMA_URI;
83 }
84 return null;
85 }
86
87 public String getPrefix(String namespaceURI) {
88 if (SCHEMA_URI.equals(namespaceURI)) {
89 return "xsd";
90 }
91 return null;
92 }
93
94 public Iterator getPrefixes(String namespaceURI) {
95 List prefixes = new ArrayList();
96 prefixes.add("xsd");
97 return prefixes.iterator();
98 }});
99 schemas = (NodeList)xpath.evaluate("//xsd:schema", document, XPathConstants.NODESET);
100 for (int x = 0; x < schemas.getLength(); x ++) {
101 schema = (Element)schemas.item(x);
102 collection.read(schema, args[0]);
103 }
104 } else {
105 collection.read(document, args[0], null);
106 }
107 System.out.println("Success.");
108 }
109
110 }