jakarta-ecs-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rdon...@apache.org
Subject svn commit: r680091 - /jakarta/ecs/trunk/src/java/org/apache/ecs/ConcreteElement.java
Date Sun, 27 Jul 2008 11:29:18 GMT
Author: rdonkin
Date: Sun Jul 27 04:29:18 2008
New Revision: 680091

URL: http://svn.apache.org/viewvc?rev=680091&view=rev
Log:
Bug fix ECS-1 problem with hashcodes on 64bit JVMs https://issues.apache.org/jira/browse/ECS-1.
Contributed by Mauro Manfrin.

Modified:
    jakarta/ecs/trunk/src/java/org/apache/ecs/ConcreteElement.java

Modified: jakarta/ecs/trunk/src/java/org/apache/ecs/ConcreteElement.java
URL: http://svn.apache.org/viewvc/jakarta/ecs/trunk/src/java/org/apache/ecs/ConcreteElement.java?rev=680091&r1=680090&r2=680091&view=diff
==============================================================================
--- jakarta/ecs/trunk/src/java/org/apache/ecs/ConcreteElement.java (original)
+++ jakarta/ecs/trunk/src/java/org/apache/ecs/ConcreteElement.java Sun Jul 27 04:29:18 2008
@@ -38,11 +38,11 @@
 */
 public class ConcreteElement extends ElementAttributes implements Cloneable
 {
-	/** The line separator to use for pretty printing */
-	private static String lineSeparator = System.getProperty("line.separator");
+    /** The line separator to use for pretty printing */
+    private static String lineSeparator = System.getProperty("line.separator");
 
     /** @serial registry registry */
-    private Hashtable registry = new Hashtable(4); // keep a list of elements that need to
be added to the element
+    private Hashtable registry; // keep a list of elements that need to be added to the element
     /** Maintain an ordered list of elements */
     private Vector registryList = new Vector(2);
 
@@ -50,17 +50,41 @@
     {
     }
 
+    private static final String HASHCODE_AUTOGENERATED="org.apache.ecs.#@HcAg@#";
+    /** Utility to test for autogenerated key names
+        @param the key as given by the keys() enumeration.
+    */
+    private boolean isAutoGenerated(String name) {
+        return (name!=null) && name.startsWith(HASHCODE_AUTOGENERATED);
+    }
+    
+    /** Utility to search for elements with autogenerated keys in the registryList
+        @param the key as given by the keys() enumeration.
+    */
+    
+    private ConcreteElement searchAutoGenerated(String name) {
+        int hashcode=Integer.parseInt(name.substring(HASHCODE_AUTOGENERATED.length()));
+        Object o;
+        for (int i=0;i<registryList.size();i++) {
+            o=registryList.get(i);
+            if (o!=null && o.hashCode()==hashcode) return (ConcreteElement)o;
+        }
+        return null;
+    }
+    
     /**
         If the object is in the registry return otherwise return null.
         @param element the name of the object to locate.
     */
-    public ConcreteElement getElement(String element)
+    public ConcreteElement getElement(String name)
     {
-        if(registry.containsKey(element))
-        {
-            return (ConcreteElement)registry.get(element);
+        if (isAutoGenerated(name)) {
+            return searchAutoGenerated(name);
+        }
+        else {
+            if (registry==null) return null;
+            return (ConcreteElement)registry.get(name);
         }
-        return null;
     }
 
     /**
@@ -71,7 +95,7 @@
     {
         if ( element == null )
             return(this);
-        addElementToRegistry(Integer.toString(element.hashCode()),element);
+        addElementToRegistry(null,element);
         return(this);
     }
 
@@ -80,17 +104,20 @@
         @param   hashcode internal name of element
         @param   element element to be added to the registry.
     */
-    public Element addElementToRegistry(String hashcode,Element element)
+    public Element addElementToRegistry(String name,Element element)
     {
-        if ( hashcode == null || element == null )
+        if ( element == null )
             return(this);
 
          element.setFilterState(getFilterState());
          if(ECSDefaults.getDefaultPrettyPrint() != element.getPrettyPrint())
               element.setPrettyPrint(getPrettyPrint());
-         registry.put(hashcode,element);
-         if(!registryList.contains(hashcode))
-            registryList.addElement(hashcode);
+         if (name!=null) {
+             if (registry==null) registry=new Hashtable(4);
+             registry.put(name,element);
+         }
+         if(!registryList.contains(element))
+            registryList.addElement(element);
          return(this);
     }
 
@@ -105,7 +132,7 @@
         if ( element == null )
             return(this);
         setFilterState(filter);
-        addElementToRegistry(Integer.toString(element.hashCode()),element);
+        addElementToRegistry(null,element);
         return(this);
     }
 
@@ -114,12 +141,12 @@
         @param   element element to be added to the registry.
         @param   filter  should we filter this element?
     */
-    public Element addElementToRegistry(String hashcode, Element element,boolean filter)
+    public Element addElementToRegistry(String name, Element element,boolean filter)
     {
-        if ( hashcode == null )
+        if ( name == null )
             return(this);
         setFilterState(filter);
-        addElementToRegistry(hashcode,element);
+        addElementToRegistry(name,element);
         return(this);
     }
 
@@ -133,7 +160,7 @@
         if ( value == null )
             return(this);
         setFilterState(filter);
-        addElementToRegistry(Integer.toString(value.hashCode()),value);
+        addElementToRegistry(value);
         return(this);
     }
 
