1 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 DELIMITER = "\uFFFE"; 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 name) { 67 return _params.getProperty(name) != null; 68 } 69 70 public synchronized String getParam(String name) { 71 return _params.getProperty(name); 72 } 73 74 public synchronized void setParam(String name, String value) { 75 _params.setProperty(name, evaluate(name, value)); 76 } 77 78 public synchronized int getIntParam(String name) { 79 String s = _params.getProperty(name); 80 try { 81 return Integer.parseInt(s); 82 } 83 catch (NumberFormatException e) { 84 throw new RuntimeException (e); 85 } 86 } 87 88 public synchronized void setIntParam(String name, int value) { 89 _params.setProperty(name, Integer.toString(value)); 90 } 91 92 public synchronized void setLongParam(String name, long value) { 93 _params.setProperty(name, Long.toString(value)); 94 } 95 96 public synchronized long getLongParam(String name) { 97 String s = _params.getProperty(name); 98 try { 99 return Long.parseLong(s); 100 } 101 catch (NumberFormatException e) { 102 throw new RuntimeException (e); 103 } 104 } 105 106 public synchronized double getDoubleParam(String name) { 107 String s = _params.getProperty(name); 108 try { 109 return Double.parseDouble(s); 110 } 111 catch (NumberFormatException e) { 112 throw new RuntimeException (e); 113 } 114 } 115 116 public synchronized void setDoubleParam(String name, double value) { 117 _params.setProperty(name, Util.formatDouble(value)); 119 } 120 121 public void serialize(StringBuffer buffer, int indent) { 122 Enumeration names = _params.keys(); 124 while (names.hasMoreElements()) { 125 String name = (String ) names.nextElement(); 126 if (name.startsWith("japex.")) { 127 String xmlName = name.substring(name.indexOf('.') + 1); 128 129 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 names = _params.keys(); 148 while (names.hasMoreElements()) { 149 String name = (String ) 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 162 private String evaluate(String name, String value) { 163 StringTokenizer tokenizer = 164 new StringTokenizer(value, "${}", true); 165 166 String t = null; 167 StringBuffer buffer = new StringBuffer (); 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 if (state != OUT_EXPR) { 215 throw new RuntimeException ("Error evaluating parameter '" 216 + name + "' of value '" + value + "'"); 217 } 218 219 222 tokenizer = new StringTokenizer(buffer.toString(), DELIMITER, true); 223 StringBuffer result = new StringBuffer (); 224 225 while (tokenizer.hasMoreTokens()) { 226 t = tokenizer.nextToken(); 227 228 if (t.equals(DELIMITER)) { 229 String paramName = tokenizer.nextToken(); 230 String paramValue = _params.getProperty(paramName); 231 if (paramValue != null) { 232 result.append(paramValue); 233 } 234 else { 235 paramValue = getEnvVariable(paramName); 237 if (paramValue != null) { 238 result.append(paramValue); 239 } 240 else { 241 throw new RuntimeException ("Undefined parameter '" 242 + paramName + "'"); 243 } 244 } 245 246 tokenizer.nextToken(); } 248 else { 249 result.append(t); 250 } 251 } 252 253 return result.toString(); 254 } 255 256 private static Vector ENV = Execute.getProcEnvironment(); 258 259 private String getEnvVariable(String name) { 260 for (int i = 0; i < ENV.size(); i++) { 261 String def = (String ) 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 |