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 class XMLUtil {
38
39
40
41 static XPath makeXPath() {
42 XPathFactory xpfactory = XPathFactory.newInstance();
43 return xpfactory.newXPath();
44 }
45
46
47
48
49
50
51 static Document parseFile(String fname) {
52 InputStream in = null;
53 try {
54 in = new FileInputStream(fname);
55 } catch (FileNotFoundException e) {
56 e.printStackTrace();
57 System.exit(1);
58 }
59 return parseInputStream(in);
60 }
61
62 static Document parseURL(String url) {
63 InputStream in = null;
64 try {
65 in = new URL(url).openStream();
66 } catch (IOException e) {
67 e.printStackTrace();
68 System.exit(1);
69 }
70 return parseInputStream(in);
71 }
72
73 static Document parseInputStream(InputStream in) {
74 Document doc = null;
75 try {
76 DocumentBuilder xml_parser = makeDOMparser();
77 doc = xml_parser.parse(in);
78 } catch (IOException e) {
79 e.printStackTrace();
80 System.exit(1);
81 } catch (SAXException e) {
82 e.printStackTrace();
83 System.exit(1);
84 }
85 return doc;
86 }
87
88 static DocumentBuilder makeDOMparser() {
89 DocumentBuilder parser = null;
90 try {
91 DocumentBuilderFactory dbfactory = DocumentBuilderFactory.newInstance();
92 dbfactory.setIgnoringElementContentWhitespace(true);
93 parser = dbfactory.newDocumentBuilder();
94 } catch (ParserConfigurationException pce) {
95 pce.printStackTrace();
96 System.exit(1);
97 }
98 return parser;
99 }
100 }