package org.jdom; import java.util.*; /** *

* The ContentList class is a list that manages the parent/child * relationship of the contained objects. It is designed to be embedded in an * Element object. *

* * @author Philippe Riand */ public class ContentList extends ArrayList { protected Element parentElement; /** * Constructs an empty list with the specified initial capacity. * * @param initialCapacity the initial capacity of the list. * @exception IllegalArgumentException if the specified initial capacity * is negative */ public ContentList(Element parentElement, int initialCapacity) { super(initialCapacity); this.parentElement = parentElement; } /** * Assign an item parent, depending on the item type. * @param obj the object to assign * @param parent the new parent Element */ private void assignParent(Object obj, Element parent) { if (obj instanceof Element) { ((Element)obj).setParent(parent); } else if (obj instanceof Text) { ((Text)obj).setParent(parent); } else if (obj instanceof Comment) { ((Comment)obj).setParent(parent); } else if (obj instanceof CDATA) { ((CDATA)obj).setParent(parent); } else if (obj instanceof ProcessingInstruction) { ((ProcessingInstruction)obj).setParent(parent); } else if (obj instanceof EntityRef) { ((EntityRef)obj).setParent(parent); } else { throw new IllegalAddException( "An Element may directly contain only objects of type " + "String, Text, Element, Comment, CDATA, EntityRef, " + "and ProcessingInstruction: " + (obj == null ? "null" : obj.getClass().getName()) + " is not allowed"); } } /** * Replaces the element at the specified position in this list with * the specified element. * * @param index index of element to replace. * @param element element to be stored at the specified position. * @return the element previously at the specified position. * @throws IndexOutOfBoundsException if index out of range * (index < 0 || index >= size()). */ public Object set(int index, Object element) { assignParent(get(index),null); assignParent(element,parentElement); return super.set(index,element); } /** * Appends the specified element to the end of this list. * * @param o element to be appended to this list. * @return true (as per the general contract of Collection.add). */ public boolean add(Object o) { assignParent(o,parentElement); return super.add(o); } /** * Inserts the specified element at the specified position in this * list. Shifts the element currently at that position (if any) and * any subsequent elements to the right (adds one to their indices). * * @param index index at which the specified element is to be inserted. * @param element element to be inserted. * @throws IndexOutOfBoundsException if index is out of range * (index < 0 || index > size()). */ public void add(int index, Object element) { assignParent(element,parentElement); super.add(index,element); } /** * Removes the element at the specified position in this list. * Shifts any subsequent elements to the left (subtracts one from their * indices). * * @param index the index of the element to removed. * @return the element that was removed from the list. * @throws IndexOutOfBoundsException if index out of range (index * < 0 || index >= size()). */ public Object remove(int index) { assignParent(get(index),null); return remove(index); } /** * Removes all of the elements from this list. The list will * be empty after this call returns. */ public void clear() { int count = size(); for( int i=0; i(index * < 0 || index > size()). */ public boolean addAll(Collection c) { int oldSize = size(); boolean result = super.addAll(c); int newSize = size(); for( int i=oldSize; i(index * < 0 || index > size()). */ public boolean addAll(int index, Collection c) { int oldSize = size(); boolean result = super.addAll(index,c); int lastIndex = index + (size()-oldSize); for( int i=index; i(toIndex - fromIndex) elements. * (If toIndex==fromIndex, this operation has no effect.) * * @param fromIndex index of first element to be removed. * @param toIndex index after last element to be removed. */ protected void removeRange(int fromIndex, int toIndex) { for( int i=fromIndex; i