KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > japex > Params


1 /*
2  * Japex ver. 0.1 software ("Software")
3  *
4  * Copyright, 2004-2005 Sun Microsystems, Inc. All Rights Reserved.
5  *
6  * This Software is distributed under the following terms:
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, is permitted provided that the following conditions are met:
10  *
11  * Redistributions of source code must retain the above copyright notice,
12  * this list of conditions and the following disclaimer.
13  *
14  * Redistribution in binary form must reproduce the above copyright notice,
15  * this list of conditions and the following disclaimer in the
16  * documentation and/or other materials provided with the distribution.
17  *
18  * Neither the name of Sun Microsystems, Inc., 'Java', 'Java'-based names,
19  * nor the names of contributors may be used to endorse or promote products
20  * derived from this Software without specific prior written permission.
21  *
22  * The Software is provided "AS IS," without a warranty of any kind. ALL
23  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING
24  * ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
25  * PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS
26  * SHALL NOT BE LIABLE FOR ANY DAMAGES OR LIABILITIES SUFFERED BY LICENSEE
27  * AS A RESULT OF OR RELATING TO USE, MODIFICATION OR DISTRIBUTION OF THE
28  * SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE
29  * LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT,
30  * SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED
31  * AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF OR
32  * INABILITY TO USE SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
33  * POSSIBILITY OF SUCH DAMAGES.
34  *
35  * You acknowledge that the Software is not designed, licensed or intended
36  * for use in the design, construction, operation or maintenance of any
37  * nuclear facility.
38  */

