1. /*
  2. * Copyright 2000,2002-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.types;
  18. import java.util.Vector;
  19. import org.apache.tools.ant.BuildException;
  20. /**
  21. * Wrapper for environment variables.
  22. *
  23. */
  24. public class Environment {
  25. /**
  26. * a vector of type Enviromment.Variable
  27. * @see Variable
  28. */
  29. protected Vector variables;
  30. /**
  31. * representation of a single env value
  32. */
  33. public static class Variable {
  34. /**
  35. * env key and value pair; everything gets expanded to a string
  36. * during assignment
  37. */
  38. private String key, value;
  39. /**
  40. * Constructor for variable
  41. *
  42. */
  43. public Variable() {
  44. super();
  45. }
  46. /**
  47. * set the key
  48. * @param key string
  49. */
  50. public void setKey(String key) {
  51. this.key = key;
  52. }
  53. /**
  54. * set the value
  55. * @param value string value
  56. */
  57. public void setValue(String value) {
  58. this.value = value;
  59. }
  60. /**
  61. * key accessor
  62. * @return key
  63. */
  64. public String getKey() {
  65. return this.key;
  66. }
  67. /**
  68. * value accessor
  69. * @return value
  70. */
  71. public String getValue() {
  72. return this.value;
  73. }
  74. /**
  75. * stringify path and assign to the value.
  76. * The value will contain all path elements separated by the appropriate
  77. * separator
  78. * @param path path
  79. */
  80. public void setPath(Path path) {
  81. this.value = path.toString();
  82. }
  83. /**
  84. * get the absolute path of a file and assign it to the value
  85. * @param file file to use as the value
  86. */
  87. public void setFile(java.io.File file) {
  88. this.value = file.getAbsolutePath();
  89. }
  90. /**
  91. * get the assigment string
  92. * This is not ready for insertion into a property file without following
  93. * the escaping rules of the properties class.
  94. * @return a string of the form key=value.
  95. * @throws BuildException if key or value are unassigned
  96. */
  97. public String getContent() throws BuildException {
  98. if (key == null || value == null) {
  99. throw new BuildException("key and value must be specified "
  100. + "for environment variables.");
  101. }
  102. StringBuffer sb = new StringBuffer(key.trim());
  103. sb.append("=").append(value.trim());
  104. return sb.toString();
  105. }
  106. }
  107. /**
  108. * constructor
  109. */
  110. public Environment() {
  111. variables = new Vector();
  112. }
  113. /**
  114. * add a variable.
  115. * Validity checking is <i>not</i> performed at this point. Duplicates
  116. * are not caught either.
  117. * @param var new variable.
  118. */
  119. public void addVariable(Variable var) {
  120. variables.addElement(var);
  121. }
  122. /**
  123. * get the variable list as an array
  124. * @return array of key=value assignment strings
  125. * @throws BuildException if any variable is misconfigured
  126. */
  127. public String[] getVariables() throws BuildException {
  128. if (variables.size() == 0) {
  129. return null;
  130. }
  131. String[] result = new String[variables.size()];
  132. for (int i = 0; i < result.length; i++) {
  133. result[i] = ((Variable) variables.elementAt(i)).getContent();
  134. }
  135. return result;
  136. }
  137. }