You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

ConditionAndTask.java 2.8 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /*
  2. * Copyright 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.taskdefs;
  18. import org.apache.tools.ant.BuildException;
  19. import org.apache.tools.ant.Task;
  20. import org.apache.tools.ant.taskdefs.condition.Condition;
  21. /**
  22. * Abstract task to allow defintion of a task or a condition.
  23. * It has property and value (for the property) attributes.
  24. *
  25. * @since Ant 1.7
  26. *
  27. * @ant.task category="control"
  28. */
  29. public abstract class ConditionAndTask extends Task implements Condition {
  30. private String property;
  31. private String value = "true";
  32. /**
  33. * Set the name of the property which will be set if the particular resource
  34. * is available.
  35. *
  36. * @param property the name of the property to set.
  37. */
  38. public void setProperty(String property) {
  39. this.property = property;
  40. }
  41. /**
  42. * Set the value to be given to the property if the desired resource is
  43. * available.
  44. *
  45. * @param value the value to be given.
  46. */
  47. public void setValue(String value) {
  48. this.value = value;
  49. }
  50. /**
  51. * This method should be overridden by derived classes.
  52. * It is used by eval() to evaluate the condition.
  53. * @return true if the condition passes, false otherwise.
  54. */
  55. protected abstract boolean evaluate();
  56. /**
  57. * This method evaluates the condition. It calls evaluate in the
  58. * derived class.
  59. * It sets the property if a property is present and if the
  60. * evaluate returns true.
  61. * @return true if the condition passes, false otherwise.
  62. */
  63. public boolean eval() {
  64. if (evaluate()) {
  65. if (property != null) {
  66. getProject().setNewProperty(property, value);
  67. }
  68. return true;
  69. } else {
  70. return false;
  71. }
  72. }
  73. /**
  74. * Entry point when operating as a task.
  75. *
  76. * @exception BuildException if the task is not configured correctly.
  77. */
  78. public void execute() throws BuildException {
  79. if (property == null) {
  80. throw new BuildException("property attribute is required",
  81. getLocation());
  82. }
  83. eval();
  84. }
  85. }