[jdom-interest] clone with super.clone()

Ken Rune Helland kenh at csc.no
Fri Mar 9 02:33:41 PST 2001


I have rewritten clone() for Attribute, CDATA, Comment, DocType
Document, Element, Entity and ProssessingInstruction to use
super.clone() insead of new when creating the clone.

This allows chaining of clone() calls in a way like chaining
of constructors when sublclassing and so awoids duplicationg
functionality.

A diff from todays sourcedrop created by ( cygwin diff )
diff -b -B -l oldSrcDir newSrcDir attached.

I hope this is on the correct form.

Maybe there could be an faq entry about submitting patches?


Best Regards
KenR

  
-------------- next part --------------
2001-03-09 11:17 *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-09 11:17 *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-09 11:17 *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-09 11:17 *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-09 11:17 *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-09 11:17 *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 to null and lett addContent and addAttribute create new ones
> 				element.content = null;
> 				element.attributes = null;
> 
> 				// cloning attributes
1559d1578
<             List list = new LinkedList();
1564a1584
> 				// cloning content
1570,1571d1589
<                 } else if (obj instanceof Element) {
<                     element.addContent((Element)((Element)obj).clone());
1574,1576d1591
<                 } else if (obj instanceof ProcessingInstruction) {
<                     element.addContent((ProcessingInstruction)
<                                         ((ProcessingInstruction)obj).clone());
1578a1594,1595
> 								} else if (obj instanceof Element) {
> 										element.addContent((Element)((Element)obj).clone());
1580a1598,1599
> 								} else if (obj instanceof ProcessingInstruction) {
> 										element.addContent((ProcessingInstruction)((ProcessingInstruction)obj).clone());
1591,1593d1609
<         // Remove out the parent
<         element.setParent(null);
< 
2001-03-09 11:17 *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-09 11:17 *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