KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > types > DummyFileSet


1 /*
2  * The Apache Software License, Version 1.1
3  *
4  * Copyright (c) 2002-2003 The Apache Software Foundation. All rights
5  * reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  *
14  * 2. Redistributions in binary form must reproduce the above copyright
15  * notice, this list of conditions and the following disclaimer in
16  * the documentation and/or other materials provided with the
17  * distribution.
18  *
19  * 3. The end-user documentation included with the redistribution, if
20  * any, must include the following acknowlegement:
21  * "This product includes software developed by the
22  * Apache Software Foundation (http://www.apache.org/)."
23  * Alternately, this acknowlegement may appear in the software itself,
24  * if and wherever such third-party acknowlegements normally appear.
25  *
26  * 4. The names "Ant" and "Apache Software
27  * Foundation" must not be used to endorse or promote products derived
28  * from this software without prior written permission. For written
29  * permission, please contact apache@apache.org.
30  *
31  * 5. Products derived from this software may not be called "Apache"
32  * nor may "Apache" appear in their names without prior written
33  * permission of the Apache Group.
34  *
35  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
36  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
37  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
38  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
39  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
42  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
43  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
44  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
45  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
46  * SUCH DAMAGE.
47  * ====================================================================
48  *
49  * This software consists of voluntary contributions made by many
50  * individuals on behalf of the Apache Software Foundation. For more
51  * information on the Apache Software Foundation, please see
52  * <http://www.apache.org/>.
53  */

54 package org.apache.tools.ant.types;
55
56 import java.io.File JavaDoc;
57 import java.util.Enumeration JavaDoc;
58 import java.util.Hashtable JavaDoc;
59 import java.util.Stack JavaDoc;
60 import java.util.Vector JavaDoc;
61
62 import org.apache.tools.ant.BuildException;
63 import org.apache.tools.ant.DirectoryScanner;
64 import org.apache.tools.ant.FileScanner;
65 import org.apache.tools.ant.Project;
66 import org.apache.tools.ant.types.AbstractFileSet;
67 import org.apache.tools.ant.types.DataType;
68 import org.apache.tools.ant.types.PatternSet;
69 import org.apache.tools.ant.types.Reference;
70 import org.apache.tools.ant.types.selectors.AndSelector;
71 import org.apache.tools.ant.types.selectors.ContainsRegexpSelector;
72 import org.apache.tools.ant.types.selectors.ContainsSelector;
73 import org.apache.tools.ant.types.selectors.DateSelector;
74 import org.apache.tools.ant.types.selectors.DependSelector;
75 import org.apache.tools.ant.types.selectors.DepthSelector;
76 import org.apache.tools.ant.types.selectors.DifferentSelector;
77 import org.apache.tools.ant.types.selectors.ExtendSelector;
78 import org.apache.tools.ant.types.selectors.FileSelector;
79 import org.apache.tools.ant.types.selectors.FilenameSelector;
80 import org.apache.tools.ant.types.selectors.MajoritySelector;
81 import org.apache.tools.ant.types.selectors.NoneSelector;
82 import org.apache.tools.ant.types.selectors.NotSelector;
83 import org.apache.tools.ant.types.selectors.OrSelector;
84 import org.apache.tools.ant.types.selectors.PresentSelector;
85 import org.apache.tools.ant.types.selectors.SelectSelector;
86 import org.apache.tools.ant.types.selectors.SelectorContainer;
87 import org.apache.tools.ant.types.selectors.SelectorScanner;
88 import org.apache.tools.ant.types.selectors.SizeSelector;
89 import org.apache.tools.ant.types.selectors.TypeSelector;
90 import org.apache.tools.ant.types.selectors.modifiedselector.ModifiedSelector;
91
92 /**
93  * Class that holds an implicit patternset and supports nested
94  * patternsets and creates a DirectoryScanner using these patterns.
95  *
96  * <p>Common base class for DirSet and FileSet.</p>
97  *
98  * @author <a HREF="mailto:ajkuiper@wxs.nl">Arnout J. Kuiper</a>
99  * @author <a HREF="mailto:stefano@apache.org">Stefano Mazzocchi</a>
100  * @author <a HREF="mailto:rubys@us.ibm.com">Sam Ruby</a>
101  * @author <a HREF="mailto:jon@clearink.com">Jon S. Stevens</a>
102  * @author Stefan Bodewig
103  * @author Magesh Umasankar
104  * @author <a HREF="mailto:bruce@callenish.com">Bruce Atherton</a>
105  * @author <a HREF="mailto:martijn@kruithof.xs4all.nl">Martijn Kruithof</a>
106  */

