[jdom-interest] Problem with the clone method in Document (latest version)

Richard McAneny rich at inskey.com
Wed Aug 23 16:24:59 PDT 2000


I have found a problem with the clone method in Document with the tip
version (and prior) of JDOM: In the clone method the root element is being
added twice.

It appears that in version 1.14 there was a change that is causing this:

1.13 looks like:

	  public final Object clone() {
      	  Document doc = new Document((Element)rootElement.clone());

	        for (Iterator i = content.iterator(); i.hasNext(); ) {
      	      Object obj = i.next();
            	if (obj instanceof Element) {
here******>>        continue;

			}
			(.....)


1.14 looks like:
	    public final Object clone() {
      	  Document doc = new Document((Element)rootElement.clone());

	        for (Iterator i = content.iterator(); i.hasNext(); ) {
      	      Object obj = i.next();
	            if (obj instanceof Element) {
here****>>>   	    Element e = (Element)obj;
	                doc.addContent((Element)e.clone());
      	      }
            	(....)


The root element is being cloned and added to the document 2 x.


Here is my sample code to see this happen.


	Element docRoot = new Element("test");
	Element child = new Element("child1");
	child.setText("hello");
	docRoot.addContent(child);
	Document document = new Document(docRoot);
	Document document2 = (Document) document.clone();


throws the following exception:
org.jdom.IllegalAddException: The element "test" could not be added as the
root of the document: The document already has a root element



Rich





More information about the jdom-interest mailing list