001: /*
002: * Copyright 2005-2007 The Kuali Foundation.
003: *
004: *
005: * Licensed under the Educational Community License, Version 1.0 (the "License");
006: * you may not use this file except in compliance with the License.
007: * You may obtain a copy of the License at
008: *
009: * http://www.opensource.org/licenses/ecl1.php
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package edu.iu.uis.eden.transformation;
018:
019: //Raja Sooriamurthi
020: //S531 Web application development
021:
022: import java.io.FileInputStream;
023: import java.io.FileNotFoundException;
024: import java.io.IOException;
025: import java.io.InputStream;
026: import java.net.URL;
027:
028: import javax.xml.parsers.DocumentBuilder;
029: import javax.xml.parsers.DocumentBuilderFactory;
030: import javax.xml.parsers.ParserConfigurationException;
031: import javax.xml.xpath.XPath;
032: import javax.xml.xpath.XPathFactory;
033:
034: import org.w3c.dom.Document;
035: import org.xml.sax.SAXException;
036:
037: public class XMLUtil {
038:
039: // an XPath constructor
040:
041: static XPath makeXPath() {
042: XPathFactory xpfactory = XPathFactory.newInstance();
043: return xpfactory.newXPath();
044: }
045:
046: // Three methods for parsing XML documents based on how they are specified
047: // ... as a file
048: // ... as a URL
049: // ... as the contents of an InputStream
050:
051: static Document parseFile(String fname) {
052: InputStream in = null;
053: try {
054: in = new FileInputStream(fname);
055: } catch (FileNotFoundException e) {
056: e.printStackTrace();
057: System.exit(1);
058: }
059: return parseInputStream(in);
060: }
061:
062: static Document parseURL(String url) {
063: InputStream in = null;
064: try {
065: in = new URL(url).openStream();
066: } catch (IOException e) {
067: e.printStackTrace();
068: System.exit(1);
069: }
070: return parseInputStream(in);
071: }
072:
073: static Document parseInputStream(InputStream in) {
074: Document doc = null;
075: try {
076: DocumentBuilder xml_parser = makeDOMparser();
077: doc = xml_parser.parse(in);
078: } catch (IOException e) {
079: e.printStackTrace();
080: System.exit(1);
081: } catch (SAXException e) {
082: e.printStackTrace();
083: System.exit(1);
084: }
085: return doc;
086: }
087:
088: static DocumentBuilder makeDOMparser() {
089: DocumentBuilder parser = null;
090: try {
091: DocumentBuilderFactory dbfactory = DocumentBuilderFactory
092: .newInstance();
093: dbfactory.setIgnoringElementContentWhitespace(true);
094: parser = dbfactory.newDocumentBuilder();
095: } catch (ParserConfigurationException pce) {
096: pce.printStackTrace();
097: System.exit(1);
098: }
099: return parser;
100: }
101: }
|