KickJava   Java API By Example, From Geeks To Geeks.

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


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;
20
21 import java.io.File JavaDoc;
22 import java.util.StringTokenizer JavaDoc;
23 import java.util.Vector JavaDoc;
24 import java.util.Iterator JavaDoc;
25
26 import org.apache.tools.ant.Project;
27 import org.apache.tools.ant.BuildException;
28 import org.apache.tools.ant.types.resources.FileResourceIterator;
29
30 /**
31  * FileList represents an explicitly named list of files. FileLists
32  * are useful when you want to capture a list of files regardless of
33  * whether they currently exist. By contrast, FileSet operates as a
34  * filter, only returning the name of a matched file if it currently
35  * exists in the file system.
36  */

37 public class FileList extends DataType implements ResourceCollection {
38
39     private Vector JavaDoc filenames = new Vector JavaDoc();
40     private File JavaDoc dir;
41
42     /**
43      * The default constructor.
44      *
45      */

46     public FileList() {
47         super();
48     }
49
50     /**
51      * A copy constructor.
52      *
53      * @param filelist a <code>FileList</code> value
54      */

55     protected FileList(FileList filelist) {
56         this.dir = filelist.dir;
57         this.filenames = filelist.filenames;
58         setProject(filelist.getProject());
59     }
60
61     /**
62      * Makes this instance in effect a reference to another FileList
63      * instance.
64      *
65      * <p>You must not set another attribute or nest elements inside
66      * this element if you make it a reference.</p>
67      * @param r the reference to another filelist.
68      * @exception BuildException if an error occurs.
69      */

70     public void setRefid(Reference r) throws BuildException {
71         if ((dir != null) || (filenames.size() != 0)) {
72             throw tooManyAttributes();
73         }
74         super.setRefid(r);
75     }
76
77     /**
78      * Set the dir attribute.
79      *
80      * @param dir the directory this filelist is relative to.
81      * @exception BuildException if an error occurs
82      */

83     public void setDir(File JavaDoc dir) throws BuildException {
84         checkAttributesAllowed();
85         this.dir = dir;
86     }
87
88     /**
89      * @param p the current project
90      * @return the directory attribute
91      */

92     public File JavaDoc getDir(Project p) {
93         if (isReference()) {
94             return getRef(p).getDir(p);
95         }
96         return dir;
97     }
98
99     /**
100      * Set the filenames attribute.
101      *
102      * @param filenames a string contains filenames, separated by , or
103      * by whitespace.
104      */

105     public void setFiles(String JavaDoc filenames) {
106         checkAttributesAllowed();
107         if (filenames != null && filenames.length() > 0) {
108             StringTokenizer JavaDoc tok = new StringTokenizer JavaDoc(
109                 filenames, ", \t\n\r\f", false);
110             while (tok.hasMoreTokens()) {
111                this.filenames.addElement(tok.nextToken());
112             }
113         }
114     }
115
116     /**
117      * Returns the list of files represented by this FileList.
118      * @param p the current project
119      * @return the list of files represented by this FileList.
120      */

121     public String JavaDoc[] getFiles(Project p) {
122         if (isReference()) {
123             return getRef(p).getFiles(p);
124         }
125
126         if (dir == null) {
127             throw new BuildException("No directory specified for filelist.");
128         }
129
130         if (filenames.size() == 0) {
131             throw new BuildException("No files specified for filelist.");
132         }
133
134         String JavaDoc[] result = new String JavaDoc[filenames.size()];
135         filenames.copyInto(result);
136         return result;
137     }
138
139     /**
140      * Performs the check for circular references and returns the
141      * referenced FileList.
142      * @param p the current project
143      * @return the FileList represented by a referenced filelist.
144      */

145     protected FileList getRef(Project p) {
146         return (FileList) getCheckedRef(p);
147     }
148
149     /**
150      * Inner class corresponding to the &lt;file&gt; nested element.
151      */

152     public static class FileName {
153         private String JavaDoc name;
154
155         /**
156          * The name attribute of the file element.
157          *
158          * @param name the name of a file to add to the file list.
159          */

160         public void setName(String JavaDoc name) {
161             this.name = name;
162         }
163
164         /**
165          * @return the name of the file for this element.
166          */

167         public String JavaDoc getName() {
168             return name;
169         }
170     }
171
172     /**
173      * Add a nested &lt;file&gt; nested element.
174      *
175      * @param name a configured file element with a name.
176      * @since Ant 1.6.2
177      */

178     public void addConfiguredFile(FileName name) {
179         if (name.getName() == null) {
180             throw new BuildException(
181                 "No name specified in nested file element");
182         }
183         filenames.addElement(name.getName());
184     }
185
186     /**
187      * Fulfill the ResourceCollection contract.
188      * @return an Iterator of Resources.
189      * @since Ant 1.7
190      */

191     public Iterator JavaDoc iterator() {
192         if (isReference()) {
193             return ((FileList) getRef(getProject())).iterator();
194         }
195         return new FileResourceIterator(dir,
196             (String JavaDoc[]) (filenames.toArray(new String JavaDoc[filenames.size()])));
197     }
198
199     /**
200      * Fulfill the ResourceCollection contract.
201      * @return number of elements as int.
202      * @since Ant 1.7
203      */

204     public int size() {
205         if (isReference()) {
206             return ((FileList) getRef(getProject())).size();
207         }
208         return filenames.size();
209     }
210
211     /**
212      * Always returns true.
213      * @return true indicating that all elements will be FileResources.
214      * @since Ant 1.7
215      */

216     public boolean isFilesystemOnly() {
217         return true;
218     }
219
220 }
221
Popular Tags