| Line | Hits | Source |
|---|---|---|
| 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 | ||
| 30 | 0 | public class XmlUtil { |
| 31 | ||
| 32 | /** Logger */ | |
| 33 | ||
| 34 | 0 | 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 | |
| 50 | 0 | Source source = new DOMSource(doc); |
| 51 | ||
| 52 | // Prepare the output file | |
| 53 | 0 | File file = new File(filename); |
| 54 | 0 | Result result = new StreamResult(file); |
| 55 | ||
| 56 | // Write the DOM document to the file | |
| 57 | 0 | Transformer xformer = |
| 58 | TransformerFactory.newInstance().newTransformer(); | |
| 59 | ||
| 60 | 0 | xformer.transform(source, result); |
| 61 | ||
| 62 | 0 | } catch (Exception e) { |
| 63 | 0 | logger.log( |
| 64 | Level.SEVERE, | |
| 65 | "Unable to write XML to file " + filename, | |
| 66 | e); | |
| 67 | 0 | } |
| 68 | ||
| 69 | 0 | } |
| 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 | ||
| 87 | 0 | Document doc = createOutputDOM(domSource, xslFileName); |
| 88 | 0 | writeXmlFile(doc, outputFileName); |
| 89 | 0 | } |
| 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) { | |
| 103 | 0 | Document domResult = null; |
| 104 | ||
| 105 | try { | |
| 106 | 0 | DocumentBuilderFactory factory = |
| 107 | DocumentBuilderFactory.newInstance(); | |
| 108 | 0 | domResult = factory.newDocumentBuilder().newDocument(); |
| 109 | ||
| 110 | 0 | DOMSource xmlSource = new DOMSource(domSource); |
| 111 | ||
| 112 | 0 | InputStream inputStream = |
| 113 | XmlUtil.class.getResourceAsStream("/" + xslFileName); | |
| 114 | 0 | Source xsltSource = new StreamSource(inputStream); |
| 115 | ||
| 116 | // the factory pattern supports different XSLT processors | |
| 117 | 0 | TransformerFactory transFact = TransformerFactory.newInstance(); |
| 118 | ||
| 119 | 0 | Transformer trans = transFact.newTransformer(xsltSource); |
| 120 | ||
| 121 | 0 | trans.transform(xmlSource, new DOMResult(domResult)); |
| 122 | ||
| 123 | 0 | } catch (Exception e) { |
| 124 | 0 | logger.log(Level.SEVERE, "Exception creating joblog file", e); |
| 125 | 0 | domResult = null; |
| 126 | 0 | } |
| 127 | ||
| 128 | 0 | 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 | ||
| 151 | 0 | DOMSource xmlSource = new DOMSource(domSource); |
| 152 | ||
| 153 | 0 | InputStream inputStream = |
| 154 | XmlUtil.class.getResourceAsStream("/" + xslFileName); | |
| 155 | 0 | Source xsltSource = new StreamSource(inputStream); |
| 156 | ||
| 157 | // the factory pattern supports different XSLT processors | |
| 158 | 0 | TransformerFactory transFact = TransformerFactory.newInstance(); |
| 159 | ||
| 160 | 0 | Transformer trans = transFact.newTransformer(xsltSource); |
| 161 | ||
| 162 | 0 | trans.transform(xmlSource, new StreamResult(outputFileName)); |
| 163 | ||
| 164 | 0 | } catch (Exception e) { |
| 165 | 0 | logger.log(Level.SEVERE, "Exception creating joblog file", e); |
| 166 | 0 | } |
| 167 | ||
| 168 | 0 | } |
| 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 | |
| 185 | 0 | DocumentBuilderFactory factory = |
| 186 | DocumentBuilderFactory.newInstance(); | |
| 187 | ||
| 188 | //Validate the XML if requested | |
| 189 | 0 | factory.setValidating(validating); |
| 190 | ||
| 191 | // Create the builder and parse the file | |
| 192 | 0 | Document doc = |
| 193 | factory.newDocumentBuilder().parse(new File(filename)); | |
| 194 | ||
| 195 | 0 | return doc; |
| 196 | ||
| 197 | 0 | } catch (SAXException e) { |
| 198 | // A parsing error occurred; the xml input is not valid | |
| 199 | 0 | logger.log( |
| 200 | Level.SEVERE, | |
| 201 | "Exception parsing XML from " + filename, | |
| 202 | e); | |
| 203 | 0 | } catch (ParserConfigurationException e) { |
| 204 | 0 | logger.log( |
| 205 | Level.SEVERE, | |
| 206 | "Exception in parser configuration while reading" + filename, | |
| 207 | e); | |
| 208 | 0 | } catch (IOException e) { |
| 209 | 0 | logger.log(Level.SEVERE, "Exception reading file" + filename, e); |
| 210 | 0 | } |
| 211 | ||
| 212 | 0 | return null; |
| 213 | ||
| 214 | } | |
| 215 | ||
| 216 | } |
|
this report was generated by version 1.0.5 of jcoverage. |
copyright © 2003, jcoverage ltd. all rights reserved. |