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.util.Enumeration;
  19. import java.io.File;
  20. import org.apache.tools.ant.Project;
  21. /**
  22. * This selector just holds one other selector and forwards all
  23. * requests to it. It exists so that there is a single selector
  24. * type that can exist outside of any targets, as an element of
  25. * project. It overrides all of the reference stuff so that it
  26. * works as expected. Note that this is the only selector you
  27. * can reference.
  28. *
  29. * @since 1.5
  30. */
  31. public class SelectSelector extends BaseSelectorContainer {
  32. private String ifProperty;
  33. private String unlessProperty;
  34. /**
  35. * Default constructor.
  36. */
  37. public SelectSelector() {
  38. }
  39. /**
  40. * @return a string describing this object
  41. */
  42. public String toString() {
  43. StringBuffer buf = new StringBuffer();
  44. if (hasSelectors()) {
  45. buf.append("{select");
  46. if (ifProperty != null) {
  47. buf.append(" if: ");
  48. buf.append(ifProperty);
  49. }
  50. if (unlessProperty != null) {
  51. buf.append(" unless: ");
  52. buf.append(unlessProperty);
  53. }
  54. buf.append(" ");
  55. buf.append(super.toString());
  56. buf.append("}");
  57. }
  58. return buf.toString();
  59. }
  60. /**
  61. * Performs the check for circular references and returns the
  62. * referenced Selector.
  63. */
  64. private SelectSelector getRef() {
  65. Object o = getCheckedRef(this.getClass(), "SelectSelector");
  66. return (SelectSelector) o;
  67. }
  68. /**
  69. * Indicates whether there are any selectors here.
  70. * @return whether any selectors are in this container
  71. */
  72. public boolean hasSelectors() {
  73. if (isReference()) {
  74. return getRef().hasSelectors();
  75. }
  76. return super.hasSelectors();
  77. }
  78. /**
  79. * Gives the count of the number of selectors in this container
  80. * @return the number of selectors in this container
  81. */
  82. public int selectorCount() {
  83. if (isReference()) {
  84. return getRef().selectorCount();
  85. }
  86. return super.selectorCount();
  87. }
  88. /**
  89. * Returns the set of selectors as an array.
  90. * @param p the current project
  91. * @return an array of selectors in this container
  92. */
  93. public FileSelector[] getSelectors(Project p) {
  94. if (isReference()) {
  95. return getRef().getSelectors(p);
  96. }
  97. return super.getSelectors(p);
  98. }
  99. /**
  100. * Returns an enumerator for accessing the set of selectors.
  101. * @return an enumerator that goes through each of the selectors
  102. */
  103. public Enumeration selectorElements() {
  104. if (isReference()) {
  105. return getRef().selectorElements();
  106. }
  107. return super.selectorElements();
  108. }
  109. /**
  110. * Add a new selector into this container.
  111. *
  112. * @param selector the new selector to add
  113. */
  114. public void appendSelector(FileSelector selector) {
  115. if (isReference()) {
  116. throw noChildrenAllowed();
  117. }
  118. super.appendSelector(selector);
  119. }
  120. /**
  121. * Makes sure that there is only one entry, sets an error message if
  122. * not.
  123. */
  124. public void verifySettings() {
  125. int cnt = selectorCount();
  126. if (cnt < 0 || cnt > 1) {
  127. setError("Only one selector is allowed within the "
  128. + "<selector> tag");
  129. }
  130. }
  131. /**
  132. * Ensures that the selector passes the conditions placed
  133. * on it with <code>if</code> and <code>unless</code>.
  134. * @return true if conditions are passed
  135. */
  136. public boolean passesConditions() {
  137. if (ifProperty != null
  138. && getProject().getProperty(ifProperty) == null) {
  139. return false;
  140. } else if (unlessProperty != null
  141. && getProject().getProperty(unlessProperty) != null) {
  142. return false;
  143. }
  144. return true;
  145. }
  146. /**
  147. * Sets the if attribute to a property which must exist for the
  148. * selector to select any files.
  149. * @param ifProperty the property to check
  150. */
  151. public void setIf(String ifProperty) {
  152. this.ifProperty = ifProperty;
  153. }
  154. /**
  155. * Sets the unless attribute to a property which cannot exist for the
  156. * selector to select any files.
  157. * @param unlessProperty the property to check
  158. */
  159. public void setUnless(String unlessProperty) {
  160. this.unlessProperty = unlessProperty;
  161. }
  162. /**
  163. * Returns true (the file is selected) only if the if property (if any)
  164. * exists, the unless property (if any) doesn't exist, and the
  165. * contained selector (if any) selects the file. If there is no contained
  166. * selector, return true (because we assume that the point was to test
  167. * the if and unless conditions).
  168. *
  169. * @param basedir the base directory the scan is being done from
  170. * @param filename the name of the file to check
  171. * @param file a java.io.File object for the filename that the selector
  172. * can use
  173. * @return whether the file should be selected or not
  174. */
  175. public boolean isSelected(File basedir, String filename, File file) {
  176. validate();
  177. // Deal with if and unless properties first
  178. if (!(passesConditions())) {
  179. return false;
  180. }
  181. Enumeration e = selectorElements();
  182. if (!(e.hasMoreElements())) {
  183. return true;
  184. }
  185. FileSelector f = (FileSelector) e.nextElement();
  186. return f.isSelected(basedir, filename, file);
  187. }
  188. }