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: package org.apache.tools.ant.taskdefs.optional.extension.resolvers;
019:
020: import java.io.File;
021: import java.net.URL;
022: import org.apache.tools.ant.BuildException;
023: import org.apache.tools.ant.Project;
024: import org.apache.tools.ant.taskdefs.Get;
025: import org.apache.tools.ant.taskdefs.optional.extension.Extension;
026: import org.apache.tools.ant.taskdefs.optional.extension.ExtensionResolver;
027:
028: /**
029: * Resolver that just returns s specified location.
030: *
031: */
032: public class URLResolver implements ExtensionResolver {
033: private File destfile;
034: private File destdir;
035: private URL url;
036:
037: /**
038: * Sets the URL
039: * @param url the url
040: */
041: public void setUrl(final URL url) {
042: this .url = url;
043: }
044:
045: /**
046: * Sets the destination file
047: * @param destfile the destination file
048: */
049: public void setDestfile(final File destfile) {
050: this .destfile = destfile;
051: }
052:
053: /**
054: * Sets the destination directory
055: * @param destdir the destination directory
056: */
057: public void setDestdir(final File destdir) {
058: this .destdir = destdir;
059: }
060:
061: /**
062: * Returns the file resolved from URL and directory
063: * @param extension the extention
064: * @param project the project
065: * @return file the file resolved
066: * @throws BuildException if the URL is invalid
067: */
068: public File resolve(final Extension extension, final Project project)
069: throws BuildException {
070: validate();
071:
072: final File file = getDest();
073:
074: final Get get = new Get();
075: get.setProject(project);
076: get.setDest(file);
077: get.setSrc(url);
078: get.execute();
079:
080: return file;
081: }
082:
083: /*
084: * Gets the destination file
085: */
086: private File getDest() {
087: File result;
088: if (null != destfile) {
089: result = destfile;
090: } else {
091: final String file = url.getFile();
092: String filename;
093: if (null == file || file.length() <= 1) {
094: filename = "default.file";
095: } else {
096: int index = file.lastIndexOf('/');
097: if (-1 == index) {
098: index = 0;
099: }
100: filename = file.substring(index);
101: }
102: result = new File(destdir, filename);
103: }
104: return result;
105: }
106:
107: /*
108: * Validates URL
109: */
110: private void validate() {
111: if (null == url) {
112: final String message = "Must specify URL";
113: throw new BuildException(message);
114: }
115:
116: if (null == destdir && null == destfile) {
117: final String message = "Must specify destination file or directory";
118: throw new BuildException(message);
119: } else if (null != destdir && null != destfile) {
120: final String 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 toString() {
130: return "URL[" + url + "]";
131: }
132: }
|