1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.kew.transformation;
17
18
19
20
21 import java.io.FileInputStream;
22 import java.io.FileNotFoundException;
23 import java.io.IOException;
24 import java.io.InputStream;
25 import java.net.URL;
26
27 import javax.xml.parsers.DocumentBuilder;
28 import javax.xml.parsers.DocumentBuilderFactory;
29 import javax.xml.parsers.ParserConfigurationException;
30 import javax.xml.xpath.XPath;
31 import javax.xml.xpath.XPathFactory;
32
33 import org.w3c.dom.Document;
34 import org.xml.sax.SAXException;
35
36 public final class XMLUtil {
37
38
39
40 private XMLUtil() {
41 throw new UnsupportedOperationException("do not call");
42 }
43
44
45
46 static XPath makeXPath() {
47 XPathFactory xpfactory = XPathFactory.newInstance();
48 return xpfactory.newXPath();
49 }
50
51
52
53
54
55
56 static Document parseFile(String fname) {
57 InputStream in = null;
58 try {
59 in = new FileInputStream(fname);
60 } catch (FileNotFoundException e) {
61 e.printStackTrace();
62 System.exit(1);
63 }
64 return parseInputStream(in);
65 }
66
67 static Document parseURL(String url) {
68 InputStream in = null;
69 try {
70 in = new URL(url).openStream();
71 } catch (IOException e) {
72 e.printStackTrace();
73 System.exit(1);
74 }
75 return parseInputStream(in);
76 }
77
78 static Document parseInputStream(InputStream in) {
79 Document doc = null;
80 try {
81 DocumentBuilder xml_parser = makeDOMparser();
82 doc = xml_parser.parse(in);
83 } catch (IOException e) {
84 e.printStackTrace();
85 System.exit(1);
86 } catch (SAXException e) {
87 e.printStackTrace();
88 System.exit(1);
89 }
90 return doc;
91 }
92
93 static DocumentBuilder makeDOMparser() {
94 DocumentBuilder parser = null;
95 try {
96 DocumentBuilderFactory dbfactory = DocumentBuilderFactory.newInstance();
97 dbfactory.setIgnoringElementContentWhitespace(true);
98 parser = dbfactory.newDocumentBuilder();
99 } catch (ParserConfigurationException pce) {
100 pce.printStackTrace();
101 System.exit(1);
102 }
103 return parser;
104 }
105
106 }