KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > types > Environment


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
19 package org.apache.tools.ant.types;
20
21 import java.util.Vector JavaDoc;
22 import org.apache.tools.ant.BuildException;
23
24 /**
25  * Wrapper for environment variables.
26  *
27  */

28 public class Environment {
29     // CheckStyle:VisibilityModifier OFF - bc
30

31     /**
32      * a vector of type Enviromment.Variable
33      * @see Variable
34      */

35     protected Vector JavaDoc variables;
36
37     // CheckStyle:VisibilityModifier ON
38

39     /**
40      * representation of a single env value
41      */

42     public static class Variable {
43
44         /**
45          * env key and value pair; everything gets expanded to a string
46          * during assignment
47          */

48         private String JavaDoc key, value;
49
50         /**
51          * Constructor for variable
52          *
53          */

54         public Variable() {
55             super();
56         }
57
58         /**
59          * set the key
60          * @param key string
61          */

62         public void setKey(String JavaDoc key) {
63             this.key = key;
64         }
65
66         /**
67          * set the value
68          * @param value string value
69          */

70         public void setValue(String JavaDoc value) {
71             this.value = value;
72         }
73
74         /**
75          * key accessor
76          * @return key
77          */

78         public String JavaDoc getKey() {
79             return this.key;
80         }
81
82         /**
83          * value accessor
84          * @return value
85          */

86         public String JavaDoc getValue() {
87             return this.value;
88         }
89
90         /**
91          * stringify path and assign to the value.
92          * The value will contain all path elements separated by the appropriate
93          * separator
94          * @param path path
95          */

96         public void setPath(Path path) {
97             this.value = path.toString();
98         }
99
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 JavaDoc 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 JavaDoc getContent() throws BuildException {
116             validate();
117             StringBuffer JavaDoc sb = new StringBuffer JavaDoc(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("key and value must be specified "
129                     + "for environment variables.");
130             }
131         }
132     }
133
134     /**
135      * constructor
136      */

137     public Environment() {
138         variables = new Vector JavaDoc();
139     }
140
141     /**
142      * add a variable.
143      * Validity checking is <i>not</i> performed at this point. Duplicates
144      * are not caught either.
145      * @param var new variable.
146      */

147     public void addVariable(Variable var) {
148         variables.addElement(var);
149     }
150
151     /**
152      * get the variable list as an array
153      * @return array of key=value assignment strings
154      * @throws BuildException if any variable is misconfigured
155      */

156     public String JavaDoc[] getVariables() throws BuildException {
157         if (variables.size() == 0) {
158             return null;
159         }
160         String JavaDoc[] result = new String JavaDoc[variables.size()];
161         for (int i = 0; i < result.length; i++) {
162             result[i] = ((Variable) variables.elementAt(i)).getContent();
163         }
164         return result;
165     }
166
167     /**
168      * Get the raw vector of variables. This is not a clone.
169      * @return a potentially empty (but never null) vector of elements of type
170      * Variable
171      * @since Ant 1.7
172      */

173     public Vector JavaDoc getVariablesVector() {
174         return variables;
175     }
176 }
177
Popular Tags