View Javadoc

1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements. See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership. The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License. You may obtain a copy of the License at
9    *
10   * http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied. See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
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   * Utility class for testing schema reading.
46   */
47  public class ReadSchemaFromURL {
48      
49      // in 1.4 there is no constant for this in the JRE.
50      private final static String SCHEMA_URI = "http://www.w3.org/2001/XMLSchema";
51  
52      /**
53       * Read a schema from a URL, perhaps provoking errors.
54       * @param args
55       * @throws ParserConfigurationException 
56       * @throws IOException 
57       * @throws SAXException 
58       * @throws XPathExpressionException 
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 }