001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018:
019: package org.apache.tools.ant.types;
020:
021: import java.util.Vector;
022: import org.apache.tools.ant.BuildException;
023:
024: /**
025: * Wrapper for environment variables.
026: *
027: */
028: public class Environment {
029: // CheckStyle:VisibilityModifier OFF - bc
030:
031: /**
032: * a vector of type Enviromment.Variable
033: * @see Variable
034: */
035: protected Vector variables;
036:
037: // CheckStyle:VisibilityModifier ON
038:
039: /**
040: * representation of a single env value
041: */
042: public static class Variable {
043:
044: /**
045: * env key and value pair; everything gets expanded to a string
046: * during assignment
047: */
048: private String key, value;
049:
050: /**
051: * Constructor for variable
052: *
053: */
054: public Variable() {
055: super ();
056: }
057:
058: /**
059: * set the key
060: * @param key string
061: */
062: public void setKey(String key) {
063: this .key = key;
064: }
065:
066: /**
067: * set the value
068: * @param value string value
069: */
070: public void setValue(String value) {
071: this .value = value;
072: }
073:
074: /**
075: * key accessor
076: * @return key
077: */
078: public String getKey() {
079: return this .key;
080: }
081:
082: /**
083: * value accessor
084: * @return value
085: */
086: public String getValue() {
087: return this .value;
088: }
089:
090: /**
091: * stringify path and assign to the value.
092: * The value will contain all path elements separated by the appropriate
093: * separator
094: * @param path path
095: */
096: public void setPath(Path path) {
097: this .value = path.toString();
098: }
099:
100: /**
101: * get the absolute path of a file and assign it to the value
102: * @param file file to use as the value
103: */
104: public void setFile(java.io.File file) {
105: this .value = file.getAbsolutePath();
106: }
107:
108: /**
109: * get the assigment string
110: * This is not ready for insertion into a property file without following
111: * the escaping rules of the properties class.
112: * @return a string of the form key=value.
113: * @throws BuildException if key or value are unassigned
114: */
115: public String getContent() throws BuildException {
116: validate();
117: StringBuffer sb = new StringBuffer(key.trim());
118: sb.append("=").append(value.trim());
119: return sb.toString();
120: }
121:
122: /**
123: * checks whether all required attributes have been specified.
124: * @throws BuildException if key or value are unassigned
125: */
126: public void validate() {
127: if (key == null || value == null) {
128: throw new BuildException(
129: "key and value must be specified "
130: + "for environment variables.");
131: }
132: }
133: }
134:
135: /**
136: * constructor
137: */
138: public Environment() {
139: variables = new Vector();
140: }
141:
142: /**
143: * add a variable.
144: * Validity checking is <i>not</i> performed at this point. Duplicates
145: * are not caught either.
146: * @param var new variable.
147: */
148: public void addVariable(Variable var) {
149: variables.addElement(var);
150: }
151:
152: /**
153: * get the variable list as an array
154: * @return array of key=value assignment strings
155: * @throws BuildException if any variable is misconfigured
156: */
157: public String[] getVariables() throws BuildException {
158: if (variables.size() == 0) {
159: return null;
160: }
161: String[] result = new String[variables.size()];
162: for (int i = 0; i < result.length; i++) {
163: result[i] = ((Variable) variables.elementAt(i))
164: .getContent();
165: }
166: return result;
167: }
168:
169: /**
170: * Get the raw vector of variables. This is not a clone.
171: * @return a potentially empty (but never null) vector of elements of type
172: * Variable
173: * @since Ant 1.7
174: */
175: public Vector getVariablesVector() {
176: return variables;
177: }
178: }
|