1. /*
  2. * Copyright 2001-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.Project;
  20. import org.apache.tools.ant.taskdefs.condition.Condition;
  21. import org.apache.tools.ant.taskdefs.condition.ConditionBase;
  22. /**
  23. * Task to set a property conditionally using <uptodate>, <available>,
  24. * and many other supported conditions.
  25. *
  26. * <p>This task supports boolean logic as well as pluggable conditions
  27. * to decide, whether a property should be set.</p>
  28. *
  29. * <p>This task does not extend Task to take advantage of
  30. * ConditionBase.</p>
  31. *
  32. * @version $Revision: 1.16.2.4 $
  33. *
  34. * @since Ant 1.4
  35. *
  36. * @ant.task category="control"
  37. */
  38. public class ConditionTask extends ConditionBase {
  39. private String property = null;
  40. private String value = "true";
  41. /**
  42. * The name of the property to set. Required.
  43. * @param p the name of the property
  44. * @since Ant 1.4
  45. */
  46. public void setProperty(String p) {
  47. property = p;
  48. }
  49. /**
  50. * The value for the property to set, if condition evaluates to true.
  51. * Defaults to "true".
  52. * @param v the value of the property
  53. * @since Ant 1.4
  54. */
  55. public void setValue(String v) {
  56. value = v;
  57. }
  58. /**
  59. * See whether our nested condition holds and set the property.
  60. *
  61. * @since Ant 1.4
  62. * @exception BuildException if an error occurs
  63. */
  64. public void execute() throws BuildException {
  65. if (countConditions() > 1) {
  66. throw new BuildException("You must not nest more than one "
  67. + "condition into <condition>");
  68. }
  69. if (countConditions() < 1) {
  70. throw new BuildException("You must nest a condition into "
  71. + "<condition>");
  72. }
  73. if (property == null) {
  74. throw new BuildException("The property attribute is required.");
  75. }
  76. Condition c = (Condition) getConditions().nextElement();
  77. if (c.eval()) {
  78. log("Condition true; setting " + property + " to " + value,
  79. Project.MSG_DEBUG);
  80. getProject().setNewProperty(property, value);
  81. } else {
  82. log("Condition false; not setting " + property,
  83. Project.MSG_DEBUG);
  84. }
  85. }
  86. }