Autopsy  4.1
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 
85  public static <T> Document loadDocument(String docPath, Class<T> clazz, String schemaResourceName) throws IOException, ParserConfigurationException, SAXException {
86  Document doc = loadDocument(docPath);
87  validateDocument(doc, clazz, schemaResourceName);
88  return doc;
89  }
90 
102  public static Document loadDocument(String docPath) throws ParserConfigurationException, SAXException, IOException {
103  DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
104  DocumentBuilder builder = builderFactory.newDocumentBuilder();
105  Document doc = builder.parse(new FileInputStream(docPath));
106  return doc;
107  }
108 
119  public static <T> void validateDocument(final Document doc, Class<T> clazz, String schemaResourceName) throws SAXException, IOException {
120  PlatformUtil.extractResourceToUserConfigDir(clazz, schemaResourceName, false);
121  File schemaFile = new File(Paths.get(PlatformUtil.getUserConfigDirectory(), schemaResourceName).toAbsolutePath().toString());
122  SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
123  Schema schema = schemaFactory.newSchema(schemaFile);
124  Validator validator = schema.newValidator();
125  validator.validate(new DOMSource(doc), new DOMResult());
126  }
127 
142  public static void saveDocument(final Document doc, String encoding, String docPath) throws TransformerConfigurationException, FileNotFoundException, UnsupportedEncodingException, TransformerException, IOException {
143  TransformerFactory xf = TransformerFactory.newInstance();
144  xf.setAttribute("indent-number", 1); //NON-NLS
145  Transformer xformer = xf.newTransformer();
146  xformer.setOutputProperty(OutputKeys.METHOD, "xml"); //NON-NLS
147  xformer.setOutputProperty(OutputKeys.INDENT, "yes"); //NON-NLS
148  xformer.setOutputProperty(OutputKeys.ENCODING, encoding);
149  xformer.setOutputProperty(OutputKeys.STANDALONE, "yes"); //NON-NLS
150  xformer.setOutputProperty(OutputKeys.VERSION, "1.0");
151  File file = new File(docPath);
152  try (FileOutputStream stream = new FileOutputStream(file)) {
153  Result out = new StreamResult(new OutputStreamWriter(stream, encoding));
154  xformer.transform(new DOMSource(doc), out);
155  stream.flush();
156  }
157  }
158 
176  // TODO: Deprecate.
177  public static <T> boolean xmlIsValid(DOMSource xmlfile, Class<T> clazz, String schemaFile) {
178  try {
179  PlatformUtil.extractResourceToUserConfigDir(clazz, schemaFile, false);
180  File schemaLoc = new File(PlatformUtil.getUserConfigDirectory() + File.separator + schemaFile);
181  SchemaFactory schm = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
182  try {
183  Schema schema = schm.newSchema(schemaLoc);
184  Validator validator = schema.newValidator();
185  DOMResult result = new DOMResult();
186  validator.validate(xmlfile, result);
187  return true;
188  } catch (SAXException e) {
189  Logger.getLogger(clazz.getName()).log(Level.WARNING, "Unable to validate XML file.", e); //NON-NLS
190  return false;
191  }
192  } catch (IOException e) {
193  Logger.getLogger(clazz.getName()).log(Level.WARNING, "Unable to load XML file [" + xmlfile.toString() + "] of type [" + schemaFile + "]", e); //NON-NLS
194  return false;
195  }
196  }
197 
215  // TODO: Deprecate.
216  public static <T> boolean xmlIsValid(Document doc, Class<T> clazz, String type) {
217  DOMSource dms = new DOMSource(doc);
218  return xmlIsValid(dms, clazz, type);
219  }
220 
227  // TODO: Deprecate.
228  public static <T> Document loadDoc(Class<T> clazz, String xmlPath) {
229  DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
230  Document ret = null;
231  try {
232  DocumentBuilder builder = builderFactory.newDocumentBuilder();
233  ret = builder.parse(new FileInputStream(xmlPath));
234  } catch (ParserConfigurationException e) {
235  Logger.getLogger(clazz.getName()).log(Level.SEVERE, "Error loading XML file " + xmlPath + " : can't initialize parser.", e); //NON-NLS
236  } catch (SAXException e) {
237  Logger.getLogger(clazz.getName()).log(Level.SEVERE, "Error loading XML file " + xmlPath + " : can't parse XML.", e); //NON-NLS
238  } catch (IOException e) {
239  //error reading file
240  Logger.getLogger(clazz.getName()).log(Level.SEVERE, "Error loading XML file " + xmlPath + " : can't read file.", e); //NON-NLS
241  }
242  return ret;
243  }
244 
252  // TODO: Deprecate
253  public static <T> Document loadDoc(Class<T> clazz, String xmlPath, String xsdPath) {
254  Document ret = loadDoc(clazz, xmlPath);
255  if (!XMLUtil.xmlIsValid(ret, clazz, xsdPath)) {
256  Logger.getLogger(clazz.getName()).log(Level.WARNING, "Error loading XML file: could not validate against [{0}], results may not be accurate", xsdPath); //NON-NLS
257  }
258  return ret;
259  }
260 
269  // TODO: Deprecate.
270  public static <T> boolean saveDoc(Class<T> clazz, String xmlPath, String encoding, final Document doc) {
271  TransformerFactory xf = TransformerFactory.newInstance();
272  xf.setAttribute("indent-number", 1); //NON-NLS
273  boolean success = false;
274  try {
275  Transformer xformer = xf.newTransformer();
276  xformer.setOutputProperty(OutputKeys.METHOD, "xml"); //NON-NLS
277  xformer.setOutputProperty(OutputKeys.INDENT, "yes"); //NON-NLS
278  xformer.setOutputProperty(OutputKeys.ENCODING, encoding);
279  xformer.setOutputProperty(OutputKeys.STANDALONE, "yes"); //NON-NLS
280  xformer.setOutputProperty(OutputKeys.VERSION, "1.0");
281  File file = new File(xmlPath);
282  try (FileOutputStream stream = new FileOutputStream(file)) {
283  Result out = new StreamResult(new OutputStreamWriter(stream, encoding));
284  xformer.transform(new DOMSource(doc), out);
285  stream.flush();
286  }
287  success = true;
288 
289  } catch (UnsupportedEncodingException e) {
290  Logger.getLogger(clazz.getName()).log(Level.SEVERE, "Should not happen", e); //NON-NLS
291  } catch (TransformerConfigurationException e) {
292  Logger.getLogger(clazz.getName()).log(Level.SEVERE, "Error writing XML file", e); //NON-NLS
293  } catch (TransformerException e) {
294  Logger.getLogger(clazz.getName()).log(Level.SEVERE, "Error writing XML file", e); //NON-NLS
295  } catch (FileNotFoundException e) {
296  Logger.getLogger(clazz.getName()).log(Level.SEVERE, "Error writing XML file: cannot write to file: " + xmlPath, e); //NON-NLS
297  } catch (IOException e) {
298  Logger.getLogger(clazz.getName()).log(Level.SEVERE, "Error writing XML file: cannot write to file: " + xmlPath, e); //NON-NLS
299  }
300  return success;
301  }
302 
303 }
static< T > Document loadDoc(Class< T > clazz, String xmlPath)
Definition: XMLUtil.java:228
static< T > Document loadDoc(Class< T > clazz, String xmlPath, String xsdPath)
Definition: XMLUtil.java:253
static< T > boolean xmlIsValid(Document doc, Class< T > clazz, String type)
Definition: XMLUtil.java:216
static< T > void validateDocument(final Document doc, Class< T > clazz, String schemaResourceName)
Definition: XMLUtil.java:119
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:85
static< T > boolean xmlIsValid(DOMSource xmlfile, Class< T > clazz, String schemaFile)
Definition: XMLUtil.java:177
static Document loadDocument(String docPath)
Definition: XMLUtil.java:102
static void saveDocument(final Document doc, String encoding, String docPath)
Definition: XMLUtil.java:142
synchronized static Logger getLogger(String name)
Definition: Logger.java:161
static< T > boolean saveDoc(Class< T > clazz, String xmlPath, String encoding, final Document doc)
Definition: XMLUtil.java:270

Copyright © 2012-2016 Basis Technology. Generated on: Tue Oct 25 2016
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.