Return-Path: Delivered-To: apmail-xerces-commits-archive@www.apache.org Received: (qmail 76373 invoked from network); 15 Sep 2008 17:40:56 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.2) by minotaur.apache.org with SMTP; 15 Sep 2008 17:40:56 -0000 Received: (qmail 68089 invoked by uid 500); 15 Sep 2008 17:40:53 -0000 Delivered-To: apmail-xerces-commits-archive@xerces.apache.org Received: (qmail 68025 invoked by uid 500); 15 Sep 2008 17:40:53 -0000 Mailing-List: contact commits-help@xerces.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Delivered-To: mailing list commits@xerces.apache.org Received: (qmail 68016 invoked by uid 99); 15 Sep 2008 17:40:53 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 15 Sep 2008 10:40:53 -0700 X-ASF-Spam-Status: No, hits=-2000.0 required=10.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 15 Sep 2008 17:40:03 +0000 Received: by eris.apache.org (Postfix, from userid 65534) id ED45523889BB; Mon, 15 Sep 2008 10:40:05 -0700 (PDT) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Subject: svn commit: r695559 - /xerces/java/branches/xml-schema-1.1-dev/src/org/apache/xerces/dom/CoreDocumentImpl.java Date: Mon, 15 Sep 2008 17:40:05 -0000 To: commits@xerces.apache.org From: mrglavas@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20080915174005.ED45523889BB@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: mrglavas Date: Mon Sep 15 10:40:05 2008 New Revision: 695559 URL: http://svn.apache.org/viewvc?rev=695559&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/branches/xml-schema-1.1-dev/src/org/apache/xerces/dom/CoreDocumentImpl.java Modified: xerces/java/branches/xml-schema-1.1-dev/src/org/apache/xerces/dom/CoreDocumentImpl.java URL: http://svn.apache.org/viewvc/xerces/java/branches/xml-schema-1.1-dev/src/org/apache/xerces/dom/CoreDocumentImpl.java?rev=695559&r1=695558&r2=695559&view=diff ============================================================================== --- xerces/java/branches/xml-schema-1.1-dev/src/org/apache/xerces/dom/CoreDocumentImpl.java (original) +++ xerces/java/branches/xml-schema-1.1-dev/src/org/apache/xerces/dom/CoreDocumentImpl.java Mon Sep 15 10:40:05 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