[jdom-interest] cloning!

Ken Rune Helland kenh at csc.no
Fri Mar 16 02:17:49 PST 2001


 From the todo:

* Fix bug with clone() that string concatenates: 
http://lists.denveronline.net/lists/jdom-interest/2000-October/003528.html 
This is due to addContent() being called in the clone and addContent() will 
merge neighboring strings. Probably clone should have a back-door that 
doesn't use the addContent() calls. Would be faster besides. See following 
item.

* Try clone() without the constructor call. See above item too. (Ken Helland)


Attaced diff for cloning metods with super().clone() insted of new.
Also now element clone doesent call addContent on cloned content but
calls setParent and directly inserts into list so above bug shoud also be 
fixed.

Patched based on sourcedrop downloaded friday.


Also noted that CDATA is imutable and have no parent, curently
Element.clone() clones CDATA. But if it is imutable this is not nessesarry,
CDATA shoud not even be clonable if its immutable.


KenR
  
-------------- next part --------------
2001-03-16 11:07 *e.java jdom\src\java\org\jdom/Attribute.java Page    1


410c410,422
<         Attribute attribute = new Attribute(name, value, namespace);
---
> 				Attribute attribute = null;
> 
> 				try{
> 					attribute = (Attribute) super.clone();
> 				}catch( CloneNotSupportedException ce ){
> 					// are cloneable this will never happen
> 				}
> 
> 				// name, namespace and value are references to imutable objects
> 				// and are copied by super.clone() ( Object.clone() )
> 
> 				// super.clone() copies reference to parent so it must be set to null
> 				attribute.parent = null;
2001-03-16 11:07 *CDATA.java jdom\src\java\org\jdom/CDATA.java Page    1


196a197,203
> 		/**
> 		 * <p>
> 		 *  This will return a clone of this <code>CDATA</code>.
> 		 * </p>
> 		 *
> 		 * @return <code>Object</code> - clone of this <code>CDATA</code>.
> 		 */
198,199c205,215
< 	CDATA clone = new CDATA(text);
< 	return clone;
---
> 			CDATA cdata = null;
> 			try{
> 				cdata = (CDATA) super.clone();
> 			}catch( CloneNotSupportedException ce ){
> 				// we are clonable so this will never happen
> 			}
> 
> 			// CDATA only contains a reference to a immutable (String) object
> 			// so Object.clone() did all we need here
> 
> 			return cdata;
2001-03-16 11:07 *ent.java jdom\src\java\org\jdom/Comment.java Page    1


285c285,300
<         Comment comment = new Comment(text);
---
> 				Comment comment = null;
> 
> 				try{
> 					comment = (Comment) super.clone();
> 				}catch(CloneNotSupportedException ce){
> 					// are cloneable, this will never happen
> 				}
> 
> 				// the text is a reference to a immutable String object
> 				// and is already copied by Object.clone();
> 
> 				// parent and document referneces is copied by Object.clone()
> 				// and must be set to null
> 				comment.parent = null;
> 				comment.document = null;
> 
2001-03-16 11:07 *ype.java jdom\src\java\org\jdom/DocType.java Page    1


297c297,307
<         DocType docType = new DocType(elementName, publicID, systemID);
---
> 				DocType docType = null;
> 
> 				try{
> 					docType = (DocType) super.clone();
> 				}catch(CloneNotSupportedException ce){
> 					// is clonable, will never happen
> 				}
> 
> 				// elementName, publicID and systemID are all
> 				// immutable (Strings) and references are copied
> 				// by Object.clone()
2001-03-16 11:07 *nt.java jdom\src\java\org\jdom/Document.java Page    1


572c572
<         Document doc = new Document((Element)null);
---
> 				Document doc = null;
573a574,587
> 				try{
> 					doc = (Document) super.clone();
> 				}catch(CloneNotSupportedException ce){
> 					// is clonable will never happen
> 				}
> 
> 				// the clone has a refernece to this objects content list
> 				// owerwrite with a empty list
> 				doc.content = new LinkedList();
> 
> 				// the clone has a reference to the original root, set to null
> 				doc.rootElement = null;
> 
> 				// add cloned content to clone
577,578c591
<                 Element e = (Element)obj;
<                 doc.setRootElement((Element)e.clone());
---
> 								doc.setRootElement((Element)((Element)obj).clone());
581,582c594
<                 Comment c = (Comment)obj;
<                 doc.addContent((Comment)c.clone());
---
> 								doc.addContent((Comment)((Comment)obj).clone());
585,586c597
<                 ProcessingInstruction pi = (ProcessingInstruction)obj;
<                 doc.addContent((ProcessingInstruction)pi.clone());
---
> 								doc.addContent((ProcessingInstruction)((ProcessingInstruction)obj).clone());
589a601
> 				// clone the docType
2001-03-16 11:07 *ent.java jdom\src\java\org\jdom/Element.java Page    1


