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.Commandline;
  20. import org.apache.tools.ant.types.DataType;
  21. import org.apache.tools.ant.types.Path;
  22. /**
  23. * A class that represents nested <arg> or <jvmarg> elements. This class
  24. * provides the same functionality as the class that represents these same
  25. * elements in a "java" task. In addition, this class supports conditional "if"
  26. * and "unless" attributes.
  27. *
  28. * @author Patrick Luby
  29. */
  30. public class ConditionalArgument extends DataType {
  31. //------------------------------------------------------------------ Fields
  32. /**
  33. * Cached "if" condition flag.
  34. */
  35. private String ifCondition = null;
  36. /**
  37. * Cached "unless" condition flag.
  38. */
  39. private String unlessCondition = null;
  40. /**
  41. * Cached command line arguments.
  42. */
  43. private String[] parts = null;
  44. //----------------------------------------------------------------- Methods
  45. /**
  46. * Get the "if" condition flag.
  47. *
  48. * @return the "if" condition flag
  49. */
  50. public String getIf() {
  51. return ProjectHelper.replaceProperties(project, ifCondition, project.getProperties());
  52. }
  53. /**
  54. * Get a single command line argument.
  55. *
  56. * @return a single command line argument
  57. */
  58. public String[] getParts() {
  59. String[] list = new String[parts.length];
  60. for (int i = 0; i < parts.length; i++)
  61. list[i] = ProjectHelper.replaceProperties(project, parts[i], project.getProperties());
  62. return list;
  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. * Set a single command line argument to the absolute
  74. * filename of the specified file.
  75. *
  76. * @param file a single command line argument
  77. */
  78. public void setFile(File file) {
  79. this.parts = new String[]{ file.getAbsolutePath() };
  80. }
  81. /**
  82. * Set the "if" condition. Tasks that nest this class as an element
  83. * should evaluate this flag in their {@link org.apache.tools.ant.Task#execute()} method. If the
  84. * following conditions are true, the task should process this element:
  85. * <ul>
  86. * <ol>The flag is neither null nor a empty string
  87. * <ol>The property that the flag resolves to after macro substitution
  88. * is defined
  89. * </ul>
  90. *
  91. * @param property a property name or macro
  92. */
  93. public void setIf(String property) {
  94. this.ifCondition = property;
  95. }
  96. /**
  97. * Set a line to split into several command line arguments.
  98. *
  99. * @param line line to split into several commandline arguments
  100. */
  101. public void setLine(String line) {
  102. parts = Commandline.translateCommandline(line);
  103. }
  104. /**
  105. * Set a single command line argument and treat it like a path. The
  106. * correct path separator for the platform is used.
  107. *
  108. * @param path a single command line argument
  109. */
  110. public void setPath(Path path) {
  111. this.parts = new String[]{ path.toString() };
  112. }
  113. /**
  114. * Set the "unless" condition. Tasks that nest this class as an element
  115. * should evaluate this flag in their {@link org.apache.tools.ant.Task#execute()} method. If the
  116. * following conditions are true, the task should ignore this element:
  117. * <ul>
  118. * <ol>The flag is neither null nor a empty string
  119. * <ol>The property that the flag resolves to after macro substitution
  120. * is defined
  121. * </ul>
  122. *
  123. * @param property a property name or macro
  124. */
  125. public void setUnless(String property) {
  126. this.unlessCondition = property;
  127. }
  128. /**
  129. * Set a single command line argument.
  130. *
  131. * @param value a single command line argument
  132. */
  133. public void setValue(String value) {
  134. this.parts = new String[]{ value };
  135. }
  136. }