source: other-projects/trunk/gs3-release-maker/apache-ant-1.6.5/src/main/org/apache/tools/ant/types/Environment.java@ 14627

Last change on this file since 14627 was 14627, checked in by oranfry, 17 years ago

initial import of the gs3-release-maker

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