1 17 18 21 package org.quartz.utils; 22 23 import java.util.ArrayList ; 24 import java.util.Enumeration ; 25 import java.util.HashSet ; 26 import java.util.Properties ; 27 import java.util.StringTokenizer ; 28 29 36 public class PropertiesParser { 37 38 45 46 Properties props = null; 47 48 55 56 public PropertiesParser(Properties props) { 57 this.props = props; 58 } 59 60 67 68 public Properties getUnderlyingProperties() { 69 return props; 70 } 71 72 77 public String getStringProperty(String name) { 78 return getStringProperty(name, null); 79 } 80 81 86 public String getStringProperty(String name, String def) { 87 String val = props.getProperty(name, def); 88 if (val == null) { 89 return def; 90 } 91 92 val = val.trim(); 93 94 return (val.length() == 0) ? def : val; 95 } 96 97 public String [] getStringArrayProperty(String name) { 98 return getStringArrayProperty(name, null); 99 } 100 101 public String [] getStringArrayProperty(String name, String [] def) { 102 String vals = getStringProperty(name); 103 if (vals == null) { 104 return def; 105 } 106 107 StringTokenizer stok = new StringTokenizer (vals, ","); 108 ArrayList strs = new ArrayList (); 109 try { 110 while (stok.hasMoreTokens()) { 111 strs.add(stok.nextToken().trim()); 112 } 113 114 return (String [])strs.toArray(new String [strs.size()]); 115 } catch (Exception e) { 116 return def; 117 } 118 } 119 120 public boolean getBooleanProperty(String name) { 121 return getBooleanProperty(name, false); 122 } 123 124 public boolean getBooleanProperty(String name, boolean def) { 125 String val = getStringProperty(name); 126 127 return (val == null) ? def : Boolean.valueOf(val).booleanValue(); 128 } 129 130 public byte getByteProperty(String name) throws NumberFormatException { 131 String val = getStringProperty(name); 132 if (val == null) { 133 throw new NumberFormatException (" null string"); 134 } 135 136 try { 137 return Byte.parseByte(val); 138 } catch (NumberFormatException nfe) { 139 throw new NumberFormatException (" '" + val + "'"); 140 } 141 } 142 143 public byte getByteProperty(String name, byte def) 144 throws NumberFormatException { 145 String val = getStringProperty(name); 146 if (val == null) { 147 return def; 148 } 149 150 try { 151 return Byte.parseByte(val); 152 } catch (NumberFormatException nfe) { 153 throw new NumberFormatException (" '" + val + "'"); 154 } 155 } 156 157 public char getCharProperty(String name) { 158 return getCharProperty(name, '\0'); 159 } 160 161 public char getCharProperty(String name, char def) { 162 String param = getStringProperty(name); 163 return (param == null) ? def : param.charAt(0); 164 } 165 166 public double getDoubleProperty(String name) throws NumberFormatException { 167 String val = getStringProperty(name); 168 if (val == null) { 169 throw new NumberFormatException (" null string"); 170 } 171 172 try { 173 return Double.parseDouble(val); 174 } catch (NumberFormatException nfe) { 175 throw new NumberFormatException (" '" + val + "'"); 176 } 177 } 178 179 public double getDoubleProperty(String name, double def) 180 throws NumberFormatException { 181 String val = getStringProperty(name); 182 if (val == null) { 183 return def; 184 } 185 186 try { 187 return Double.parseDouble(val); 188 } catch (NumberFormatException nfe) { 189 throw new NumberFormatException (" '" + val + "'"); 190 } 191 } 192 193 public float getFloatProperty(String name) throws NumberFormatException { 194 String val = getStringProperty(name); 195 if (val == null) { 196 throw new NumberFormatException (" null string"); 197 } 198 199 try { 200 return Float.parseFloat(val); 201 } catch (NumberFormatException nfe) { 202 throw new NumberFormatException (" '" + val + "'"); 203 } 204 } 205 206 public float getFloatProperty(String name, float def) 207 throws NumberFormatException { 208 String val = getStringProperty(name); 209 if (val == null) { 210 return def; 211 } 212 213 try { 214 return Float.parseFloat(val); 215 } catch (NumberFormatException nfe) { 216 throw new NumberFormatException (" '" + val + "'"); 217 } 218 } 219 220 public int getIntProperty(String name) throws NumberFormatException { 221 String val = getStringProperty(name); 222 if (val == null) { 223 throw new NumberFormatException (" null string"); 224 } 225 226 try { 227 return Integer.parseInt(val); 228 } catch (NumberFormatException nfe) { 229 throw new NumberFormatException (" '" + val + "'"); 230 } 231 } 232 233 public int getIntProperty(String name, int def) 234 throws NumberFormatException { 235 String val = getStringProperty(name); 236 if (val == null) { 237 return def; 238 } 239 240 try { 241 return Integer.parseInt(val); 242 } catch (NumberFormatException nfe) { 243 throw new NumberFormatException (" '" + val + "'"); 244 } 245 } 246 247 public int[] getIntArrayProperty(String name) throws NumberFormatException { 248 return getIntArrayProperty(name, null); 249 } 250 251 public int[] getIntArrayProperty(String name, int[] def) 252 throws NumberFormatException { 253 String vals = getStringProperty(name); 254 if (vals == null) { 255 return def; 256 } 257 258 StringTokenizer stok = new StringTokenizer (vals, ","); 259 ArrayList ints = new ArrayList (); 260 try { 261 while (stok.hasMoreTokens()) { 262 try { 263 ints.add(new Integer (stok.nextToken().trim())); 264 } catch (NumberFormatException nfe) { 265 throw new NumberFormatException (" '" + vals + "'"); 266 } 267 } 268 269 int[] outInts = new int[ints.size()]; 270 for (int i = 0; i < ints.size(); i++) { 271 outInts[i] = ((Integer )ints.get(i)).intValue(); 272 } 273 return outInts; 274 } catch (Exception e) { 275 return def; 276 } 277 } 278 279 public long getLongProperty(String name) throws NumberFormatException { 280 String val = getStringProperty(name); 281 if (val == null) { 282 throw new NumberFormatException (" null string"); 283 } 284 285 try { 286 return Long.parseLong(val); 287 } catch (NumberFormatException nfe) { 288 throw new NumberFormatException (" '" + val + "'"); 289 } 290 } 291 292 public long getLongProperty(String name, long def) 293 throws NumberFormatException { 294 String val = getStringProperty(name); 295 if (val == null) { 296 return def; 297 } 298 299 try { 300 return Long.parseLong(val); 301 } catch (NumberFormatException nfe) { 302 throw new NumberFormatException (" '" + val + "'"); 303 } 304 } 305 306 public short getShortProperty(String name) throws NumberFormatException { 307 String val = getStringProperty(name); 308 if (val == null) { 309 throw new NumberFormatException (" null string"); 310 } 311 312 try { 313 return Short.parseShort(val); 314 } catch (NumberFormatException nfe) { 315 throw new NumberFormatException (" '" + val + "'"); 316 } 317 } 318 319 public short getShortProperty(String name, short def) 320 throws NumberFormatException { 321 String val = getStringProperty(name); 322 if (val == null) { 323 return def; 324 } 325 326 try { 327 return Short.parseShort(val); 328 } catch (NumberFormatException nfe) { 329 throw new NumberFormatException (" '" + val + "'"); 330 } 331 } 332 333 public String [] getPropertyGroups(String prefix) { 334 Enumeration keys = props.propertyNames(); 335 HashSet groups = new HashSet (10); 336 337 if (!prefix.endsWith(".")) { 338 prefix += "."; 339 } 340 341 while (keys.hasMoreElements()) { 342 String key = (String ) keys.nextElement(); 343 if (key.startsWith(prefix)) { 344 String groupName = key.substring(prefix.length(), key.indexOf( 345 '.', prefix.length())); 346 groups.add(groupName); 347 } 348 } 349 350 return (String []) groups.toArray(new String [groups.size()]); 351 } 352 353 public Properties getPropertyGroup(String prefix) { 354 return getPropertyGroup(prefix, false, null); 355 } 356 357 public Properties getPropertyGroup(String prefix, boolean stripPrefix) { 358 return getPropertyGroup(prefix, stripPrefix, null); 359 } 360 361 376 public Properties getPropertyGroup(String prefix, boolean stripPrefix, String [] excludedPrefixes) { 377 Enumeration keys = props.propertyNames(); 378 Properties group = new Properties (); 379 380 if (!prefix.endsWith(".")) { 381 prefix += "."; 382 } 383 384 while (keys.hasMoreElements()) { 385 String key = (String ) keys.nextElement(); 386 if (key.startsWith(prefix)) { 387 388 boolean exclude = false; 389 if (excludedPrefixes != null) { 390 for (int i = 0; (i < excludedPrefixes.length) && (exclude == false); i++) { 391 exclude = key.startsWith(excludedPrefixes[i]); 392 } 393 } 394 395 if (exclude == false) { 396 String value = getStringProperty(key, ""); 397 398 if (stripPrefix) { 399 group.put(key.substring(prefix.length()), value); 400 } else { 401 group.put(key, value); 402 } 403 } 404 } 405 } 406 407 return group; 408 } 409 } 410 | Popular Tags |