39
40 package com.sun.japex;
41
42 import java.util.*;
43
44 import org.apache.tools.ant.taskdefs.Execute;
45
46 public class Params {
47     
48     final static int OUT_EXPR = 0;
49     final static int IN_EXPR = 1;
50     final static String JavaDoc DELIMITER = "\uFFFE"; // A Unicode nonchar
51

52     private Properties _params;
53     
54     public Params() {
55         this(new Properties());
56     }
57     
58     public Params(Properties params) {
59         _params = params;
60     }
61
62     public synchronized Properties getParams() {
63         return _params;
64     }
65     
66     public synchronized boolean hasParam(String JavaDoc name) {
67         return _params.getProperty(name) != null;
68     }
69     
70     public synchronized String JavaDoc getParam(String JavaDoc name) {
71         return _params.getProperty(name);
72     }
73     
74     public synchronized void setParam(String JavaDoc name, String JavaDoc value) {
75         _params.setProperty(name, evaluate(name, value));
76     }
77     
78     public synchronized int getIntParam(String JavaDoc name) {
79         String JavaDoc s = _params.getProperty(name);
80         try {
81             return Integer.parseInt(s);
82         }
83         catch (NumberFormatException JavaDoc e) {
84             throw new RuntimeException JavaDoc(e);
85         }
86     }
87     
88     public synchronized void setIntParam(String JavaDoc name, int value) {
89         _params.setProperty(name, Integer.toString(value));
90     }
91     
92     public synchronized void setLongParam(String JavaDoc name, long value) {
93         _params.setProperty(name, Long.toString(value));
94     }
95     
96     public synchronized long getLongParam(String JavaDoc name) {
97         String JavaDoc s = _params.getProperty(name);
98         try {
99             return Long.parseLong(s);
100         }
101         catch (NumberFormatException JavaDoc e) {
102             throw new RuntimeException JavaDoc(e);
103         }
104     }
105     
106     public synchronized double getDoubleParam(String JavaDoc name) {
107         String JavaDoc s = _params.getProperty(name);
108         try {
109             return Double.parseDouble(s);
110         }
111         catch (NumberFormatException JavaDoc e) {
112             throw new RuntimeException JavaDoc(e);
113         }
114     }
115     
116     public synchronized void setDoubleParam(String JavaDoc name, double value) {
117         // Cconversion to String is less than ideal in this case.
118
_params.setProperty(name, Util.formatDouble(value));
119     }
120     
121     public void serialize(StringBuffer JavaDoc buffer, int indent) {
122         // Serialize built-in params first (TODO sort)
123
Enumeration names = _params.keys();
124         while (names.hasMoreElements()) {
125             String JavaDoc name = (String JavaDoc) names.nextElement();
126             if (name.startsWith("japex.")) {
127                 String JavaDoc xmlName = name.substring(name.indexOf('.') + 1);
128                 
129                 // Replace path.separator by a single space
130
if (name.equals(Constants.CLASS_PATH)) {
131                     buffer.append(Util.getSpaces(indent)
132                         + "<" + xmlName + ">"
133                         + _params.getProperty(name).replaceAll(
134                               System.getProperty("path.separator"), "\n")
135                         + "</" + xmlName + ">\n");
136                 }
137                 else {
138                     buffer.append(Util.getSpaces(indent)
139                         + "<" + xmlName + ">"
140                         + _params.getProperty(name)
141                         + "</" + xmlName + ">\n");
142                 }
143             }
144         }
145         
146         // Serialize driver-defined params (TODO sort)
147
names = _params.keys();
148         while (names.hasMoreElements()) {
149             String JavaDoc name = (String JavaDoc) names.nextElement();
150             if (!name.startsWith("japex.")) {
151                 buffer.append(Util.getSpaces(indent)
152                     + "<" + name + " xmlns=\"\">"
153                     + _params.getProperty(name)
154                     + "</" + name + ">\n");
155             }
156         }
157     }
158
159     /**
160      * Expand expression of the form $[paramname}
161      */

162     private String JavaDoc evaluate(String JavaDoc name, String JavaDoc value) {
163         StringTokenizer tokenizer =
164             new StringTokenizer(value, "${}", true);
165         
166         String JavaDoc t = null;
167         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
168         int state = OUT_EXPR;
169         
170         while (tokenizer.hasMoreTokens()) {
171             t = tokenizer.nextToken();
172             
173             if (t.length() == 1) {
174                 switch (t.charAt(0)) {
175                     case '$':
176                         switch (state) {
177                             case OUT_EXPR:
178                                 t = tokenizer.nextToken();
179                                 if (t.equals("{")) {
180                                     buffer.append(DELIMITER);
181                                     state = IN_EXPR;
182                                 }
183                                 else {
184                                     buffer.append("$" + t);
185                                 }
186                                 break;
187                             case IN_EXPR:
188                                 buffer.append('$');
189                                 break;
190                         }
191                         break;
192                     case '}':
193                         switch (state) {
194                             case OUT_EXPR:
195                                 buffer.append('}');
196                                 break;
197                             case IN_EXPR:
198                                 buffer.append(DELIMITER);
199                                 state = OUT_EXPR;
200                                 break;
201                         }
202                         break;
203                     default:
204                         buffer.append(t);
205                         break;
206                 }
207             }
208             else {
209                 buffer.append(t);
210             }
211         }
212
213         // Must be in OUT_EXPR at the end of parsing
214
if (state != OUT_EXPR) {
215             throw new RuntimeException JavaDoc("Error evaluating parameter '"
216                 + name + "' of value '" + value + "'");
217         }
218         
219         /*
220           * Second pass: split up buffer into literal and non-literal expressions.
221           */

222         tokenizer = new StringTokenizer(buffer.toString(), DELIMITER, true);
223         StringBuffer JavaDoc result = new StringBuffer JavaDoc();
224         
225         while (tokenizer.hasMoreTokens()) {
226             t = tokenizer.nextToken();
227             
228             if (t.equals(DELIMITER)) {
229                 String JavaDoc paramName = tokenizer.nextToken();
230                 String JavaDoc paramValue = _params.getProperty(paramName);
231                 if (paramValue != null) {
232                     result.append(paramValue);
233                 }
234                 else {
235                     // If not defined, check OS environment
236
paramValue = getEnvVariable(paramName);
237                     if (paramValue != null) {
238                         result.append(paramValue);
239                     }
240                     else {
241                         throw new RuntimeException JavaDoc("Undefined parameter '"
242                           + paramName + "'");
243                     }
244                 }
245                 
246                 tokenizer.nextToken(); // consume other delimiter
247
}
248             else {
249                 result.append(t);
250             }
251         }
252         
253         return result.toString();
254     }
255
256     // Use Ant class to get environment
257
private static Vector ENV = Execute.getProcEnvironment();
258     
259     private String JavaDoc getEnvVariable(String JavaDoc name) {
260         for (int i = 0; i < ENV.size(); i++) {
261             String JavaDoc def = (String JavaDoc) ENV.get(i);
262             int k = def.indexOf('=');
263             if (k > 0) {
264                 if (name.equals(def.substring(0, k))) {
265                     return def.substring(k + 1);
266                 }
267             }
268         }
269         return null;
270     }
271
272 }
273
Popular Tags