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 org.apache.tools.ant.Project;
  20. import org.apache.tools.ant.types.Parameter;
  21. /**
  22. * Selector that filters files based on the filename.
  23. *
  24. * @since 1.5
  25. */
  26. public class FilenameSelector extends BaseExtendSelector {
  27. private String pattern = null;
  28. private boolean casesensitive = true;
  29. private boolean negated = false;
  30. /** Used for parameterized custom selector */
  31. public static final String NAME_KEY = "name";
  32. /** Used for parameterized custom selector */
  33. public static final String CASE_KEY = "casesensitive";
  34. /** Used for parameterized custom selector */
  35. public static final String NEGATE_KEY = "negate";
  36. /**
  37. * Creates a new <code>FilenameSelector</code> instance.
  38. *
  39. */
  40. public FilenameSelector() {
  41. }
  42. /**
  43. * @return a string describing this object
  44. */
  45. public String toString() {
  46. StringBuffer buf = new StringBuffer("{filenameselector name: ");
  47. buf.append(pattern);
  48. buf.append(" negate: ");
  49. if (negated) {
  50. buf.append("true");
  51. } else {
  52. buf.append("false");
  53. }
  54. buf.append(" casesensitive: ");
  55. if (casesensitive) {
  56. buf.append("true");
  57. } else {
  58. buf.append("false");
  59. }
  60. buf.append("}");
  61. return buf.toString();
  62. }
  63. /**
  64. * The name of the file, or the pattern for the name, that
  65. * should be used for selection.
  66. *
  67. * @param pattern the file pattern that any filename must match
  68. * against in order to be selected.
  69. */
  70. public void setName(String pattern) {
  71. pattern = pattern.replace('/', File.separatorChar).replace('\\',
  72. File.separatorChar);
  73. if (pattern.endsWith(File.separator)) {
  74. pattern += "**";
  75. }
  76. this.pattern = pattern;
  77. }
  78. /**
  79. * Whether to ignore case when checking filenames.
  80. *
  81. * @param casesensitive whether to pay attention to case sensitivity
  82. */
  83. public void setCasesensitive(boolean casesensitive) {
  84. this.casesensitive = casesensitive;
  85. }
  86. /**
  87. * You can optionally reverse the selection of this selector,
  88. * thereby emulating an <exclude> tag, by setting the attribute
  89. * negate to true. This is identical to surrounding the selector
  90. * with <not></not>.
  91. *
  92. * @param negated whether to negate this selection
  93. */
  94. public void setNegate(boolean negated) {
  95. this.negated = negated;
  96. }
  97. /**
  98. * When using this as a custom selector, this method will be called.
  99. * It translates each parameter into the appropriate setXXX() call.
  100. *
  101. * @param parameters the complete set of parameters for this selector
  102. */
  103. public void setParameters(Parameter[] parameters) {
  104. super.setParameters(parameters);
  105. if (parameters != null) {
  106. for (int i = 0; i < parameters.length; i++) {
  107. String paramname = parameters[i].getName();
  108. if (NAME_KEY.equalsIgnoreCase(paramname)) {
  109. setName(parameters[i].getValue());
  110. } else if (CASE_KEY.equalsIgnoreCase(paramname)) {
  111. setCasesensitive(Project.toBoolean(
  112. parameters[i].getValue()));
  113. } else if (NEGATE_KEY.equalsIgnoreCase(paramname)) {
  114. setNegate(Project.toBoolean(parameters[i].getValue()));
  115. } else {
  116. setError("Invalid parameter " + paramname);
  117. }
  118. }
  119. }
  120. }
  121. /**
  122. * Checks to make sure all settings are kosher. In this case, it
  123. * means that the name attribute has been set.
  124. *
  125. */
  126. public void verifySettings() {
  127. if (pattern == null) {
  128. setError("The name attribute is required");
  129. }
  130. }
  131. /**
  132. * The heart of the matter. This is where the selector gets to decide
  133. * on the inclusion of a file in a particular fileset. Most of the work
  134. * for this selector is offloaded into SelectorUtils, a static class
  135. * that provides the same services for both FilenameSelector and
  136. * DirectoryScanner.
  137. *
  138. * @param basedir the base directory the scan is being done from
  139. * @param filename is the name of the file to check
  140. * @param file is a java.io.File object the selector can use
  141. * @return whether the file should be selected or not
  142. */
  143. public boolean isSelected(File basedir, String filename, File file) {
  144. validate();
  145. return (SelectorUtils.matchPath(pattern, filename,
  146. casesensitive) == !(negated));
  147. }
  148. }