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