Coverage details for umich.cac.util.XmlUtil

LineHitsSource
1 package umich.cac.util;
2  
3 import java.io.File;
4 import java.io.IOException;
5 import java.io.InputStream;
6 import java.util.logging.Level;
7 import java.util.logging.Logger;
8  
9 import javax.xml.parsers.DocumentBuilderFactory;
10 import javax.xml.parsers.ParserConfigurationException;
11 import javax.xml.transform.Result;
12 import javax.xml.transform.Source;
13 import javax.xml.transform.Transformer;
14 import javax.xml.transform.TransformerFactory;
15 import javax.xml.transform.dom.DOMResult;
16 import javax.xml.transform.dom.DOMSource;
17 import javax.xml.transform.stream.StreamResult;
18 import javax.xml.transform.stream.StreamSource;
19  
20 import org.w3c.dom.Document;
21 import org.xml.sax.SAXException;
22  
23 /**
24  * XML utilities
25  *
26  * @author rmach@umich.edu
27  * @version $Header: /cvsroot/pbsaccounting/pbsaccounting/src/umich/cac/util/XmlUtil.java,v 1.5 2003/10/21 19:08:00 rodmach Exp $
28  */
29  
300public class XmlUtil {
31  
32     /** Logger */
33  
340    public static Logger logger = Logger.getLogger(XmlUtil.class.getName());
35  
36     /**
37      * This method writes a DOM document to a file
38      *
39      * @param doc
40      * DOM to write to file
41      * @param filename
42      * Name of the file to write
43      */
44  
45     public static void writeXmlFile(Document doc, String filename) {
46  
47         try {
48  
49             // Prepare the DOM document for writing
500            Source source = new DOMSource(doc);
51  
52             // Prepare the output file
530            File file = new File(filename);
540            Result result = new StreamResult(file);
55  
56             // Write the DOM document to the file
570            Transformer xformer =
58                 TransformerFactory.newInstance().newTransformer();
59  
600            xformer.transform(source, result);
61  
620        } catch (Exception e) {
630            logger.log(
64                 Level.SEVERE,
65                 "Unable to write XML to file " + filename,
66                 e);
670        }
68  
690    }
70  
71     /**
72      * Create a output file from a XSL transform given DOM source and XSLfile
73      *
74      * @param domSource
75      * DOM object to write out
76      * @param xslFileName
77      * name of XSL file to use
78      * @param outputFileName
79      * name of output file
80      */
81  
82     public static void createOutputFile(
83         Document domSource,
84         String xslFileName,
85         String outputFileName) {
86  
870        Document doc = createOutputDOM(domSource, xslFileName);
880        writeXmlFile(doc, outputFileName);
890    }
90  
91     /**
92      * Create an output DOM from an XSL transform given DOM source and XSLfile.
93      *
94      * @param domSource
95      * DOM object to write out
96      * @param xslFileName
97      * name of XSL file to use
98      * @return DOM output.
99      */
100     public static Document createOutputDOM(
101         Document domSource,
102         String xslFileName) {
1030        Document domResult = null;
104  
105         try {
1060            DocumentBuilderFactory factory =
107                 DocumentBuilderFactory.newInstance();
1080            domResult = factory.newDocumentBuilder().newDocument();
109  
1100            DOMSource xmlSource = new DOMSource(domSource);
111  
1120            InputStream inputStream =
113                 XmlUtil.class.getResourceAsStream("/" + xslFileName);
1140            Source xsltSource = new StreamSource(inputStream);
115  
116             // the factory pattern supports different XSLT processors
1170            TransformerFactory transFact = TransformerFactory.newInstance();
118  
1190            Transformer trans = transFact.newTransformer(xsltSource);
120  
1210            trans.transform(xmlSource, new DOMResult(domResult));
122  
1230        } catch (Exception e) {
1240            logger.log(Level.SEVERE, "Exception creating joblog file", e);
1250            domResult = null;
1260        }
127  
1280        return domResult;
129     }
130  
131     /**
132      * Create a output file from a XSL transform given DOM source and XSLfile
133      * Used for joblog and other type transforms that don't result in DOM
134      * result after transform
135      *
136      * @param domSource
137      * DOM object to write out
138      * @param xslFileName
139      * name of XSL file to use
140      * @param outputFileName
141      * name of output file
142      */
143  
144     public static void createStreamOutputFile(
145         Document domSource,
146         String xslFileName,
147         String outputFileName) {
148  
149         try {
150  
1510            DOMSource xmlSource = new DOMSource(domSource);
152  
1530            InputStream inputStream =
154                 XmlUtil.class.getResourceAsStream("/" + xslFileName);
1550            Source xsltSource = new StreamSource(inputStream);
156  
157             // the factory pattern supports different XSLT processors
1580            TransformerFactory transFact = TransformerFactory.newInstance();
159  
1600            Transformer trans = transFact.newTransformer(xsltSource);
161  
1620            trans.transform(xmlSource, new StreamResult(outputFileName));
163  
1640        } catch (Exception e) {
1650            logger.log(Level.SEVERE, "Exception creating joblog file", e);
1660        }
167  
1680    }
169  
170     /**
171      * Parses an XML file and returns a DOM document.
172      *
173      * @param filename
174      * Filename containing XML to parse
175      * @param validating
176      * true, validate contents against the DTD specified in filename
177      * @return Document representation of XML, or null if couldn't parse
178      */
179  
180     public static Document parseXmlFile(String filename, boolean validating) {
181  
182         try {
183  
184             // Create a builder factory
1850            DocumentBuilderFactory factory =
186                 DocumentBuilderFactory.newInstance();
187  
188             //Validate the XML if requested
1890            factory.setValidating(validating);
190  
191             // Create the builder and parse the file
1920            Document doc =
193                 factory.newDocumentBuilder().parse(new File(filename));
194  
1950            return doc;
196  
1970        } catch (SAXException e) {
198             // A parsing error occurred; the xml input is not valid
1990            logger.log(
200                 Level.SEVERE,
201                 "Exception parsing XML from " + filename,
202                 e);
2030        } catch (ParserConfigurationException e) {
2040            logger.log(
205                 Level.SEVERE,
206                 "Exception in parser configuration while reading" + filename,
207                 e);
2080        } catch (IOException e) {
2090            logger.log(Level.SEVERE, "Exception reading file" + filename, e);
2100        }
211  
2120        return null;
213  
214     }
215  
216 }

this report was generated by version 1.0.5 of jcoverage.
visit www.jcoverage.com for updates.

copyright © 2003, jcoverage ltd. all rights reserved.
Java is a trademark of Sun Microsystems, Inc. in the United States and other countries.