1556c1556
<         Element element = new Element(name, namespace);
---
> 				Element element = null;
1557a1558,1577
> 				try{
> 					element = (Element) super.clone();
> 				}catch(CloneNotSupportedException ce){
> 					//we are clonable so this will never happen
> 				}
> 
> 				// name and namespace are references to imutable objects
> 				// so super.clone() handles them ok
> 
> 				// reference to parent and document are copied by super.clone() (Object.clone() )so we have to remove it
> 				element.parent = null;
> 				element.document = null;
> 
> 
> 				// reference til content list and attribute lists are copyed by super.clone()
> 				// so we sett it new lists if the original had lists
> 				element.content = (this.content == null)? null : new LinkedList();
> 				element.attributes = (this.attributes == null)? null : new LinkedList();
> 
> 				// cloning attributes
1559d1578
<             List list = new LinkedList();
1561c1580,1582
<                 element.addAttribute((Attribute)((Attribute)i.next()).clone());
---
> 								Attribute a = (Attribute)((Attribute)i.next()).clone();
> 								a.setParent(element);
> 								element.attributes.add(a);
1564a1586
> 				// cloning content
1569,1571c1591
<                     element.addContent((String)obj);
<                 } else if (obj instanceof Element) {
<                     element.addContent((Element)((Element)obj).clone());
---
> 										element.content.add(obj);
1573,1576c1593,1595
<                     element.addContent((Comment)((Comment)obj).clone());
<                 } else if (obj instanceof ProcessingInstruction) {
<                     element.addContent((ProcessingInstruction)
<                                         ((ProcessingInstruction)obj).clone());
---
> 										Comment c = (Comment)((Comment)obj).clone();
> 										c.setParent(element);
> 										element.content.add(c);
1578c1597,1603
<                     element.addContent((Entity)((Entity)obj).clone());
---
> 										Entity e = (Entity)((Entity)obj).clone();
> 										e.setParent(element);
> 										element.content.add(e);
> 								} else if (obj instanceof Element) {
> 										Element e = (Element)((Element)obj).clone();
> 										e.setParent(element);
> 										element.content.add(e);
1580c1605,1608
<                     element.addContent((CDATA)((CDATA)obj).clone());
---
2001-03-16 11:07 *ent.java jdom\src\java\org\jdom/Element.java Page    2


> 										element.content.add((CDATA)((CDATA)obj).clone());
> 								} else if (obj instanceof ProcessingInstruction) {
> 										ProcessingInstruction p = (ProcessingInstruction)((ProcessingInstruction)obj).clone();
> 										element.content.add(p);
1591,1593d1618
<         // Remove out the parent
<         element.setParent(null);
< 
2001-03-16 11:07 *tity.java jdom\src\java\org\jdom/Entity.java Page    1


491,492c491,525
<         Entity entity = new Entity(name);
<         entity.content = (List)((LinkedList)content).clone();
---
> 				Entity entity = null;
> 
> 				try{
> 					entity = (Entity) super.clone();
> 				}catch(CloneNotSupportedException ce){
> 					// is clonable, will never happen
> 				}
> 
> 				// name is a reference to an immutable (String) object
> 				// and is copied by Object.clone()
> 
> 				// the parent and document references are copied by Object.clone()
> 				// must set to null
> 				entity.parent = null;
> 				entity.document = null;
> 
> 				//give the clone an empty list for content
> 				entity.content = new LinkedList();
> 
> 				//clone content
> 				if( this.content != null ){
> 					Iterator i = this.content.iterator();
> 
> 					while(i.hasNext()){
> 						Object obj = i.next();
> 
> 						if( obj instanceof Element ){
> 							entity.addChild( (Element)((Element)obj).clone() ); // add clone
> 						}
> 						else if( obj instanceof String ){
> 							entity.addChild((String)obj);  // strings are immutable (and not clonable)
> 																						// so let clone get copy of reference
> 						}
> 					}
> 				}
2001-03-16 11:07 *src\java\org\jdom/ProcessingInstruction.java Page    1


479c479,501
<         return new ProcessingInstruction(target, rawData);
---
> 				ProcessingInstruction pi = null;
> 
> 				try{
> 					pi = (ProcessingInstruction) super.clone();
> 				}catch(CloneNotSupportedException ce){
> 					// is clonable, will never happen
> 				}
> 
> 				// target and rawdata are immutable and references copied by
> 				// Object.clone()
> 
> 				// parent and document references copied by Object.clone()
> 				// must set to null
> 
> 				pi.parent = null;
> 				pi.document = null;
> 
> 				// create a new Map object for the clone
> 				if( mapData != null ){
> 					pi.mapData = parseData(rawData);
> 				}
> 
> 				return pi;
Common subdirectories: old\jdom\src\java\org\jdom/adapters and jdom\src\java\org\jdom/adapters
Common subdirectories: old\jdom\src\java\org\jdom/input and jdom\src\java\org\jdom/input
Common subdirectories: old\jdom\src\java\org\jdom/output and jdom\src\java\org\jdom/output


More information about the jdom-interest mailing list