1
1
/*
2
2
* Autopsy Forensic Browser
3
3
*
4
- * Copyright 2012-2014 Basis Technology Corp.
4
+ * Copyright 2012-2020 Basis Technology Corp.
5
5
* Contact: carrier <at> sleuthkit <dot> org
6
6
*
7
7
* Licensed under the Apache License, Version 2.0 (the "License");
54
54
* -Loading documents from disk
55
55
*/
56
56
public class XMLUtil {
57
+
58
+ private static DocumentBuilder getDocumentBuilder () throws ParserConfigurationException {
59
+ // See JIRA-6958 for details about class loading and jaxb.
60
+ ClassLoader original = Thread .currentThread ().getContextClassLoader ();
61
+ try {
62
+ Thread .currentThread ().setContextClassLoader (XMLUtil .class .getClassLoader ());
63
+ DocumentBuilderFactory builderFactory = DocumentBuilderFactory .newInstance ();
64
+ return builderFactory .newDocumentBuilder ();
65
+ } finally {
66
+ Thread .currentThread ().setContextClassLoader (original );
67
+ }
68
+ }
69
+
70
+ private static SchemaFactory getSchemaFactory (String schemaLanguage ) {
71
+ // See JIRA-6958 for details about class loading and jaxb.
72
+ ClassLoader original = Thread .currentThread ().getContextClassLoader ();
73
+ try {
74
+ Thread .currentThread ().setContextClassLoader (XMLUtil .class .getClassLoader ());
75
+ return SchemaFactory .newInstance (schemaLanguage );
76
+ } finally {
77
+ Thread .currentThread ().setContextClassLoader (original );
78
+ }
79
+ }
80
+
81
+ private static TransformerFactory getTransformerFactory () {
82
+ // See JIRA-6958 for details about class loading and jaxb.
83
+ ClassLoader original = Thread .currentThread ().getContextClassLoader ();
84
+ try {
85
+ Thread .currentThread ().setContextClassLoader (XMLUtil .class .getClassLoader ());
86
+ return TransformerFactory .newInstance ();
87
+ } finally {
88
+ Thread .currentThread ().setContextClassLoader (original );
89
+ }
90
+ }
57
91
58
92
/**
59
93
* Creates a W3C DOM.
@@ -63,9 +97,7 @@ public class XMLUtil {
63
97
* @throws ParserConfigurationException
64
98
*/
65
99
public static Document createDocument () throws ParserConfigurationException {
66
- DocumentBuilderFactory builderFactory = DocumentBuilderFactory .newInstance ();
67
- DocumentBuilder builder = builderFactory .newDocumentBuilder ();
68
- return builder .newDocument ();
100
+ return getDocumentBuilder ().newDocument ();
69
101
}
70
102
71
103
/**
@@ -100,8 +132,7 @@ public static <T> Document loadDocument(String docPath, Class<T> clazz, String s
100
132
* @throws IOException
101
133
*/
102
134
public static Document loadDocument (String docPath ) throws ParserConfigurationException , SAXException , IOException {
103
- DocumentBuilderFactory builderFactory = DocumentBuilderFactory .newInstance ();
104
- DocumentBuilder builder = builderFactory .newDocumentBuilder ();
135
+ DocumentBuilder builder = getDocumentBuilder ();
105
136
Document doc = builder .parse (new FileInputStream (docPath ));
106
137
return doc ;
107
138
}
@@ -119,7 +150,7 @@ public static Document loadDocument(String docPath) throws ParserConfigurationEx
119
150
public static <T > void validateDocument (final Document doc , Class <T > clazz , String schemaResourceName ) throws SAXException , IOException {
120
151
PlatformUtil .extractResourceToUserConfigDir (clazz , schemaResourceName , false );
121
152
File schemaFile = new File (Paths .get (PlatformUtil .getUserConfigDirectory (), schemaResourceName ).toAbsolutePath ().toString ());
122
- SchemaFactory schemaFactory = SchemaFactory . newInstance (XMLConstants .W3C_XML_SCHEMA_NS_URI );
153
+ SchemaFactory schemaFactory = getSchemaFactory (XMLConstants .W3C_XML_SCHEMA_NS_URI );
123
154
Schema schema = schemaFactory .newSchema (schemaFile );
124
155
Validator validator = schema .newValidator ();
125
156
validator .validate (new DOMSource (doc ), new DOMResult ());
@@ -140,7 +171,7 @@ public static <T> void validateDocument(final Document doc, Class<T> clazz, Stri
140
171
* @throws IOException
141
172
*/
142
173
public static void saveDocument (final Document doc , String encoding , String docPath ) throws TransformerConfigurationException , FileNotFoundException , UnsupportedEncodingException , TransformerException , IOException {
143
- TransformerFactory xf = TransformerFactory . newInstance ();
174
+ TransformerFactory xf = getTransformerFactory ();
144
175
xf .setAttribute ("indent-number" , 1 ); //NON-NLS
145
176
Transformer xformer = xf .newTransformer ();
146
177
xformer .setOutputProperty (OutputKeys .METHOD , "xml" ); //NON-NLS
@@ -178,7 +209,7 @@ public static <T> boolean xmlIsValid(DOMSource xmlfile, Class<T> clazz, String s
178
209
try {
179
210
PlatformUtil .extractResourceToUserConfigDir (clazz , schemaFile , false );
180
211
File schemaLoc = new File (PlatformUtil .getUserConfigDirectory () + File .separator + schemaFile );
181
- SchemaFactory schm = SchemaFactory . newInstance (XMLConstants .W3C_XML_SCHEMA_NS_URI );
212
+ SchemaFactory schm = getSchemaFactory (XMLConstants .W3C_XML_SCHEMA_NS_URI );
182
213
try {
183
214
Schema schema = schm .newSchema (schemaLoc );
184
215
Validator validator = schema .newValidator ();
@@ -226,10 +257,9 @@ public static <T> boolean xmlIsValid(Document doc, Class<T> clazz, String type)
226
257
*/
227
258
// TODO: Deprecate.
228
259
public static <T > Document loadDoc (Class <T > clazz , String xmlPath ) {
229
- DocumentBuilderFactory builderFactory = DocumentBuilderFactory .newInstance ();
230
260
Document ret = null ;
231
261
try {
232
- DocumentBuilder builder = builderFactory . newDocumentBuilder ();
262
+ DocumentBuilder builder = getDocumentBuilder ();
233
263
ret = builder .parse (new FileInputStream (xmlPath ));
234
264
} catch (ParserConfigurationException e ) {
235
265
Logger .getLogger (clazz .getName ()).log (Level .SEVERE , "Error loading XML file " + xmlPath + " : can't initialize parser." , e ); //NON-NLS
@@ -268,7 +298,7 @@ public static <T> Document loadDoc(Class<T> clazz, String xmlPath, String xsdPat
268
298
*/
269
299
// TODO: Deprecate.
270
300
public static <T > boolean saveDoc (Class <T > clazz , String xmlPath , String encoding , final Document doc ) {
271
- TransformerFactory xf = TransformerFactory . newInstance ();
301
+ TransformerFactory xf = getTransformerFactory ();
272
302
xf .setAttribute ("indent-number" , 1 ); //NON-NLS
273
303
boolean success = false ;
274
304
try {
0 commit comments