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 4.4 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. package org.apache.tools.ant.taskdefs;
  19. import org.apache.tools.ant.BuildException;
  20. import org.apache.tools.ant.Project;
  21. import org.apache.tools.ant.PropertyHelper;
  22. import org.apache.tools.ant.taskdefs.condition.Condition;
  23. import org.apache.tools.ant.taskdefs.condition.ConditionBase;
  24. /**
  25. * Task to set a property conditionally using <uptodate>, <available>,
  26. * and many other supported conditions.
  27. *
  28. * <p>This task supports boolean logic as well as pluggable conditions
  29. * to decide, whether a property should be set.</p>
  30. *
  31. * <p>This task does not extend Task to take advantage of
  32. * ConditionBase.</p>
  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 Object value = "true";
  41. private Object alternative = null;
  42. /**
  43. * Constructor, names this task "condition".
  44. */
  45. public ConditionTask() {
  46. super("condition");
  47. }
  48. /**
  49. * The name of the property to set. Required.
  50. * @param p the name of the property
  51. * @since Ant 1.4
  52. */
  53. public void setProperty(String p) {
  54. property = p;
  55. }
  56. /**
  57. * The value for the property to set, if condition evaluates to true.
  58. * Defaults to "true".
  59. * @param value the (Object) value of the property
  60. * @since Ant 1.8
  61. */
  62. public void setValue(Object value) {
  63. this.value = value;
  64. }
  65. /**
  66. * The value for the property to set, if condition evaluates to true.
  67. * Defaults to "true".
  68. * @param v the value of the property
  69. * @since Ant 1.4
  70. */
  71. public void setValue(String v) {
  72. setValue((Object) v);
  73. }
  74. /**
  75. * The value for the property to set, if condition evaluates to false.
  76. * If this attribute is not specified, the property will not be set.
  77. * @param alt the alternate value of the property.
  78. * @since Ant 1.8
  79. */
  80. public void setElse(Object alt) {
  81. alternative = alt;
  82. }
  83. /**
  84. * The value for the property to set, if condition evaluates to false.
  85. * If this attribute is not specified, the property will not be set.
  86. * @param e the alternate value of the property.
  87. * @since Ant 1.6.3
  88. */
  89. public void setElse(String e) {
  90. setElse((Object) e);
  91. }
  92. /**
  93. * See whether our nested condition holds and set the property.
  94. *
  95. * @since Ant 1.4
  96. * @exception BuildException if an error occurs
  97. */
  98. public void execute() throws BuildException {
  99. if (countConditions() > 1) {
  100. throw new BuildException("You must not nest more than one condition into <"
  101. + getTaskName() + ">");
  102. }
  103. if (countConditions() < 1) {
  104. throw new BuildException("You must nest a condition into <" + getTaskName() + ">");
  105. }
  106. if (property == null) {
  107. throw new BuildException("The property attribute is required.");
  108. }
  109. Condition c = (Condition) getConditions().nextElement();
  110. if (c.eval()) {
  111. log("Condition true; setting " + property + " to " + value, Project.MSG_DEBUG);
  112. PropertyHelper.getPropertyHelper(getProject()).setNewProperty(property, value);
  113. } else if (alternative != null) {
  114. log("Condition false; setting " + property + " to " + alternative, Project.MSG_DEBUG);
  115. PropertyHelper.getPropertyHelper(getProject()).setNewProperty(property, alternative);
  116. } else {
  117. log("Condition false; not setting " + property, Project.MSG_DEBUG);
  118. }
  119. }
  120. }