1. /*
  2. * Copyright 1999-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. package org.apache.commons.launcher.types;
  17. import java.io.File;
  18. import org.apache.tools.ant.ProjectHelper;
  19. import org.apache.tools.ant.types.DataType;
  20. import org.apache.tools.ant.types.Path;
  21. /**
  22. * A class that represents nested <sysproperty> or <env> elements. This class
  23. * provides the same functionality as the class that represents these same
  24. * elements in a "java" task. In addition, this class supports conditional "if"
  25. * and "unless" attributes.
  26. *
  27. * @author Patrick Luby
  28. */
  29. public class ConditionalVariable extends DataType {
  30. //------------------------------------------------------------------ Fields
  31. /**
  32. * Cached "if" condition flag.
  33. */
  34. private String ifCondition = null;
  35. /**
  36. * Cached key.
  37. */
  38. private String key = null;
  39. /**
  40. * Cached "unless" condition flag.
  41. */
  42. private String unlessCondition = null;
  43. /**
  44. * Cached value.
  45. */
  46. private String value = null;
  47. //----------------------------------------------------------------- Methods
  48. /**
  49. * Get the "if" condition flag.
  50. *
  51. * @return the "if" condition flag
  52. */
  53. public String getIf() {
  54. return ProjectHelper.replaceProperties(project, ifCondition, project.getProperties());
  55. }
  56. /**
  57. * Get the key.
  58. *
  59. * @return the key for this variable
  60. */
  61. public String getKey() {
  62. return ProjectHelper.replaceProperties(project, key, project.getProperties());
  63. }
  64. /**
  65. * Get the "unless" condition flag.
  66. *
  67. * @return the "unless" condition flag
  68. */
  69. public String getUnless() {
  70. return ProjectHelper.replaceProperties(project, unlessCondition, project.getProperties());
  71. }
  72. /**
  73. * Get the value.
  74. *
  75. * @return the value for this variable
  76. */
  77. public String getValue() {
  78. return ProjectHelper.replaceProperties(project, value, project.getProperties());
  79. }
  80. /**
  81. * Set the value to a {@link File}.
  82. *
  83. * @param value the {@link File} for this variable
  84. */
  85. public void setFile(File file) {
  86. this.value = file.getAbsolutePath();
  87. }
  88. /**
  89. * Set the value to a {@link Path}.
  90. *
  91. * @param value the {@link Path} for this variable
  92. */
  93. public void setPath(Path path) {
  94. this.value = path.toString();
  95. }
  96. /**
  97. * Set the "if" condition. Tasks that nest this class as an element
  98. * should evaluate this flag in their {@link org.apache.tools.ant.Task#execute()} method. If the
  99. * following conditions are true, the task should process this element:
  100. * <ul>
  101. * <ol>The flag is neither null nor a empty string
  102. * <ol>The property that the flag resolves to after macro substitution
  103. * is defined
  104. * </ul>
  105. *
  106. * @param property a property name or macro
  107. */
  108. public void setIf(String property) {
  109. this.ifCondition = property;
  110. }
  111. /**
  112. * Set the key.
  113. *
  114. * @param key the key for this variable
  115. */
  116. public void setKey(String key) {
  117. this.key = key;
  118. }
  119. /**
  120. * Set the value to a {@link Path}.
  121. *
  122. * @param value the {@link Path} for this variable
  123. */
  124. public void setFile(Path path) {
  125. this.value = path.toString();
  126. }
  127. /**
  128. * Set the "unless" condition. Tasks that nest this class as an element
  129. * should evaluate this flag in their {@link org.apache.tools.ant.Task#execute()} method. If the
  130. * following conditions are true, the task should ignore this element:
  131. * <ul>
  132. * <ol>The flag is neither null nor a empty string
  133. * <ol>The property that the flag resolves to after macro substitution
  134. * is defined
  135. * </ul>
  136. *
  137. * @param property a property name or macro
  138. */
  139. public void setUnless(String property) {
  140. this.unlessCondition = property;
  141. }
  142. /**
  143. * Set the value.
  144. *
  145. * @param value the value for this variable
  146. */
  147. public void setValue(String value) {
  148. this.value = value;
  149. }
  150. }