View Javadoc
1   package org.opentrafficsim.demo;
2   
3   import java.io.File;
4   import java.io.FileInputStream;
5   import java.io.IOException;
6   import java.io.InputStream;
7   
8   import javax.xml.parsers.DocumentBuilder;
9   import javax.xml.parsers.DocumentBuilderFactory;
10  import javax.xml.xpath.XPath;
11  import javax.xml.xpath.XPathConstants;
12  import javax.xml.xpath.XPathFactory;
13  
14  import org.djutils.io.ResourceResolver;
15  import org.w3c.dom.Document;
16  import org.w3c.dom.NamedNodeMap;
17  import org.w3c.dom.Node;
18  import org.w3c.dom.NodeList;
19  import org.xml.sax.EntityResolver;
20  import org.xml.sax.InputSource;
21  
22  /**
23   * ParseXml.java.
24   * <p>
25   * Copyright (c) 2022-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
26   * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
27   * </p>
28   * @author <a href="https://github.com/averbraeck">Alexander Verbraeck</a>
29   * @author <a href="https://github.com/peter-knoppers">Peter Knoppers</a>
30   * @author <a href="https://github.com/wjschakel">Wouter Schakel</a>
31   */
32  public class ParseXml
33  {
34      /**
35       * Make a DOM tree and do some xpath.
36       * @throws Exception exception
37       */
38      public ParseXml() throws Exception
39      {
40          domTree(ResourceResolver.resolve("/resources/conflict/Simple.xml").asUrl().getPath());
41      }
42  
43      /**
44       * Print dom tree.
45       * @param path path
46       * @throws Exception exception
47       */
48      private void domTree(final String path) throws Exception
49      {
50          File file = new File(path);
51          FileInputStream fileIS = new FileInputStream(file);
52          DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
53          builderFactory.setXIncludeAware(true);
54          builderFactory.setValidating(false); // no DTD validation
55          // builderFactory.setFeature("http://apache.org/xml/features/validation/schema", true);
56          // builderFactory.setFeature("http://apache.org/xml/features/validation/schema-full-checking", true);
57          // builderFactory.setFeature("http://apache.org/xml/features/validation/id-idref-checking", true);
58          // builderFactory.setFeature("http://apache.org/xml/features/validation/identity-constraint-checking", true);
59          // builderFactory.setFeature("http://apache.org/xml/features/xinclude", true);
60          builderFactory.setNamespaceAware(true);
61          DocumentBuilder builder = builderFactory.newDocumentBuilder();
62          builder.setEntityResolver(new DefaultsResolver());
63          Document xmlDocument = builder.parse(fileIS);
64          print(xmlDocument, "*");
65          print(xmlDocument, "*/ots:Ots");
66          print(xmlDocument, ".//ots:GTUTYPE");
67      }
68  
69      /**
70       * Print document.
71       * @param xmlDocument document
72       * @param expression expression
73       * @throws Exception exception
74       */
75      private void print(final Document xmlDocument, final String expression) throws Exception
76      {
77          XPath xPath = XPathFactory.newInstance().newXPath();
78          NodeList nodeList = (NodeList) xPath.compile(expression).evaluate(xmlDocument, XPathConstants.NODESET);
79          System.out.print(expression + " = [ ");
80          boolean first = true;
81          for (int i = 0; i < nodeList.getLength(); i++)
82          {
83              if (first)
84              {
85                  first = false;
86              }
87              else
88              {
89                  System.out.print(", ");
90              }
91              Node n = nodeList.item(i);
92              System.out.print("{" + n.getNodeName());
93              NamedNodeMap attMap = n.getAttributes();
94              if (attMap != null)
95              {
96                  boolean f2 = true;
97                  for (int j = 0; j < attMap.getLength(); j++)
98                  {
99                      if (f2)
100                     {
101                         f2 = false;
102                     }
103                     else
104                     {
105                         System.out.print(":");
106                     }
107                     System.out.print(" " + attMap.item(j).getNodeName() + "=" + attMap.item(j).getNodeValue());
108                 }
109             }
110             System.out.print("}");
111         }
112         System.out.println(" ]");
113     }
114 
115     /**
116      * Main method.
117      * @param args args
118      * @throws Exception exception
119      */
120     public static void main(final String[] args) throws Exception
121     {
122         new ParseXml();
123     }
124 
125     /**
126      * Defaults resolver.
127      */
128     static class DefaultsResolver implements EntityResolver
129     {
130         /**
131          * Constructor.
132          */
133         DefaultsResolver()
134         {
135             //
136         }
137 
138         @Override
139         public InputSource resolveEntity(final String publicId, final String systemId) throws IOException
140         {
141             if (systemId.contains("defaults/"))
142             {
143                 System.out.println("\nINCLUDING " + systemId);
144                 String location = "/resources/xsd/defaults" + systemId.substring(systemId.lastIndexOf('/'));
145                 InputStream stream = ResourceResolver.resolve(location).openStream();
146                 return new InputSource(stream);
147             }
148             else
149             {
150                 return new InputSource(ResourceResolver.resolve(systemId).openStream());
151             }
152         }
153     }
154 
155 }