KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > taskdefs > MakeUrl


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.taskdefs;
19
20
21 import org.apache.tools.ant.BuildException;
22 import org.apache.tools.ant.Project;
23 import org.apache.tools.ant.Task;
24 import org.apache.tools.ant.DirectoryScanner;
25 import org.apache.tools.ant.util.FileUtils;
26 import org.apache.tools.ant.types.FileSet;
27 import org.apache.tools.ant.types.Path;
28
29 import java.io.File JavaDoc;
30 import java.util.List JavaDoc;
31 import java.util.LinkedList JavaDoc;
32 import java.util.ListIterator JavaDoc;
33
34 /**
35  * This task takes file and turns them into a URL, which it then assigns
36  * to a property. Use when for setting up RMI codebases.
37  * <p/>
38  * nested filesets are supported; if present, these are turned into the
39  * url with the given separator between them (default = " ").
40  *
41  * @ant.task category="core" name="makeurl"
42  */

43
44 public class MakeUrl extends Task {
45
46     /**
47      * name of the property to set
48      */

49     private String JavaDoc property;
50
51     /**
52      * name of a file to turn into a URL
53      */

54     private File JavaDoc file;
55
56     /**
57      * separator char
58      */

59     private String JavaDoc separator = " ";
60
61     /**
62      * filesets of nested files to add to this url
63      */

64     private List JavaDoc filesets = new LinkedList JavaDoc();
65
66     /**
67      * paths to add
68      */

69     private List JavaDoc paths = new LinkedList JavaDoc();
70
71     /**
72      * validation flag
73      */

74     private boolean validate = true;
75
76     // error message strings
77
/** Missing file */
78     public static final String JavaDoc ERROR_MISSING_FILE = "A source file is missing :";
79     /** No property defined */
80     public static final String JavaDoc ERROR_NO_PROPERTY = "No property defined";
81     /** No files defined */
82     public static final String JavaDoc ERROR_NO_FILES = "No files defined";
83
84     /**
85      * set the name of a property to fill with the URL
86      *
87      * @param property the name of the property.
88      */

89     public void setProperty(String JavaDoc property) {
90         this.property = property;
91     }
92
93     /**
94      * the name of a file to be converted into a URL
95      *
96      * @param file the file to be converted.
97      */

98     public void setFile(File JavaDoc file) {
99         this.file = file;
100     }
101
102     /**
103      * a fileset of jar files to include in the URL, each
104      * separated by the separator
105      *
106      * @param fileset the fileset to be added.
107      */

108     public void addFileSet(FileSet fileset) {
109         filesets.add(fileset);
110     }
111
112     /**
113      * set the separator for the multi-url option.
114      *
115      * @param separator the separator to use.
116      */

117     public void setSeparator(String JavaDoc separator) {
118         this.separator = separator;
119     }
120
121     /**
122      * set this flag to trigger validation that every named file exists.
123      * Optional: default=true
124      *
125      * @param validate a <code>boolean</code> value.
126      */

127     public void setValidate(boolean validate) {
128         this.validate = validate;
129     }
130
131     /**
132      * add a path to the URL. All elements in the path
133      * will be converted to individual URL entries
134      *
135      * @param path a path value.
136      */

137     public void addPath(Path path) {
138         paths.add(path);
139     }
140
141     /**
142      * convert the filesets to urls.
143      *
144      * @return null for no files
145      */

146     private String JavaDoc filesetsToURL() {
147         if (filesets.isEmpty()) {
148             return "";
149         }
150         int count = 0;
151         StringBuffer JavaDoc urls = new StringBuffer JavaDoc();
152         ListIterator JavaDoc list = filesets.listIterator();
153         while (list.hasNext()) {
154             FileSet set = (FileSet) list.next();
155             DirectoryScanner scanner = set.getDirectoryScanner(getProject());
156             String JavaDoc[] files = scanner.getIncludedFiles();
157             for (int i = 0; i < files.length; i++) {
158                 File JavaDoc f = new File JavaDoc(scanner.getBasedir(), files[i]);
159                 validateFile(f);
160                 String JavaDoc asUrl = toURL(f);
161                 urls.append(asUrl);
162                 log(asUrl, Project.MSG_DEBUG);
163                 urls.append(separator);
164                 count++;
165             }
166         }
167         //at this point there is one trailing space to remove, if the list is not empty.
168
return stripTrailingSeparator(urls, count);
169     }
170
171     /**
172      * convert the string buffer to a string, potentially stripping
173      * out any trailing separator
174      *
175      * @param urls URL buffer
176      * @param count number of URL entries
177      * @return trimmed string, or empty string
178      */

179     private String JavaDoc stripTrailingSeparator(StringBuffer JavaDoc urls,
180                                           int count) {
181         if (count > 0) {
182             urls.delete(urls.length() - separator.length(), urls.length());
183             return new String JavaDoc(urls);
184         } else {
185             return "";
186         }
187     }
188
189
190     /**
191      * convert all paths to URLs
192      *
193      * @return the paths as a separated list of URLs
194      */

195     private String JavaDoc pathsToURL() {
196         if (paths.isEmpty()) {
197             return "";
198         }
199         int count = 0;
200         StringBuffer JavaDoc urls = new StringBuffer JavaDoc();
201         ListIterator JavaDoc list = paths.listIterator();
202         while (list.hasNext()) {
203             Path path = (Path) list.next();
204             String JavaDoc[] elements = path.list();
205             for (int i = 0; i < elements.length; i++) {
206                 File JavaDoc f = new File JavaDoc(elements[i]);
207                 validateFile(f);
208                 String JavaDoc asUrl = toURL(f);
209                 urls.append(asUrl);
210                 log(asUrl, Project.MSG_DEBUG);
211                 urls.append(separator);
212                 count++;
213             }
214         }
215         //at this point there is one trailing space to remove, if the list is not empty.
216
return stripTrailingSeparator(urls, count);
217     }
218
219     /**
220      * verify that the file exists, if {@link #validate} is set
221      *
222      * @param fileToCheck file that may need to exist
223      * @throws BuildException with text beginning {@link #ERROR_MISSING_FILE}
224      */

225     private void validateFile(File JavaDoc fileToCheck) {
226         if (validate && !fileToCheck.exists()) {
227             throw new BuildException(ERROR_MISSING_FILE + fileToCheck.toString());
228         }
229     }
230
231     /**
232      * Create the url
233      *
234      * @throws org.apache.tools.ant.BuildException
235      * if something goes wrong with the build
236      */

237     public void execute() throws BuildException {
238         validate();
239         //now exit here if the property is already set
240
if (getProject().getProperty(property) != null) {
241             return;
242         }
243         String JavaDoc url;
244         String JavaDoc filesetURL = filesetsToURL();
245         if (file != null) {
246             validateFile(file);
247             url = toURL(file);
248             //and add any files if also defined
249
if (filesetURL.length() > 0) {
250                 url = url + separator + filesetURL;
251             }
252         } else {
253             url = filesetURL;
254         }
255         //add path URLs
256
String JavaDoc pathURL = pathsToURL();
257         if (pathURL.length() > 0) {
258             if (url.length() > 0) {
259                 url = url + separator + pathURL;
260             } else {
261                 url = pathURL;
262             }
263         }
264         log("Setting " + property + " to URL " + url, Project.MSG_VERBOSE);
265         getProject().setNewProperty(property, url);
266     }
267
268     /**
269      * check for errors
270      * @throws BuildException if we are not configured right
271      */

272     private void validate() {
273         //validation
274
if (property == null) {
275             throw new BuildException(ERROR_NO_PROPERTY);
276         }
277         if (file == null && filesets.isEmpty() && paths.isEmpty()) {
278             throw new BuildException(ERROR_NO_FILES);
279         }
280     }
281
282     /**
283      * convert a file to a URL;
284      *
285      * @param fileToConvert
286      * @return the file converted to a URL
287      */

288     private String JavaDoc toURL(File JavaDoc fileToConvert) {
289         String JavaDoc url;
290         //create the URL
291
//ant equivalent of fileToConvert.toURI().toURL().toExternalForm();
292
url = FileUtils.getFileUtils().toURI(fileToConvert.getAbsolutePath());
293
294         return url;
295     }
296
297 }
298
Popular Tags