[jdom-interest] RE: Need help
Syloke Soong
ssoong at protedyne.com
Mon Mar 19 12:12:41 PST 2007
> This is what you should have done:
> Plan.addContent(
> (List)
> ((Element) oneLevelDeep)
> .getChildren().clone()
> );
Oopsie-doopsie ...
Clone() and getChildren() methods are found in Element class.
Element getChildren() produces a java.util.List.
However, java.util.List does not have a clone() method.
Therefore, you need to decide, if you wish to either
1. clone each child element extracted by getChildren()?
2. or, clone the parent element.
I presume you wish to clone each child Element and deposit the clone in
a List. Therefore, you should probably break it up rather than stacking
your functional calls.
The following compiled without errors (but haven't been test-run):
[code example='1']
public List cloneChildrenOnly(Element element)
{
List clonedElementList = new Vector();
List origElementList = element.getChildren();
Iterator iter = origElementList.iterator();
while (iter.hasNext())
{
Element eorig = (Element)iter.next();
Element eclone = (Element)eorig.clone() ;
clonedElementList.add(eclone);
}
return clonedElementList;
}
[/code]
But then, since you might like to stack your functional calls,
The following would be similar to example 1:
[code example='2']
public List cloneChildrenOnly(Element element)
{
List clonedElementList = new Vector();
Iterator iter = element.getChildren().iterator();
while (iter.hasNext())
clonedElementList.add(((Element)iter.next()).clone());
return clonedElementList;
}
[/code]
You could then code:
Plan.addContent(cloneChildrenOnly(whateverElement));
On the other hand, I am musing over creating a List from cloneContent(),
And then eliminating non-Elements from the List:
[code example='3']
public List cloneChildrenOnly(Element element)
{
List clonedList = element.cloneContent();
Iterator iter = clonedList.iterator();
while (iter.hasNext())
while (iter.hasNext())
{
Object what = iter.next();
if (!(what instanceof Element))
clonedList.remove(what);
}
return clonedList;
}
[/code]
I like example3 (besides needing pardon from oo-purists for the instance
of instanceof), because I presume it removes the overhead of creating a
second List. Somebody please comment if I am right?
This email and any files transmitted with it are confidential and intended solely for the use of the individual or entity to whom they are addressed. If you have received this email in error please notify the sender immediately. Please note that any views or opinions presented in this email are solely those of the author and do not necessarily represent those of the company. Even though this company takes every precaution to ensure this email is virus-free, the recipient should check this email and any attachments for the presence of viruses. The company accepts no liability for any damage caused by any virus transmitted by this email.
Protedyne Corporation, 1000 Day Hill Rd, Windsor, CT 06095, USA,
www.protedyne.com
More information about the jdom-interest
mailing list