19package org.sleuthkit.autopsy.coreutils;
22import java.io.FileInputStream;
23import java.io.FileNotFoundException;
24import java.io.FileOutputStream;
25import java.io.IOException;
26import java.io.OutputStreamWriter;
27import java.io.UnsupportedEncodingException;
28import java.nio.file.Paths;
29import java.util.logging.Level;
30import javax.xml.XMLConstants;
31import javax.xml.parsers.DocumentBuilder;
32import javax.xml.parsers.DocumentBuilderFactory;
33import javax.xml.parsers.ParserConfigurationException;
34import javax.xml.transform.OutputKeys;
35import javax.xml.transform.Result;
36import javax.xml.transform.Transformer;
37import javax.xml.transform.TransformerConfigurationException;
38import javax.xml.transform.TransformerException;
39import javax.xml.transform.TransformerFactory;
40import javax.xml.transform.dom.DOMResult;
41import javax.xml.transform.dom.DOMSource;
42import javax.xml.transform.stream.StreamResult;
43import javax.xml.validation.Schema;
44import javax.xml.validation.SchemaFactory;
45import javax.xml.validation.Validator;
46import org.w3c.dom.Document;
47import 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();
70 builderFactory.setFeature(
"http://apache.org/xml/features/disallow-doctype-decl",
true);
71 builderFactory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING,
true);
72 builderFactory.setFeature(
"http://xml.org/sax/features/external-general-entities",
false);
73 builderFactory.setFeature(
"http://xml.org/sax/features/external-parameter-entities",
false);
74 builderFactory.setFeature(
"http://apache.org/xml/features/nonvalidating/load-external-dtd",
false);
75 builderFactory.setXIncludeAware(
false);
76 builderFactory.setExpandEntityReferences(
false);
77 return builderFactory.newDocumentBuilder();
79 Thread.currentThread().setContextClassLoader(original);
84 return SchemaFactory.newInstance(schemaLanguage);
89 ClassLoader original = Thread.currentThread().getContextClassLoader();
91 Thread.currentThread().setContextClassLoader(
XMLUtil.class.getClassLoader());
92 return TransformerFactory.newInstance();
94 Thread.currentThread().setContextClassLoader(original);
123 public static <T> Document
loadDocument(String docPath, Class<T> clazz, String schemaResourceName)
throws IOException, ParserConfigurationException, SAXException {
140 public static Document
loadDocument(String docPath)
throws ParserConfigurationException, SAXException, IOException {
142 Document doc = builder.parse(
new FileInputStream(docPath));
156 public static <T>
void validateDocument(
final Document doc, Class<T> clazz, String schemaResourceName)
throws SAXException, IOException {
159 SchemaFactory schemaFactory =
getSchemaFactory(XMLConstants.W3C_XML_SCHEMA_NS_URI);
160 Schema schema = schemaFactory.newSchema(schemaFile);
161 Validator validator = schema.newValidator();
162 validator.validate(
new DOMSource(doc),
new DOMResult());
179 public static void saveDocument(
final Document doc, String encoding, String docPath)
throws TransformerConfigurationException, FileNotFoundException, UnsupportedEncodingException, TransformerException, IOException {
181 xf.setAttribute(
"indent-number", 1);
182 Transformer xformer = xf.newTransformer();
183 xformer.setOutputProperty(OutputKeys.METHOD,
"xml");
184 xformer.setOutputProperty(OutputKeys.INDENT,
"yes");
185 xformer.setOutputProperty(OutputKeys.ENCODING, encoding);
186 xformer.setOutputProperty(OutputKeys.STANDALONE,
"yes");
187 xformer.setOutputProperty(OutputKeys.VERSION,
"1.0");
188 File file =
new File(docPath);
189 try (FileOutputStream stream =
new FileOutputStream(file)) {
190 Result out =
new StreamResult(
new OutputStreamWriter(stream, encoding));
191 xformer.transform(
new DOMSource(doc), out);
214 public static <T>
boolean xmlIsValid(DOMSource xmlfile, Class<T> clazz, String schemaFile) {
220 Schema schema = schm.newSchema(schemaLoc);
221 Validator validator = schema.newValidator();
222 DOMResult result =
new DOMResult();
223 validator.validate(xmlfile, result);
225 }
catch (SAXException e) {
226 Logger.
getLogger(clazz.getName()).log(Level.WARNING,
"Unable to validate XML file.", e);
229 }
catch (IOException e) {
230 Logger.
getLogger(clazz.getName()).log(Level.WARNING,
"Unable to load XML file [" + xmlfile.toString() +
"] of type [" + schemaFile +
"]", e);
253 public static <T>
boolean xmlIsValid(Document doc, Class<T> clazz, String type) {
254 DOMSource dms =
new DOMSource(doc);
265 public static <T> Document
loadDoc(Class<T> clazz, String xmlPath) {
269 ret = builder.parse(
new FileInputStream(xmlPath));
270 }
catch (ParserConfigurationException e) {
271 Logger.
getLogger(clazz.getName()).log(Level.SEVERE,
"Error loading XML file " + xmlPath +
" : can't initialize parser.", e);
272 }
catch (SAXException e) {
273 Logger.
getLogger(clazz.getName()).log(Level.SEVERE,
"Error loading XML file " + xmlPath +
" : can't parse XML.", e);
274 }
catch (IOException e) {
276 Logger.
getLogger(clazz.getName()).log(Level.SEVERE,
"Error loading XML file " + xmlPath +
" : can't read file.", e);
289 public static <T> Document
loadDoc(Class<T> clazz, String xmlPath, String xsdPath) {
290 Document ret =
loadDoc(clazz, xmlPath);
292 Logger.
getLogger(clazz.getName()).log(Level.WARNING,
"Error loading XML file: could not validate against [{0}], results may not be accurate", xsdPath);
306 public static <T>
boolean saveDoc(Class<T> clazz, String xmlPath, String encoding,
final Document doc) {
308 xf.setAttribute(
"indent-number", 1);
309 boolean success =
false;
311 Transformer xformer = xf.newTransformer();
312 xformer.setOutputProperty(OutputKeys.METHOD,
"xml");
313 xformer.setOutputProperty(OutputKeys.INDENT,
"yes");
314 xformer.setOutputProperty(OutputKeys.ENCODING, encoding);
315 xformer.setOutputProperty(OutputKeys.STANDALONE,
"yes");
316 xformer.setOutputProperty(OutputKeys.VERSION,
"1.0");
317 File file =
new File(xmlPath);
318 try (FileOutputStream stream =
new FileOutputStream(file)) {
319 Result out =
new StreamResult(
new OutputStreamWriter(stream, encoding));
320 xformer.transform(
new DOMSource(doc), out);
325 }
catch (UnsupportedEncodingException e) {
326 Logger.
getLogger(clazz.getName()).log(Level.SEVERE,
"Should not happen", e);
327 }
catch (TransformerConfigurationException e) {
328 Logger.
getLogger(clazz.getName()).log(Level.SEVERE,
"Error writing XML file", e);
329 }
catch (TransformerException e) {
330 Logger.
getLogger(clazz.getName()).log(Level.SEVERE,
"Error writing XML file", e);
331 }
catch (FileNotFoundException e) {
332 Logger.
getLogger(clazz.getName()).log(Level.SEVERE,
"Error writing XML file: cannot write to file: " + xmlPath, e);
333 }
catch (IOException e) {
334 Logger.
getLogger(clazz.getName()).log(Level.SEVERE,
"Error writing XML file: cannot write to file: " + xmlPath, e);
synchronized static Logger getLogger(String name)
static< T > Document loadDocument(String docPath, Class< T > clazz, String schemaResourceName)
static Document loadDocument(String docPath)
static< T > void validateDocument(final Document doc, Class< T > clazz, String schemaResourceName)
static< T > Document loadDoc(Class< T > clazz, String xmlPath, String xsdPath)
static TransformerFactory getTransformerFactory()
static SchemaFactory getSchemaFactory(String schemaLanguage)
static< T > Document loadDoc(Class< T > clazz, String xmlPath)
static Document createDocument()
static< T > boolean xmlIsValid(DOMSource xmlfile, Class< T > clazz, String schemaFile)
static DocumentBuilder getDocumentBuilder()
static void saveDocument(final Document doc, String encoding, String docPath)
static< T > boolean xmlIsValid(Document doc, Class< T > clazz, String type)
static< T > boolean saveDoc(Class< T > clazz, String xmlPath, String encoding, final Document doc)