1 | /*
|
---|
2 | * Copyright 2002,2004-2005 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 | package org.apache.tools.ant.taskdefs.optional.extension.resolvers;
|
---|
18 |
|
---|
19 | import java.io.File;
|
---|
20 | import java.net.URL;
|
---|
21 | import org.apache.tools.ant.BuildException;
|
---|
22 | import org.apache.tools.ant.Project;
|
---|
23 | import org.apache.tools.ant.taskdefs.Get;
|
---|
24 | import org.apache.tools.ant.taskdefs.optional.extension.Extension;
|
---|
25 | import org.apache.tools.ant.taskdefs.optional.extension.ExtensionResolver;
|
---|
26 |
|
---|
27 | /**
|
---|
28 | * Resolver that just returns s specified location.
|
---|
29 | *
|
---|
30 | */
|
---|
31 | public class URLResolver implements ExtensionResolver {
|
---|
32 | private File destfile;
|
---|
33 | private File destdir;
|
---|
34 | private URL url;
|
---|
35 |
|
---|
36 | /**
|
---|
37 | * Sets the URL
|
---|
38 | * @param url the url
|
---|
39 | */
|
---|
40 | public void setUrl(final URL url) {
|
---|
41 | this.url = url;
|
---|
42 | }
|
---|
43 |
|
---|
44 | /**
|
---|
45 | * Sets the destination file
|
---|
46 | * @param destfile the destination file
|
---|
47 | */
|
---|
48 | public void setDestfile(final File destfile) {
|
---|
49 | this.destfile = destfile;
|
---|
50 | }
|
---|
51 |
|
---|
52 | /**
|
---|
53 | * Sets the destination directory
|
---|
54 | * @param destdir the destination directory
|
---|
55 | */
|
---|
56 | public void setDestdir(final File destdir) {
|
---|
57 | this.destdir = destdir;
|
---|
58 | }
|
---|
59 |
|
---|
60 | /**
|
---|
61 | * Returns the file resolved from URL and directory
|
---|
62 | * @param extension the extention
|
---|
63 | * @param project the project
|
---|
64 | * @return file the file resolved
|
---|
65 | * @throws BuildException if the URL is invalid
|
---|
66 | */
|
---|
67 | public File resolve(final Extension extension,
|
---|
68 | final Project project) throws BuildException {
|
---|
69 | validate();
|
---|
70 |
|
---|
71 | final File file = getDest();
|
---|
72 |
|
---|
73 | final Get get = (Get) project.createTask("get");
|
---|
74 | get.setDest(file);
|
---|
75 | get.setSrc(url);
|
---|
76 | get.execute();
|
---|
77 |
|
---|
78 | return file;
|
---|
79 | }
|
---|
80 |
|
---|
81 | /*
|
---|
82 | * Gets the destination file
|
---|
83 | */
|
---|
84 | private File getDest() {
|
---|
85 | File result;
|
---|
86 | if (null != destfile) {
|
---|
87 | result = destfile;
|
---|
88 | } else {
|
---|
89 | final String file = url.getFile();
|
---|
90 | String filename;
|
---|
91 | if (null == file || file.length() <= 1) {
|
---|
92 | filename = "default.file";
|
---|
93 | } else {
|
---|
94 | int index = file.lastIndexOf('/');
|
---|
95 | if (-1 == index) {
|
---|
96 | index = 0;
|
---|
97 | }
|
---|
98 | filename = file.substring(index);
|
---|
99 | }
|
---|
100 | result = new File(destdir, filename);
|
---|
101 | }
|
---|
102 | return result;
|
---|
103 | }
|
---|
104 |
|
---|
105 | /*
|
---|
106 | * Validates URL
|
---|
107 | */
|
---|
108 | private void validate() {
|
---|
109 | if (null == url) {
|
---|
110 | final String message = "Must specify URL";
|
---|
111 | throw new BuildException(message);
|
---|
112 | }
|
---|
113 |
|
---|
114 | if (null == destdir && null == destfile) {
|
---|
115 | final String message = "Must specify destination file or directory";
|
---|
116 | throw new BuildException(message);
|
---|
117 | } else if (null != destdir && null != destfile) {
|
---|
118 | final String message = "Must not specify both destination file or directory";
|
---|
119 | throw new BuildException(message);
|
---|
120 | }
|
---|
121 | }
|
---|
122 |
|
---|
123 | /**
|
---|
124 | * Returns a string representation of the URL
|
---|
125 | * @return the string representation
|
---|
126 | */
|
---|
127 | public String toString() {
|
---|
128 | return "URL[" + url + "]";
|
---|
129 | }
|
---|
130 | }
|
---|