Prepares SAXParserFactory one time, so avoids expensive ClassLoader lokups.

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

Review by: scottb@google.com

git-svn-id: https://google-web-toolkit.googlecode.com/svn/trunk@10187 8db76d5a-ed1c-0410-87a9-c151d255dfc7
diff --git a/dev/core/src/com/google/gwt/dev/util/xml/ReflectiveParser.java b/dev/core/src/com/google/gwt/dev/util/xml/ReflectiveParser.java
index db07218..e75b9b5 100644
--- a/dev/core/src/com/google/gwt/dev/util/xml/ReflectiveParser.java
+++ b/dev/core/src/com/google/gwt/dev/util/xml/ReflectiveParser.java
@@ -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);