You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@xerces.apache.org by mr...@apache.org on 2005/11/25 07:13:16 UTC

svn commit: r348887 - in /xerces/java/trunk/src/org/apache/xerces/impl/xs: SchemaGrammar.java XSAnnotationImpl.java

Author: mrglavas
Date: Thu Nov 24 22:13:12 2005
New Revision: 348887

URL: http://svn.apache.org/viewcvs?rev=348887&view=rev
Log:
Improvement for writing annotations:
- IntegratedParserConfiguration is ancient and slow. Use the better performing XML11Configuration.
- Parser instances use quite a bit of memory. Store them in SoftReferences so they can be
  reclaimed if there is high demand for memory.
- Use adopt node when possible to avoid creating a copy of the DOM before attaching it to the target.
- Turn off the deferred DOM feature. There's no benefit to having it on since the target 
  is another document. We would end up traversing the entire DOM even when calling adoptNode.

Modified:
    xerces/java/trunk/src/org/apache/xerces/impl/xs/SchemaGrammar.java
    xerces/java/trunk/src/org/apache/xerces/impl/xs/XSAnnotationImpl.java

Modified: xerces/java/trunk/src/org/apache/xerces/impl/xs/SchemaGrammar.java
URL: http://svn.apache.org/viewcvs/xerces/java/trunk/src/org/apache/xerces/impl/xs/SchemaGrammar.java?rev=348887&r1=348886&r2=348887&view=diff
==============================================================================
--- xerces/java/trunk/src/org/apache/xerces/impl/xs/SchemaGrammar.java (original)
+++ xerces/java/trunk/src/org/apache/xerces/impl/xs/SchemaGrammar.java Thu Nov 24 22:13:12 2005
@@ -1,5 +1,5 @@
 /*
- * Copyright 2001-2004 The Apache Software Foundation.
+ * Copyright 2001-2005 The Apache Software Foundation.
  * 
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -16,6 +16,7 @@
 
 package org.apache.xerces.impl.xs;
 
+import java.lang.ref.SoftReference;
 import java.util.Vector;
 
 import org.apache.xerces.impl.Constants;
@@ -29,8 +30,8 @@
 import org.apache.xerces.impl.xs.util.XSNamedMapImpl;
 import org.apache.xerces.impl.xs.util.XSObjectListImpl;
 import org.apache.xerces.parsers.DOMParser;
-import org.apache.xerces.parsers.IntegratedParserConfiguration;
 import org.apache.xerces.parsers.SAXParser;
+import org.apache.xerces.parsers.XML11Configuration;
 import org.apache.xerces.util.SymbolHash;
 import org.apache.xerces.util.SymbolTable;
 import org.apache.xerces.xni.NamespaceContext;
@@ -51,6 +52,7 @@
 import org.apache.xerces.xs.XSParticle;
 import org.apache.xerces.xs.XSTypeDefinition;
 import org.apache.xerces.xs.XSWildcard;
+import org.xml.sax.SAXException;
 
 /**
  * This class is to hold all schema component declaration that are declared
@@ -95,8 +97,8 @@
     // symbol table for constructing parsers (annotation support)
     private SymbolTable fSymbolTable = null;
     // parsers for annotation support
-    private SAXParser fSAXParser = null;
-    private DOMParser fDOMParser = null;
+    private SoftReference fSAXParser = null;
+    private SoftReference fDOMParser = null;
 
     //
     // Constructors
@@ -985,31 +987,48 @@
 
     // annotation support
     synchronized DOMParser getDOMParser() {
-        if (fDOMParser != null) return fDOMParser;
+        if (fDOMParser != null) {
+            DOMParser parser = (DOMParser) fDOMParser.get();
+            if (parser != null) {
+                return parser;
+            }
+        }
         // REVISIT:  when schema handles XML 1.1, will need to 
         // revisit this (and the practice of not prepending an XML decl to the annotation string
-        IntegratedParserConfiguration config = new IntegratedParserConfiguration(fSymbolTable);
+        XML11Configuration config = new XML11Configuration(fSymbolTable);
         // note that this should never produce errors or require
         // entity resolution, so just a barebones configuration with
         // a couple of feature  set will do fine
         config.setFeature(Constants.SAX_FEATURE_PREFIX + Constants.NAMESPACES_FEATURE, true);
         config.setFeature(Constants.SAX_FEATURE_PREFIX + Constants.VALIDATION_FEATURE, false);
-        fDOMParser = new DOMParser(config);
-        return fDOMParser;
+        
+        DOMParser parser = new DOMParser(config);
+        try {
+            parser.setFeature(Constants.XERCES_FEATURE_PREFIX + Constants.DEFER_NODE_EXPANSION_FEATURE, false);
+        }
+        catch (SAXException exc) {}
+        fDOMParser = new SoftReference(parser);
+        return parser;
     }
 
     synchronized SAXParser getSAXParser() {
-        if (fSAXParser != null) return fSAXParser;
+        if (fSAXParser != null) {
+            SAXParser parser = (SAXParser) fSAXParser.get();
+            if (parser != null) {
+                return parser;
+            }
+        }
         // REVISIT:  when schema handles XML 1.1, will need to 
         // revisit this (and the practice of not prepending an XML decl to the annotation string
-        IntegratedParserConfiguration config = new IntegratedParserConfiguration(fSymbolTable);
+        XML11Configuration config = new XML11Configuration(fSymbolTable);
         // note that this should never produce errors or require
         // entity resolution, so just a barebones configuration with
         // a couple of feature  set will do fine
         config.setFeature(Constants.SAX_FEATURE_PREFIX + Constants.NAMESPACES_FEATURE, true);
         config.setFeature(Constants.SAX_FEATURE_PREFIX + Constants.VALIDATION_FEATURE, false);
-        fSAXParser = new SAXParser(config);
-        return fSAXParser;
+        SAXParser parser = new SAXParser(config);
+        fSAXParser = new SoftReference(parser);
+        return parser;
     }
 
     /**

Modified: xerces/java/trunk/src/org/apache/xerces/impl/xs/XSAnnotationImpl.java
URL: http://svn.apache.org/viewcvs/xerces/java/trunk/src/org/apache/xerces/impl/xs/XSAnnotationImpl.java?rev=348887&r1=348886&r2=348887&view=diff
==============================================================================
--- xerces/java/trunk/src/org/apache/xerces/impl/xs/XSAnnotationImpl.java (original)
+++ xerces/java/trunk/src/org/apache/xerces/impl/xs/XSAnnotationImpl.java Thu Nov 24 22:13:12 2005
@@ -1,5 +1,5 @@
 /*
- * Copyright 2001-2004 The Apache Software Foundation.
+ * Copyright 2001-2005 The Apache Software Foundation.
  * 
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -18,6 +18,8 @@
 import org.apache.xerces.xs.XSAnnotation;
 import org.apache.xerces.xs.XSConstants;
 import org.apache.xerces.xs.XSNamespaceItem;
+import org.apache.xerces.dom.CoreDocumentImpl;
+import org.apache.xerces.dom.PSVIDocumentImpl;
 import org.apache.xerces.parsers.SAXParser;
 import org.apache.xerces.parsers.DOMParser;
 
@@ -141,23 +143,37 @@
 
     // this creates the new Annotation element as the first child
     // of the Node
-    private synchronized void writeToDOM(Node target, short type){
-        Document futureOwner = (type == XSAnnotation.W3C_DOM_ELEMENT)?target.getOwnerDocument():(Document)target;
+    private synchronized void writeToDOM(Node target, short type) {
+        Document futureOwner = (type == XSAnnotation.W3C_DOM_ELEMENT) ? 
+                target.getOwnerDocument() : (Document)target;
         DOMParser parser = fGrammar.getDOMParser();
         StringReader aReader = new StringReader(fData);
         InputSource aSource = new InputSource(aReader);
         try {
             parser.parse(aSource);
-        } catch (SAXException e) {
+        } 
+        catch (SAXException e) {
             // this should never happen!
             // REVISIT:  what to do with this?; should really not
             // eat it...
-        } catch (IOException i) {
+        } 
+        catch (IOException i) {
             // ditto with above
         }
         Document aDocument = parser.getDocument();
         Element annotation = aDocument.getDocumentElement();
-        Node newElem = futureOwner.importNode(annotation, true);
+        Node newElem = null;
+        if (futureOwner instanceof CoreDocumentImpl &&
+            !(futureOwner instanceof PSVIDocumentImpl)) {
+            newElem = futureOwner.adoptNode(annotation);
+            // this should never fail but if it does, import the node instead
+            if (newElem == null) {
+                newElem = futureOwner.importNode(annotation, true);
+            }
+        }
+        else {
+            newElem = futureOwner.importNode(annotation, true);
+        }
         target.insertBefore(newElem, target.getFirstChild());
     }
 



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@xerces.apache.org
For additional commands, e-mail: commits-help@xerces.apache.org