1 /*
2 * Copyright 2005 University Corporation for Advanced Internet Development, Inc.
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 edu.internet2.middleware.ant.util;
18
19 import java.io.File;
20 import java.net.MalformedURLException;
21 import java.net.URL;
22
23 import org.apache.tools.ant.BuildException;
24 import org.apache.tools.ant.Task;
25
26 /**
27 * Converts a filesystem path into a URL.
28 *
29 * This ant task requires two attributes:
30 * <ul>
31 * <li><strong>path</strong> - the path that will be converted in to a URL</li>
32 * <li><strong>addProperty</strong> - name of the property that will receive the file contents as a string</li>
33 * <ul>
34 */
35 public class PathToUrl extends Task {
36
37 /** Property name to which the file URL is added. */
38 private String addProperty;
39
40 /** Path to convert into a URL. */
41 private String path;
42
43 /** {@inheritDoc} */
44 public void execute() throws BuildException {
45
46 if (addProperty != null && getProject().getProperty(addProperty) != null) {
47 log("Skipping " + getTaskName() + " as property " + addProperty + " has already been set.");
48 return;
49 }
50
51 if (path == null) {
52 log("Skipping " + getTaskName() + " because path was not specified.");
53 return;
54 }
55
56 try {
57 URL fileUrl = new File(path).toURI().toURL();
58 getProject().setNewProperty(addProperty, fileUrl.toString());
59 } catch (MalformedURLException e) {
60 log("Skipping " + getTaskName() + " because path (" + path + ") could not be converted to a URL.");
61 return;
62 }
63 }
64
65 /**
66 * Sets the name of the property that will contain the URL to the file.
67 *
68 * @param property name of the property that will contain the URL to the file
69 */
70 public void setAddProperty(String property) {
71 addProperty = property;
72 }
73
74 /**
75 * Sets the path that will be converted into a file URL.
76 *
77 * @param path path that will be converted into a file URL
78 */
79 public void setPath(String path) {
80 this.path = path;
81 }
82 }