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.BuildException;
  20. import org.apache.tools.ant.types.DataType;
  21. /**
  22. * A convenience base class that you can subclass Selectors from. It
  23. * provides some helpful common behaviour. Note that there is no need
  24. * for Selectors to inherit from this class, it is only necessary that
  25. * they implement FileSelector.
  26. *
  27. * @since 1.5
  28. */
  29. public abstract class BaseSelector extends DataType implements FileSelector {
  30. private String errmsg = null;
  31. /**
  32. * Do nothing constructor.
  33. */
  34. public BaseSelector() {
  35. }
  36. /**
  37. * Allows all selectors to indicate a setup error. Note that only
  38. * the first error message is recorded.
  39. *
  40. * @param msg The error message any BuildException should throw.
  41. */
  42. public void setError(String msg) {
  43. if (errmsg == null) {
  44. errmsg = msg;
  45. }
  46. }
  47. /**
  48. * Returns any error messages that have been set.
  49. *
  50. * @return the error condition
  51. */
  52. public String getError() {
  53. return errmsg;
  54. }
  55. /**
  56. * <p>Subclasses can override this method to provide checking of their
  57. * state. So long as they call validate() from isSelected(), this will
  58. * be called automatically (unless they override validate()).</p>
  59. * <p>Implementations should check for incorrect settings and call
  60. * setError() as necessary.</p>
  61. */
  62. public void verifySettings() {
  63. }
  64. /**
  65. * Subclasses can use this to throw the requisite exception
  66. * in isSelected() in the case of an error condition.
  67. */
  68. public void validate() {
  69. if (getError() == null) {
  70. verifySettings();
  71. }
  72. if (getError() != null) {
  73. throw new BuildException(errmsg);
  74. }
  75. }
  76. /**
  77. * Method that each selector will implement to create their
  78. * selection behaviour. If there is a problem with the setup
  79. * of a selector, it can throw a BuildException to indicate
  80. * the problem.
  81. *
  82. * @param basedir A java.io.File object for the base directory
  83. * @param filename The name of the file to check
  84. * @param file A File object for this filename
  85. * @return whether the file should be selected or not
  86. */
  87. public abstract boolean isSelected(File basedir, String filename,
  88. File file);
  89. }