1 18 package org.apache.tools.ant.types.resources; 19 20 import java.io.File ; 21 import java.util.Stack ; 22 import java.util.Iterator ; 23 import java.util.Collection ; 24 25 import org.apache.tools.ant.Project; 26 import org.apache.tools.ant.BuildException; 27 import org.apache.tools.ant.types.DataType; 28 import org.apache.tools.ant.types.ResourceCollection; 29 30 35 public abstract class BaseResourceCollectionWrapper 36 extends DataType implements ResourceCollection, Cloneable { 37 private static final String ONE_NESTED_MESSAGE 38 = " expects exactly one nested resource collection."; 39 40 private ResourceCollection rc; 41 private Collection coll = null; 42 private boolean cache = true; 43 44 48 public synchronized void setCache(boolean b) { 49 cache = b; 50 } 51 52 56 public synchronized boolean isCache() { 57 return cache; 58 } 59 60 65 public synchronized void add(ResourceCollection c) throws BuildException { 66 if (isReference()) { 67 throw noChildrenAllowed(); 68 } 69 if (c == null) { 70 return; 71 } 72 if (rc != null) { 73 throw oneNested(); 74 } 75 rc = c; 76 setChecked(false); 77 } 78 79 83 public final synchronized Iterator iterator() { 84 if (isReference()) { 85 return ((BaseResourceCollectionWrapper) getCheckedRef()).iterator(); 86 } 87 dieOnCircularReference(); 88 return new FailFast(this, cacheCollection().iterator()); 89 } 90 91 95 public synchronized int size() { 96 if (isReference()) { 97 return ((BaseResourceCollectionWrapper) getCheckedRef()).size(); 98 } 99 dieOnCircularReference(); 100 return cacheCollection().size(); 101 } 102 103 107 public synchronized boolean isFilesystemOnly() { 108 if (isReference()) { 109 return ((BaseResourceCollectionContainer) getCheckedRef()).isFilesystemOnly(); 110 } 111 dieOnCircularReference(); 112 113 if (rc == null || rc.isFilesystemOnly()) { 114 return true; 115 } 116 118 for (Iterator i = cacheCollection().iterator(); i.hasNext();) { 119 if (!(i.next() instanceof FileResource)) { 120 return false; 121 } 122 } 123 return true; 124 } 125 126 133 protected synchronized void dieOnCircularReference(Stack stk, Project p) 134 throws BuildException { 135 if (isChecked()) { 136 return; 137 } 138 if (isReference()) { 139 super.dieOnCircularReference(stk, p); 140 } else { 141 if (rc instanceof DataType) { 142 stk.push(rc); 143 invokeCircularReferenceCheck((DataType) rc, stk, p); 144 stk.pop(); 145 } 146 setChecked(true); 147 } 148 } 149 150 155 protected final synchronized ResourceCollection getResourceCollection() { 156 dieOnCircularReference(); 157 if (rc == null) { 158 throw oneNested(); 159 } 160 return rc; 161 } 162 163 167 protected abstract Collection getCollection(); 168 169 173 public synchronized String toString() { 174 if (isReference()) { 175 return getCheckedRef().toString(); 176 } 177 if (cacheCollection().size() == 0) { 178 return ""; 179 } 180 StringBuffer sb = new StringBuffer (); 181 for (Iterator i = coll.iterator(); i.hasNext();) { 182 if (sb.length() > 0) { 183 sb.append(File.pathSeparatorChar); 184 } 185 sb.append(i.next()); 186 } 187 return sb.toString(); 188 } 189 190 private synchronized Collection cacheCollection() { 191 if (coll == null || !isCache()) { 192 coll = getCollection(); 193 } 194 return coll; 195 } 196 197 private BuildException oneNested() { 198 return new BuildException(super.toString() + ONE_NESTED_MESSAGE); 199 } 200 201 } 202 | Popular Tags |