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