19 package org.sleuthkit.autopsy.coreutils;
22 import java.io.FileInputStream;
23 import java.io.FileNotFoundException;
24 import java.io.FileOutputStream;
25 import java.io.IOException;
26 import java.io.OutputStreamWriter;
27 import java.io.UnsupportedEncodingException;
28 import java.nio.file.Paths;
29 import java.util.logging.Level;
30 import javax.xml.XMLConstants;
31 import javax.xml.parsers.DocumentBuilder;
32 import javax.xml.parsers.DocumentBuilderFactory;
33 import javax.xml.parsers.ParserConfigurationException;
34 import javax.xml.transform.OutputKeys;
35 import javax.xml.transform.Result;
36 import javax.xml.transform.Transformer;
37 import javax.xml.transform.TransformerConfigurationException;
38 import javax.xml.transform.TransformerException;
39 import javax.xml.transform.TransformerFactory;
40 import javax.xml.transform.dom.DOMResult;
41 import javax.xml.transform.dom.DOMSource;
42 import javax.xml.transform.stream.StreamResult;
43 import javax.xml.validation.Schema;
44 import javax.xml.validation.SchemaFactory;
45 import javax.xml.validation.Validator;
46 import org.w3c.dom.Document;
47 import org.xml.sax.SAXException;
60 System.setProperty(
"javax.xml.transform.TransformerFactory",
"com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl");
65 ClassLoader original = Thread.currentThread().getContextClassLoader();
67 Thread.currentThread().setContextClassLoader(
XMLUtil.class.getClassLoader());
68 DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
69 return builderFactory.newDocumentBuilder();
71 Thread.currentThread().setContextClassLoader(original);
77 ClassLoader original = Thread.currentThread().getContextClassLoader();
79 Thread.currentThread().setContextClassLoader(
XMLUtil.class.getClassLoader());
80 return SchemaFactory.newInstance(schemaLanguage);
82 Thread.currentThread().setContextClassLoader(original);
88 ClassLoader original = Thread.currentThread().getContextClassLoader();
90 Thread.currentThread().setContextClassLoader(
XMLUtil.class.getClassLoader());
91 return TransformerFactory.newInstance();
93 Thread.currentThread().setContextClassLoader(original);
122 public static <T> Document
loadDocument(String docPath, Class<T> clazz, String schemaResourceName)
throws IOException, ParserConfigurationException, SAXException {
139 public static Document
loadDocument(String docPath)
throws ParserConfigurationException, SAXException, IOException {
141 Document doc = builder.parse(
new FileInputStream(docPath));
155 public static <T>
void validateDocument(
final Document doc, Class<T> clazz, String schemaResourceName)
throws SAXException, IOException {
158 SchemaFactory schemaFactory =
getSchemaFactory(XMLConstants.W3C_XML_SCHEMA_NS_URI);
159 Schema schema = schemaFactory.newSchema(schemaFile);
160 Validator validator = schema.newValidator();
161 validator.validate(
new DOMSource(doc),
new DOMResult());
178 public static void saveDocument(
final Document doc, String encoding, String docPath)
throws TransformerConfigurationException, FileNotFoundException, UnsupportedEncodingException, TransformerException, IOException {
180 xf.setAttribute(
"indent-number", 1);
181 Transformer xformer = xf.newTransformer();
182 xformer.setOutputProperty(OutputKeys.METHOD,
"xml");
183 xformer.setOutputProperty(OutputKeys.INDENT,
"yes");
184 xformer.setOutputProperty(OutputKeys.ENCODING, encoding);
185 xformer.setOutputProperty(OutputKeys.STANDALONE,
"yes");
186 xformer.setOutputProperty(OutputKeys.VERSION,
"1.0");
187 File file =
new File(docPath);
188 try (FileOutputStream stream =
new FileOutputStream(file)) {
189 Result out =
new StreamResult(
new OutputStreamWriter(stream, encoding));
190 xformer.transform(
new DOMSource(doc), out);
213 public static <T>
boolean xmlIsValid(DOMSource xmlfile, Class<T> clazz, String schemaFile) {
219 Schema schema = schm.newSchema(schemaLoc);
220 Validator validator = schema.newValidator();
221 DOMResult result =
new DOMResult();
222 validator.validate(xmlfile, result);
224 }
catch (SAXException e) {
225 Logger.
getLogger(clazz.getName()).log(Level.WARNING,
"Unable to validate XML file.", e);
228 }
catch (IOException e) {
229 Logger.
getLogger(clazz.getName()).log(Level.WARNING,
"Unable to load XML file [" + xmlfile.toString() +
"] of type [" + schemaFile +
"]", e);
252 public static <T>
boolean xmlIsValid(Document doc, Class<T> clazz, String type) {
253 DOMSource dms =
new DOMSource(doc);
264 public static <T> Document
loadDoc(Class<T> clazz, String xmlPath) {
268 ret = builder.parse(
new FileInputStream(xmlPath));
269 }
catch (ParserConfigurationException e) {
270 Logger.
getLogger(clazz.getName()).log(Level.SEVERE,
"Error loading XML file " + xmlPath +
" : can't initialize parser.", e);
271 }
catch (SAXException e) {
272 Logger.
getLogger(clazz.getName()).log(Level.SEVERE,
"Error loading XML file " + xmlPath +
" : can't parse XML.", e);
273 }
catch (IOException e) {
275 Logger.
getLogger(clazz.getName()).log(Level.SEVERE,
"Error loading XML file " + xmlPath +
" : can't read file.", e);
288 public static <T> Document
loadDoc(Class<T> clazz, String xmlPath, String xsdPath) {
289 Document ret =
loadDoc(clazz, xmlPath);
291 Logger.
getLogger(clazz.getName()).log(Level.WARNING,
"Error loading XML file: could not validate against [{0}], results may not be accurate", xsdPath);
305 public static <T>
boolean saveDoc(Class<T> clazz, String xmlPath, String encoding,
final Document doc) {
307 xf.setAttribute(
"indent-number", 1);
308 boolean success =
false;
310 Transformer xformer = xf.newTransformer();
311 xformer.setOutputProperty(OutputKeys.METHOD,
"xml");
312 xformer.setOutputProperty(OutputKeys.INDENT,
"yes");
313 xformer.setOutputProperty(OutputKeys.ENCODING, encoding);
314 xformer.setOutputProperty(OutputKeys.STANDALONE,
"yes");
315 xformer.setOutputProperty(OutputKeys.VERSION,
"1.0");
316 File file =
new File(xmlPath);
317 try (FileOutputStream stream =
new FileOutputStream(file)) {
318 Result out =
new StreamResult(
new OutputStreamWriter(stream, encoding));
319 xformer.transform(
new DOMSource(doc), out);
324 }
catch (UnsupportedEncodingException e) {
325 Logger.
getLogger(clazz.getName()).log(Level.SEVERE,
"Should not happen", e);
326 }
catch (TransformerConfigurationException e) {
327 Logger.
getLogger(clazz.getName()).log(Level.SEVERE,
"Error writing XML file", e);
328 }
catch (TransformerException e) {
329 Logger.
getLogger(clazz.getName()).log(Level.SEVERE,
"Error writing XML file", e);
330 }
catch (FileNotFoundException e) {
331 Logger.
getLogger(clazz.getName()).log(Level.SEVERE,
"Error writing XML file: cannot write to file: " + xmlPath, e);
332 }
catch (IOException e) {
333 Logger.
getLogger(clazz.getName()).log(Level.SEVERE,
"Error writing XML file: cannot write to file: " + xmlPath, e);
static< T > Document loadDoc(Class< T > clazz, String xmlPath)
static Document createDocument()
static TransformerFactory getTransformerFactory()
static< T > Document loadDoc(Class< T > clazz, String xmlPath, String xsdPath)
static SchemaFactory getSchemaFactory(String schemaLanguage)
static< T > boolean xmlIsValid(Document doc, Class< T > clazz, String type)
static< T > void validateDocument(final Document doc, Class< T > clazz, String schemaResourceName)
static< T > Document loadDocument(String docPath, Class< T > clazz, String schemaResourceName)
static< T > boolean xmlIsValid(DOMSource xmlfile, Class< T > clazz, String schemaFile)
static Document loadDocument(String docPath)
static void saveDocument(final Document doc, String encoding, String docPath)
synchronized static Logger getLogger(String name)
static DocumentBuilder getDocumentBuilder()
static< T > boolean saveDoc(Class< T > clazz, String xmlPath, String encoding, final Document doc)