1616import java .io .ByteArrayInputStream ;
1717import java .io .IOException ;
1818import java .io .InputStream ;
19+ import java .lang .reflect .Method ;
1920import java .net .URL ;
2021import java .nio .ByteBuffer ;
2122import java .util .Date ;
3435import org .w3c .dom .NodeList ;
3536import org .xml .sax .SAXException ;
3637
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-
4238/**
4339 * Utility methods for extracting data from XML documents using Xpath
4440 * expressions.
@@ -50,24 +46,36 @@ public class XpathUtils {
5046 /** The default property name to load the Xalan Document Builder Factory. */
5147 private static final String DOCUMENT_BUILDER_FACTORY_PROP_NAME =
5248 "javax.xml.parsers.DocumentBuilderFactory" ;
49+ /** The FQCN of the desired DocumentBuilderFactory implementation. */
50+ private static final String DOCUMENT_BUILDER_FACTORY_IMPL_CLASS_NAME =
51+ "com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl" ;
52+ /** The FQCN of the internal XPathContext class. */
53+ private static final String XPATH_CONTEXT_CLASS_NAME =
54+ "com.sun.org.apache.xpath.internal.XPathContext" ;
55+ /** The FQCN of the desired DTMManager implementation. */
56+ private static final String DTM_MANAGER_IMPL_CLASS_NAME =
57+ "com.sun.org.apache.xml.internal.dtm.ref.DTMManagerDefault" ;
5358 private static final Log log = LogFactory .getLog (XpathUtils .class );
5459
5560 /**
5661 * Used to optimize performance by avoiding expensive file access every time
5762 * a DTMManager is constructed as a result of constructing a Xalan xpath
5863 * context!
5964 */
60- private static void speedUpDTMManager () {
65+ private static void speedUpDTMManager () throws Exception {
6166 // https://github.com/aws/aws-sdk-java/issues/238
6267 // 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 ) {
68+ if (System .getProperty (DTM_MANAGER_DEFAULT_PROP_NAME ) == null ) {
69+ Class <?> XPathContextClass = Class .forName (XPATH_CONTEXT_CLASS_NAME );
70+ Method getDTMManager = XPathContextClass .getMethod ("getDTMManager" );
71+ Object XPathContext = XPathContextClass .newInstance ();
72+ Object dtmManager = getDTMManager .invoke (XPathContext );
73+
74+ if (DTM_MANAGER_IMPL_CLASS_NAME .equals (dtmManager .getClass ().getName ())) {
6775 // This would avoid the file system to be accessed every time
6876 // the internal XPathContext is instantiated.
6977 System .setProperty (DTM_MANAGER_DEFAULT_PROP_NAME ,
70- DTMManagerDefault . class . getName () );
78+ DTM_MANAGER_IMPL_CLASS_NAME );
7179 }
7280 }
7381 }
@@ -78,14 +86,13 @@ private static void speedUpDTMManager() {
7886 * Xalan document factory.
7987 */
8088 private static void speedUpDcoumentBuilderFactory () {
81- String className = System .getProperty (DOCUMENT_BUILDER_FACTORY_PROP_NAME );
82- if (className == null ) {
89+ if (System .getProperty (DOCUMENT_BUILDER_FACTORY_PROP_NAME ) == null ) {
8390 DocumentBuilderFactory factory = DocumentBuilderFactory .newInstance ();
84- if (factory instanceof DocumentBuilderFactoryImpl ) {
91+ if (DOCUMENT_BUILDER_FACTORY_IMPL_CLASS_NAME . equals ( factory . getClass (). getName ()) ) {
8592 // This would avoid the file system to be accessed every time
8693 // the internal DocumentBuilderFactory is instantiated.
8794 System .setProperty (DOCUMENT_BUILDER_FACTORY_PROP_NAME ,
88- DocumentBuilderFactoryImpl . class . getName () );
95+ DOCUMENT_BUILDER_FACTORY_IMPL_CLASS_NAME );
8996 }
9097 }
9198 }
0 commit comments