1 18 19 package org.apache.tools.ant.types.resources; 20 21 import java.io.File ; 22 import java.util.List ; 23 import java.util.Stack ; 24 import java.util.Vector ; 25 import java.util.Iterator ; 26 import java.util.Collection ; 27 import java.util.Collections ; 28 import java.util.AbstractCollection ; 29 import java.util.NoSuchElementException ; 30 31 import org.apache.tools.ant.Project; 32 import org.apache.tools.ant.BuildException; 33 import org.apache.tools.ant.types.DataType; 34 import org.apache.tools.ant.types.ResourceCollection; 35 36 41 public class Resources extends DataType implements ResourceCollection { 42 43 public static final ResourceCollection NONE = new ResourceCollection() { 44 public boolean isFilesystemOnly() { 45 return true; 46 } 47 public Iterator iterator() { 48 return EMPTY_ITERATOR; 49 } 50 public int size() { 51 return 0; 52 } 53 }; 54 55 56 public static final Iterator EMPTY_ITERATOR = new Iterator () { 57 public Object next() { 58 throw new NoSuchElementException (); 59 } 60 public boolean hasNext() { 61 return false; 62 } 63 public void remove() { 64 throw new UnsupportedOperationException (); 65 } 66 }; 67 68 private class MyCollection extends AbstractCollection { 69 private int size; 70 71 MyCollection() { 72 size = 0; 73 for (Iterator rci = getNested().iterator(); rci.hasNext();) { 74 size += ((ResourceCollection) rci.next()).size(); 75 } 76 } 77 public int size() { 78 return size; 79 } 80 public Iterator iterator() { 81 return new MyIterator(); 82 } 83 private class MyIterator implements Iterator { 84 private Iterator rci = getNested().iterator(); 85 private Iterator ri = null; 86 87 public boolean hasNext() { 88 boolean result = ri != null && ri.hasNext(); 89 while (!result && rci.hasNext()) { 90 ri = ((ResourceCollection) rci.next()).iterator(); 91 result = ri.hasNext(); 92 } 93 return result; 94 } 95 public Object next() { 96 if (!hasNext()) { 97 throw new NoSuchElementException (); 98 } 99 return ri.next(); 100 } 101 public void remove() { 102 throw new UnsupportedOperationException (); 103 } 104 } 105 } 106 107 private Vector rc; 108 private Collection coll; 109 110 114 public synchronized void add(ResourceCollection c) { 115 if (isReference()) { 116 throw noChildrenAllowed(); 117 } 118 if (c == null) { 119 return; 120 } 121 if (rc == null) { 122 rc = new Vector (); 123 } 124 rc.add(c); 125 FailFast.invalidate(this); 126 coll = null; 127 setChecked(false); 128 } 129 130 134 public synchronized Iterator iterator() { 135 if (isReference()) { 136 return getRef().iterator(); 137 } 138 validate(); 139 return new FailFast(this, coll.iterator()); 140 } 141 142 146 public synchronized int size() { 147 if (isReference()) { 148 return getRef().size(); 149 } 150 validate(); 151 return coll.size(); 152 } 153 154 158 public boolean isFilesystemOnly() { 159 if (isReference()) { 160 return getRef().isFilesystemOnly(); 161 } 162 validate(); 163 164 for (Iterator i = getNested().iterator(); i.hasNext();) { 165 if ((!((ResourceCollection) i.next()).isFilesystemOnly())) { 166 return false; 167 } 168 } 169 return true; 170 } 171 172 176 public synchronized String toString() { 177 if (isReference()) { 178 return getCheckedRef().toString(); 179 } 180 if (coll == null || coll.isEmpty()) { 181 return ""; 182 } 183 StringBuffer sb = new StringBuffer (); 184 for (Iterator i = coll.iterator(); i.hasNext();) { 185 if (sb.length() > 0) { 186 sb.append(File.pathSeparatorChar); 187 } 188 sb.append(i.next()); 189 } 190 return sb.toString(); 191 } 192 193 200 protected void dieOnCircularReference(Stack stk, Project p) 201 throws BuildException { 202 if (isChecked()) { 203 return; 204 } 205 if (isReference()) { 206 super.dieOnCircularReference(stk, p); 207 } else { 208 for (Iterator i = getNested().iterator(); i.hasNext();) { 209 Object o = i.next(); 210 if (o instanceof DataType) { 211 invokeCircularReferenceCheck((DataType) o, stk, p); 212 } 213 } 214 setChecked(true); 215 } 216 } 217 218 222 private ResourceCollection getRef() { 223 return (ResourceCollection) getCheckedRef( 224 ResourceCollection.class, "ResourceCollection"); 225 } 226 227 private synchronized void validate() { 228 dieOnCircularReference(); 229 coll = (coll == null) ? new MyCollection() : coll; 230 } 231 232 private synchronized List getNested() { 233 return rc == null ? Collections.EMPTY_LIST : rc; 234 } 235 } 236 | Popular Tags |