[jdom-interest] Element clone() CDATA problem

Elliotte Rusty Harold elharo at metalab.unc.edu
Mon Jun 3 14:57:50 PDT 2002


The attached patch fixes a problem with the clone() method that would 
result in CDATA sections being cloned as Text objects rather than CDATA 
objects. It is now necessary to check for CDATA before checking for Text 
because CDATA is a subclass of Text.

It also corrects one minor spelling error in the comments.

-- 
+-----------------------+------------------------+-------------------+
| Elliotte Rusty Harold | elharo at metalab.unc.edu | Writer/Programmer |
+-----------------------+------------------------+-------------------+ 
|           The XML Bible, 2nd Edition (IDG Books, 2001)             |
|             http://www.cafeconleche.org/books/bible2/              |
|   http://www.amazon.com/exec/obidos/ISBN=0764547607/cafeaulaitA/   |
+----------------------------------+---------------------------------+
|  Read Cafe au Lait for Java News:   http://www.cafeaulait.org/     | 
|  Read Cafe con Leche for XML News:  http://www.cafeconleche.org/   |
+----------------------------------+---------------------------------+

-------------- next part --------------
Index: src/java/org/jdom/Element.java
===================================================================
RCS file: /home/cvspublic/jdom/src/java/org/jdom/Element.java,v
retrieving revision 1.124
diff -d -u -r1.124 Element.java
--- src/java/org/jdom/Element.java	2002/05/17 15:45:03	1.124
+++ src/java/org/jdom/Element.java	2002/06/04 06:03:41
@@ -1458,7 +1458,7 @@
             // Can't happen
         }
 
-        // name and namespace are references to imutable objects
+        // name and namespace are references to immutable objects
         // so super.clone() handles them ok
 
         // Reference to parent is copied by super.clone()
@@ -1486,16 +1486,16 @@
                 if (obj instanceof Element) {
                     Element elt = (Element)((Element)obj).clone();
                     element.content.add(elt);
+                }  else if (obj instanceof CDATA) {
+                    CDATA cdata = (CDATA)((CDATA)obj).clone();
+                    element.content.add(cdata);
                 } else if (obj instanceof Text) {
                     Text text = (Text)((Text)obj).clone();
                     element.content.add(text);
                 } else if (obj instanceof Comment) {
                     Comment comment = (Comment)((Comment)obj).clone();
                     element.content.add(comment);
-                } else if (obj instanceof CDATA) {
-                    CDATA cdata = (CDATA)((CDATA)obj).clone();
-                    element.content.add(cdata);
-                } else if (obj instanceof ProcessingInstruction) {
+                }else if (obj instanceof ProcessingInstruction) {
                     ProcessingInstruction pi = (ProcessingInstruction)
                         ((ProcessingInstruction)obj).clone();
                     element.content.add(pi);


More information about the jdom-interest mailing list