KickJava   Java API By Example, From Geeks To Geeks.

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


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 package org.apache.tools.ant.types.resources;
19
20 import java.io.File JavaDoc;
21 import java.util.Stack JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.Collection JavaDoc;
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 /**
31  * Base class for a ResourceCollection that wraps a single nested
32  * ResourceCollection.
33  * @since Ant 1.7
34  */

35 public abstract class BaseResourceCollectionWrapper
36     extends DataType implements ResourceCollection, Cloneable JavaDoc {
37     private static final String JavaDoc ONE_NESTED_MESSAGE
38         = " expects exactly one nested resource collection.";
39
40     private ResourceCollection rc;
41     private Collection JavaDoc coll = null;
42     private boolean cache = true;
43
44     /**
45      * Set whether to cache collections.
46      * @param b boolean cache flag.
47      */

48     public synchronized void setCache(boolean b) {
49         cache = b;
50     }
51
52     /**
53      * Learn whether to cache collections. Default is <code>true</code>.
54      * @return boolean cache flag.
55      */

56     public synchronized boolean isCache() {
57         return cache;
58     }
59
60     /**
61      * Add a ResourceCollection to the container.
62      * @param c the ResourceCollection to add.
63      * @throws BuildException on error.
64      */

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     /**
80      * Fulfill the ResourceCollection contract.
81      * @return an Iterator of Resources.
82      */

83     public final synchronized Iterator JavaDoc iterator() {
84         if (isReference()) {
85             return ((BaseResourceCollectionWrapper) getCheckedRef()).iterator();
86         }
87         dieOnCircularReference();
88         return new FailFast(this, cacheCollection().iterator());
89     }
90
91     /**
92      * Fulfill the ResourceCollection contract.
93      * @return number of elements as int.
94      */

95     public synchronized int size() {
96         if (isReference()) {
97             return ((BaseResourceCollectionWrapper) getCheckedRef()).size();
98         }
99         dieOnCircularReference();
100         return cacheCollection().size();
101     }
102
103     /**
104      * Fulfill the ResourceCollection contract.
105      * @return whether this is a filesystem-only resource collection.
106      */

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         /* now check each Resource in case the child only
117            lets through files from any children IT may have: */

118         for (Iterator JavaDoc i = cacheCollection().iterator(); i.hasNext();) {
119             if (!(i.next() instanceof FileResource)) {
120                 return false;
121             }
122         }
123         return true;
124     }
125
126     /**
127      * Overrides the version of DataType to recurse on all DataType
128      * child elements that may have been added.
129      * @param stk the stack of data types to use (recursively).
130      * @param p the project to use to dereference the references.
131      * @throws BuildException on error.
132      */

133     protected synchronized void dieOnCircularReference(Stack JavaDoc 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     /**
151      * Get the nested ResourceCollection.
152      * @return a ResourceCollection.
153      * @throws BuildException if no nested ResourceCollection has been provided.
154      */

155     protected final synchronized ResourceCollection getResourceCollection() {
156         dieOnCircularReference();
157         if (rc == null) {
158             throw oneNested();
159         }
160         return rc;
161     }
162
163     /**
164      * Template method for subclasses to return a Collection of Resources.
165      * @return Collection.
166      */

167     protected abstract Collection JavaDoc getCollection();
168
169     /**
170      * Format this BaseResourceCollectionWrapper as a String.
171      * @return a descriptive <code>String</code>.
172      */

173     public synchronized String JavaDoc toString() {
174         if (isReference()) {
175             return getCheckedRef().toString();
176         }
177         if (cacheCollection().size() == 0) {
178             return "";
179         }
180         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
181         for (Iterator JavaDoc 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 JavaDoc 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