001/* 002 * Copyright 2001 (C) MetaStuff, Ltd. All Rights Reserved. 003 * 004 * This software is open source. 005 * See the bottom of this file for the licence. 006 * 007 * $Id: ContentListFacade.java,v 1.3 2001/03/21 00:53:57 jstrachan Exp $ 008 */ 009 010package org.dom4j.tree; 011 012import java.util.Collection; 013import java.util.AbstractList; 014import java.util.Iterator; 015import java.util.LinkedList; 016import java.util.List; 017 018import org.dom4j.Branch; 019import org.dom4j.Comment; 020import org.dom4j.CDATA; 021import org.dom4j.Element; 022import org.dom4j.Entity; 023import org.dom4j.IllegalAddException; 024import org.dom4j.Namespace; 025import org.dom4j.Node; 026import org.dom4j.Text; 027 028/** <p><code>ContentListFacade</code> represents a facade of the 029 * content of a {@link Branch} which is returned via calls to the 030 * {@link Branch#content} method to allow users to modify the content 031 * of a {@link Branch} directly using the {@link List} interface. 032 * This list is backed by the branch such that changes to the list will 033 * be reflected in the branch and changes to the branch will be reflected 034 * be reflected in this list.</p> 035 * 036 * @author <a href="mailto:james.strachan@metastuff.com">James Strachan</a> 037 * @version $Revision: 1.3 $ 038 */ 039public class ContentListFacade extends AbstractList { 040 041 /** The content of the Branch which is modified if I am modified */ 042 private List branchContent; 043 044 /** The <code>AbstractBranch</code> instance which owns the content */ 045 private AbstractBranch branch; 046 047 048 public ContentListFacade(AbstractBranch branch, List branchContent) { 049 this.branch = branch; 050 this.branchContent = branchContent; 051 } 052 053 public boolean add(Object object) { 054 branch.childAdded( asNode( object ) ); 055 return branchContent.add(object); 056 } 057 058 public void add(int index, Object object) { 059 branch.childAdded( asNode( object ) ); 060 branchContent.add(index, object); 061 } 062 063 public Object set(int index, Object object) { 064 branch.childAdded( asNode( object ) ); 065 return branchContent.set(index, object); 066 } 067 068 public boolean remove(Object object) { 069 branch.childRemoved( asNode( object ) ); 070 return branchContent.remove(object); 071 } 072 073 public Object remove(int index) { 074 Object object = branchContent.remove(index); 075 if ( object != null ) { 076 branch.childRemoved( asNode( object ) ); 077 } 078 return object; 079 } 080 081 public boolean addAll(Collection collection) { 082 int count = branchContent.size(); 083 for (Iterator iter = collection.iterator(); iter.hasNext(); count++ ) { 084 add(iter.next()); 085 } 086 return count == branchContent.size(); 087 } 088 089 public boolean addAll(int index, Collection collection) { 090 int count = branchContent.size(); 091 for (Iterator iter = collection.iterator(); iter.hasNext(); count-- ) { 092 add(index++, iter.next()); 093 } 094 return count == branchContent.size(); 095 } 096 097 public void clear() { 098 for ( Iterator iter = iterator(); iter.hasNext(); ) { 099 Object object = iter.next(); 100 branch.childRemoved( asNode( object ) ); 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 return branchContent.removeAll(c); 111 } 112 113 public int size() { 114 return branchContent.size(); 115 } 116 117 public boolean isEmpty() { 118 return branchContent.isEmpty(); 119 } 120 121 public boolean contains(Object o) { 122 return branchContent.contains(o); 123 } 124 125 public Object[] toArray() { 126 return branchContent.toArray(); 127 } 128 129 public Object[] toArray(Object[] a) { 130 return branchContent.toArray(a); 131 } 132 133 public boolean containsAll(Collection c) { 134 return branchContent.containsAll(c); 135 } 136 137 public Object get(int index) { 138 return branchContent.get(index); 139 } 140 141 public int indexOf(Object o) { 142 return branchContent.indexOf(o); 143 } 144 145 public int lastIndexOf(Object o) { 146 return branchContent.lastIndexOf(o); 147 } 148 149 protected Node asNode(Object object) { 150 if (object instanceof Node) { 151 return (Node) object; 152 } 153 else { 154 throw new IllegalAddException( "This list must contain instances of Node. Invalid type: "+ object ); 155 } 156 } 157 158 protected List getBackingList() { 159 return branchContent; 160 } 161} 162 163 164 165 166/* 167 * Redistribution and use of this software and associated documentation 168 * ("Software"), with or without modification, are permitted provided 169 * that the following conditions are met: 170 * 171 * 1. Redistributions of source code must retain copyright 172 * statements and notices. Redistributions must also contain a 173 * copy of this document. 174 * 175 * 2. Redistributions in binary form must reproduce the 176 * above copyright notice, this list of conditions and the 177 * following disclaimer in the documentation and/or other 178 * materials provided with the distribution. 179 * 180 * 3. The name "DOM4J" must not be used to endorse or promote 181 * products derived from this Software without prior written 182 * permission of MetaStuff, Ltd. For written permission, 183 * please contact dom4j-info@metastuff.com. 184 * 185 * 4. Products derived from this Software may not be called "DOM4J" 186 * nor may "DOM4J" appear in their names without prior written 187 * permission of MetaStuff, Ltd. DOM4J is a registered 188 * trademark of MetaStuff, Ltd. 189 * 190 * 5. Due credit should be given to the DOM4J Project 191 * (http://dom4j.org/). 192 * 193 * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS 194 * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT 195 * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 196 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 197 * METASTUFF, LTD. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 198 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 199 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 200 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 201 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 202 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 203 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 204 * OF THE POSSIBILITY OF SUCH DAMAGE. 205 * 206 * Copyright 2001 (C) MetaStuff, Ltd. All Rights Reserved. 207 * 208 * $Id: ContentListFacade.java,v 1.3 2001/03/21 00:53:57 jstrachan Exp $ 209 */