@@ -143,12 +170,12 @@
         @param   element element to be added to the registry.
         @param   filter does this need to be filtered?
     */
-    public Element addElementToRegistry(String hashcode, String value,boolean filter)
+    public Element addElementToRegistry(String name, String value,boolean filter)
     {
-        if ( hashcode == null )
+        if ( name == null )
             return(this);
         setFilterState(filter);
-        addElementToRegistry(hashcode,value);
+        addElementToRegistry(name,value);
         return(this);
     }
 
@@ -168,9 +195,9 @@
         Registers an element in the head element list
         @param   element element to be added to the registry.
     */
-    public Element addElementToRegistry(String hashcode,String value)
+    public Element addElementToRegistry(String name,String value)
     {
-        if ( hashcode == null )
+        if ( name == null )
             return(this);
 
         // We do it this way so that filtering will work.
@@ -183,7 +210,7 @@
         se.setFilterState(getFilterState());
         se.setFilter(getFilter());
         se.setPrettyPrint(getPrettyPrint());
-        addElementToRegistry(hashcode,se);
+        addElementToRegistry(name,se);
         return(this);
     }
 
@@ -193,7 +220,17 @@
     */
     public Element removeElementFromRegistry(Element element)
     {
-        removeElementFromRegistry(Integer.toString(element.hashCode()));
+        boolean contained=registryList.remove(element);
+        if (contained && registry!=null) {
+            java.util.Iterator i=registry.entrySet().iterator();
+            java.util.Map.Entry me;
+            while (i.hasNext()) {
+                me=(java.util.Map.Entry)i.next();
+                if (element==me.getValue()) {
+                    registry.remove(me.getKey());
+                }
+            }
+        }
         return(this);
     }
 
@@ -201,10 +238,17 @@
         Removes an element from the head element registry
         @param   hashcode element to be added to the registry.
     */
-    public Element removeElementFromRegistry(String hashcode)
+    public Element removeElementFromRegistry(String name)
     {
-        registry.remove(hashcode);
-        registryList.removeElement(hashcode);
+        if (isAutoGenerated(name)) {
+            Object o=searchAutoGenerated(name);
+            if (o!=null) registryList.remove(o);
+        }
+        else {
+            if (registry==null) return this;
+            Object o=registry.remove(name);
+            if (o!=null) registryList.removeElement(o);
+        }
         return(this);
     }
 
