PageRenderTime 42ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/projects/ant-1.8.2/src/main/org/apache/tools/ant/types/Environment.java

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