You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

MakeUrl.java 8.8 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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. import org.apache.tools.ant.BuildException;
  20. import org.apache.tools.ant.Project;
  21. import org.apache.tools.ant.Task;
  22. import org.apache.tools.ant.DirectoryScanner;
  23. import org.apache.tools.ant.util.FileUtils;
  24. import org.apache.tools.ant.types.FileSet;
  25. import org.apache.tools.ant.types.Path;
  26. import java.io.File;
  27. import java.util.List;
  28. import java.util.LinkedList;
  29. import java.util.ListIterator;
  30. /**
  31. * <p>This task takes file and turns them into a URL, which it then assigns
  32. * to a property. Use when for setting up RMI codebases.</p>
  33. *
  34. * <p>nested filesets are supported; if present, these are turned into the
  35. * url with the given separator between them (default = " ").</p>
  36. *
  37. * @ant.task category="core" name="makeurl"
  38. */
  39. public class MakeUrl extends Task {
  40. /**
  41. * name of the property to set
  42. */
  43. private String property;
  44. /**
  45. * name of a file to turn into a URL
  46. */
  47. private File file;
  48. /**
  49. * separator char
  50. */
  51. private String separator = " ";
  52. /**
  53. * filesets of nested files to add to this url
  54. */
  55. private List<FileSet> filesets = new LinkedList<FileSet>();
  56. /**
  57. * paths to add
  58. */
  59. private List<Path> paths = new LinkedList<Path>();
  60. /**
  61. * validation flag
  62. */
  63. private boolean validate = true;
  64. // error message strings
  65. /** Missing file */
  66. public static final String ERROR_MISSING_FILE = "A source file is missing: ";
  67. /** No property defined */
  68. public static final String ERROR_NO_PROPERTY = "No property defined";
  69. /** No files defined */
  70. public static final String ERROR_NO_FILES = "No files defined";
  71. /**
  72. * set the name of a property to fill with the URL
  73. *
  74. * @param property the name of the property.
  75. */
  76. public void setProperty(String property) {
  77. this.property = property;
  78. }
  79. /**
  80. * the name of a file to be converted into a URL
  81. *
  82. * @param file the file to be converted.
  83. */
  84. public void setFile(File file) {
  85. this.file = file;
  86. }
  87. /**
  88. * a fileset of jar files to include in the URL, each
  89. * separated by the separator
  90. *
  91. * @param fileset the fileset to be added.
  92. */
  93. public void addFileSet(FileSet fileset) {
  94. filesets.add(fileset);
  95. }
  96. /**
  97. * set the separator for the multi-url option.
  98. *
  99. * @param separator the separator to use.
  100. */
  101. public void setSeparator(String separator) {
  102. this.separator = separator;
  103. }
  104. /**
  105. * set this flag to trigger validation that every named file exists.
  106. * Optional: default=true
  107. *
  108. * @param validate a <code>boolean</code> value.
  109. */
  110. public void setValidate(boolean validate) {
  111. this.validate = validate;
  112. }
  113. /**
  114. * add a path to the URL. All elements in the path
  115. * will be converted to individual URL entries
  116. *
  117. * @param path a path value.
  118. */
  119. public void addPath(Path path) {
  120. paths.add(path);
  121. }
  122. /**
  123. * convert the filesets to urls.
  124. *
  125. * @return null for no files
  126. */
  127. private String filesetsToURL() {
  128. if (filesets.isEmpty()) {
  129. return "";
  130. }
  131. int count = 0;
  132. StringBuilder urls = new StringBuilder();
  133. ListIterator<FileSet> list = filesets.listIterator();
  134. while (list.hasNext()) {
  135. FileSet set = (FileSet) list.next();
  136. DirectoryScanner scanner = set.getDirectoryScanner(getProject());
  137. String[] files = scanner.getIncludedFiles();
  138. for (int i = 0; i < files.length; i++) {
  139. File f = new File(scanner.getBasedir(), files[i]);
  140. validateFile(f);
  141. String asUrl = toURL(f);
  142. urls.append(asUrl);
  143. log(asUrl, Project.MSG_DEBUG);
  144. urls.append(separator);
  145. count++;
  146. }
  147. }
  148. //at this point there is one trailing space to remove, if the list is not empty.
  149. return stripTrailingSeparator(urls, count);
  150. }
  151. /**
  152. * convert the string buffer to a string, potentially stripping
  153. * out any trailing separator
  154. *
  155. * @param urls URL buffer
  156. * @param count number of URL entries
  157. * @return trimmed string, or empty string
  158. */
  159. private String stripTrailingSeparator(StringBuilder urls,
  160. int count) {
  161. if (count > 0) {
  162. urls.delete(urls.length() - separator.length(), urls.length());
  163. return new String(urls);
  164. } else {
  165. return "";
  166. }
  167. }
  168. /**
  169. * convert all paths to URLs
  170. *
  171. * @return the paths as a separated list of URLs
  172. */
  173. private String pathsToURL() {
  174. if (paths.isEmpty()) {
  175. return "";
  176. }
  177. int count = 0;
  178. StringBuilder urls = new StringBuilder();
  179. ListIterator<Path> list = paths.listIterator();
  180. while (list.hasNext()) {
  181. Path path = (Path) list.next();
  182. String[] elements = path.list();
  183. for (int i = 0; i < elements.length; i++) {
  184. File f = new File(elements[i]);
  185. validateFile(f);
  186. String asUrl = toURL(f);
  187. urls.append(asUrl);
  188. log(asUrl, Project.MSG_DEBUG);
  189. urls.append(separator);
  190. count++;
  191. }
  192. }
  193. //at this point there is one trailing space to remove, if the list is not empty.
  194. return stripTrailingSeparator(urls, count);
  195. }
  196. /**
  197. * verify that the file exists, if {@link #validate} is set
  198. *
  199. * @param fileToCheck file that may need to exist
  200. * @throws BuildException with text beginning {@link #ERROR_MISSING_FILE}
  201. */
  202. private void validateFile(File fileToCheck) {
  203. if (validate && !fileToCheck.exists()) {
  204. throw new BuildException(ERROR_MISSING_FILE + fileToCheck.toString());
  205. }
  206. }
  207. /**
  208. * Create the url
  209. *
  210. * @throws org.apache.tools.ant.BuildException
  211. * if something goes wrong with the build
  212. */
  213. public void execute() throws BuildException {
  214. validate();
  215. //now exit here if the property is already set
  216. if (getProject().getProperty(property) != null) {
  217. return;
  218. }
  219. String url;
  220. String filesetURL = filesetsToURL();
  221. if (file != null) {
  222. validateFile(file);
  223. url = toURL(file);
  224. //and add any files if also defined
  225. if (filesetURL.length() > 0) {
  226. url = url + separator + filesetURL;
  227. }
  228. } else {
  229. url = filesetURL;
  230. }
  231. //add path URLs
  232. String pathURL = pathsToURL();
  233. if (pathURL.length() > 0) {
  234. if (url.length() > 0) {
  235. url = url + separator + pathURL;
  236. } else {
  237. url = pathURL;
  238. }
  239. }
  240. log("Setting " + property + " to URL " + url, Project.MSG_VERBOSE);
  241. getProject().setNewProperty(property, url);
  242. }
  243. /**
  244. * check for errors
  245. * @throws BuildException if we are not configured right
  246. */
  247. private void validate() {
  248. //validation
  249. if (property == null) {
  250. throw new BuildException(ERROR_NO_PROPERTY);
  251. }
  252. if (file == null && filesets.isEmpty() && paths.isEmpty()) {
  253. throw new BuildException(ERROR_NO_FILES);
  254. }
  255. }
  256. /**
  257. * convert a file to a URL;
  258. *
  259. * @param fileToConvert
  260. * @return the file converted to a URL
  261. */
  262. private String toURL(File fileToConvert) {
  263. String url;
  264. //create the URL
  265. //ant equivalent of fileToConvert.toURI().toURL().toExternalForm();
  266. url = FileUtils.getFileUtils().toURI(fileToConvert.getAbsolutePath());
  267. return url;
  268. }
  269. }