16
16
import java .io .ByteArrayInputStream ;
17
17
import java .io .IOException ;
18
18
import java .io .InputStream ;
19
+ import java .lang .reflect .InvocationTargetException ;
20
+ import java .lang .reflect .Method ;
19
21
import java .net .URL ;
20
22
import java .nio .ByteBuffer ;
21
23
import java .util .Date ;
34
36
import org .w3c .dom .NodeList ;
35
37
import org .xml .sax .SAXException ;
36 38
37
- import com .sun .org .apache .xerces .internal .jaxp .DocumentBuilderFactoryImpl ;
38
- import com .sun .org .apache .xml .internal .dtm .DTMManager ;
39
- import com .sun .org .apache .xml .internal .dtm .ref .DTMManagerDefault ;
40
- import com .sun .org .apache .xpath .internal .XPathContext ;
41
-
42
39
/**
43
40
* Utility methods for extracting data from XML documents using Xpath
44
41
* expressions.
@@ -50,24 +47,36 @@ public class XpathUtils {
50
47
/** The default property name to load the Xalan Document Builder Factory. */
51
48
private static final String DOCUMENT_BUILDER_FACTORY_PROP_NAME =
52
49
"javax.xml.parsers.DocumentBuilderFactory" ;
50
+ /** The FQCN of the desired DocumentBuilderFactory implementation. */
51
+ private static final String DOCUMENT_BUILDER_FACTORY_IMPL_CLASS_NAME =
52
+ "com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl" ;
53
+ /** The FQCN of the internal XPathContext class. */
54
+ private static final String XPATH_CONTEXT_CLASS_NAME =
55
+ "com.sun.org.apache.xpath.internal.XPathContext" ;
56
+ /** The FQCN of the desired DTMManager implementation. */
57
+ private static final String DTM_MANAGER_IMPL_CLASS_NAME =
58
+ "com.sun.org.apache.xml.internal.dtm.ref.DTMManagerDefault" ;
53
59
private static final Log log = LogFactory .getLog (XpathUtils .class );
54
60
55
61
/**
56
62
* Used to optimize performance by avoiding expensive file access every time
57
63
* a DTMManager is constructed as a result of constructing a Xalan xpath
58
64
* context!
59
65
*/
60
- private static void speedUpDTMManager () {
66
+ private static void speedUpDTMManager () throws Exception {
61
67
// https://github.com/aws/aws-sdk-java/issues/238
62
68
// http://stackoverflow.com/questions/6340802/java-xpath-apache-jaxp-implementation-performance
63
- String className = System .getProperty (DTM_MANAGER_DEFAULT_PROP_NAME );
64
- if (className == null ) {
65
- DTMManager dtmManager = new XPathContext ().getDTMManager ();
66
- if (dtmManager instanceof DTMManagerDefault ) {
69
+ if (System .getProperty (DTM_MANAGER_DEFAULT_PROP_NAME ) == null ) {
70
+ Class <?> XPathContextClass = Class .forName (XPATH_CONTEXT_CLASS_NAME );
71
+ Method getDTMManager = XPathContextClass .getMethod ("getDTMManager" );
72
+ Object XPathContext = XPathContextClass .newInstance ();
73
+ Object dtmManager = getDTMManager .invoke (XPathContext );
74
+
75
+ if (DTM_MANAGER_IMPL_CLASS_NAME .equals (dtmManager .getClass ().getName ())) {
67
76
// This would avoid
C222
the file system to be accessed every time
68
77
// the internal XPathContext is instantiated.
69
78
System .setProperty (DTM_MANAGER_DEFAULT_PROP_NAME ,
70
- DTMManagerDefault . class . getName () );
79
+ DTM_MANAGER_IMPL_CLASS_NAME );
71
80
}
72
81
}
73
82
}
@@ -78,14 +87,13 @@ private static void speedUpDTMManager() {
78
87
* Xalan document factory.
79
88
*/
80
89
private static void speedUpDcoumentBuilderFactory () {
81
- String className = System .getProperty (DOCUMENT_BUILDER_FACTORY_PROP_NAME );
82
- if (className == null ) {
90
+ if (System .getProperty (DOCUMENT_BUILDER_FACTORY_PROP_NAME ) == null ) {
83
91
DocumentBuilderFactory factory = DocumentBuilderFactory .newInstance ();
84
- if (factory instanceof DocumentBuilderFactoryImpl ) {
92
+ if (DOCUMENT_BUILDER_FACTORY_IMPL_CLASS_NAME . equals ( factory . getClass (). getName ()) ) {
85
93
// This would avoid the file system to be accessed every time
86
94
// the internal DocumentBuilderFactory is instantiated.
87
95
System .setProperty (DOCUMENT_BUILDER_FACTORY_PROP_NAME ,
88
- DocumentBuilderFactoryImpl . class . getName () );
96
+ DOCUMENT_BUILDER_FACTORY_IMPL_CLASS_NAME );
89
97
}
90
98
}
91
99
}
0 commit comments