xerces-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mrgla...@apache.org
Subject svn commit: r695558 - /xerces/java/trunk/src/org/apache/xerces/dom/CoreDocumentImpl.java
Date Mon, 15 Sep 2008 17:39:44 GMT
Author: mrglavas
Date: Mon Sep 15 10:39:43 2008
New Revision: 695558

URL: http://svn.apache.org/viewvc?rev=695558&view=rev
Log:
JIRA Issue #1298:
http://issues.apache.org/jira/browse/XERCESJ-1298

Store user data in a WeakHashMap to allow it and the node associated with it
to be garbage collected if no other references exist for the node. Thanks to
Ludger B√ľnger for the patch. I made a few modifications and added readObject(),
writeObject() methods to preserve object serialization compatibility with
previous versions of Xerces.

Modified:
    xerces/java/trunk/src/org/apache/xerces/dom/CoreDocumentImpl.java

Modified: xerces/java/trunk/src/org/apache/xerces/dom/CoreDocumentImpl.java
URL: http://svn.apache.org/viewvc/xerces/java/trunk/src/org/apache/xerces/dom/CoreDocumentImpl.java?rev=695558&r1=695557&r2=695558&view=diff
==============================================================================
--- xerces/java/trunk/src/org/apache/xerces/dom/CoreDocumentImpl.java (original)
+++ xerces/java/trunk/src/org/apache/xerces/dom/CoreDocumentImpl.java Mon Sep 15 10:39:43
2008
@@ -17,9 +17,15 @@
 
 package org.apache.xerces.dom;
 
+import java.io.IOException;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
 import java.lang.reflect.Constructor;
 import java.util.Enumeration;
 import java.util.Hashtable;
+import java.util.Map;
+import java.util.WeakHashMap;
+
 import org.apache.xerces.util.URI;
 
 import org.w3c.dom.DOMConfiguration;
@@ -124,10 +130,8 @@
     /**Experimental DOM Level 3 feature: documentURI */
     protected String fDocumentURI;
 
-	//Revisit :: change to a better data structure.
     /** Table for user data attached to this document nodes. */
-    protected Hashtable userData;
-
+    protected Map userData;
 
     /** Identifiers. */
     protected Hashtable identifiers;
@@ -1765,26 +1769,28 @@
         
         // Return null if the source is null
         
-        if (source == null ) {
+        if (source == null) {
         	return null;
-        } else if (source != null && source.getOwnerDocument() != null) {
+        } 
+        else if (source != null && source.getOwnerDocument() != null) {
 
             DOMImplementation thisImpl = this.getImplementation();
             DOMImplementation otherImpl = source.getOwnerDocument().getImplementation();
             
             // when the source node comes from a different implementation.
             if (thisImpl != otherImpl) {
-            
-                // Adopting from a DefferedDOM to DOM
+                // Adopting from a deferred DOM to a non-deferred DOM
                 if (thisImpl instanceof org.apache.xerces.dom.DOMImplementationImpl &&
                         otherImpl instanceof org.apache.xerces.dom.DeferredDOMImplementationImpl)
{
-                    // traverse the DOM and expand deffered nodes and then allow adoption
+                    // traverse the DOM and expand deferred nodes and then allow adoption
                     undeferChildren (node);
-                } else if ( thisImpl instanceof org.apache.xerces.dom.DeferredDOMImplementationImpl
+                } 
+                else if ( thisImpl instanceof org.apache.xerces.dom.DeferredDOMImplementationImpl
                         && otherImpl instanceof org.apache.xerces.dom.DOMImplementationImpl)
{
-                    // Adopting from a DOM into a DefferedDOM, this should be okay
-                } else {
-                    // Adopting between two dissimilar DOM's is not allowed
+                    // Adopting from a non-deferred DOM into a deferred DOM, this should
be okay
+                } 
+                else {
+                    // Adopting between two dissimilar DOMs is not allowed
                     return null;  
                 }
         	}
@@ -2332,7 +2338,7 @@
         else {
             Hashtable t;
             if (userData == null) {
-                userData = new Hashtable();
+                userData = new WeakHashMap();
                 t = new Hashtable();
                 userData.put(n, t);
             }
@@ -2408,8 +2414,9 @@
      * @param data The user data table.
      */
     void setUserDataTable(Node n, Hashtable data) {
-		if (userData == null)
-			userData = new Hashtable();
+        if (userData == null) {
+            userData = new WeakHashMap();
+        }
         if (data != null) {
             userData.put(n, data);
         }
@@ -2754,5 +2761,37 @@
      */
     void renamedElement(Element oldEl, Element newEl) {
     }
+    
+    /**
+     * Serialized form of user data is a Hashtable. 
+     * Convert it into a WeakHashMap on load.
+     */
+    private void readObject(ObjectInputStream in)
+        throws IOException, ClassNotFoundException {
+        in.defaultReadObject();
+        if (userData != null) {
+            userData = new WeakHashMap(userData);
+        }
+    }
+    
+    /**
+     * To allow DOM trees serialized by newer versions of Xerces
+     * to be read by older versions briefly move the user data
+     * into a Hashtable.
+     */
+    private void writeObject(ObjectOutputStream out) throws IOException {
+        final Map oldUserData = this.userData;
+        try {
+            if (oldUserData != null) {
+                this.userData = new Hashtable(oldUserData);
+            } 
+            out.defaultWriteObject();
+        }
+        // If the write fails for some reason ensure 
+        // that we restore the original object.
+        finally {
+            this.userData = oldUserData;
+        }
+    }
 
 } // class CoreDocumentImpl



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


Mime
View raw message