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 class XMLUtil {
38  
39  	// an XPath constructor
40  	
41  	static XPath makeXPath() {
42  		XPathFactory xpfactory = XPathFactory.newInstance();
43  		return xpfactory.newXPath();
44  	}
45  
46  	// Three methods for parsing XML documents based on how they are specified
47  	//   ... as a file
48  	//   ... as a URL
49  	//   ... as the contents of an InputStream
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 }