@@ -214,38 +258,58 @@
     */
     public boolean registryHasElement(Element element)
     {
-        return(registry.contains(element));
+        return(registryList.contains(element));
     }
 
-	/**
-		Get the keys of this element.
-	*/
-	public Enumeration keys()
-	{
-		return(registryList.elements());
-	}
+    /**
+            Get the keys of this element.
+     * @deprecated
+    */
+    public Enumeration keys()
+    {
+        Vector v=new Vector(registryList.size());
+        for (int j=0;j<registryList.size();j++) {
+            v.add(HASHCODE_AUTOGENERATED+registryList.get(j).hashCode());
+        }
+        if (registry!=null) {
+            java.util.Iterator i=registry.entrySet().iterator();
+            while (i.hasNext()) {
+                java.util.Map.Entry me=(java.util.Map.Entry)i.next();
+                int j=registryList.indexOf(me.getValue());
+                if (j>=0) v.set(j,me.getKey());
+            }
+        }
+        return v.elements();
+    }
 
     /**
         Get an enumeration of the elements that this element contains.
     */
     public Enumeration elements()
     {
-        return(registry.elements());
+        return(registryList.elements());
     }
 
     /**
         Find out if this element is in the element registry.
         @param element find out if this element is in the registry
     */
-    public boolean registryHasElement(String hashcode)
+    public boolean registryHasElement(String name)
     {
-        return(registry.containsKey(hashcode));
+        if (isAutoGenerated(name)) {
+            Object o=searchAutoGenerated(name);
+            return o!=null;
+        }
+        else {
+            if (registry==null) return false;
+            return(registry.containsKey(name));
+        }
     }
