Autopsy  4.0
Graphical digital forensics platform for The Sleuth Kit and other tools.
XMLUtil.java
Go to the documentation of this file.
1 /*
2  * Autopsy Forensic Browser
3  *
4  * Copyright 2012-2014 Basis Technology Corp.
5  * Contact: carrier <at> sleuthkit <dot> org
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19 package org.sleuthkit.autopsy.coreutils;
20 
21 import java.io.File;
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;
48 
56 public class XMLUtil {
57 
65  public static Document createDocument() throws ParserConfigurationException {
66  DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
67  DocumentBuilder builder = builderFactory.newDocumentBuilder();
68  return builder.newDocument();
69  }
70 
87  public static <T> Document loadDocument(String docPath, Class<T> clazz, String schemaResourceName) throws IOException, ParserConfigurationException, SAXException {
88  Document doc = loadDocument(docPath);
89  validateDocument(doc, clazz, schemaResourceName);
90  return doc;
91  }
92 
104  public static Document loadDocument(String docPath) throws ParserConfigurationException, SAXException, IOException {
105  DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
106  DocumentBuilder builder = builderFactory.newDocumentBuilder();
107  Document doc = builder.parse(new FileInputStream(docPath));
108  return doc;
109  }
110 
121  public static <T> void validateDocument(final Document doc, Class<T> clazz, String schemaResourceName) throws SAXException, IOException {
122  PlatformUtil.extractResourceToUserConfigDir(clazz, schemaResourceName, false);
123  File schemaFile = new File(Paths.get(PlatformUtil.getUserConfigDirectory(), schemaResourceName).toAbsolutePath().toString());
124  SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
125  Schema schema = schemaFactory.newSchema(schemaFile);
126  Validator validator = schema.newValidator();
127  validator.validate(new DOMSource(doc), new DOMResult());
128  }
129 
144  public static void saveDocument(final Document doc, String encoding, String docPath) throws TransformerConfigurationException, FileNotFoundException, UnsupportedEncodingException, TransformerException, IOException {
145  TransformerFactory xf = TransformerFactory.newInstance();
146  xf.setAttribute("indent-number", 1); //NON-NLS
147  Transformer xformer = xf.newTransformer();
148  xformer.setOutputProperty(OutputKeys.METHOD, "xml"); //NON-NLS
149  xformer.setOutputProperty(OutputKeys.INDENT, "yes"); //NON-NLS
150  xformer.setOutputProperty(OutputKeys.ENCODING, encoding);
151  xformer.setOutputProperty(OutputKeys.STANDALONE, "yes"); //NON-NLS
152  xformer.setOutputProperty(OutputKeys.VERSION, "1.0");
153  File file = new File(docPath);
154  try (FileOutputStream stream = new FileOutputStream(file)) {
155  Result out = new StreamResult(new OutputStreamWriter(stream, encoding));
156  xformer.transform(new DOMSource(doc), out);
157  stream.flush();
158  }
159  }
160 
178  // TODO: Deprecate.
179  public static <T> boolean xmlIsValid(DOMSource xmlfile, Class<T> clazz, String schemaFile) {
180  try {
181  PlatformUtil.extractResourceToUserConfigDir(clazz, schemaFile, false);
182  File schemaLoc = new File(PlatformUtil.getUserConfigDirectory() + File.separator + schemaFile);
183  SchemaFactory schm = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
184  try {
185  Schema schema = schm.newSchema(schemaLoc);
186  Validator validator = schema.newValidator();
187  DOMResult result = new DOMResult();
188  validator.validate(xmlfile, result);
189  return true;
190  } catch (SAXException e) {
191  Logger.getLogger(clazz.getName()).log(Level.WARNING, "Unable to validate XML file.", e); //NON-NLS
192  return false;
193  }
194  } catch (IOException e) {
195  Logger.getLogger(clazz.getName()).log(Level.WARNING, "Unable to load XML file [" + xmlfile.toString() + "] of type [" + schemaFile + "]", e); //NON-NLS
196  return false;
197  }
198  }
199 
217  // TODO: Deprecate.
218  public static <T> boolean xmlIsValid(Document doc, Class<T> clazz, String type) {
219  DOMSource dms = new DOMSource(doc);
220  return xmlIsValid(dms, clazz, type);
221  }
222 
229  // TODO: Deprecate.
230  public static <T> Document loadDoc(Class<T> clazz, String xmlPath) {
231  DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
232  Document ret = null;
233  try {
234  DocumentBuilder builder = builderFactory.newDocumentBuilder();
235  ret = builder.parse(new FileInputStream(xmlPath));
236  } catch (ParserConfigurationException e) {
237  Logger.getLogger(clazz.getName()).log(Level.SEVERE, "Error loading XML file " + xmlPath + " : can't initialize parser.", e); //NON-NLS
238  } catch (SAXException e) {
239  Logger.getLogger(clazz.getName()).log(Level.SEVERE, "Error loading XML file " + xmlPath + " : can't parse XML.", e); //NON-NLS
240  } catch (IOException e) {
241  //error reading file
242  Logger.getLogger(clazz.getName()).log(Level.SEVERE, "Error loading XML file " + xmlPath + " : can't read file.", e); //NON-NLS
243  }
244  return ret;
245  }
246 
254  // TODO: Deprecate
255  public static <T> Document loadDoc(Class<T> clazz, String xmlPath, String xsdPath) {
256  Document ret = loadDoc(clazz, xmlPath);
257  if (!XMLUtil.xmlIsValid(ret, clazz, xsdPath)) {
258  Logger.getLogger(clazz.getName()).log(Level.WARNING, "Error loading XML file: could not validate against [{0}], results may not be accurate", xsdPath); //NON-NLS
259  }
260  return ret;
261  }
262 
271  // TODO: Deprecate.
272  public static <T> boolean saveDoc(Class<T> clazz, String xmlPath, String encoding, final Document doc) {
273  TransformerFactory xf = TransformerFactory.newInstance();
274  xf.setAttribute("indent-number", 1); //NON-NLS
275  boolean success = false;
276  try {
277  Transformer xformer = xf.newTransformer();
278  xformer.setOutputProperty(OutputKeys.METHOD, "xml"); //NON-NLS
279  xformer.setOutputProperty(OutputKeys.INDENT, "yes"); //NON-NLS
280  xformer.setOutputProperty(OutputKeys.ENCODING, encoding);
281  xformer.setOutputProperty(OutputKeys.STANDALONE, "yes"); //NON-NLS
282  xformer.setOutputProperty(OutputKeys.VERSION, "1.0");
283  File file = new File(xmlPath);
284  try (FileOutputStream stream = new FileOutputStream(file)) {
285  Result out = new StreamResult(new OutputStreamWriter(stream, encoding));
286  xformer.transform(new DOMSource(doc), out);
287  stream.flush();
288  }
289  success = true;
290 
291  } catch (UnsupportedEncodingException e) {
292  Logger.getLogger(clazz.getName()).log(Level.SEVERE, "Should not happen", e); //NON-NLS
293  } catch (TransformerConfigurationException e) {
294  Logger.getLogger(clazz.getName()).log(Level.SEVERE, "Error writing XML file", e); //NON-NLS
295  } catch (TransformerException e) {
296  Logger.getLogger(clazz.getName()).log(Level.SEVERE, "Error writing XML file", e); //NON-NLS
297  } catch (FileNotFoundException e) {
298  Logger.getLogger(clazz.getName()).log(Level.SEVERE, "Error writing XML file: cannot write to file: " + xmlPath, e); //NON-NLS
299  } catch (IOException e) {
300  Logger.getLogger(clazz.getName()).log(Level.SEVERE, "Error writing XML file: cannot write to file: " + xmlPath, e); //NON-NLS
301  }
302  return success;
303  }
304 
305 }
static< T > Document loadDoc(Class< T > clazz, String xmlPath)
Definition: XMLUtil.java:230
static< T > Document loadDoc(Class< T > clazz, String xmlPath, String xsdPath)
Definition: XMLUtil.java:255
static< T > boolean xmlIsValid(Document doc, Class< T > clazz, String type)
Definition: XMLUtil.java:218
static< T > void validateDocument(final Document doc, Class< T > clazz, String schemaResourceName)
Definition: XMLUtil.java:121
static< T > boolean extractResourceToUserConfigDir(final Class< T > resourceClass, final String resourceFileName, boolean overWrite)
static< T > Document loadDocument(String docPath, Class< T > clazz, String schemaResourceName)
Definition: XMLUtil.java:87
static< T > boolean xmlIsValid(DOMSource xmlfile, Class< T > clazz, String schemaFile)
Definition: XMLUtil.java:179
static Document loadDocument(String docPath)
Definition: XMLUtil.java:104
static void saveDocument(final Document doc, String encoding, String docPath)
Definition: XMLUtil.java:144
synchronized static Logger getLogger(String name)
Definition: Logger.java:166
static< T > boolean saveDoc(Class< T > clazz, String xmlPath, String encoding, final Document doc)
Definition: XMLUtil.java:272

Copyright © 2012-2015 Basis Technology. Generated on: Wed Apr 6 2016
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.