View Javadoc

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