<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META content="text/html; charset=iso-8859-1" http-equiv=Content-Type>
<META content="MSHTML 5.00.2314.1000" name=GENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY bgColor=#ffffff>
<DIV><FONT face=Arial size=2>Bug?</FONT></DIV>
<DIV><FONT face=Arial size=2>------------</FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=Arial size=2>The following piece of code throws a
java.util.NoSuchElementException<BR> <BR>
Element e = new Element("koe");<BR>
<BR> List v = new
Vector();<BR> v.add(new
Element("koe1"));<BR> v.add(new
Element("koe2"));<BR>
<BR>
e.getChildren().addAll(v);<BR>
<BR> List v2 = new
Vector();<BR> v2.add(new
Element("koe1"));<BR> v2.add(new
Element("koe2"));<BR>
<BR> e.getChildren("koe").addAll(v2);
<- exception thrown from this line:</FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=Arial size=2>stacktrace:</FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=Arial size=2>Exception in thread "main"
java.util.NoSuchElementException<BR>
at
java.util.LinkedList.getLast(LinkedList.java:109)<BR>
at
org.jdom.PartialList.addAll(PartialList.java:248)<BR>
at SAXBuilderDemo.main(SAXBuilderDemo.java:215)</FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=Arial size=2>I'm using todays' snapshot of jdom;
18.6.2001</FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=Arial size=2>----------</FONT></DIV>
<DIV><FONT face=Arial size=2>Fixing?</FONT></DIV>
<DIV><FONT face=Arial size=2>----------</FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=Arial size=2>The reason for this seems quite obvious as we can
see from the source of PartialList:</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face=Arial size=2>public boolean addAll(Collection c)
{<BR> if (backingList.isEmpty())
{<BR> return
addAll(0, c);<BR> } else
{<BR> return
addAll(backingList.indexOf(getLast()) + 1,
c);<BR> }<BR>}<BR></FONT></DIV>
<DIV><FONT face=Arial size=2>if this PartialList is empty and backing list is
not the getLast method will throw it.</FONT></DIV>
<DIV><FONT face=Arial size=2> </DIV></FONT>
<DIV><FONT face=Arial size=2>I tried this simple fix which seem to be obvious
solution also solving some other problems of addAll:</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face=Arial size=2>public boolean addAll(Collection c) {</FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=Arial size=2> // XXX What
if the element already has a
parent?<BR> for (Iterator i =
c.iterator(); i.hasNext(); )
{<BR> Object o
=
i.next();<BR>
if (o instanceof Element)
{<BR>
((Element)o).setParent(parent); // null is
OK<BR>
}<BR> }</FONT></DIV>
<DIV><FONT face=Arial size=2><BR> if
(backingList.isEmpty() || this.isEmpty())
{<BR> return
(backingList.addAll(c) &&
super.addAll(c));
<BR> } else
{<BR> return (
backingList.addAll(backingList.indexOf(this.getLast()) + 1, c)
&&<BR>
super.addAll(c) );<BR>
}<BR>}</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face=Arial size=2>but the result was unexpected: it did not throw any
exception but the element e looked like this:</FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=Arial size=2><koe><koe1 /><koe2 /><koe1
/><koe2 /><koe1 /><koe2 /><koe1 /><koe2
/><koe1 /><koe2 /></koe></FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=Arial size=2>instead of 4 subelements there was 10 of them
???????</FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=Arial size=2>Now i'm using this:</FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=Arial size=2>public boolean addAll(Collection c)
{<BR> Iterator i =
c.iterator();<BR>
<BR> boolean value =
true;<BR> while(i.hasNext() &&
value ) value =
this.add(i.next());<BR>
<BR> return
value;<BR> }</FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=Arial size=2>This fix seems to work but of course all the
elements are always added to the end of the entire content of e not after the
last element in PartialList (like it should be ... ?)</FONT></DIV>
<DIV> </DIV>
<DIV> </DIV>
<DIV> </DIV>
<DIV><FONT face=Arial size=2>Mika Haapakorpi</FONT></DIV>
<DIV><FONT face=Arial size=2>Sensor Software Consulting</FONT></DIV>
<DIV><FONT face=Arial size=2><A
href="mailto:mika.haapakorpi@sensor-sc.fi">mika.haapakorpi@sensor-sc.fi</A><BR></DIV></FONT>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face=Arial size=2> </DIV></FONT></BODY></HTML>