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;
 
   64     public static Document 
createDocument() throws ParserConfigurationException {
 
   65         DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
 
   66         DocumentBuilder builder = builderFactory.newDocumentBuilder();
 
   67         return builder.newDocument();
 
   83     public static <T> Document 
loadDocument(String docPath, Class<T> clazz, String schemaResourceName) 
throws IOException, ParserConfigurationException, SAXException {
 
   98     public static Document 
loadDocument(String docPath) 
throws ParserConfigurationException, SAXException, IOException {
 
   99         DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
 
  100         DocumentBuilder builder = builderFactory.newDocumentBuilder();
 
  101         Document doc = builder.parse(
new FileInputStream(docPath));
 
  114     public static <T> 
void validateDocument(
final Document doc, Class<T> clazz, String schemaResourceName) 
throws SAXException, IOException {
 
  117         SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
 
  118         Schema schema = schemaFactory.newSchema(schemaFile);
 
  119         Validator validator = schema.newValidator();
 
  120         validator.validate(
new DOMSource(doc), 
new DOMResult());
 
  136     public static void saveDocument(
final Document doc, String encoding, String docPath) 
throws TransformerConfigurationException, FileNotFoundException, UnsupportedEncodingException, TransformerException, IOException {
 
  137         TransformerFactory xf = TransformerFactory.newInstance();
 
  138         xf.setAttribute(
"indent-number", 1); 
 
  139         Transformer xformer = xf.newTransformer();
 
  140         xformer.setOutputProperty(OutputKeys.METHOD, 
"xml"); 
 
  141         xformer.setOutputProperty(OutputKeys.INDENT, 
"yes"); 
 
  142         xformer.setOutputProperty(OutputKeys.ENCODING, encoding);
 
  143         xformer.setOutputProperty(OutputKeys.STANDALONE, 
"yes"); 
 
  144         xformer.setOutputProperty(OutputKeys.VERSION, 
"1.0");
 
  145         File file = 
new File(docPath);
 
  146         try (FileOutputStream stream = 
new FileOutputStream(file)) {
 
  147             Result out = 
new StreamResult(
new OutputStreamWriter(stream, encoding));
 
  148             xformer.transform(
new DOMSource(doc), out);
 
  171     public static <T> 
boolean xmlIsValid(DOMSource xmlfile, Class<T> clazz, String schemaFile) {
 
  175             SchemaFactory schm = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
 
  177                 Schema schema = schm.newSchema(schemaLoc);
 
  178                 Validator validator = schema.newValidator();
 
  179                 DOMResult result = 
new DOMResult();
 
  180                 validator.validate(xmlfile, result);
 
  182             } 
catch (SAXException e) {
 
  183                 Logger.
getLogger(clazz.getName()).log(Level.WARNING, 
"Unable to validate XML file.", e); 
 
  186         } 
catch (IOException e) {
 
  187             Logger.
getLogger(clazz.getName()).log(Level.WARNING, 
"Unable to load XML file [" + xmlfile.toString() + 
"] of type [" + schemaFile + 
"]", e); 
 
  209     public static <T> 
boolean xmlIsValid(Document doc, Class<T> clazz, String type) {
 
  210         DOMSource dms = 
new DOMSource(doc);
 
  221     public static <T> Document 
loadDoc(Class<T> clazz, String xmlPath) {
 
  222         DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
 
  225             DocumentBuilder builder = builderFactory.newDocumentBuilder();
 
  226             ret = builder.parse(
new FileInputStream(xmlPath));
 
  227         } 
catch (ParserConfigurationException e) {
 
  228             Logger.
getLogger(clazz.getName()).log(Level.SEVERE, 
"Error loading XML file " + xmlPath + 
" : can't initialize parser.", e); 
 
  229         } 
catch (SAXException e) {
 
  230             Logger.
getLogger(clazz.getName()).log(Level.SEVERE, 
"Error loading XML file " + xmlPath + 
" : can't parse XML.", e); 
 
  231         } 
catch (IOException e) {
 
  233             Logger.
getLogger(clazz.getName()).log(Level.SEVERE, 
"Error loading XML file " + xmlPath + 
" : can't read file.", e); 
 
  246     public static <T> Document 
loadDoc(Class<T> clazz, String xmlPath, String xsdPath) {
 
  247         Document ret = 
loadDoc(clazz, xmlPath);
 
  249             Logger.
getLogger(clazz.getName()).log(Level.WARNING, 
"Error loading XML file: could not validate against [{0}], results may not be accurate", xsdPath); 
 
  263     public static <T> 
boolean saveDoc(Class<T> clazz, String xmlPath, String encoding, 
final Document doc) {
 
  264         TransformerFactory xf = TransformerFactory.newInstance();
 
  265         xf.setAttribute(
"indent-number", 1); 
 
  266         boolean success = 
false;
 
  268             Transformer xformer = xf.newTransformer();
 
  269             xformer.setOutputProperty(OutputKeys.METHOD, 
"xml"); 
 
  270             xformer.setOutputProperty(OutputKeys.INDENT, 
"yes"); 
 
  271             xformer.setOutputProperty(OutputKeys.ENCODING, encoding);
 
  272             xformer.setOutputProperty(OutputKeys.STANDALONE, 
"yes"); 
 
  273             xformer.setOutputProperty(OutputKeys.VERSION, 
"1.0");
 
  274             File file = 
new File(xmlPath);
 
  275             try (FileOutputStream stream = 
new FileOutputStream(file)) {
 
  276                 Result out = 
new StreamResult(
new OutputStreamWriter(stream, encoding));
 
  277                 xformer.transform(
new DOMSource(doc), out);
 
  282         } 
catch (UnsupportedEncodingException e) {
 
  283             Logger.
getLogger(clazz.getName()).log(Level.SEVERE, 
"Should not happen", e); 
 
  284         } 
catch (TransformerConfigurationException e) {
 
  285             Logger.
getLogger(clazz.getName()).log(Level.SEVERE, 
"Error writing XML file", e); 
 
  286         } 
catch (TransformerException e) {
 
  287             Logger.
getLogger(clazz.getName()).log(Level.SEVERE, 
"Error writing XML file", e); 
 
  288         } 
catch (FileNotFoundException e) {
 
  289             Logger.
getLogger(clazz.getName()).log(Level.SEVERE, 
"Error writing XML file: cannot write to file: " + xmlPath, e); 
 
  290         } 
catch (IOException e) {
 
  291             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< T > Document loadDoc(Class< T > clazz, String xmlPath, String xsdPath)
 
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)
 
static Logger getLogger(String name)
 
static< T > boolean saveDoc(Class< T > clazz, String xmlPath, String encoding, final Document doc)