View Javadoc

    1   /*
    2    * Copyright 2001-2005 (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
    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  * Redistribution and use of this software and associated documentation
    167  * ("Software"), with or without modification, are permitted provided that the
    168  * following conditions are met:
    169  *
    170  * 1. Redistributions of source code must retain copyright statements and
    171  * notices. Redistributions must also contain a copy of this document.
    172  *
    173  * 2. Redistributions in binary form must reproduce the above copyright notice,
    174  * this list of conditions and the following disclaimer in the documentation
    175  * and/or other materials provided with the distribution.
    176  *
    177  * 3. The name "DOM4J" must not be used to endorse or promote products derived
    178  * from this Software without prior written permission of MetaStuff, Ltd. For
    179  * written permission, please contact dom4j-info@metastuff.com.
    180  *
    181  * 4. Products derived from this Software may not be called "DOM4J" nor may
    182  * "DOM4J" appear in their names without prior written permission of MetaStuff,
    183  * Ltd. DOM4J is a registered trademark of MetaStuff, Ltd.
    184  *
    185  * 5. Due credit should be given to the DOM4J Project - http://www.dom4j.org
    186  *
    187  * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS ``AS IS'' AND
    188  * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    189  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    190  * ARE DISCLAIMED. IN NO EVENT SHALL METASTUFF, LTD. OR ITS CONTRIBUTORS BE
    191  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
    192  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    193  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    194  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    195  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    196  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    197  * POSSIBILITY OF SUCH DAMAGE.
    198  *
    199  * Copyright 2001-2005 (C) MetaStuff, Ltd. All Rights Reserved.
    200  */