1 18 package org.apache.tools.ant.types.resources; 19 20 import java.io.File ; 21 import java.util.List ; 22 import java.util.Stack ; 23 import java.util.Iterator ; 24 import java.util.ArrayList ; 25 import java.util.Collection ; 26 import java.util.Collections ; 27 28 import org.apache.tools.ant.Project; 29 import org.apache.tools.ant.BuildException; 30 import org.apache.tools.ant.types.DataType; 31 import org.apache.tools.ant.types.ResourceCollection; 32 33 37 public abstract class BaseResourceCollectionContainer 38 extends DataType implements ResourceCollection, Cloneable { 39 private List rc = new ArrayList (); 40 private Collection coll = null; 41 private boolean cache = true; 42 43 47 public synchronized void setCache(boolean b) { 48 cache = b; 49 } 50 51 55 public synchronized boolean isCache() { 56 return cache; 57 } 58 59 63 public synchronized void clear() throws BuildException { 64 if (isReference()) { 65 throw noChildrenAllowed(); 66 } 67 rc.clear(); 68 FailFast.invalidate(this); 69 coll = null; 70 setChecked(false); 71 } 72 73 78 public synchronized void add(ResourceCollection c) throws BuildException { 79 if (isReference()) { 80 throw noChildrenAllowed(); 81 } 82 if (c == null) { 83 return; 84 } 85 rc.add(c); 86 FailFast.invalidate(this); 87 coll = null; 88 setChecked(false); 89 } 90 91 96 public synchronized void addAll(Collection c) throws BuildException { 97 if (isReference()) { 98 throw noChildrenAllowed(); 99 } 100 try { 101 for (Iterator i = c.iterator(); i.hasNext();) { 102 add((ResourceCollection) i.next()); 103 } 104 } catch (ClassCastException e) { 105 throw new BuildException(e); 106 } 107 } 108 109 115 public final synchronized Iterator iterator() { 116 if (isReference()) { 117 return ((BaseResourceCollectionContainer) getCheckedRef()).iterator(); 118 } 119 dieOnCircularReference(); 120 return new FailFast(this, cacheCollection().iterator()); 121 } 122 123 127 public synchronized int size() { 128 if (isReference()) { 129 return ((BaseResourceCollectionContainer) getCheckedRef()).size(); 130 } 131 dieOnCircularReference(); 132 return cacheCollection().size(); 133 } 134 135 139 public synchronized boolean isFilesystemOnly() { 140 if (isReference()) { 141 return ((BaseResourceCollectionContainer) getCheckedRef()).isFilesystemOnly(); 142 } 143 dieOnCircularReference(); 144 boolean goEarly = true; 146 for (Iterator i = rc.iterator(); goEarly && i.hasNext();) { 147 goEarly &= ((ResourceCollection) i.next()).isFilesystemOnly(); 148 } 149 if (goEarly) { 150 return true; 151 } 152 154 for (Iterator i = cacheCollection().iterator(); i.hasNext();) { 155 if (!(i.next() instanceof FileResource)) { 156 return false; 157 } 158 } 159 return true; 160 } 161 162 169 protected synchronized void dieOnCircularReference(Stack stk, Project p) 170 throws BuildException { 171 if (isChecked()) { 172 return; 173 } 174 if (isReference()) { 175 super.dieOnCircularReference(stk, p); 176 } else { 177 for (Iterator i = rc.iterator(); i.hasNext();) { 178 Object o = i.next(); 179 if (o instanceof DataType) { 180 stk.push(o); 181 invokeCircularReferenceCheck((DataType) o, stk, p); 182 stk.pop(); 183 } 184 } 185 setChecked(true); 186 } 187 } 188 189 193 protected final synchronized List getResourceCollections() { 194 dieOnCircularReference(); 195 return Collections.unmodifiableList(rc); 196 } 197 198 202 protected abstract Collection getCollection(); 203 204 209 public Object clone() { 210 try { 211 BaseResourceCollectionContainer c 212 = (BaseResourceCollectionContainer) super.clone(); 213 c.rc = new ArrayList (rc); 214 c.coll = null; 215 return c; 216 } catch (CloneNotSupportedException e) { 217 throw new BuildException(e); 218 } 219 } 220 221 225 public synchronized String toString() { 226 if (isReference()) { 227 return getCheckedRef().toString(); 228 } 229 if (cacheCollection().size() == 0) { 230 return ""; 231 } 232 StringBuffer sb = new StringBuffer (); 233 for (Iterator i = coll.iterator(); i.hasNext();) { 234 if (sb.length() > 0) { 235 sb.append(File.pathSeparatorChar); 236 } 237 sb.append(i.next()); 238 } 239 return sb.toString(); 240 } 241 242 private synchronized Collection cacheCollection() { 243 if (coll == null || !isCache()) { 244 coll = getCollection(); 245 } 246 return coll; 247 } 248 249 } 250 | Popular Tags |