001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018:
019: package org.apache.tools.ant.types;
020:
021: import java.io.File;
022: import java.util.StringTokenizer;
023: import java.util.Vector;
024: import java.util.Iterator;
025:
026: import org.apache.tools.ant.Project;
027: import org.apache.tools.ant.BuildException;
028: import org.apache.tools.ant.types.resources.FileResourceIterator;
029:
030: /**
031: * FileList represents an explicitly named list of files. FileLists
032: * are useful when you want to capture a list of files regardless of
033: * whether they currently exist. By contrast, FileSet operates as a
034: * filter, only returning the name of a matched file if it currently
035: * exists in the file system.
036: */
037: public class FileList extends DataType implements ResourceCollection {
038:
039: private Vector filenames = new Vector();
040: private File dir;
041:
042: /**
043: * The default constructor.
044: *
045: */
046: public FileList() {
047: super ();
048: }
049:
050: /**
051: * A copy constructor.
052: *
053: * @param filelist a <code>FileList</code> value
054: */
055: protected FileList(FileList filelist) {
056: this .dir = filelist.dir;
057: this .filenames = filelist.filenames;
058: setProject(filelist.getProject());
059: }
060:
061: /**
062: * Makes this instance in effect a reference to another FileList
063: * instance.
064: *
065: * <p>You must not set another attribute or nest elements inside
066: * this element if you make it a reference.</p>
067: * @param r the reference to another filelist.
068: * @exception BuildException if an error occurs.
069: */
070: public void setRefid(Reference r) throws BuildException {
071: if ((dir != null) || (filenames.size() != 0)) {
072: throw tooManyAttributes();
073: }
074: super .setRefid(r);
075: }
076:
077: /**
078: * Set the dir attribute.
079: *
080: * @param dir the directory this filelist is relative to.
081: * @exception BuildException if an error occurs
082: */
083: public void setDir(File dir) throws BuildException {
084: checkAttributesAllowed();
085: this .dir = dir;
086: }
087:
088: /**
089: * @param p the current project
090: * @return the directory attribute
091: */
092: public File getDir(Project p) {
093: if (isReference()) {
094: return getRef(p).getDir(p);
095: }
096: return dir;
097: }
098:
099: /**
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 filenames) {
106: checkAttributesAllowed();
107: if (filenames != null && filenames.length() > 0) {
108: StringTokenizer tok = new StringTokenizer(filenames,
109: ", \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[] getFiles(Project p) {
122: if (isReference()) {
123: return getRef(p).getFiles(p);
124: }
125:
126: if (dir == null) {
127: throw new BuildException(
128: "No directory specified for filelist.");
129: }
130:
131: if (filenames.size() == 0) {
132: throw new BuildException("No files specified for filelist.");
133: }
134:
135: String[] result = new String[filenames.size()];
136: filenames.copyInto(result);
137: return result;
138: }
139:
140: /**
141: * Performs the check for circular references and returns the
142: * referenced FileList.
143: * @param p the current project
144: * @return the FileList represented by a referenced filelist.
145: */
146: protected FileList getRef(Project p) {
147: return (FileList) getCheckedRef(p);
148: }
149:
150: /**
151: * Inner class corresponding to the <file> nested element.
152: */
153: public static class FileName {
154: private String name;
155:
156: /**
157: * The name attribute of the file element.
158: *
159: * @param name the name of a file to add to the file list.
160: */
161: public void setName(String name) {
162: this .name = name;
163: }
164:
165: /**
166: * @return the name of the file for this element.
167: */
168: public String getName() {
169: return name;
170: }
171: }
172:
173: /**
174: * Add a nested <file> nested element.
175: *
176: * @param name a configured file element with a name.
177: * @since Ant 1.6.2
178: */
179: public void addConfiguredFile(FileName name) {
180: if (name.getName() == null) {
181: throw new BuildException(
182: "No name specified in nested file element");
183: }
184: filenames.addElement(name.getName());
185: }
186:
187: /**
188: * Fulfill the ResourceCollection contract.
189: * @return an Iterator of Resources.
190: * @since Ant 1.7
191: */
192: public Iterator iterator() {
193: if (isReference()) {
194: return ((FileList) getRef(getProject())).iterator();
195: }
196: return new FileResourceIterator(dir, (String[]) (filenames
197: .toArray(new String[filenames.size()])));
198: }
199:
200: /**
201: * Fulfill the ResourceCollection contract.
202: * @return number of elements as int.
203: * @since Ant 1.7
204: */
205: public int size() {
206: if (isReference()) {
207: return ((FileList) getRef(getProject())).size();
208: }
209: return filenames.size();
210: }
211:
212: /**
213: * Always returns true.
214: * @return true indicating that all elements will be FileResources.
215: * @since Ant 1.7
216: */
217: public boolean isFilesystemOnly() {
218: return true;
219: }
220:
221: }
|