1 | /*
|
---|
2 | * Copyright 2001-2002,2004 The Apache Software Foundation
|
---|
3 | *
|
---|
4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
---|
5 | * you may not use this file except in compliance with the License.
|
---|
6 | * You may obtain a copy of the License at
|
---|
7 | *
|
---|
8 | * http://www.apache.org/licenses/LICENSE-2.0
|
---|
9 | *
|
---|
10 | * Unless required by applicable law or agreed to in writing, software
|
---|
11 | * distributed under the License is distributed on an "AS IS" BASIS,
|
---|
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
---|
13 | * See the License for the specific language governing permissions and
|
---|
14 | * limitations under the License.
|
---|
15 | *
|
---|
16 | */
|
---|
17 |
|
---|
18 | package org.apache.tools.ant.types;
|
---|
19 |
|
---|
20 | import java.io.File;
|
---|
21 | import java.util.Stack;
|
---|
22 | import java.util.StringTokenizer;
|
---|
23 | import java.util.Vector;
|
---|
24 | import org.apache.tools.ant.BuildException;
|
---|
25 | import org.apache.tools.ant.Project;
|
---|
26 |
|
---|
27 | /**
|
---|
28 | * FileList represents an explicitly named list of files. FileLists
|
---|
29 | * are useful when you want to capture a list of files regardless of
|
---|
30 | * whether they currently exist. By contrast, FileSet operates as a
|
---|
31 | * filter, only returning the name of a matched file if it currently
|
---|
32 | * exists in the file system.
|
---|
33 | */
|
---|
34 | public class FileList extends DataType {
|
---|
35 |
|
---|
36 | private Vector filenames = new Vector();
|
---|
37 | private File dir;
|
---|
38 |
|
---|
39 | /**
|
---|
40 | * The default constructor.
|
---|
41 | *
|
---|
42 | */
|
---|
43 | public FileList() {
|
---|
44 | super();
|
---|
45 | }
|
---|
46 |
|
---|
47 | /**
|
---|
48 | * A copy constructor.
|
---|
49 | *
|
---|
50 | * @param filelist a <code>FileList</code> value
|
---|
51 | */
|
---|
52 | protected FileList(FileList filelist) {
|
---|
53 | this.dir = filelist.dir;
|
---|
54 | this.filenames = filelist.filenames;
|
---|
55 | setProject(filelist.getProject());
|
---|
56 | }
|
---|
57 |
|
---|
58 | /**
|
---|
59 | * Makes this instance in effect a reference to another FileList
|
---|
60 | * instance.
|
---|
61 | *
|
---|
62 | * <p>You must not set another attribute or nest elements inside
|
---|
63 | * this element if you make it a reference.</p>
|
---|
64 | * @param r the reference to another filelist.
|
---|
65 | * @exception BuildException if an error occurs.
|
---|
66 | */
|
---|
67 | public void setRefid(Reference r) throws BuildException {
|
---|
68 | if ((dir != null) || (filenames.size() != 0)) {
|
---|
69 | throw tooManyAttributes();
|
---|
70 | }
|
---|
71 | super.setRefid(r);
|
---|
72 | }
|
---|
73 |
|
---|
74 | /**
|
---|
75 | * Set the dir attribute.
|
---|
76 | *
|
---|
77 | * @param dir the directory this filelist is relative to.
|
---|
78 | * @exception BuildException if an error occurs
|
---|
79 | */
|
---|
80 | public void setDir(File dir) throws BuildException {
|
---|
81 | if (isReference()) {
|
---|
82 | throw tooManyAttributes();
|
---|
83 | }
|
---|
84 | this.dir = dir;
|
---|
85 | }
|
---|
86 |
|
---|
87 | /**
|
---|
88 | * @param p the current project
|
---|
89 | * @return the directory attribute
|
---|
90 | */
|
---|
91 | public File getDir(Project p) {
|
---|
92 | if (isReference()) {
|
---|
93 | return getRef(p).getDir(p);
|
---|
94 | }
|
---|
95 | return dir;
|
---|
96 | }
|
---|
97 |
|
---|
98 | /**
|
---|
99 | * Set the filenames attribute.
|
---|
100 | *
|
---|
101 | * @param filenames a string contains filenames, separated by , or
|
---|
102 | * by whitespace.
|
---|
103 | */
|
---|
104 | public void setFiles(String filenames) {
|
---|
105 | if (isReference()) {
|
---|
106 | throw tooManyAttributes();
|
---|
107 | }
|
---|
108 | if (filenames != null && filenames.length() > 0) {
|
---|
109 | StringTokenizer tok = new StringTokenizer(
|
---|
110 | filenames, ", \t\n\r\f", false);
|
---|
111 | while (tok.hasMoreTokens()) {
|
---|
112 | this.filenames.addElement(tok.nextToken());
|
---|
113 | }
|
---|
114 | }
|
---|
115 | }
|
---|
116 |
|
---|
117 | /**
|
---|
118 | * Returns the list of files represented by this FileList.
|
---|
119 | * @param p the current project
|
---|
120 | * @return the list of files represented by this FileList.
|
---|
121 | */
|
---|
122 | public String[] getFiles(Project p) {
|
---|
123 | if (isReference()) {
|
---|
124 | return getRef(p).getFiles(p);
|
---|
125 | }
|
---|
126 |
|
---|
127 | if (dir == null) {
|
---|
128 | throw new BuildException("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 | if (!isChecked()) {
|
---|
148 | Stack stk = new Stack();
|
---|
149 | stk.push(this);
|
---|
150 | dieOnCircularReference(stk, p);
|
---|
151 | }
|
---|
152 |
|
---|
153 | Object o = getRefid().getReferencedObject(p);
|
---|
154 | if (!(o instanceof FileList)) {
|
---|
155 | String msg = getRefid().getRefId() + " doesn\'t denote a filelist";
|
---|
156 | throw new BuildException(msg);
|
---|
157 | } else {
|
---|
158 | return (FileList) o;
|
---|
159 | }
|
---|
160 | }
|
---|
161 |
|
---|
162 | /**
|
---|
163 | * Inner class corresponding to the <file> nested element.
|
---|
164 | */
|
---|
165 | public static class FileName {
|
---|
166 | private String name;
|
---|
167 |
|
---|
168 | /**
|
---|
169 | * The name attribute of the file element.
|
---|
170 | *
|
---|
171 | * @param name the name of a file to add to the file list.
|
---|
172 | */
|
---|
173 | public void setName(String name) {
|
---|
174 | this.name = name;
|
---|
175 | }
|
---|
176 |
|
---|
177 | /**
|
---|
178 | * @return the name of the file for this element.
|
---|
179 | */
|
---|
180 | public String getName() {
|
---|
181 | return name;
|
---|
182 | }
|
---|
183 | }
|
---|
184 |
|
---|
185 | /**
|
---|
186 | * Add a nested <file> nested element.
|
---|
187 | *
|
---|
188 | * @param name a configured file element with a name.
|
---|
189 | */
|
---|
190 | public void addConfiguredFile(FileName name) {
|
---|
191 | if (name.getName() == null) {
|
---|
192 | throw new BuildException(
|
---|
193 | "No name specified in nested file element");
|
---|
194 | }
|
---|
195 | filenames.addElement(name.getName());
|
---|
196 | }
|
---|
197 | }
|
---|