1. /*
  2. * Copyright 2000-2004 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;
  18. import java.io.File;
  19. import java.util.Enumeration;
  20. import java.util.StringTokenizer;
  21. import org.apache.tools.ant.DirectoryScanner;
  22. import org.apache.tools.ant.Project;
  23. import org.apache.tools.ant.Task;
  24. import org.apache.tools.ant.types.FileSet;
  25. import org.apache.tools.ant.types.PatternSet;
  26. import org.apache.tools.ant.types.selectors.AndSelector;
  27. import org.apache.tools.ant.types.selectors.ContainsRegexpSelector;
  28. import org.apache.tools.ant.types.selectors.ContainsSelector;
  29. import org.apache.tools.ant.types.selectors.DateSelector;
  30. import org.apache.tools.ant.types.selectors.DependSelector;
  31. import org.apache.tools.ant.types.selectors.DepthSelector;
  32. import org.apache.tools.ant.types.selectors.DifferentSelector;
  33. import org.apache.tools.ant.types.selectors.ExtendSelector;
  34. import org.apache.tools.ant.types.selectors.FileSelector;
  35. import org.apache.tools.ant.types.selectors.FilenameSelector;
  36. import org.apache.tools.ant.types.selectors.MajoritySelector;
  37. import org.apache.tools.ant.types.selectors.NoneSelector;
  38. import org.apache.tools.ant.types.selectors.NotSelector;
  39. import org.apache.tools.ant.types.selectors.OrSelector;
  40. import org.apache.tools.ant.types.selectors.PresentSelector;
  41. import org.apache.tools.ant.types.selectors.SelectSelector;
  42. import org.apache.tools.ant.types.selectors.SelectorContainer;
  43. import org.apache.tools.ant.types.selectors.SizeSelector;
  44. import org.apache.tools.ant.types.selectors.TypeSelector;
  45. import org.apache.tools.ant.types.selectors.modifiedselector.ModifiedSelector;
  46. /**
  47. * This is an abstract task that should be used by all those tasks that
  48. * require to include or exclude files based on pattern matching.
  49. *
  50. * @since Ant 1.1
  51. */
  52. public abstract class MatchingTask extends Task implements SelectorContainer {
  53. protected FileSet fileset = new FileSet();
  54. /**
  55. * @see org.apache.tools.ant.ProjectComponent#setProject
  56. */
  57. public void setProject(Project project) {
  58. super.setProject(project);
  59. fileset.setProject(project);
  60. }
  61. /**
  62. * add a name entry on the include list
  63. * @return a NameEntry object to be configured
  64. */
  65. public PatternSet.NameEntry createInclude() {
  66. return fileset.createInclude();
  67. }
  68. /**
  69. * add a name entry on the include files list
  70. * @return an NameEntry object to be configured
  71. */
  72. public PatternSet.NameEntry createIncludesFile() {
  73. return fileset.createIncludesFile();
  74. }
  75. /**
  76. * add a name entry on the exclude list
  77. * @return an NameEntry object to be configured
  78. */
  79. public PatternSet.NameEntry createExclude() {
  80. return fileset.createExclude();
  81. }
  82. /**
  83. * add a name entry on the include files list
  84. * @return an NameEntry object to be configured
  85. */
  86. public PatternSet.NameEntry createExcludesFile() {
  87. return fileset.createExcludesFile();
  88. }
  89. /**
  90. * add a set of patterns
  91. * @return PatternSet object to be configured
  92. */
  93. public PatternSet createPatternSet() {
  94. return fileset.createPatternSet();
  95. }
  96. /**
  97. * Sets the set of include patterns. Patterns may be separated by a comma
  98. * or a space.
  99. *
  100. * @param includes the string containing the include patterns
  101. */
  102. public void setIncludes(String includes) {
  103. fileset.setIncludes(includes);
  104. }
  105. /**
  106. * Set this to be the items in the base directory that you want to be
  107. * included. You can also specify "*" for the items (ie: items="*")
  108. * and it will include all the items in the base directory.
  109. *
  110. * @param itemString the string containing the files to include.
  111. */
  112. public void XsetItems(String itemString) {
  113. log("The items attribute is deprecated. "
  114. + "Please use the includes attribute.", Project.MSG_WARN);
  115. if (itemString == null || itemString.equals("*")
  116. || itemString.equals(".")) {
  117. createInclude().setName("**");
  118. } else {
  119. StringTokenizer tok = new StringTokenizer(itemString, ", ");
  120. while (tok.hasMoreTokens()) {
  121. String pattern = tok.nextToken().trim();
  122. if (pattern.length() > 0) {
  123. createInclude().setName(pattern + "/**");
  124. }
  125. }
  126. }
  127. }
  128. /**
  129. * Sets the set of exclude patterns. Patterns may be separated by a comma
  130. * or a space.
  131. *
  132. * @param excludes the string containing the exclude patterns
  133. */
  134. public void setExcludes(String excludes) {
  135. fileset.setExcludes(excludes);
  136. }
  137. /**
  138. * List of filenames and directory names to not include. They should be
  139. * either , or " " (space) separated. The ignored files will be logged.
  140. *
  141. * @param ignoreString the string containing the files to ignore.
  142. */
  143. public void XsetIgnore(String ignoreString) {
  144. log("The ignore attribute is deprecated."
  145. + "Please use the excludes attribute.", Project.MSG_WARN);
  146. if (ignoreString != null && ignoreString.length() > 0) {
  147. StringTokenizer tok = new StringTokenizer(ignoreString, ", ",
  148. false);
  149. while (tok.hasMoreTokens()) {
  150. createExclude().setName("**/" + tok.nextToken().trim() + "/**");
  151. }
  152. }
  153. }
  154. /**
  155. * Sets whether default exclusions should be used or not.
  156. *
  157. * @param useDefaultExcludes "true"|"on"|"yes" when default exclusions
  158. * should be used, "false"|"off"|"no" when they
  159. * shouldn't be used.
  160. */
  161. public void setDefaultexcludes(boolean useDefaultExcludes) {
  162. fileset.setDefaultexcludes(useDefaultExcludes);
  163. }
  164. /**
  165. * Returns the directory scanner needed to access the files to process.
  166. */
  167. protected DirectoryScanner getDirectoryScanner(File baseDir) {
  168. fileset.setDir(baseDir);
  169. return fileset.getDirectoryScanner(getProject());
  170. }
  171. /**
  172. * Sets the name of the file containing the includes patterns.
  173. *
  174. * @param includesfile A string containing the filename to fetch
  175. * the include patterns from.
  176. */
  177. public void setIncludesfile(File includesfile) {
  178. fileset.setIncludesfile(includesfile);
  179. }
  180. /**
  181. * Sets the name of the file containing the includes patterns.
  182. *
  183. * @param excludesfile A string containing the filename to fetch
  184. * the include patterns from.
  185. */
  186. public void setExcludesfile(File excludesfile) {
  187. fileset.setExcludesfile(excludesfile);
  188. }
  189. /**
  190. * Sets case sensitivity of the file system
  191. *
  192. * @param isCaseSensitive "true"|"on"|"yes" if file system is case
  193. * sensitive, "false"|"off"|"no" when not.
  194. */
  195. public void setCaseSensitive(boolean isCaseSensitive) {
  196. fileset.setCaseSensitive(isCaseSensitive);
  197. }
  198. /**
  199. * Sets whether or not symbolic links should be followed.
  200. *
  201. * @param followSymlinks whether or not symbolic links should be followed
  202. */
  203. public void setFollowSymlinks(boolean followSymlinks) {
  204. fileset.setFollowSymlinks(followSymlinks);
  205. }
  206. /**
  207. * Indicates whether there are any selectors here.
  208. *
  209. * @return whether any selectors are in this container
  210. */
  211. public boolean hasSelectors() {
  212. return fileset.hasSelectors();
  213. }
  214. /**
  215. * Gives the count of the number of selectors in this container
  216. *
  217. * @return the number of selectors in this container
  218. */
  219. public int selectorCount() {
  220. return fileset.selectorCount();
  221. }
  222. /**
  223. * Returns the set of selectors as an array.
  224. * @param p the current project
  225. * @return an array of selectors in this container
  226. */
  227. public FileSelector[] getSelectors(Project p) {
  228. return fileset.getSelectors(p);
  229. }
  230. /**
  231. * Returns an enumerator for accessing the set of selectors.
  232. *
  233. * @return an enumerator that goes through each of the selectors
  234. */
  235. public Enumeration selectorElements() {
  236. return fileset.selectorElements();
  237. }
  238. /**
  239. * Add a new selector into this container.
  240. *
  241. * @param selector the new selector to add
  242. */
  243. public void appendSelector(FileSelector selector) {
  244. fileset.appendSelector(selector);
  245. }
  246. /* Methods below all add specific selectors */
  247. /**
  248. * add a "Select" selector entry on the selector list
  249. * @param selector the selector to add
  250. */
  251. public void addSelector(SelectSelector selector) {
  252. fileset.addSelector(selector);
  253. }
  254. /**
  255. * add an "And" selector entry on the selector list
  256. * @param selector the selector to add
  257. */
  258. public void addAnd(AndSelector selector) {
  259. fileset.addAnd(selector);
  260. }
  261. /**
  262. * add an "Or" selector entry on the selector list
  263. * @param selector the selector to add
  264. */
  265. public void addOr(OrSelector selector) {
  266. fileset.addOr(selector);
  267. }
  268. /**
  269. * add a "Not" selector entry on the selector list
  270. * @param selector the selector to add
  271. */
  272. public void addNot(NotSelector selector) {
  273. fileset.addNot(selector);
  274. }
  275. /**
  276. * add a "None" selector entry on the selector list
  277. * @param selector the selector to add
  278. */
  279. public void addNone(NoneSelector selector) {
  280. fileset.addNone(selector);
  281. }
  282. /**
  283. * add a majority selector entry on the selector list
  284. * @param selector the selector to add
  285. */
  286. public void addMajority(MajoritySelector selector) {
  287. fileset.addMajority(selector);
  288. }
  289. /**
  290. * add a selector date entry on the selector list
  291. * @param selector the selector to add
  292. */
  293. public void addDate(DateSelector selector) {
  294. fileset.addDate(selector);
  295. }
  296. /**
  297. * add a selector size entry on the selector list
  298. * @param selector the selector to add
  299. */
  300. public void addSize(SizeSelector selector) {
  301. fileset.addSize(selector);
  302. }
  303. /**
  304. * add a selector filename entry on the selector list
  305. * @param selector the selector to add
  306. */
  307. public void addFilename(FilenameSelector selector) {
  308. fileset.addFilename(selector);
  309. }
  310. /**
  311. * add an extended selector entry on the selector list
  312. * @param selector the selector to add
  313. */
  314. public void addCustom(ExtendSelector selector) {
  315. fileset.addCustom(selector);
  316. }
  317. /**
  318. * add a contains selector entry on the selector list
  319. * @param selector the selector to add
  320. */
  321. public void addContains(ContainsSelector selector) {
  322. fileset.addContains(selector);
  323. }
  324. /**
  325. * add a present selector entry on the selector list
  326. * @param selector the selector to add
  327. */
  328. public void addPresent(PresentSelector selector) {
  329. fileset.addPresent(selector);
  330. }
  331. /**
  332. * add a depth selector entry on the selector list
  333. * @param selector the selector to add
  334. */
  335. public void addDepth(DepthSelector selector) {
  336. fileset.addDepth(selector);
  337. }
  338. /**
  339. * add a depends selector entry on the selector list
  340. * @param selector the selector to add
  341. */
  342. public void addDepend(DependSelector selector) {
  343. fileset.addDepend(selector);
  344. }
  345. /**
  346. * add a regular expression selector entry on the selector list
  347. * @param selector the selector to add
  348. */
  349. public void addContainsRegexp(ContainsRegexpSelector selector) {
  350. fileset.addContainsRegexp(selector);
  351. }
  352. /**
  353. * add a type selector entry on the type list
  354. * @param selector the selector to add
  355. * @since ant 1.6
  356. */
  357. public void addDifferent(DifferentSelector selector) {
  358. fileset.addDifferent(selector);
  359. }
  360. /**
  361. * add a type selector entry on the type list
  362. * @param selector the selector to add
  363. * @since ant 1.6
  364. */
  365. public void addType(TypeSelector selector) {
  366. fileset.addType(selector);
  367. }
  368. /**
  369. * add the modified selector
  370. * @param selector the selector to add
  371. * @since ant 1.6
  372. */
  373. public void addModified(ModifiedSelector selector) {
  374. fileset.addModified(selector);
  375. }
  376. /**
  377. * add an arbitary selector
  378. * @param selector the selector to add
  379. * @since Ant 1.6
  380. */
  381. public void add(FileSelector selector) {
  382. fileset.add(selector);
  383. }
  384. /**
  385. * Accessor for the implicit fileset.
  386. *
  387. * @since Ant 1.5.2
  388. */
  389. protected final FileSet getImplicitFileSet() {
  390. return fileset;
  391. }
  392. }