[jdom-interest] setAttributes(List attributes) patch

Jools jools at jools.org
Thu Sep 27 14:30:20 PDT 2001


Hi 

Attached is a patch to ensure that setAttributes(List attributes) works
in
a similar manor to setContent(List content).

Code was cribbed from code I'm currently putting into jdom-wip.

--Jools
-------------- next part --------------
Index: Element.java
===================================================================
RCS file: /home/cvspublic/jdom/src/java/org/jdom/Element.java,v
retrieving revision 1.99
diff -r1.99 Element.java
1546,1547c1546,1582
<         // XXX Verify attributes are all parentless first
<         this.attributes = attributes;
---
>         // Save the old list.
>         List oldList = this.attributes;
>         this.attributes = null;
>         
>         int itemsAdded = 0;
>         RuntimeException exception = null;
>         try {
>             Iterator iter = attributes.iterator();
>             while (iter.hasNext()) {
>                 setAttribute((Attribute) iter.next());
>                 itemsAdded++;
>             }
>         } catch (RuntimeException re) {
>             exception = re;
>         } finally {
>             if (exception != null) {
>                 // Copy back the old list (might be null)            
>                 this.attributes = oldList;
>                 
>                 // Only reset the attributes we have copied.
>                 Iterator i = attributes.iterator();
>                 while (itemsAdded-- > 0) {    
>                     ((Attribute)i.next()).setParent(null);
>                 }
> 
>                 throw exception;
>             }
>         }
> 
>         // Orphan the old attributes.
>         if (oldList != null && oldList.size() > 0) {
>             Iterator iter = oldList.iterator();
>             while (iter.hasNext()) {
>                 ((Attribute)iter.next()).setParent(null);
>             }
>         }
>         


More information about the jdom-interest mailing list