KickJava   Java API By Example, From Geeks To Geeks.

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


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.Vector JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.Collections JavaDoc;
24
25 import org.apache.tools.ant.Project;
26 import org.apache.tools.ant.BuildException;
27 import org.apache.tools.ant.DirectoryScanner;
28 import org.apache.tools.ant.types.Reference;
29 import org.apache.tools.ant.types.PatternSet;
30 import org.apache.tools.ant.types.ResourceCollection;
31 import org.apache.tools.ant.types.selectors.FileSelector;
32 import org.apache.tools.ant.types.selectors.AbstractSelectorContainer;
33
34 /**
35  * ResourceCollection implementation; like AbstractFileSet with absolute paths.
36  * @since Ant 1.7
37  */

38 public class Files extends AbstractSelectorContainer
39     implements Cloneable JavaDoc, ResourceCollection {
40
41     private static final Iterator JavaDoc EMPTY_ITERATOR
42         = Collections.EMPTY_SET.iterator();
43
44     private PatternSet defaultPatterns = new PatternSet();
45     private Vector JavaDoc additionalPatterns = new Vector JavaDoc();
46     private Vector JavaDoc selectors = new Vector JavaDoc();
47
48     private boolean useDefaultExcludes = true;
49     private boolean caseSensitive = true;
50     private boolean followSymlinks = true;
51
52     /* cached DirectoryScanner instance */
53     private DirectoryScanner ds = null;
54
55     /**
56      * Construct a new <code>Files</code> collection.
57      */

58     public Files() {
59         super();
60     }
61
62     /**
63      * Construct a new <code>Files</code> collection, shallowly cloned
64      * from the specified <code>Files</code>.
65      * @param f the <code>Files</code> to use as a template.
66      */

67     protected Files(Files f) {
68         this.defaultPatterns = f.defaultPatterns;
69         this.additionalPatterns = f.additionalPatterns;
70         this.selectors = f.selectors;
71         this.useDefaultExcludes = f.useDefaultExcludes;
72         this.caseSensitive = f.caseSensitive;
73         this.followSymlinks = f.followSymlinks;
74         this.ds = f.ds;
75         setProject(f.getProject());
76     }
77
78     /**
79      * Make this instance in effect a reference to another instance.
80      *
81      * <p>You must not set another attribute or nest elements inside
82      * this element if you make it a reference.</p>
83      * @param r the <code>Reference</code> to use.
84      * @throws BuildException if there is a problem.
85      */

86     public void setRefid(Reference r) throws BuildException {
87         if (hasPatterns(defaultPatterns)) {
88             throw tooManyAttributes();
89         }
90         if (!additionalPatterns.isEmpty()) {
91             throw noChildrenAllowed();
92         }
93         if (!selectors.isEmpty()) {
94             throw noChildrenAllowed();
95         }
96         super.setRefid(r);
97     }
98
99     /**
100      * Create a nested patternset.
101      * @return <code>PatternSet</code>.
102      */

103     public synchronized PatternSet createPatternSet() {
104         if (isReference()) {
105             throw noChildrenAllowed();
106         }
107         PatternSet patterns = new PatternSet();
108         additionalPatterns.addElement(patterns);
109         ds = null;
110         return patterns;
111     }
112
113     /**
114      * Add a name entry to the include list.
115      * @return <code>PatternSet.NameEntry</code>.
116      */

117     public synchronized PatternSet.NameEntry createInclude() {
118         if (isReference()) {
119             throw noChildrenAllowed();
120         }
121         ds = null;
122         return defaultPatterns.createInclude();
123     }
124
125     /**
126      * Add a name entry to the include files list.
127      * @return <code>PatternSet.NameEntry</code>.
128      */

129     public synchronized PatternSet.NameEntry createIncludesFile() {
130         if (isReference()) {
131             throw noChildrenAllowed();
132         }
133         ds = null;
134         return defaultPatterns.createIncludesFile();
135     }
136
137     /**
138      * Add a name entry to the exclude list.
139      * @return <code>PatternSet.NameEntry</code>.
140      */

141     public synchronized PatternSet.NameEntry createExclude() {
142         if (isReference()) {
143             throw noChildrenAllowed();
144         }
145         ds = null;
146         return defaultPatterns.createExclude();
147     }
148
149     /**
150      * Add a name entry to the excludes files list.
151      * @return <code>PatternSet.NameEntry</code>.
152      */

153     public synchronized PatternSet.NameEntry createExcludesFile() {
154         if (isReference()) {
155             throw noChildrenAllowed();
156         }
157         ds = null;
158         return defaultPatterns.createExcludesFile();
159     }
160
161     /**
162      * Append <code>includes</code> to the current list of include
163      * patterns.
164      *
165      * <p>Patterns may be separated by a comma or a space.</p>
166      *
167      * @param includes the <code>String</code> containing the include patterns.
168      */

169     public synchronized void setIncludes(String JavaDoc includes) {
170         checkAttributesAllowed();
171         defaultPatterns.setIncludes(includes);
172         ds = null;
173     }
174
175     /**
176      * Append <code>includes</code> to the current list of include
177      * patterns.
178      *
179      * @param includes array containing the include patterns.
180      */

181     public synchronized void appendIncludes(String JavaDoc[] includes) {
182         checkAttributesAllowed();
183         if (includes != null) {
184             for (int i = 0; i < includes.length; i++) {
185                 defaultPatterns.createInclude().setName(includes[i]);
186             }
187             ds = null;
188         }
189     }
190
191     /**
192      * Append <code>excludes</code> to the current list of exclude
193      * patterns.
194      *
195      * <p>Patterns may be separated by a comma or a space.</p>
196      *
197      * @param excludes the <code>String</code> containing the exclude patterns.
198      */

199     public synchronized void setExcludes(String JavaDoc excludes) {
200         checkAttributesAllowed();
201         defaultPatterns.setExcludes(excludes);
202         ds = null;
203     }
204
205     /**
206      * Append <code>excludes</code> to the current list of include
207      * patterns.
208      *
209      * @param excludes array containing the exclude patterns.
210      */

211     public synchronized void appendExcludes(String JavaDoc[] excludes) {
212         checkAttributesAllowed();
213         if (excludes != null) {
214             for (int i = 0; i < excludes.length; i++) {
215                 defaultPatterns.createExclude().setName(excludes[i]);
216             }
217             ds = null;
218         }
219     }
220
221     /**
222      * Set the <code>File</code> containing the includes patterns.
223      *
224      * @param incl <code>File</code> instance.
225      * @throws BuildException if there is a problem.
226      */

227     public synchronized void setIncludesfile(File JavaDoc incl) throws BuildException {
228         checkAttributesAllowed();
229         defaultPatterns.setIncludesfile(incl);
230         ds = null;
231     }
232
233     /**
234      * Set the <code>File</code> containing the excludes patterns.
235      *
236      * @param excl <code>File</code> instance.
237      * @throws BuildException if there is a problem.
238      */

239     public synchronized void setExcludesfile(File JavaDoc excl) throws BuildException {
240         checkAttributesAllowed();
241         defaultPatterns.setExcludesfile(excl);
242         ds = null;
243     }
244
245     /**
246      * Set whether default exclusions should be used or not.
247      *
248      * @param useDefaultExcludes <code>boolean</code>.
249      */

250     public synchronized void setDefaultexcludes(boolean useDefaultExcludes) {
251         checkAttributesAllowed();
252         this.useDefaultExcludes = useDefaultExcludes;
253         ds = null;
254     }
255
256     /**
257      * Get whether default exclusions should be used or not.
258      * @return the defaultexclusions value.
259      */

260     public synchronized boolean getDefaultexcludes() {
261         return (isReference())
262             ? getRef().getDefaultexcludes() : useDefaultExcludes;
263     }
264
265     /**
266      * Set case-sensitivity of the Files collection.
267      *
268      * @param caseSensitive <code>boolean</code>.
269      */

270     public synchronized void setCaseSensitive(boolean caseSensitive) {
271         checkAttributesAllowed();
272         this.caseSensitive = caseSensitive;
273         ds = null;
274     }
275
276     /**
277      * Find out if this Files collection is case-sensitive.
278      *
279      * @return <code>boolean</code> indicating whether the Files
280      * collection is case-sensitive.
281      */

282     public synchronized boolean isCaseSensitive() {
283         return (isReference())
284             ? getRef().isCaseSensitive() : caseSensitive;
285     }
286
287     /**
288      * Set whether or not symbolic links should be followed.
289      *
290      * @param followSymlinks whether or not symbolic links should be followed.
291      */

292     public synchronized void setFollowSymlinks(boolean followSymlinks) {
293         checkAttributesAllowed();
294         this.followSymlinks = followSymlinks;
295         ds = null;
296     }
297
298     /**
299      * Find out whether symbolic links should be followed.
300      *
301      * @return <code>boolean</code> indicating whether symbolic links
302      * should be followed.
303      */

304     public synchronized boolean isFollowSymlinks() {
305         return (isReference())
306             ? getRef().isFollowSymlinks() : followSymlinks;
307     }
308
309     /**
310      * Fulfill the ResourceCollection contract.
311      * @return an Iterator of Resources.
312      */

313     public synchronized Iterator JavaDoc iterator() {
314         if (isReference()) {
315             return getRef().iterator();
316         }
317         ensureDirectoryScannerSetup();
318         ds.scan();
319         int fct = ds.getIncludedFilesCount();
320         int dct = ds.getIncludedDirsCount();
321         if (fct + dct == 0) {
322             return EMPTY_ITERATOR;
323         }
324         FileResourceIterator result = new FileResourceIterator();
325         if (fct > 0) {
326             result.addFiles(ds.getIncludedFiles());
327         }
328         if (dct > 0) {
329             result.addFiles(ds.getIncludedDirectories());
330         }
331         return result;
332     }
333
334     /**
335      * Fulfill the ResourceCollection contract.
336      * @return number of elements as int.
337      */

338     public synchronized int size() {
339         if (isReference()) {
340             return getRef().size();
341         }
342         ensureDirectoryScannerSetup();
343         ds.scan();
344         return ds.getIncludedFilesCount() + ds.getIncludedDirsCount();
345     }
346
347     /**
348      * Find out whether this Files collection has patterns.
349      *
350      * @return whether any patterns are in this container.
351      */

352     public synchronized boolean hasPatterns() {
353         if (isReference()) {
354             return getRef().hasPatterns();
355         }
356         if (hasPatterns(defaultPatterns)) {
357             return true;
358         }
359         for (Iterator JavaDoc i = additionalPatterns.iterator(); i.hasNext();) {
360             if (hasPatterns((PatternSet) i.next())) {
361                 return true;
362             }
363         }
364         return false;
365     }
366
367     /**
368      * Add a new selector into this container.
369      *
370      * @param selector the new <code>FileSelector</code> to add.
371      */

372     public synchronized void appendSelector(FileSelector selector) {
373         if (isReference()) {
374             throw noChildrenAllowed();
375         }
376         super.appendSelector(selector);
377         ds = null;
378     }
379
380     /**
381      * Format this Files collection as a String.
382      * @return a descriptive <code>String</code>.
383      */

384     public String JavaDoc toString() {
385         if (isReference()) {
386             return getRef().toString();
387         }
388         Iterator JavaDoc i = iterator();
389         if (!i.hasNext()) {
390             return "";
391         }
392         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
393         while (i.hasNext()) {
394             if (sb.length() > 0) {
395                 sb.append(File.pathSeparatorChar);
396             }
397             sb.append(i.next());
398         }
399         return sb.toString();
400     }
401
402     /**
403      * Create a deep clone of this instance, except for the nested selectors
404      * (the list of selectors is a shallow clone of this instance's list).
405      * @return a cloned Object.
406      */

407     public synchronized Object JavaDoc clone() {
408         if (isReference()) {
409             return getRef().clone();
410         }
411         try {
412             Files f = (Files) super.clone();
413             f.defaultPatterns = (PatternSet) defaultPatterns.clone();
414             f.additionalPatterns = new Vector JavaDoc(additionalPatterns.size());
415             for (Iterator JavaDoc iter = additionalPatterns.iterator(); iter.hasNext();) {
416                 PatternSet ps = (PatternSet) iter.next();
417                 f.additionalPatterns.add(ps.clone());
418             }
419             f.selectors = new Vector JavaDoc(selectors);
420             return f;
421         } catch (CloneNotSupportedException JavaDoc e) {
422             throw new BuildException(e);
423         }
424     }
425
426     /**
427      * Get the merged include patterns for this Files collection.
428      * @param p Project instance.
429      * @return the include patterns of the default pattern set and all
430      * nested patternsets.
431      */

432     public String JavaDoc[] mergeIncludes(Project p) {
433         return mergePatterns(p).getIncludePatterns(p);
434     }
435
436     /**
437      * Get the merged exclude patterns for this Files collection.
438      * @param p Project instance.
439      * @return the exclude patterns of the default pattern set and all
440      * nested patternsets.
441      */

442     public String JavaDoc[] mergeExcludes(Project p) {
443         return mergePatterns(p).getExcludePatterns(p);
444     }
445
446     /**
447      * Get the merged patterns for this Files collection.
448      * @param p Project instance.
449      * @return the default patternset merged with the additional sets
450      * in a new PatternSet instance.
451      */

452     public synchronized PatternSet mergePatterns(Project p) {
453         if (isReference()) {
454             return getRef().mergePatterns(p);
455         }
456         PatternSet ps = new PatternSet();
457         ps.append(defaultPatterns, p);
458         final int count = additionalPatterns.size();
459         for (int i = 0; i < count; i++) {
460             Object JavaDoc o = additionalPatterns.elementAt(i);
461             ps.append((PatternSet) o, p);
462         }
463         return ps;
464     }
465
466     /**
467      * Always returns true.
468      * @return true indicating that all elements of a Files collection
469      * will be FileResources.
470      */

471     public boolean isFilesystemOnly() {
472         return true;
473     }
474
475     /**
476      * Perform the check for circular references and return the
477      * referenced Files collection.
478      * @return <code>FileCollection</code>.
479      */

480     protected Files getRef() {
481         return (Files) getCheckedRef();
482     }
483
484     private synchronized void ensureDirectoryScannerSetup() {
485         if (ds == null) {
486             ds = new DirectoryScanner();
487             PatternSet ps = mergePatterns(getProject());
488             ds.setIncludes(ps.getIncludePatterns(getProject()));
489             ds.setExcludes(ps.getExcludePatterns(getProject()));
490             ds.setSelectors(getSelectors(getProject()));
491             if (useDefaultExcludes) {
492                 ds.addDefaultExcludes();
493             }
494             ds.setCaseSensitive(caseSensitive);
495             ds.setFollowSymlinks(followSymlinks);
496         }
497     }
498
499     private boolean hasPatterns(PatternSet ps) {
500         return ps.getIncludePatterns(getProject()).length > 0
501             || ps.getExcludePatterns(getProject()).length > 0;
502     }
503
504 }
505
Popular Tags