Revision: 10187
Author:   scheg...@google.com
Date:     Mon May 16 05:56:39 2011
Log: Prepares SAXParserFactory one time, so avoids expensive ClassLoader lokups.

Review at http://gwt-code-reviews.appspot.com/1442806

Review by: sco...@google.com
http://code.google.com/p/google-web-toolkit/source/detail?r=10187

Modified:
 /trunk/dev/core/src/com/google/gwt/dev/util/xml/ReflectiveParser.java

=======================================
--- /trunk/dev/core/src/com/google/gwt/dev/util/xml/ReflectiveParser.java Wed Aug 11 09:22:23 2010 +++ /trunk/dev/core/src/com/google/gwt/dev/util/xml/ReflectiveParser.java Mon May 16 05:56:39 2011
@@ -44,6 +44,26 @@
  * elements) is ignored, so think attributes.
  */
 public final class ReflectiveParser {
+
+  private static SAXParserFactory saxParserFactory;
+
+ private static synchronized SAXParser createNewSaxParser() throws ParserConfigurationException,
+      SAXException {
+    if (saxParserFactory == null) {
+      Thread currentThread = Thread.currentThread();
+      ClassLoader oldClassLoader = currentThread.getContextClassLoader();
+      try {
+        // use system ClassLoader to avoid using expensive GWT ClassLoader
+ currentThread.setContextClassLoader(ClassLoader.getSystemClassLoader());
+        saxParserFactory = SAXParserFactory.newInstance();
+        saxParserFactory.setFeature(
+ "http://apache.org/xml/features/nonvalidating/load-external-dtd";, false);
+      } finally {
+        currentThread.setContextClassLoader(oldClassLoader);
+      }
+    }
+    return saxParserFactory.newSAXParser();
+  }

   private static final class Impl extends DefaultHandler {

@@ -320,11 +340,7 @@
       Throwable caught = null;
       try {
         this.reader = reader;
-        SAXParserFactory factory = SAXParserFactory.newInstance();
-        factory.setFeature(
-            "http://apache.org/xml/features/nonvalidating/load-external-dtd";,
-            false);
-        SAXParser parser = factory.newSAXParser();
+        SAXParser parser = createNewSaxParser();
         InputSource inputSource = new InputSource(this.reader);
         XMLReader xmlReader = parser.getXMLReader();
         xmlReader.setContentHandler(this);

--
http://groups.google.com/group/Google-Web-Toolkit-Contributors

Reply via email to