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.

MatchingTask.java 14 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. /*
  2. * The Apache Software License, Version 1.1
  3. *
  4. * Copyright (c) 2000-2002 The Apache Software Foundation. All rights
  5. * reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. *
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in
  16. * the documentation and/or other materials provided with the
  17. * distribution.
  18. *
  19. * 3. The end-user documentation included with the redistribution, if
  20. * any, must include the following acknowlegement:
  21. * "This product includes software developed by the
  22. * Apache Software Foundation (http://www.apache.org/)."
  23. * Alternately, this acknowlegement may appear in the software itself,
  24. * if and wherever such third-party acknowlegements normally appear.
  25. *
  26. * 4. The names "The Jakarta Project", "Ant", and "Apache Software
  27. * Foundation" must not be used to endorse or promote products derived
  28. * from this software without prior written permission. For written
  29. * permission, please contact apache@apache.org.
  30. *
  31. * 5. Products derived from this software may not be called "Apache"
  32. * nor may "Apache" appear in their names without prior written
  33. * permission of the Apache Group.
  34. *
  35. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  36. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  37. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  38. * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
  39. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  40. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  41. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  42. * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  43. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  44. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  45. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  46. * SUCH DAMAGE.
  47. * ====================================================================
  48. *
  49. * This software consists of voluntary contributions made by many
  50. * individuals on behalf of the Apache Software Foundation. For more
  51. * information on the Apache Software Foundation, please see
  52. * <http://www.apache.org/>.
  53. */
  54. package org.apache.tools.ant.taskdefs;
  55. import java.io.File;
  56. import java.util.Enumeration;
  57. import java.util.StringTokenizer;
  58. import org.apache.tools.ant.DirectoryScanner;
  59. import org.apache.tools.ant.Project;
  60. import org.apache.tools.ant.Task;
  61. import org.apache.tools.ant.types.FileSet;
  62. import org.apache.tools.ant.types.PatternSet;
  63. import org.apache.tools.ant.types.selectors.AndSelector;
  64. import org.apache.tools.ant.types.selectors.ContainsSelector;
  65. import org.apache.tools.ant.types.selectors.DateSelector;
  66. import org.apache.tools.ant.types.selectors.DependSelector;
  67. import org.apache.tools.ant.types.selectors.DepthSelector;
  68. import org.apache.tools.ant.types.selectors.ExtendSelector;
  69. import org.apache.tools.ant.types.selectors.FileSelector;
  70. import org.apache.tools.ant.types.selectors.FilenameSelector;
  71. import org.apache.tools.ant.types.selectors.MajoritySelector;
  72. import org.apache.tools.ant.types.selectors.NoneSelector;
  73. import org.apache.tools.ant.types.selectors.NotSelector;
  74. import org.apache.tools.ant.types.selectors.OrSelector;
  75. import org.apache.tools.ant.types.selectors.PresentSelector;
  76. import org.apache.tools.ant.types.selectors.SelectSelector;
  77. import org.apache.tools.ant.types.selectors.SelectorContainer;
  78. import org.apache.tools.ant.types.selectors.SizeSelector;
  79. /**
  80. * This is an abstract task that should be used by all those tasks that
  81. * require to include or exclude files based on pattern matching.
  82. *
  83. * @author Arnout J. Kuiper
  84. * <a href="mailto:ajkuiper@wxs.nl">ajkuiper@wxs.nl</a>
  85. * @author Stefano Mazzocchi
  86. * <a href="mailto:stefano@apache.org">stefano@apache.org</a>
  87. * @author Sam Ruby <a href="mailto:rubys@us.ibm.com">rubys@us.ibm.com</a>
  88. * @author Jon S. Stevens <a href="mailto:jon@clearink.com">jon@clearink.com</a>
  89. * @author <a href="mailto:stefan.bodewig@epost.de">Stefan Bodewig</a>
  90. * @author <a href="mailto:bruce@callenish.com">Bruce Atherton</a>
  91. * @since Ant 1.1
  92. */
  93. public abstract class MatchingTask extends Task implements SelectorContainer {
  94. protected boolean useDefaultExcludes = true;
  95. protected FileSet fileset = new FileSet();
  96. /**
  97. * @see org.apache.tools.ant.ProjectComponent#setProject
  98. */
  99. public void setProject(Project project) {
  100. super.setProject(project);
  101. fileset.setProject(project);
  102. }
  103. /**
  104. * add a name entry on the include list
  105. */
  106. public PatternSet.NameEntry createInclude() {
  107. return fileset.createInclude();
  108. }
  109. /**
  110. * add a name entry on the include files list
  111. */
  112. public PatternSet.NameEntry createIncludesFile() {
  113. return fileset.createIncludesFile();
  114. }
  115. /**
  116. * add a name entry on the exclude list
  117. */
  118. public PatternSet.NameEntry createExclude() {
  119. return fileset.createExclude();
  120. }
  121. /**
  122. * add a name entry on the include files list
  123. */
  124. public PatternSet.NameEntry createExcludesFile() {
  125. return fileset.createExcludesFile();
  126. }
  127. /**
  128. * add a set of patterns
  129. */
  130. public PatternSet createPatternSet() {
  131. return fileset.createPatternSet();
  132. }
  133. /**
  134. * Sets the set of include patterns. Patterns may be separated by a comma
  135. * or a space.
  136. *
  137. * @param includes the string containing the include patterns
  138. */
  139. public void setIncludes(String includes) {
  140. fileset.setIncludes(includes);
  141. }
  142. /**
  143. * Set this to be the items in the base directory that you want to be
  144. * included. You can also specify "*" for the items (ie: items="*")
  145. * and it will include all the items in the base directory.
  146. *
  147. * @param itemString the string containing the files to include.
  148. */
  149. public void XsetItems(String itemString) {
  150. log("The items attribute is deprecated. " +
  151. "Please use the includes attribute.",
  152. Project.MSG_WARN);
  153. if (itemString == null || itemString.equals("*")
  154. || itemString.equals(".")) {
  155. createInclude().setName("**");
  156. } else {
  157. StringTokenizer tok = new StringTokenizer(itemString, ", ");
  158. while (tok.hasMoreTokens()) {
  159. String pattern = tok.nextToken().trim();
  160. if (pattern.length() > 0) {
  161. createInclude().setName(pattern + "/**");
  162. }
  163. }
  164. }
  165. }
  166. /**
  167. * Sets the set of exclude patterns. Patterns may be separated by a comma
  168. * or a space.
  169. *
  170. * @param excludes the string containing the exclude patterns
  171. */
  172. public void setExcludes(String excludes) {
  173. fileset.setExcludes(excludes);
  174. }
  175. /**
  176. * List of filenames and directory names to not include. They should be
  177. * either , or " " (space) separated. The ignored files will be logged.
  178. *
  179. * @param ignoreString the string containing the files to ignore.
  180. */
  181. public void XsetIgnore(String ignoreString) {
  182. log("The ignore attribute is deprecated." +
  183. "Please use the excludes attribute.",
  184. Project.MSG_WARN);
  185. if (ignoreString != null && ignoreString.length() > 0) {
  186. StringTokenizer tok = new StringTokenizer(ignoreString, ", ",
  187. false);
  188. while (tok.hasMoreTokens()) {
  189. createExclude().setName("**/" + tok.nextToken().trim() + "/**");
  190. }
  191. }
  192. }
  193. /**
  194. * Sets whether default exclusions should be used or not.
  195. *
  196. * @param useDefaultExcludes "true"|"on"|"yes" when default exclusions
  197. * should be used, "false"|"off"|"no" when they
  198. * shouldn't be used.
  199. */
  200. public void setDefaultexcludes(boolean useDefaultExcludes) {
  201. this.useDefaultExcludes = useDefaultExcludes;
  202. }
  203. /**
  204. * Returns the directory scanner needed to access the files to process.
  205. */
  206. protected DirectoryScanner getDirectoryScanner(File baseDir) {
  207. fileset.setDir(baseDir);
  208. fileset.setDefaultexcludes(useDefaultExcludes);
  209. return fileset.getDirectoryScanner(getProject());
  210. }
  211. /**
  212. * Sets the name of the file containing the includes patterns.
  213. *
  214. * @param includesfile A string containing the filename to fetch
  215. * the include patterns from.
  216. */
  217. public void setIncludesfile(File includesfile) {
  218. fileset.setIncludesfile(includesfile);
  219. }
  220. /**
  221. * Sets the name of the file containing the includes patterns.
  222. *
  223. * @param excludesfile A string containing the filename to fetch
  224. * the include patterns from.
  225. */
  226. public void setExcludesfile(File excludesfile) {
  227. fileset.setExcludesfile(excludesfile);
  228. }
  229. /**
  230. * Sets case sensitivity of the file system
  231. *
  232. * @param isCaseSensitive "true"|"on"|"yes" if file system is case
  233. * sensitive, "false"|"off"|"no" when not.
  234. */
  235. public void setCaseSensitive(boolean isCaseSensitive) {
  236. fileset.setCaseSensitive(isCaseSensitive);
  237. }
  238. /**
  239. * Sets whether or not symbolic links should be followed.
  240. *
  241. * @param followSymlinks whether or not symbolic links should be followed
  242. */
  243. public void setFollowSymlinks(boolean followSymlinks) {
  244. fileset.setFollowSymlinks(followSymlinks);
  245. }
  246. /**
  247. * Indicates whether there are any selectors here.
  248. *
  249. * @return whether any selectors are in this container
  250. */
  251. public boolean hasSelectors() {
  252. return fileset.hasSelectors();
  253. }
  254. /**
  255. * Gives the count of the number of selectors in this container
  256. *
  257. * @return the number of selectors in this container
  258. */
  259. public int selectorCount() {
  260. return fileset.selectorCount();
  261. }
  262. /**
  263. * Returns the set of selectors as an array.
  264. *
  265. * @return an array of selectors in this container
  266. */
  267. public FileSelector[] getSelectors(Project p) {
  268. return fileset.getSelectors(p);
  269. }
  270. /**
  271. * Returns an enumerator for accessing the set of selectors.
  272. *
  273. * @return an enumerator that goes through each of the selectors
  274. */
  275. public Enumeration selectorElements() {
  276. return fileset.selectorElements();
  277. }
  278. /**
  279. * Add a new selector into this container.
  280. *
  281. * @param selector the new selector to add
  282. */
  283. public void appendSelector(FileSelector selector) {
  284. fileset.appendSelector(selector);
  285. }
  286. /* Methods below all add specific selectors */
  287. /**
  288. * add a "Select" selector entry on the selector list
  289. */
  290. public void addSelector(SelectSelector selector) {
  291. fileset.addSelector(selector);
  292. }
  293. /**
  294. * add an "And" selector entry on the selector list
  295. */
  296. public void addAnd(AndSelector selector) {
  297. fileset.addAnd(selector);
  298. }
  299. /**
  300. * add an "Or" selector entry on the selector list
  301. */
  302. public void addOr(OrSelector selector) {
  303. fileset.addOr(selector);
  304. }
  305. /**
  306. * add a "Not" selector entry on the selector list
  307. */
  308. public void addNot(NotSelector selector) {
  309. fileset.addNot(selector);
  310. }
  311. /**
  312. * add a "None" selector entry on the selector list
  313. */
  314. public void addNone(NoneSelector selector) {
  315. fileset.addNone(selector);
  316. }
  317. /**
  318. * add a majority selector entry on the selector list
  319. */
  320. public void addMajority(MajoritySelector selector) {
  321. fileset.addMajority(selector);
  322. }
  323. /**
  324. * add a selector date entry on the selector list
  325. */
  326. public void addDate(DateSelector selector) {
  327. fileset.addDate(selector);
  328. }
  329. /**
  330. * add a selector size entry on the selector list
  331. */
  332. public void addSize(SizeSelector selector) {
  333. fileset.addSize(selector);
  334. }
  335. /**
  336. * add a selector filename entry on the selector list
  337. */
  338. public void addFilename(FilenameSelector selector) {
  339. fileset.addFilename(selector);
  340. }
  341. /**
  342. * add an extended selector entry on the selector list
  343. */
  344. public void addCustom(ExtendSelector selector) {
  345. fileset.addCustom(selector);
  346. }
  347. /**
  348. * add a contains selector entry on the selector list
  349. */
  350. public void addContains(ContainsSelector selector) {
  351. fileset.addContains(selector);
  352. }
  353. /**
  354. * add a present selector entry on the selector list
  355. */
  356. public void addPresent(PresentSelector selector) {
  357. fileset.addPresent(selector);
  358. }
  359. /**
  360. * add a depth selector entry on the selector list
  361. */
  362. public void addDepth(DepthSelector selector) {
  363. fileset.addDepth(selector);
  364. }
  365. /**
  366. * add a depends selector entry on the selector list
  367. */
  368. public void addDepend(DependSelector selector) {
  369. fileset.addDepend(selector);
  370. }
  371. }