1. /*
  2. * Copyright 2003-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.types.EnumeratedAttribute;
  20. import org.apache.tools.ant.types.Parameter;
  21. /**
  22. * Selector that selects a certain kind of file: directory or regular.
  23. *
  24. * @since 1.6
  25. */
  26. public class TypeSelector extends BaseExtendSelector {
  27. private String type = null;
  28. /** Key to used for parameterized custom selector */
  29. public static final String TYPE_KEY = "type";
  30. /**
  31. * Creates a new <code>TypeSelector</code> instance.
  32. *
  33. */
  34. public TypeSelector() {
  35. }
  36. /**
  37. * @return a string describing this object
  38. */
  39. public String toString() {
  40. StringBuffer buf = new StringBuffer("{typeselector type: ");
  41. buf.append(type);
  42. buf.append("}");
  43. return buf.toString();
  44. }
  45. /**
  46. * Set the type of file to require.
  47. * @param fileTypes the type of file - file or dir
  48. */
  49. public void setType(FileType fileTypes) {
  50. this.type = fileTypes.getValue();
  51. }
  52. /**
  53. * When using this as a custom selector, this method will be called.
  54. * It translates each parameter into the appropriate setXXX() call.
  55. *
  56. * @param parameters the complete set of parameters for this selector
  57. */
  58. public void setParameters(Parameter[] parameters) {
  59. super.setParameters(parameters);
  60. if (parameters != null) {
  61. for (int i = 0; i < parameters.length; i++) {
  62. String paramname = parameters[i].getName();
  63. if (TYPE_KEY.equalsIgnoreCase(paramname)) {
  64. FileType type = new FileType();
  65. type.setValue(parameters[i].getValue());
  66. setType(type);
  67. } else {
  68. setError("Invalid parameter " + paramname);
  69. }
  70. }
  71. }
  72. }
  73. /**
  74. * Checks to make sure all settings are kosher. In this case, it
  75. * means that the pattern attribute has been set.
  76. *
  77. */
  78. public void verifySettings() {
  79. if (type == null) {
  80. setError("The type attribute is required");
  81. }
  82. }
  83. /**
  84. * The heart of the matter. This is where the selector gets to decide
  85. * on the inclusion of a file in a particular fileset.
  86. *
  87. * @param basedir the base directory the scan is being done from
  88. * @param filename is the name of the file to check
  89. * @param file is a java.io.File object the selector can use
  90. * @return whether the file should be selected or not
  91. */
  92. public boolean isSelected(File basedir, String filename, File file) {
  93. // throw BuildException on error
  94. validate();
  95. if (file.isDirectory()) {
  96. return type.equals(FileType.DIR);
  97. } else {
  98. return type.equals(FileType.FILE);
  99. }
  100. }
  101. /**
  102. * Enumerated attribute with the values for types of file
  103. */
  104. public static class FileType extends EnumeratedAttribute {
  105. /** the string value for file */
  106. public static final String FILE = "file";
  107. /** the string value for dir */
  108. public static final String DIR = "dir";
  109. /**
  110. * @return the values as an array of strings
  111. */
  112. public String[] getValues() {
  113. return new String[]{FILE, DIR};
  114. }
  115. }
  116. }