View Javadoc

1   /*
2    * Copyright 2001-2004 (C) MetaStuff, Ltd. All Rights Reserved.
3    * 
4    * This software is open source. 
5    * See the bottom of this file for the licence.
6    * 
7    * $Id: ContentListFacade.java,v 1.8 2004/06/25 12:34:50 maartenc Exp $
8    */
9   
10  package org.dom4j.tree;
11  
12  import java.util.AbstractList;
13  import java.util.Collection;
14  import java.util.Iterator;
15  import java.util.List;
16  
17  import org.dom4j.IllegalAddException;
18  import org.dom4j.Node;
19  
20  /*** <p><code>ContentListFacade</code> represents a facade of the 
21    * content of a {@link org.dom4j.Branch} which is returned via calls to the
22    * {@link org.dom4j.Branch#content} method to allow users to modify the content
23    * of a {@link org.dom4j.Branch} directly using the {@link List} interface. 
24    * This list is backed by the branch such that changes to the list will
25    * be reflected in the branch and changes to the branch will be reflected
26    * be reflected in this list.</p>
27    *
28    * @author <a href="mailto:james.strachan@metastuff.com">James Strachan</a>
29    * @version $Revision: 1.8 $
30    */
31  public class ContentListFacade extends AbstractList {
32  
33      /*** The content of the Branch which is modified if I am modified */
34      private List branchContent;
35  
36      /*** The <code>AbstractBranch</code> instance which owns the content */
37      private AbstractBranch branch;
38  
39      
40      public ContentListFacade(AbstractBranch branch, List branchContent) { 
41          this.branch = branch;
42          this.branchContent = branchContent;
43      }
44      
45      public boolean add(Object object) {
46          branch.childAdded( asNode( object ) );
47          return branchContent.add(object);
48      }
49      
50      public void add(int index, Object object) {
51          branch.childAdded( asNode( object ) );
52          branchContent.add(index, object);
53      }
54      
55      public Object set(int index, Object object) {
56          branch.childAdded( asNode( object ) );
57          return branchContent.set(index, object);
58      }
59      
60      public boolean remove(Object object) {
61          branch.childRemoved( asNode( object ) );
62          return branchContent.remove(object);
63      }
64  
65      public Object remove(int index) {
66          Object object = branchContent.remove(index);
67          if ( object != null ) {
68              branch.childRemoved( asNode( object ) );
69          }
70          return object;
71      }
72  
73      public boolean addAll(Collection collection) {
74          int count = branchContent.size();
75          for (Iterator iter = collection.iterator(); iter.hasNext(); count++ ) {
76              add(iter.next());
77          }
78          return count == branchContent.size();
79      }
80      
81      public boolean addAll(int index, Collection collection) {
82          int count = branchContent.size();
83          for (Iterator iter = collection.iterator(); iter.hasNext(); count-- ) {
84              add(index++, iter.next());
85          }
86          return count == branchContent.size();
87      }
88      
89      public void clear() {
90          for ( Iterator iter = iterator(); iter.hasNext(); ) {
91              Object object = iter.next();
92              branch.childRemoved( asNode( object ) );
93          }
94          branchContent.clear();
95      }
96      
97      public boolean removeAll(Collection c) {
98          for ( Iterator iter = c.iterator(); iter.hasNext(); ) {
99              Object object = iter.next();
100             branch.childRemoved( asNode( object ) );
101         }
102         return branchContent.removeAll(c);
103     }
104     
105     public int size() {
106         return branchContent.size();
107     }
108     
109     public boolean isEmpty() {
110         return branchContent.isEmpty();
111     }
112     
113     public boolean contains(Object o) {
114         return branchContent.contains(o);
115     }
116     
117     public Object[] toArray() {
118         return branchContent.toArray();
119     }
120     
121     public Object[] toArray(Object[] a) {
122         return branchContent.toArray(a);
123     }
124     
125     public boolean containsAll(Collection c) {
126         return branchContent.containsAll(c);
127     }
128     
129     public Object get(int index) {
130         return branchContent.get(index);
131     }
132     
133     public int indexOf(Object o) {
134         return branchContent.indexOf(o);
135     }
136     
137     public int lastIndexOf(Object o) {
138         return branchContent.lastIndexOf(o);
139     }
140     
141     protected Node asNode(Object object) {
142         if (object instanceof Node) {
143             return (Node) object;
144         }
145         else {
146             throw new IllegalAddException( "This list must contain instances of Node. Invalid type: "+ object );
147         }
148     }    
149 
150     protected List getBackingList() {
151         return branchContent;
152     }
153 }
154 
155 
156 
157 
158 /*
159  * Redistribution and use of this software and associated documentation
160  * ("Software"), with or without modification, are permitted provided
161  * that the following conditions are met:
162  *
163  * 1. Redistributions of source code must retain copyright
164  *    statements and notices.  Redistributions must also contain a
165  *    copy of this document.
166  *
167  * 2. Redistributions in binary form must reproduce the
168  *    above copyright notice, this list of conditions and the
169  *    following disclaimer in the documentation and/or other
170  *    materials provided with the distribution.
171  *
172  * 3. The name "DOM4J" must not be used to endorse or promote
173  *    products derived from this Software without prior written
174  *    permission of MetaStuff, Ltd.  For written permission,
175  *    please contact dom4j-info@metastuff.com.
176  *
177  * 4. Products derived from this Software may not be called "DOM4J"
178  *    nor may "DOM4J" appear in their names without prior written
179  *    permission of MetaStuff, Ltd. DOM4J is a registered
180  *    trademark of MetaStuff, Ltd.
181  *
182  * 5. Due credit should be given to the DOM4J Project - 
183  *    http://www.dom4j.org
184  *
185  * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS
186  * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
187  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
188  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
189  * METASTUFF, LTD. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
190  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
191  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
192  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
193  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
194  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
195  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
196  * OF THE POSSIBILITY OF SUCH DAMAGE.
197  *
198  * Copyright 2001-2004 (C) MetaStuff, Ltd. All Rights Reserved.
199  *
200  * $Id: ContentListFacade.java,v 1.8 2004/06/25 12:34:50 maartenc Exp $
201  */