KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > types > resources > Resources


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */

18
19 package org.apache.tools.ant.types.resources;
20
21 import java.io.File JavaDoc;
22 import java.util.List JavaDoc;
23 import java.util.Stack JavaDoc;
24 import java.util.Vector JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.Collection JavaDoc;
27 import java.util.Collections JavaDoc;
28 import java.util.AbstractCollection JavaDoc;
29 import java.util.NoSuchElementException JavaDoc;
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 /**
37  * Generic ResourceCollection: Either stores nested ResourceCollections,
38  * making no attempt to remove duplicates, or references another ResourceCollection.
39  * @since Ant 1.7
40  */

41 public class Resources extends DataType implements ResourceCollection {
42     /** static empty ResourceCollection */
43     public static final ResourceCollection NONE = new ResourceCollection() {
44         public boolean isFilesystemOnly() {
45             return true;
46         }
47         public Iterator JavaDoc iterator() {
48             return EMPTY_ITERATOR;
49         }
50         public int size() {
51             return 0;
52         }
53     };
54
55     /** static empty Iterator */
56     public static final Iterator JavaDoc EMPTY_ITERATOR = new Iterator JavaDoc() {
57         public Object JavaDoc next() {
58             throw new NoSuchElementException JavaDoc();
59         }
60         public boolean hasNext() {
61             return false;
62         }
63         public void remove() {
64             throw new UnsupportedOperationException JavaDoc();
65         }
66     };
67
68     private class MyCollection extends AbstractCollection JavaDoc {
69         private int size;
70
71         MyCollection() {
72             size = 0;
73             for (Iterator JavaDoc rci = getNested().iterator(); rci.hasNext();) {
74                 size += ((ResourceCollection) rci.next()).size();
75             }
76         }
77         public int size() {
78             return size;
79         }
80         public Iterator JavaDoc iterator() {
81             return new MyIterator();
82         }
83         private class MyIterator implements Iterator JavaDoc {
84             private Iterator JavaDoc rci = getNested().iterator();
85             private Iterator JavaDoc 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 JavaDoc next() {
96                 if (!hasNext()) {
97                     throw new NoSuchElementException JavaDoc();
98                 }
99                 return ri.next();
100             }
101             public void remove() {
102                 throw new UnsupportedOperationException JavaDoc();
103             }
104         }
105     }
106
107     private Vector JavaDoc rc;
108     private Collection JavaDoc coll;
109
110     /**
111      * Add a ResourceCollection.
112      * @param c the ResourceCollection to add.
113      */

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 JavaDoc();
123         }
124         rc.add(c);
125         FailFast.invalidate(this);
126         coll = null;
127         setChecked(false);
128     }
129
130     /**
131      * Fulfill the ResourceCollection contract.
132      * @return an Iterator of Resources.
133      */

134     public synchronized Iterator JavaDoc iterator() {
135         if (isReference()) {
136             return getRef().iterator();
137         }
138         validate();
139         return new FailFast(this, coll.iterator());
140     }
141
142     /**
143      * Fulfill the ResourceCollection contract.
144      * @return number of elements as int.
145      */

146     public synchronized int size() {
147         if (isReference()) {
148             return getRef().size();
149         }
150         validate();
151         return coll.size();
152     }
153
154     /**
155      * Fulfill the ResourceCollection contract.
156      * @return true if all Resources represent files.
157      */

158     public boolean isFilesystemOnly() {
159         if (isReference()) {
160             return getRef().isFilesystemOnly();
161         }
162         validate();
163
164         for (Iterator JavaDoc i = getNested().iterator(); i.hasNext();) {
165             if ((!((ResourceCollection) i.next()).isFilesystemOnly())) {
166                 return false;
167             }
168         }
169         return true;
170     }
171
172     /**
173      * Format this BaseResourceCollectionContainer as a String.
174      * @return a descriptive <code>String</code>.
175      */

176     public synchronized String JavaDoc toString() {
177         if (isReference()) {
178             return getCheckedRef().toString();
179         }
180         if (coll == null || coll.isEmpty()) {
181             return "";
182         }
183         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
184         for (Iterator JavaDoc 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     /**
194      * Overrides the version of DataType to recurse on all DataType
195      * child elements that may have been added.
196      * @param stk the stack of data types to use (recursively).
197      * @param p the project to use to dereference the references.
198      * @throws BuildException on error.
199      */

200     protected void dieOnCircularReference(Stack JavaDoc 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 JavaDoc i = getNested().iterator(); i.hasNext();) {
209                 Object JavaDoc o = i.next();
210                 if (o instanceof DataType) {
211                     invokeCircularReferenceCheck((DataType) o, stk, p);
212                 }
213             }
214             setChecked(true);
215         }
216     }
217
218     /**
219      * Resolves references, allowing any ResourceCollection.
220      * @return the referenced ResourceCollection.
221      */

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 JavaDoc getNested() {
233         return rc == null ? Collections.EMPTY_LIST : rc;
234     }
235 }
236
Popular Tags