summaryrefslogtreecommitdiff
blob: 03fbe6d121d9d3020ee03f2a24c69f89a5f31dcf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import org.xml.sax.SAXException;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.ParserConfigurationException;

import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;

import java.io.File;
import java.io.IOException;

import java.util.ArrayList;
import java.util.Collection;

public class XMLManager
{
   /* ... */
   /* private static final DocumentBuilderFactoryCreatorMakerInstanciator; */
   /* private static final DocumentBuilderFactoryCreatorMaker; */
   /* private static final DocumentBuilderFactoryCreator; */
   private static final DocumentBuilderFactory DOC_BUILDER_FACTORY;
   private static final DocumentBuilder DOC_BUILDER;

   private static final XPathFactory XPATH_FACTORY;
   private static final XPath XPATH;

   static
   {
      DocumentBuilder i_dont_even;

      DOC_BUILDER_FACTORY = DocumentBuilderFactory.newInstance();

      try
      {
         i_dont_even = DOC_BUILDER_FACTORY.newDocumentBuilder();
      }
      catch (final Exception e)
      {
         i_dont_even = null;

         System.err.println
         (
            "[E] Err... You somehow managed to trigger an exception from purely"
            + " static members:"
         );

         e.printStackTrace();

         System.exit(-1);
      }

      DOC_BUILDER = i_dont_even;

      XPATH_FACTORY = XPathFactory.newInstance();
      XPATH = XPATH_FACTORY.newXPath();
   }

   private XMLManager () {} /* Utility Class. */

   public static Document get_document (final String filename)
   throws
      SAXException,
      IOException
   {
      final File file;

      file = new File(filename, "r");

      return DOC_BUILDER.parse(file);
   }

   public static XPathExpression compile (final String expression)
   throws XPathExpressionException
   {
      return XPATH.compile(expression);
   }

   public static Collection<Node> node_list_to_node_collection
   (
      final NodeList nl
   )
   {
      final Collection<Node> result;
      final int nl_length;

      result = new ArrayList<Node>();

      nl_length = nl.getLength();

      for (int i = 0; i < nl_length; ++i)
      {
         result.add(nl.item(i));
      }

      return result;
   }
}