KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > taskdefs > optional > extension > resolvers > URLResolver


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.optional.extension.resolvers;
19
20 import java.io.File JavaDoc;
21 import java.net.URL JavaDoc;
22 import org.apache.tools.ant.BuildException;
23 import org.apache.tools.ant.Project;
24 import org.apache.tools.ant.taskdefs.Get;
25 import org.apache.tools.ant.taskdefs.optional.extension.Extension;
26 import org.apache.tools.ant.taskdefs.optional.extension.ExtensionResolver;
27
28 /**
29  * Resolver that just returns s specified location.
30  *
31  */

32 public class URLResolver implements ExtensionResolver {
33     private File JavaDoc destfile;
34     private File JavaDoc destdir;
35     private URL JavaDoc url;
36
37     /**
38      * Sets the URL
39      * @param url the url
40      */

41     public void setUrl(final URL JavaDoc url) {
42         this.url = url;
43     }
44
45     /**
46      * Sets the destination file
47      * @param destfile the destination file
48      */

49     public void setDestfile(final File JavaDoc destfile) {
50         this.destfile = destfile;
51     }
52
53     /**
54      * Sets the destination directory
55      * @param destdir the destination directory
56      */

57     public void setDestdir(final File JavaDoc destdir) {
58         this.destdir = destdir;
59     }
60
61     /**
62      * Returns the file resolved from URL and directory
63      * @param extension the extention
64      * @param project the project
65      * @return file the file resolved
66      * @throws BuildException if the URL is invalid
67      */

68     public File JavaDoc resolve(final Extension extension,
69                          final Project project) throws BuildException {
70         validate();
71
72         final File JavaDoc file = getDest();
73
74         final Get get = new Get();
75         get.setProject(project);
76         get.setDest(file);
77         get.setSrc(url);
78         get.execute();
79
80         return file;
81     }
82
83     /*
84      * Gets the destination file
85      */

86     private File JavaDoc getDest() {
87         File JavaDoc result;
88         if (null != destfile) {
89             result = destfile;
90         } else {
91             final String JavaDoc file = url.getFile();
92             String JavaDoc filename;
93             if (null == file || file.length() <= 1) {
94                 filename = "default.file";
95             } else {
96                 int index = file.lastIndexOf('/');
97                 if (-1 == index) {
98                     index = 0;
99                 }
100                 filename = file.substring(index);
101             }
102             result = new File JavaDoc(destdir, filename);
103         }
104         return result;
105     }
106
107     /*
108      * Validates URL
109      */

110     private void validate() {
111         if (null == url) {
112             final String JavaDoc message = "Must specify URL";
113             throw new BuildException(message);
114         }
115
116         if (null == destdir && null == destfile) {
117             final String JavaDoc message = "Must specify destination file or directory";
118             throw new BuildException(message);
119         } else if (null != destdir && null != destfile) {
120             final String JavaDoc message = "Must not specify both destination file or directory";
121             throw new BuildException(message);
122         }
123     }
124
125     /**
126      * Returns a string representation of the URL
127      * @return the string representation
128      */

129     public String JavaDoc toString() {
130         return "URL[" + url + "]";
131     }
132 }
133
Popular Tags