-
+    
     /**
         Overload output(OutputStream).
         @param output OutputStream to write to.
-		@param ConcreteElement	Instance of ConcreteElement
+        @param ConcreteElement  Instance of ConcreteElement
     */
     public static void output(OutputStream out, ConcreteElement ce) 
     {
@@ -262,8 +326,8 @@
         int tabLevel = ce.getTabLevel();
         try
         {
-            if (ce.registry.size() == 0)
-            {	
+            if (ce.registryList.size() == 0)
+            {   
                 ce.output(out);
             }
             else
@@ -281,7 +345,7 @@
 
                 while(enum_.hasMoreElements())
                 {
-                    Object obj = ce.registry.get((String)enum_.nextElement());
+                    Object obj = (Object)enum_.nextElement();
                     if(obj instanceof GenericElement)
                     {
                         Element e = (Element)obj;
@@ -329,34 +393,34 @@
             ioe.printStackTrace(new PrintWriter(out));
         }
     }
-	
+    
     /**
         Override output(OutputStream) incase any elements are in the registry.
         @param output OutputStream to write to.
     */
     public void output(OutputStream out)
-	{
-		if (this.registry.size() == 0)
-		{
-				int tabLevel = getTabLevel();
-				if ((getPrettyPrint() && this instanceof Printable) && (tabLevel >
0))  
-				{
-					try 
-					{
-						this.putTabs(tabLevel, out);
-					}
-					catch(IOException ioe) 
-					{
-						ioe.printStackTrace(new PrintWriter(out));					
-					}
-				}
+    {
+        if (this.registryList.size() == 0)
+        {
+                int tabLevel = getTabLevel();
+                if ((getPrettyPrint() && this instanceof Printable) && (tabLevel
> 0))  
+                {
+                    try 
+                    {
+                        this.putTabs(tabLevel, out);
+                    }
+                    catch(IOException ioe) 
+                    {
+                        ioe.printStackTrace(new PrintWriter(out));                  
+                    }
+                }
                 super.output(out);
-		} 
-		else  
-		{
-			output(out,this);
-		}
-	}
+        } 
+        else  
+        {
+            output(out,this);
+        }
+    }
 
     /**
         Writer version of this method.
@@ -373,72 +437,72 @@
         @param output OutputStream to write to.
     */
     public void output(PrintWriter out)
-	{
-		boolean prettyPrint = getPrettyPrint();
-		int tabLevel = getTabLevel();
-		if (registry.size() == 0)
-		{
-			if ((prettyPrint && this instanceof Printable) && (tabLevel > 0))
-				putTabs(tabLevel, out);
-
-			super.output(out);
-		}
-		else
-		{
-			if ((prettyPrint && this instanceof Printable) && (tabLevel > 0))
-				putTabs(tabLevel, out);
+    {
+        boolean prettyPrint = getPrettyPrint();
+        int tabLevel = getTabLevel();
+        if (registryList.size() == 0)
+        {
+            if ((prettyPrint && this instanceof Printable) && (tabLevel >
0))
+                putTabs(tabLevel, out);
 
-			out.write(createStartTag());
+            super.output(out);
+        }
+        else
+        {
+            if ((prettyPrint && this instanceof Printable) && (tabLevel >
0))
+                putTabs(tabLevel, out);
+
+            out.write(createStartTag());
             // If this is a StringElement that has ChildElements still print the TagText
             if(getTagText() != null)
                 out.write(getTagText());
 
             Enumeration enum_ = registryList.elements();
-			while(enum_.hasMoreElements())
-			{
-				Object obj = registry.get((String)enum_.nextElement());
-				if(obj instanceof GenericElement)
-				{
-					Element e = (Element)obj;
-					if (prettyPrint && this instanceof Printable)
-					{
+            while(enum_.hasMoreElements())
+            {
+                Object obj = (Object)enum_.nextElement();
+                if(obj instanceof GenericElement)
+                {
+                    Element e = (Element)obj;
+                    if (prettyPrint && this instanceof Printable)
+                    {
                         if (getNeedLineBreak()) {
-							out.write(lineSeparator);
-							e.setTabLevel(tabLevel + 1);
-						}
-					}
-					e.output(out);
-				}
-				else
-				{
-					if (prettyPrint && this instanceof Printable)
-					{
+                            out.write(lineSeparator);
+                            e.setTabLevel(tabLevel + 1);
+                        }
+                    }
+                    e.output(out);
+                }
+                else
+                {
+                    if (prettyPrint && this instanceof Printable)
+                    {
                         if (getNeedLineBreak()) {
-							out.write(lineSeparator);
-							putTabs(tabLevel + 1, out);
-						}
-					}
-					String string = obj.toString();
-					if(getFilterState())
-						out.write(getFilter().process(string));
-					else
-						out.write(string);
-				}
-			}
-			if (getNeedClosingTag())
-			{
-				if (prettyPrint && this instanceof Printable)
-				{
+                            out.write(lineSeparator);
+                            putTabs(tabLevel + 1, out);
+                        }
+                    }
+                    String string = obj.toString();
+                    if(getFilterState())
+                        out.write(getFilter().process(string));
+                    else
+                        out.write(string);
+                }
+            }
+            if (getNeedClosingTag())
+            {
+                if (prettyPrint && this instanceof Printable)
+                {
                     if (getNeedLineBreak()) {
-						out.write(lineSeparator);
-						if (tabLevel > 0)
-							putTabs(tabLevel, out);
-					}
-				}
-			   out.write(createEndTag());
-			}
-		}
-	}
+                        out.write(lineSeparator);
+                        if (tabLevel > 0)
+                            putTabs(tabLevel, out);
+                    }
+                }
+               out.write(createEndTag());
+            }
+        }
+    }
 
     /**
         Allows all Elements the ability to be cloned.
@@ -475,4 +539,5 @@
     {
         return registryList.isEmpty();
     }
+    
 }



---------------------------------------------------------------------
To unsubscribe, e-mail: ecs-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: ecs-dev-help@jakarta.apache.org


Mime
View raw message