1. /*
  2. * Copyright 2002-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.types.selectors;
  18. import java.io.File;
  19. import java.util.Enumeration;
  20. import java.util.Vector;
  21. import org.apache.tools.ant.BuildException;
  22. import org.apache.tools.ant.Project;
  23. import org.apache.tools.ant.types.selectors.modifiedselector.ModifiedSelector;
  24. /**
  25. * This is the base class for selectors that can contain other selectors.
  26. *
  27. * @since 1.5
  28. */
  29. public abstract class BaseSelectorContainer extends BaseSelector
  30. implements SelectorContainer {
  31. private Vector selectorsList = new Vector();
  32. /**
  33. * Default constructor.
  34. */
  35. public BaseSelectorContainer() {
  36. }
  37. /**
  38. * Indicates whether there are any selectors here.
  39. * @return true if there are selectors
  40. */
  41. public boolean hasSelectors() {
  42. return !(selectorsList.isEmpty());
  43. }
  44. /**
  45. * Gives the count of the number of selectors in this container
  46. * @return the number of selectors
  47. */
  48. public int selectorCount() {
  49. return selectorsList.size();
  50. }
  51. /**
  52. * Returns the set of selectors as an array.
  53. * @param p the current project
  54. * @return an array of selectors
  55. */
  56. public FileSelector[] getSelectors(Project p) {
  57. FileSelector[] result = new FileSelector[selectorsList.size()];
  58. selectorsList.copyInto(result);
  59. return result;
  60. }
  61. /**
  62. * Returns an enumerator for accessing the set of selectors.
  63. * @return an enumerator for the selectors
  64. */
  65. public Enumeration selectorElements() {
  66. return selectorsList.elements();
  67. }
  68. /**
  69. * Convert the Selectors within this container to a string. This will
  70. * just be a helper class for the subclasses that put their own name
  71. * around the contents listed here.
  72. *
  73. * @return comma separated list of Selectors contained in this one
  74. */
  75. public String toString() {
  76. StringBuffer buf = new StringBuffer();
  77. Enumeration e = selectorElements();
  78. if (e.hasMoreElements()) {
  79. while (e.hasMoreElements()) {
  80. buf.append(e.nextElement().toString());
  81. if (e.hasMoreElements()) {
  82. buf.append(", ");
  83. }
  84. }
  85. }
  86. return buf.toString();
  87. }
  88. /**
  89. * Add a new selector into this container.
  90. *
  91. * @param selector the new selector to add
  92. */
  93. public void appendSelector(FileSelector selector) {
  94. selectorsList.addElement(selector);
  95. }
  96. /**
  97. * <p>This implementation validates the container by calling
  98. * verifySettings() and then validates each contained selector
  99. * provided that the selector implements the validate interface.
  100. * </p>
  101. * <p>Ordinarily, this will validate all the elements of a selector
  102. * container even if the isSelected() method of some elements is
  103. * never called. This has two effects:</p>
  104. * <ul>
  105. * <li>Validation will often occur twice.
  106. * <li>Since it is not required that selectors derive from
  107. * BaseSelector, there could be selectors in the container whose
  108. * error conditions are not detected if their isSelected() call
  109. * is never made.
  110. * </ul>
  111. */
  112. public void validate() {
  113. verifySettings();
  114. String errmsg = getError();
  115. if (errmsg != null) {
  116. throw new BuildException(errmsg);
  117. }
  118. Enumeration e = selectorElements();
  119. while (e.hasMoreElements()) {
  120. Object o = e.nextElement();
  121. if (o instanceof BaseSelector) {
  122. ((BaseSelector) o).validate();
  123. }
  124. }
  125. }
  126. /**
  127. * Method that each selector will implement to create their selection
  128. * behaviour. This is what makes SelectorContainer abstract.
  129. *
  130. * @param basedir the base directory the scan is being done from
  131. * @param filename the name of the file to check
  132. * @param file a java.io.File object for the filename that the selector
  133. * can use
  134. * @return whether the file should be selected or not
  135. */
  136. public abstract boolean isSelected(File basedir, String filename,
  137. File file);
  138. /* Methods below all add specific selectors */
  139. /**
  140. * add a "Select" selector entry on the selector list
  141. * @param selector the selector to add
  142. */
  143. public void addSelector(SelectSelector selector) {
  144. appendSelector(selector);
  145. }
  146. /**
  147. * add an "And" selector entry on the selector list
  148. * @param selector the selector to add
  149. */
  150. public void addAnd(AndSelector selector) {
  151. appendSelector(selector);
  152. }
  153. /**
  154. * add an "Or" selector entry on the selector list
  155. * @param selector the selector to add
  156. */
  157. public void addOr(OrSelector selector) {
  158. appendSelector(selector);
  159. }
  160. /**
  161. * add a "Not" selector entry on the selector list
  162. * @param selector the selector to add
  163. */
  164. public void addNot(NotSelector selector) {
  165. appendSelector(selector);
  166. }
  167. /**
  168. * add a "None" selector entry on the selector list
  169. * @param selector the selector to add
  170. */
  171. public void addNone(NoneSelector selector) {
  172. appendSelector(selector);
  173. }
  174. /**
  175. * add a majority selector entry on the selector list
  176. * @param selector the selector to add
  177. */
  178. public void addMajority(MajoritySelector selector) {
  179. appendSelector(selector);
  180. }
  181. /**
  182. * add a selector date entry on the selector list
  183. * @param selector the selector to add
  184. */
  185. public void addDate(DateSelector selector) {
  186. appendSelector(selector);
  187. }
  188. /**
  189. * add a selector size entry on the selector list
  190. * @param selector the selector to add
  191. */
  192. public void addSize(SizeSelector selector) {
  193. appendSelector(selector);
  194. }
  195. /**
  196. * add a selector filename entry on the selector list
  197. * @param selector the selector to add
  198. */
  199. public void addFilename(FilenameSelector selector) {
  200. appendSelector(selector);
  201. }
  202. /**
  203. * add an extended selector entry on the selector list
  204. * @param selector the selector to add
  205. */
  206. public void addCustom(ExtendSelector selector) {
  207. appendSelector(selector);
  208. }
  209. /**
  210. * add a contains selector entry on the selector list
  211. * @param selector the selector to add
  212. */
  213. public void addContains(ContainsSelector selector) {
  214. appendSelector(selector);
  215. }
  216. /**
  217. * add a present selector entry on the selector list
  218. * @param selector the selector to add
  219. */
  220. public void addPresent(PresentSelector selector) {
  221. appendSelector(selector);
  222. }
  223. /**
  224. * add a depth selector entry on the selector list
  225. * @param selector the selector to add
  226. */
  227. public void addDepth(DepthSelector selector) {
  228. appendSelector(selector);
  229. }
  230. /**
  231. * add a depends selector entry on the selector list
  232. * @param selector the selector to add
  233. */
  234. public void addDepend(DependSelector selector) {
  235. appendSelector(selector);
  236. }
  237. /**
  238. * adds a different selector to the selector list
  239. * @param selector the selector to add
  240. */
  241. public void addDifferent(DifferentSelector selector) {
  242. appendSelector(selector);
  243. }
  244. /**
  245. * adds a type selector to the selector list
  246. * @param selector the selector to add
  247. */
  248. public void addType(TypeSelector selector) {
  249. appendSelector(selector);
  250. }
  251. /**
  252. * add a regular expression selector entry on the selector list
  253. * @param selector the selector to add
  254. */
  255. public void addContainsRegexp(ContainsRegexpSelector selector) {
  256. appendSelector(selector);
  257. }
  258. /**
  259. * add the modified selector
  260. * @param selector the selector to add
  261. * @since ant 1.6
  262. */
  263. public void addModified(ModifiedSelector selector) {
  264. appendSelector(selector);
  265. }
  266. /**
  267. * add an arbitary selector
  268. * @param selector the selector to add
  269. * @since Ant 1.6
  270. */
  271. public void add(FileSelector selector) {
  272. appendSelector(selector);
  273. }
  274. }