View Javadoc

1   /*
2    * Copyright 2005-2008 The Kuali Foundation
3    * 
4    * 
5    * Licensed under the Educational Community License, Version 2.0 (the "License");
6    * you may not use this file except in compliance with the License.
7    * You may obtain a copy of the License at
8    * 
9    * http://www.opensource.org/licenses/ecl2.php
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.kuali.rice.kew.transformation;
18  
19  //Raja Sooriamurthi
20  //S531 Web application development
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  	// an XPath constructor
46  	
47  	static XPath makeXPath() {
48  		XPathFactory xpfactory = XPathFactory.newInstance();
49  		return xpfactory.newXPath();
50  	}
51  
52  	// Three methods for parsing XML documents based on how they are specified
53  	//   ... as a file
54  	//   ... as a URL
55  	//   ... as the contents of an InputStream
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 }