1   
2   
3   
4   
5   
6   
7   
8   package org.dom4j.tree;
9   
10  import java.util.AbstractList;
11  import java.util.Collection;
12  import java.util.Iterator;
13  import java.util.List;
14  
15  import org.dom4j.IllegalAddException;
16  import org.dom4j.Node;
17  
18  /***
19   * <p>
20   * <code>ContentListFacade</code> represents a facade of the content of a
21   * {@link org.dom4j.Branch} which is returned via calls to the {@link
22   * org.dom4j.Branch#content}  method to allow users to modify the content of a
23   * {@link org.dom4j.Branch} directly using the {@link List} interface. This list
24   * is backed by the branch such that changes to the list will be reflected in
25   * the branch and changes to the branch will be reflected in this list.
26   * </p>
27   * 
28   * @author <a href="mailto:james.strachan@metastuff.com">James Strachan </a>
29   * @version $Revision: 1.11 $
30   */
31  public class ContentListFacade extends AbstractList {
32      /*** The content of the Branch which is modified if I am modified */
33      private List branchContent;
34  
35      /*** The <code>AbstractBranch</code> instance which owns the content */
36      private AbstractBranch branch;
37  
38      public ContentListFacade(AbstractBranch branch, List branchContent) {
39          this.branch = branch;
40          this.branchContent = branchContent;
41      }
42  
43      public boolean add(Object object) {
44          branch.childAdded(asNode(object));
45  
46          return branchContent.add(object);
47      }
48  
49      public void add(int index, Object object) {
50          branch.childAdded(asNode(object));
51          branchContent.add(index, object);
52      }
53  
54      public Object set(int index, Object object) {
55          branch.childAdded(asNode(object));
56  
57          return branchContent.set(index, object);
58      }
59  
60      public boolean remove(Object object) {
61          branch.childRemoved(asNode(object));
62  
63          return branchContent.remove(object);
64      }
65  
66      public Object remove(int index) {
67          Object object = branchContent.remove(index);
68  
69          if (object != null) {
70              branch.childRemoved(asNode(object));
71          }
72  
73          return object;
74      }
75  
76      public boolean addAll(Collection collection) {
77          int count = branchContent.size();
78  
79          for (Iterator iter = collection.iterator(); iter.hasNext(); count++) {
80              add(iter.next());
81          }
82  
83          return count == branchContent.size();
84      }
85  
86      public boolean addAll(int index, Collection collection) {
87          int count = branchContent.size();
88  
89          for (Iterator iter = collection.iterator(); iter.hasNext(); count--) {
90              add(index++, iter.next());
91          }
92  
93          return count == branchContent.size();
94      }
95  
96      public void clear() {
97          for (Iterator iter = iterator(); iter.hasNext();) {
98              Object object = iter.next();
99              branch.childRemoved(asNode(object));
100         }
101 
102         branchContent.clear();
103     }
104 
105     public boolean removeAll(Collection c) {
106         for (Iterator iter = c.iterator(); iter.hasNext();) {
107             Object object = iter.next();
108             branch.childRemoved(asNode(object));
109         }
110 
111         return branchContent.removeAll(c);
112     }
113 
114     public int size() {
115         return branchContent.size();
116     }
117 
118     public boolean isEmpty() {
119         return branchContent.isEmpty();
120     }
121 
122     public boolean contains(Object o) {
123         return branchContent.contains(o);
124     }
125 
126     public Object[] toArray() {
127         return branchContent.toArray();
128     }
129 
130     public Object[] toArray(Object[] a) {
131         return branchContent.toArray(a);
132     }
133 
134     public boolean containsAll(Collection c) {
135         return branchContent.containsAll(c);
136     }
137 
138     public Object get(int index) {
139         return branchContent.get(index);
140     }
141 
142     public int indexOf(Object o) {
143         return branchContent.indexOf(o);
144     }
145 
146     public int lastIndexOf(Object o) {
147         return branchContent.lastIndexOf(o);
148     }
149 
150     protected Node asNode(Object object) {
151         if (object instanceof Node) {
152             return (Node) object;
153         } else {
154             throw new IllegalAddException(
155                     "This list must contain instances of "
156                             + "Node. Invalid type: " + object);
157         }
158     }
159 
160     protected List getBackingList() {
161         return branchContent;
162     }
163 }
164 
165 
166 
167 
168 
169 
170 
171 
172 
173 
174 
175 
176 
177 
178 
179 
180 
181 
182 
183 
184 
185 
186 
187 
188 
189 
190 
191 
192 
193 
194 
195 
196 
197 
198 
199 
200