[jdom-interest] bug in Element.getCopy ?

Jochen Strunk strunk at signal7.de
Tue Feb 27 02:53:28 PST 2001


Hi,

i think there is a bug in Element.getCopy. It doesn' t deep copy the 
namespace, which makes it difficult to change the default namespace of an 
element. The patch below solves the problem for me.

jochen

--- org/jdom/Element.java~      Tue Feb 27 11:29:51 2001
+++ org/jdom/Element.java       Mon Feb 26 13:51:34 2001
@@ -204,10 +204,10 @@
       * @return <code>Element</code> copy of this <code>Element</code>.
       */
      public Element getCopy(String name, Namespace ns) {
-        Element clone = (Element)clone();
+        Element clone = clone(ns);
          clone.namespace = ns;
          clone.name = name;
-
+
          return clone;
      }

@@ -1510,8 +1510,11 @@
       * @return the clone of this element
       */
      public Object clone() {
+       return clone(namespace);
+    }
+
+    private Element clone(Namespace namespace) {
          Element element = new Element(name, namespace);
-
          if (attributes != null) {
              List list = new LinkedList();
              for (Iterator i = attributes.iterator(); i.hasNext(); ) {
@@ -1529,7 +1532,7 @@
                  } else if (obj instanceof Entity) {
                      element.addContent((Entity)((Entity)obj).clone());
                  } else if (obj instanceof Element) {
-                    element.addContent((Element)((Element)obj).clone());
+                    element.addContent(((Element)obj).clone(namespace));
                  } else if (obj instanceof CDATA) {
                      element.addContent((CDATA)((CDATA)obj).clone());
                  }
@@ -1548,6 +1551,7 @@
          return element;
      }

+
      // Support a custom Namespace serialization so no two namespace
      // object instances may exist for the same prefix/uri pair
      private void writeObject(ObjectOutputStream out) throws IOException 
{



(-) Jochen Strunk
(-) SIGNAL 7 Gesellschaft für Informationstechnologie mbH
(-) Brüder-Knauß-Str. 79 - 64285 Darmstadt,
(-) Tel: 06151 665402, Fax: 06151 665373
(-) strunk at signal7.de, www.signal7.de




More information about the jdom-interest mailing list