[jdom-interest] clone() catinates neighbouring String components

Rotan Hanrahan rotan at eircom.net
Thu Oct 26 14:56:41 PDT 2000


I have been trying JDOM-b5 and observed that if an Element has neighbouring
String components and you clone the Element, the clone is not a true clone.
The clone will merge the two strings into one.

The following output and code demonstrates this behaviour. It should either
be documented as "by design" with an admission that "clone" is probably not
a good description, or it should be repaired. Does anyone have a comment on
this?

I use {braces} to delimit the strings.

---Rotan Hanrahan, Dublin, Ireland (rotan at ieee.org).

OUTPUT:
These strings should be equal:
{Some }
{Some ZZZ}
--ORIGINAL---------- 4 components
<body>Some ZZZ<b>inside</b> here</body>
--------------------
{Some }{ZZZ}{<b>inside</b>}{ here}
--CLONE------------- 3 components
<body>Some ZZZ<b>inside</b> here</body>
--------------------
{Some ZZZ}{<b>inside</b>}{ here}
--------------------


CODE:
import java.io.*;
import java.util.*;
import org.jdom.*;
import org.jdom.input.SAXBuilder;
import org.jdom.output.XMLOutputter;
public class JDOMtest {
	public static void main(String[] args) throws IOException, JDOMException {
		// original = <tag>Untagged <body>Some ZZZ <b>inside</b> here</body>more
text</tag>
		// The ZZZ text is inserted via add() to the live list from
getMixedContent().
		Element original = new Element("tag");
		original.addContent("Untagged ");
		Element body = new Element("body");
		body.addContent("Some ");
		Element b = new Element("b");
		b.addContent("inside");
		body.addContent(b);
		body.addContent(" here");
		original.addContent(body);
		original.addContent("more text");
		List mixed = body.getMixedContent();
		mixed.add(1,"ZZZ");
		// Now clone it and see what we get...
		Element originalbody = original.getChild("body");
		Object originalobj   = originalbody.getMixedContent().get(0);
		Element copy         = (Element)original.clone();
		Element copybody     = copy.getChild("body");
		Object copyobj       = copybody.getMixedContent().get(0);
		System.out.println("These strings should be equal:");
		System.out.println("{"+(String)originalobj +"}");
		System.out.println("{"+(String)copyobj+"}");
		XMLOutputter xo = new XMLOutputter();
		try {
			int s = originalbody.getMixedContent().size();
			System.out.println("\n--ORIGINAL---------- " + s + " components");
			xo.output(originalbody,System.out);
			System.out.println("\n--------------------");
			ListIterator c = originalbody.getMixedContent().listIterator();
			while (c.hasNext()) {
				System.out.print("{");
				Object o = c.next();
				if (o instanceof String) { xo.output((String)o,System.out); }
				else if (o instanceof Element) { xo.output((Element)o,System.out); }
				System.out.print("}");
			}
			s = copybody.getMixedContent().size();
			System.out.println("\n--CLONE------------- " + s + " components");
			xo.output(copybody,System.out);
			System.out.println("\n--------------------");
			ListIterator k = copybody.getMixedContent().listIterator();
			while (k.hasNext()) {
				System.out.print("{");
				Object o = k.next();
				if (o instanceof String) { xo.output((String)o,System.out); }
				else if (o instanceof Element) { xo.output((Element)o,System.out); }
				System.out.print("}");
			}
			System.out.println("\n--------------------");
		}
		catch (IOException ioe) {}
	}
}




More information about the jdom-interest mailing list