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.

ConditionTask.java 3.4 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /*
  2. * Copyright 2001-2005 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. *
  33. * @since Ant 1.4
  34. *
  35. * @ant.task category="control"
  36. */
  37. public class ConditionTask extends ConditionBase {
  38. private String property = null;
  39. private String value = "true";
  40. private String alternative = null;
  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. * The value for the property to set, if condition evaluates to false.
  60. * If this attribute is not specified, the property will not be set.
  61. * @param e the alternate value of the property.
  62. * @since Ant 1.6.3
  63. */
  64. public void setElse(String e) {
  65. alternative = e;
  66. }
  67. /**
  68. * See whether our nested condition holds and set the property.
  69. *
  70. * @since Ant 1.4
  71. * @exception BuildException if an error occurs
  72. */
  73. public void execute() throws BuildException {
  74. if (countConditions() > 1) {
  75. throw new BuildException("You must not nest more than one "
  76. + "condition into <condition>");
  77. }
  78. if (countConditions() < 1) {
  79. throw new BuildException("You must nest a condition into "
  80. + "<condition>");
  81. }
  82. if (property == null) {
  83. throw new BuildException("The property attribute is required.");
  84. }
  85. Condition c = (Condition) getConditions().nextElement();
  86. if (c.eval()) {
  87. log("Condition true; setting " + property + " to " + value,
  88. Project.MSG_DEBUG);
  89. getProject().setNewProperty(property, value);
  90. } else if (alternative != null) {
  91. log("Condition false; setting " + property + " to " + alternative,
  92. Project.MSG_DEBUG);
  93. getProject().setNewProperty(property, alternative);
  94. } else {
  95. log("Condition false; not setting " + property,
  96. Project.MSG_DEBUG);
  97. }
  98. }
  99. }