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: BackedList.java,v 1.3 2001/01/26 11:21:54 jstrachan Exp $ 008 */ 009 010package org.dom4j.tree; 011 012import java.util.Collection; 013import java.util.ArrayList; 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>BackedList</code> represents a list of content 029 * of a {@link Branch}. Changes to the list will 030 * be reflected in the branch, though changes to the branch will not 031 * be reflected in this list.</p> 032 * 033 * @author <a href="mailto:james.strachan@metastuff.com">James Strachan</a> 034 * @version $Revision: 1.3 $ 035 */ 036public class BackedList extends ArrayList { 037 038 /** The content of the Branch which is modified if I am modified */ 039 private List branchContent; 040 041 /** The <code>AbstractBranch</code> instance which owns the content */ 042 private AbstractBranch branch; 043 044 045 public BackedList(AbstractBranch branch,List branchContent) { 046 this( branch, branchContent, branchContent.size() ); 047 } 048 049 public BackedList(AbstractBranch branch,List branchContent,int capacity) { 050 super( capacity ); 051 this.branch = branch; 052 this.branchContent = branchContent; 053 } 054 055 public BackedList(AbstractBranch branch,List branchContent,List initialContent) { 056 super( initialContent ); 057 this.branch = branch; 058 this.branchContent = branchContent; 059 } 060 061 public boolean add(Object object) { 062 branch.addNode( asNode( object ) ); 063 return super.add(object); 064 } 065 066 public void add(int index, Object object) { 067 int realIndex = branchContent.indexOf( get(index) ); 068 if ( realIndex < 0 ) { 069 realIndex = ( index == 0 ) ? 0 : Integer.MAX_VALUE; 070 } 071 if ( realIndex < branchContent.size() ) { 072 branchContent.add(realIndex, object); 073 } 074 else { 075 branchContent.add(object); 076 } 077 branch.childAdded( asNode( object ) ); 078 super.add(index, object); 079 } 080 081 public Object set(int index, Object object) { 082 int realIndex = branchContent.indexOf( get(index) ); 083 if ( realIndex < 0 ) { 084 realIndex = ( index == 0 ) ? 0 : Integer.MAX_VALUE; 085 } 086 if ( realIndex < branchContent.size() ) { 087 branchContent.set(realIndex, object); 088 } 089 else { 090 branchContent.add(object); 091 } 092 branch.childAdded( asNode( object ) ); 093 return super.set(index, object); 094 } 095 096 public boolean remove(Object object) { 097 branch.removeNode( asNode( object ) ); 098 return super.remove(object); 099 } 100 101 public Object remove(int index) { 102 Object object = super.remove(index); 103 if ( object != null ) { 104 branch.removeNode( asNode( object ) ); 105 } 106 return object; 107 } 108 109 public boolean addAll(Collection collection) { 110 ensureCapacity(size() + collection.size()); 111 112 int count = size(); 113 for (Iterator iter = collection.iterator(); iter.hasNext(); count-- ) { 114 add(iter.next()); 115 } 116 return count != 0; 117 } 118 119 public boolean addAll(int index, Collection collection) { 120 ensureCapacity(size() + collection.size()); 121 122 int count = size(); 123 for (Iterator iter = collection.iterator(); iter.hasNext(); count-- ) { 124 add(index++, iter.next()); 125 } 126 return count != 0; 127 } 128 129 public void clear() { 130 for ( Iterator iter = iterator(); iter.hasNext(); ) { 131 Object object = iter.next(); 132 branchContent.remove(object); 133 branch.childRemoved( asNode( object ) ); 134 } 135 super.clear(); 136 } 137 138 /** Performs a local addition which is not forward through to the 139 * Branch or backing list 140 */ 141 public void addLocal(Object object) { 142 super.add(object); 143 } 144 145 protected Node asNode(Object object) { 146 if (object instanceof Node) { 147 return (Node) object; 148 } 149 else { 150 throw new IllegalAddException( "This list must contain instances of Node. Invalid type: "+ object ); 151 } 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://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 (C) MetaStuff, Ltd. All Rights Reserved. 199 * 200 * $Id: BackedList.java,v 1.3 2001/01/26 11:21:54 jstrachan Exp $ 201 */