8027201: Enhance JAX-P set up
authorjoehw
Thu, 14 Nov 2013 10:18:48 -0800 (2013-11-14)
changeset 22417 278570df46ad
parent 22416 5473b73ce396
child 22418 1ee8fd0184d1
8027201: Enhance JAX-P set up Reviewed-by: alanb, dfuchs, lancea, hawtin
jaxp/src/com/sun/org/apache/xalan/internal/lib/ExsltStrings.java
jaxp/src/com/sun/org/apache/xalan/internal/lib/Extensions.java
--- a/jaxp/src/com/sun/org/apache/xalan/internal/lib/ExsltStrings.java	Tue Oct 22 13:15:50 2013 -0700
+++ b/jaxp/src/com/sun/org/apache/xalan/internal/lib/ExsltStrings.java	Thu Nov 14 10:18:48 2013 -0800
@@ -227,7 +227,7 @@
         token = str.substring(fromIndex);
       }
 
-      Document doc = DocumentHolder.m_doc;
+      Document doc = getDocument();
       synchronized (doc)
       {
         Element element = doc.createElement("token");
@@ -291,7 +291,7 @@
     {
       StringTokenizer lTokenizer = new StringTokenizer(toTokenize, delims);
 
-      Document doc = DocumentHolder.m_doc;
+      Document doc = getDocument();
       synchronized (doc)
       {
         while (lTokenizer.hasMoreTokens())
@@ -307,7 +307,7 @@
     else
     {
 
-      Document doc = DocumentHolder.m_doc;
+      Document doc = getDocument();
       synchronized (doc)
       {
         for (int i = 0; i < toTokenize.length(); i++)
@@ -329,35 +329,23 @@
   {
     return tokenize(toTokenize, " \t\n\r");
   }
+
     /**
-     * This class is not loaded until first referenced (see Java Language
-     * Specification by Gosling/Joy/Steele, section 12.4.1)
-     *
-     * The static members are created when this class is first referenced, as a
-     * lazy initialization not needing checking against null or any
-     * synchronization.
-     *
+   * @return an instance of DOM Document
      */
-    private static class DocumentHolder
-    {
-        // Reuse the Document object to reduce memory usage.
-        private static final Document m_doc;
-        static {
-            try
-            {
-                if (System.getSecurityManager() == null) {
-                    m_doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
-                } else {
-                    m_doc = DocumentBuilderFactory.newInstance(JDK_DEFAULT_DOM, null).newDocumentBuilder().newDocument();
-                }
+   private static Document getDocument()
+   {
+        try
+        {
+            if (System.getSecurityManager() == null) {
+                return DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
+            } else {
+                return DocumentBuilderFactory.newInstance(JDK_DEFAULT_DOM, null).newDocumentBuilder().newDocument();
             }
-
-            catch(ParserConfigurationException pce)
-            {
-                  throw new com.sun.org.apache.xml.internal.utils.WrappedRuntimeException(pce);
-            }
-
+        }
+        catch(ParserConfigurationException pce)
+        {
+            throw new com.sun.org.apache.xml.internal.utils.WrappedRuntimeException(pce);
         }
     }
-
 }
--- a/jaxp/src/com/sun/org/apache/xalan/internal/lib/Extensions.java	Tue Oct 22 13:15:50 2013 -0700
+++ b/jaxp/src/com/sun/org/apache/xalan/internal/lib/Extensions.java	Thu Nov 14 10:18:48 2013 -0800
@@ -115,23 +115,14 @@
 
       // This no longer will work right since the DTM.
       // Document myDoc = myProcessor.getContextNode().getOwnerDocument();
-      try
-      {
-        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
-        DocumentBuilder db = dbf.newDocumentBuilder();
-        Document myDoc = db.newDocument();
+      Document myDoc = getDocument();
 
         Text textNode = myDoc.createTextNode(textNodeValue);
         DocumentFragment docFrag = myDoc.createDocumentFragment();
 
         docFrag.appendChild(textNode);
 
-        return new NodeSet(docFrag);
-      }
-      catch(ParserConfigurationException pce)
-      {
-        throw new com.sun.org.apache.xml.internal.utils.WrappedRuntimeException(pce);
-      }
+      return new NodeSet(docFrag);
     }
   }
 
@@ -250,8 +241,7 @@
   public static NodeList tokenize(String toTokenize, String delims)
   {
 
-    Document doc = DocumentHolder.m_doc;
-
+    Document doc = getDocument();
 
     StringTokenizer lTokenizer = new StringTokenizer(toTokenize, delims);
     NodeSet resultSet = new NodeSet();
@@ -309,17 +299,7 @@
   public static Node checkEnvironment(ExpressionContext myContext)
   {
 
-    Document factoryDocument;
-    try
-    {
-      DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
-      DocumentBuilder db = dbf.newDocumentBuilder();
-      factoryDocument = db.newDocument();
-    }
-    catch(ParserConfigurationException pce)
-    {
-      throw new com.sun.org.apache.xml.internal.utils.WrappedRuntimeException(pce);
-    }
+    Document factoryDocument = getDocument();
 
     Node resultNode = null;
     try
@@ -392,34 +372,21 @@
   }
 
     /**
-     * This class is not loaded until first referenced (see Java Language
-     * Specification by Gosling/Joy/Steele, section 12.4.1)
-     *
-     * The static members are created when this class is first referenced, as a
-     * lazy initialization not needing checking against null or any
-     * synchronization.
-     *
+   * @return an instance of DOM Document
      */
-    private static class DocumentHolder
-    {
-        // Reuse the Document object to reduce memory usage.
-        private static final Document m_doc;
-        static
+   private static Document getDocument()
+   {
+        try
         {
-            try
-            {
-                if (System.getSecurityManager() == null) {
-                    m_doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
-                } else {
-                    m_doc = DocumentBuilderFactory.newInstance(JDK_DEFAULT_DOM, null).newDocumentBuilder().newDocument();
-                }
+            if (System.getSecurityManager() == null) {
+                return DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
+            } else {
+                return DocumentBuilderFactory.newInstance(JDK_DEFAULT_DOM, null).newDocumentBuilder().newDocument();
             }
-
-            catch(ParserConfigurationException pce)
-            {
-                  throw new com.sun.org.apache.xml.internal.utils.WrappedRuntimeException(pce);
-            }
-
+        }
+        catch(ParserConfigurationException pce)
+        {
+            throw new com.sun.org.apache.xml.internal.utils.WrappedRuntimeException(pce);
         }
     }
 }