107 public class DummyFileSet extends DataType implements Cloneable JavaDoc,
108         SelectorContainer {
109
110     private PatternSet defaultPatterns = new PatternSet();
111     private Vector JavaDoc additionalPatterns = new Vector JavaDoc();
112     private Vector JavaDoc selectors = new Vector JavaDoc();
113
114     private File JavaDoc dir;
115     private boolean useDefaultExcludes = true;
116     private boolean isCaseSensitive = true;
117     private boolean followSymlinks = true;
118
119     public DummyFileSet() {
120         super();
121     }
122
123     /**
124      * Makes this instance in effect a reference to another instance.
125      *
126      * <p>You must not set another attribute or nest elements inside
127      * this element if you make it a reference.</p>
128      */

129     public void setRefid(Reference r) throws BuildException {
130         if (dir != null || defaultPatterns.hasPatterns(getProject())) {
131             throw tooManyAttributes();
132         }
133         if (!additionalPatterns.isEmpty()) {
134             throw noChildrenAllowed();
135         }
136         if (!selectors.isEmpty()) {
137             throw noChildrenAllowed();
138         }
139         super.setRefid(r);
140     }
141
142     /**
143      * Sets the base-directory for this instance.
144      */

145     public void setDir(File JavaDoc dir) throws BuildException {
146         if (isReference()) {
147             throw tooManyAttributes();
148         }
149
150         this.dir = dir;
151     }
152
153     /**
154      * Retrieves the base-directory for this instance.
155      */

156     public File JavaDoc getDir(Project p) {
157         if (isReference()) {
158             return getRef(p).getDir(p);
159         }
160         return dir;
161     }
162
163     /**
164      * Creates a nested patternset.
165      */

166     public PatternSet createPatternSet() {
167         if (isReference()) {
168             throw noChildrenAllowed();
169         }
170         PatternSet patterns = new PatternSet();
171         additionalPatterns.addElement(patterns);
172         return patterns;
173     }
174
175     /**
176      * add a name entry on the include list
177      */

178     public PatternSet.NameEntry createInclude() {
179         if (isReference()) {
180             throw noChildrenAllowed();
181         }
182         return defaultPatterns.createInclude();
183     }
184
185     /**
186      * add a name entry on the include files list
187      */

188     public PatternSet.NameEntry createIncludesFile() {
189         if (isReference()) {
190             throw noChildrenAllowed();
191         }
192         return defaultPatterns.createIncludesFile();
193     }
194
195     /**
196      * add a name entry on the exclude list
197      */

198     public PatternSet.NameEntry createExclude() {
199         if (isReference()) {
200             throw noChildrenAllowed();
201         }
202         return defaultPatterns.createExclude();
203     }
204
205     /**
206      * add a name entry on the excludes files list
207      */

208     public PatternSet.NameEntry createExcludesFile() {
209         if (isReference()) {
210             throw noChildrenAllowed();
211         }
212         return defaultPatterns.createExcludesFile();
213     }
214
215     /**
216      * Creates a single file fileset.
217      */

218     public void setFile(File JavaDoc file) {
219         if (isReference()) {
220             throw tooManyAttributes();
221         }
222         setDir(file.getParentFile());
223
224         PatternSet.NameEntry include = createInclude();
225         include.setName(file.getName());
226     }
227
228     /**
229      * Appends <code>includes</code> to the current list of include
230      * patterns.
231      *
232      * <p>Patterns may be separated by a comma or a space.</p>
233      *
234      * @param includes the string containing the include patterns
235      */

236     public void setIncludes(String JavaDoc includes) {
237         if (isReference()) {
238             throw tooManyAttributes();
239         }
240
241         defaultPatterns.setIncludes(includes);
242     }
243
244     /**
245      * Appends <code>excludes</code> to the current list of exclude
246      * patterns.
247      *
248      * <p>Patterns may be separated by a comma or a space.</p>
249      *
250      * @param excludes the string containing the exclude patterns
251      */

252     public void setExcludes(String JavaDoc excludes) {
253         if (isReference()) {
254             throw tooManyAttributes();
255         }
256
257         defaultPatterns.setExcludes(excludes);
258     }
259
260     /**
261      * Sets the name of the file containing the includes patterns.
262      *
263      * @param incl The file to fetch the include patterns from.
264      */

265      public void setIncludesfile(File JavaDoc incl) throws BuildException {
266          if (isReference()) {
267              throw tooManyAttributes();
268          }
269
270          defaultPatterns.setIncludesfile(incl);
271      }
272
273     /**
274      * Sets the name of the file containing the excludes patterns.
275      *
276      * @param excl The file to fetch the exclude patterns from.
277      */

278      public void setExcludesfile(File JavaDoc excl) throws BuildException {
279          if (isReference()) {
280              throw tooManyAttributes();
281          }
282
283          defaultPatterns.setExcludesfile(excl);
284      }
285
286     /**
287      * Sets whether default exclusions should be used or not.
288      *
289      * @param useDefaultExcludes "true"|"on"|"yes" when default exclusions
290      * should be used, "false"|"off"|"no" when they
291      * shouldn't be used.
292      */

293     public void setDefaultexcludes(boolean useDefaultExcludes) {
294         if (isReference()) {
295             throw tooManyAttributes();
296         }
297
298         this.useDefaultExcludes = useDefaultExcludes;
299     }
300
301     /**
302      * Sets case sensitivity of the file system
303      *
304      * @param isCaseSensitive "true"|"on"|"yes" if file system is case
305      * sensitive, "false"|"off"|"no" when not.
306      */

307     public void setCaseSensitive(boolean isCaseSensitive) {
308         if (isReference()) {
309             throw tooManyAttributes();
310         }
311         this.isCaseSensitive = isCaseSensitive;
312     }
313
314     /**
315      * Sets whether or not symbolic links should be followed.
316      *
317      * @param followSymlinks whether or not symbolic links should be followed
318      */

319     public void setFollowSymlinks(boolean followSymlinks) {
320         if (isReference()) {
321             throw tooManyAttributes();
322         }
323         this.followSymlinks = followSymlinks;
324     }
325
326     /**
327      * find out if the fileset wants to follow symbolic links
328      *
329      * @return flag indicating whether or not symbolic links should be followed
330      *
331      * @since ant 1.6
332      */

333     public boolean isFollowSymlinks() {
334         if (isReference()) {
335             return getRef(getProject()).isFollowSymlinks();
336         } else {
337             return followSymlinks;
338         }
339     }
340
341     /**
342      * sets the name used for this datatype instance.
343      */

344     protected String JavaDoc getDataTypeName() {
345         // look up the types in project and see if they match this class
346
Project project = getProject();
347         if (project != null) {
348             Hashtable JavaDoc typedefs = project.getDataTypeDefinitions();
349             for (Enumeration JavaDoc e = typedefs.keys(); e.hasMoreElements();) {
350                 String JavaDoc typeName = (String JavaDoc) e.nextElement();
351                 Class JavaDoc typeClass = (Class JavaDoc) typedefs.get(typeName);
352                 if (typeClass == getClass()) {
353                     return typeName;
354                 }
355             }
356         }
357
358         String JavaDoc classname = getClass().getName();
359
360         int dotIndex = classname.lastIndexOf(".");
361         if (dotIndex == -1) {
362             return classname;
363         }
364         return classname.substring(dotIndex + 1);
365     }
366
367     /**
368      * Returns the directory scanner needed to access the files to process.
369      */

370     public DirectoryScanner getDirectoryScanner(Project p) {
371         if (isReference()) {
372             return getRef(p).getDirectoryScanner(p);
373         }
374
375         if (dir == null) {
376             throw new BuildException("No directory specified for "
377                                      + getDataTypeName() + ".");
378         }
379
380         if (!dir.exists()) {
381             throw new BuildException(dir.getAbsolutePath() + " not found.");
382         }
383         if (!dir.isDirectory()) {
384             throw new BuildException(dir.getAbsolutePath()
385                                      + " is not a directory.");
386         }
387
388         DirectoryScanner ds = new DirectoryScanner();
389         setupDirectoryScanner(ds, p);
390         ds.setFollowSymlinks(followSymlinks);
391         ds.scan();
392         return ds;
393     }
394
395     public void setupDirectoryScanner(FileScanner ds, Project p) {
396         if (isReference()) {
397             getRef(p).setupDirectoryScanner(ds, p);
398             return;
399         }
400
401         if (ds == null) {
402             throw new IllegalArgumentException JavaDoc("ds cannot be null");
403         }
404
405         ds.setBasedir(dir);
406
407         final int count = additionalPatterns.size();
408         for (int i = 0; i < count; i++) {
409             Object JavaDoc o = additionalPatterns.elementAt(i);
410             defaultPatterns.append((PatternSet) o, p);
411         }
412
413         p.log(getDataTypeName() + ": Setup scanner in dir " + dir
414             + " with " + defaultPatterns, Project.MSG_DEBUG);
415
416         ds.setIncludes(defaultPatterns.getIncludePatterns(p));
417         ds.setExcludes(defaultPatterns.getExcludePatterns(p));
418         if (ds instanceof SelectorScanner) {
419             SelectorScanner ss = (SelectorScanner) ds;
420             ss.setSelectors(getSelectors(p));
421         }
422
423         if (useDefaultExcludes) {
424             ds.addDefaultExcludes();
425         }
426         ds.setCaseSensitive(isCaseSensitive);
427     }
428
429     /**
430      * Performs the check for circular references and returns the
431      * referenced FileSet.
432      */

433     protected AbstractFileSet getRef(Project p) {
434         if (!isChecked()) {
435             Stack JavaDoc stk = new Stack JavaDoc();
436             stk.push(this);
437             dieOnCircularReference(stk, p);
438         }
439
440         Object JavaDoc o = getRefid().getReferencedObject(p);
441         if (!getClass().isAssignableFrom(o.getClass())) {
442             String JavaDoc msg = getRefid().getRefId() + " doesn\'t denote a "
443                 + getDataTypeName();
444             throw new BuildException(msg);
445         } else {
446             return (AbstractFileSet) o;
447         }
448     }
449
450     // SelectorContainer methods
451

452     /**
453      * Indicates whether there are any selectors here.
454      *
455      * @return whether any selectors are in this container
456      */

457     public boolean hasSelectors() {
458         if (isReference() && getProject() != null) {
459             return getRef(getProject()).hasSelectors();
460         }
461         return !(selectors.isEmpty());
462     }
463
464     /**
465      * Indicates whether there are any patterns here.
466      *
467      * @return whether any patterns are in this container
468      */

469     public boolean hasPatterns() {
470         if (isReference() && getProject() != null) {
471             return getRef(getProject()).hasPatterns();
472         }
473
474         if (defaultPatterns.hasPatterns(getProject())) {
475             return true;
476         }
477
478         Enumeration JavaDoc e = additionalPatterns.elements();
479         while (e.hasMoreElements()) {
480             PatternSet ps = (PatternSet) e.nextElement();
481             if (ps.hasPatterns(getProject())) {
482                 return true;
483             }
484         }
485
486         return false;
487     }
488
489     /**
490      * Gives the count of the number of selectors in this container
491      *
492      * @return the number of selectors in this container
493      */

494     public int selectorCount() {
495         if (isReference() && getProject() != null) {
496             return getRef(getProject()).selectorCount();
497         }
498         return selectors.size();
499     }
500
501     /**
502      * Returns the set of selectors as an array.
503      *
504      * @return an array of selectors in this container
505      */

506     public FileSelector[] getSelectors(Project p) {
507         if (isReference()) {
508             return getRef(p).getSelectors(p);
509         } else {
510             FileSelector[] result = new FileSelector[selectors.size()];
511             selectors.copyInto(result);
512             return result;
513         }
514     }
515
516     /**
517      * Returns an enumerator for accessing the set of selectors.
518      *
519      * @return an enumerator that goes through each of the selectors
520      */

521     public Enumeration JavaDoc selectorElements() {
522         if (isReference() && getProject() != null) {
523             return getRef(getProject()).selectorElements();
524         }
525         return selectors.elements();
526     }
527
528     /**
529      * Add a new selector into this container.
530      *
531      * @param selector the new selector to add
532      */

533     public void appendSelector(FileSelector selector) {
534         if (isReference()) {
535             throw noChildrenAllowed();
536         }
537         selectors.addElement(selector);
538     }
539
540     /* Methods below all add specific selectors */
541
542     /**
543      * add a "Select" selector entry on the selector list
544      * @param selector the selector to add
545      */

546     public void addSelector(SelectSelector selector) {
547         appendSelector(selector);
548     }
549
550     /**
551      * add an "And" selector entry on the selector list
552      * @param selector the selector to add
553      */

554     public void addAnd(AndSelector selector) {
555         appendSelector(selector);
556     }
557
558     /**
559      * add an "Or" selector entry on the selector list
560      * @param selector the selector to add
561      */

562     public void addOr(OrSelector selector) {
563         appendSelector(selector);
564     }
565
566     /**
567      * add a "Not" selector entry on the selector list
568      * @param selector the selector to add
569      */

570     public void addNot(NotSelector selector) {
571         appendSelector(selector);
572     }
573
574     /**
575      * add a "None" selector entry on the selector list
576      * @param selector the selector to add
577      */

578     public void addNone(NoneSelector selector) {
579         appendSelector(selector);
580     }
581
582     /**
583      * add a majority selector entry on the selector list
584      * @param selector the selector to add
585      */

586     public void addMajority(MajoritySelector selector) {
587         appendSelector(selector);
588     }
589
590     /**
591      * add a selector date entry on the selector list
592      * @param selector the selector to add
593      */

594     public void addDate(DateSelector selector) {
595         appendSelector(selector);
596     }
597
598     /**
599      * add a selector size entry on the selector list
600      * @param selector the selector to add
601      */

602     public void addSize(SizeSelector selector) {
603         appendSelector(selector);
604     }
605
606     /**
607      * add a DifferentSelector entry on the selector list
608      * @param selector the selector to add
609      */

610     public void addDifferent(DifferentSelector selector) {
611         appendSelector(selector);
612     }
613
614     /**
615      * add a selector filename entry on the selector list
616      * @param selector the selector to add
617      */

618     public void addFilename(FilenameSelector selector) {
619         appendSelector(selector);
620     }
621
622     /**
623      * add a selector type entry on the selector list
624      * @param selector the selector to add
625      */

626     public void addType(TypeSelector selector) {
627         appendSelector(selector);
628     }
629
630     /**
631      * add an extended selector entry on the selector list
632      * @param selector the selector to add
633      */

634     public void addCustom(ExtendSelector selector) {
635         appendSelector(selector);
636     }
637
638     /**
639      * add a contains selector entry on the selector list
640      * @param selector the selector to add
641      */

642     public void addContains(ContainsSelector selector) {
643         appendSelector(selector);
644     }
645
646     /**
647      * add a present selector entry on the selector list
648      * @param selector the selector to add
649      */

650     public void addPresent(PresentSelector selector) {
651         appendSelector(selector);
652     }
653
654     /**
655      * add a depth selector entry on the selector list
656      * @param selector the selector to add
657      */

658     public void addDepth(DepthSelector selector) {
659         appendSelector(selector);
660     }
661
662     /**
663      * add a depends selector entry on the selector list
664      * @param selector the selector to add
665      */

666     public void addDepend(DependSelector selector) {
667         appendSelector(selector);
668     }
669
670     /**
671      * add a regular expression selector entry on the selector list
672      * @param selector the selector to add
673      */

674     public void addContainsRegexp(ContainsRegexpSelector selector) {
675         appendSelector(selector);
676     }
677
678     /**
679      * add the modified selector
680      * @param selector the selector to add
681      * @since ant 1.6
682      */

683     public void addModified(ModifiedSelector selector) {
684         appendSelector(selector);
685     }
686
687     /**
688      * add an arbitary selector
689      * @param selector the selector to add
690      * @since Ant 1.6
691      */

692     public void add(FileSelector selector) {
693         appendSelector(selector);
694     }
695
696     /**
697      * Changed to just list out the patterns
698      *
699      * @return String object with included filenames
700      */

701     public String JavaDoc toString() {
702         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
703         sb.append("DefaultPatternset:\n");
704         sb.append(defaultPatterns.toString());
705         return sb.toString();
706     }
707
708 }
709
Popular Tags