1 23 24 package org.apache.slide.util.conf; 25 26 import java.util.Enumeration ; 27 28 37 public abstract class AbstractConfiguration implements Configuration { 38 42 protected String location=null; 43 44 47 protected AbstractConfiguration() { 48 this(null,-1); 49 } 50 51 54 protected AbstractConfiguration(String source, int line) { 55 super(); 56 this.location=""; 57 if (source!=null) this.location=source; 58 if ((line>=0)&&(this.location.length()>0)) this.location+=" "; 59 if (line>0) this.location+="line "+line; 60 if (this.location.length()>0) this.location="("+this.location+")"; 61 else this.location=null; 62 } 63 64 67 public int getValueAsInt() 68 throws ConfigurationException { 69 String value=this.getValue(); 70 try { 71 if (value.startsWith("0x")) 72 return(Integer.parseInt(value.substring(2),16)); 73 else if (value.startsWith("0o")) 74 return(Integer.parseInt(value.substring(2),8)); 75 else if (value.startsWith("0b")) 76 return(Integer.parseInt(value.substring(2),2)); 77 else return(Integer.parseInt(value)); 78 } catch (NumberFormatException e) { 79 throw new ConfigurationException("Cannot parse the value of the "+ 80 "configuration element \""+this.getName()+"\" as an integer", 81 this); 82 } 83 } 84 85 88 public long getValueAsLong() 89 throws ConfigurationException { 90 String value=this.getValue(); 91 try { 92 if (value.startsWith("0x")) 93 return(Long.parseLong(value.substring(2),16)); 94 else if (value.startsWith("0o")) 95 return(Long.parseLong(value.substring(2),8)); 96 else if (value.startsWith("0b")) 97 return(Long.parseLong(value.substring(2),2)); 98 else return(Integer.parseInt(value)); 99 } catch (NumberFormatException e) { 100 throw new ConfigurationException("Cannot parse the value of the "+ 101 "configuration element \""+this.getName()+"\" as a long", this); 102 } 103 } 104 105 108 public float getValueAsFloat() 109 throws ConfigurationException { 110 String value=this.getValue(); 111 try { 112 return(Float.valueOf(value).floatValue()); 113 } catch (NumberFormatException e) { 114 throw new ConfigurationException("Cannot parse the value of the "+ 115 "configuration element \""+this.getName()+"\" as a float", 116 this); 117 } 118 } 119 120 123 public boolean getValueAsBoolean() 124 throws ConfigurationException { 125 String value=this.getValue(); 126 if (value.equals("true")) return(true); 127 if (value.equals("false")) return(false); 128 throw new ConfigurationException("Cannot parse the value of the "+ 129 "configuration element \""+this.getName()+"\" as a boolean", 130 this); 131 } 132 133 136 public String getValue(String defaultValue) { 137 try { 138 return(this.getValue()); 139 } catch (ConfigurationException e) { 140 return(defaultValue); 141 } 142 } 143 144 147 public int getValueAsInt(int defaultValue) { 148 try { 149 return(this.getValueAsInt()); 150 } catch (ConfigurationException e) { 151 return(defaultValue); 152 } 153 } 154 155 158 public long getValueAsLong(long defaultValue) { 159 try { 160 return(this.getValueAsLong()); 161 } catch (ConfigurationException e) { 162 return(defaultValue); 163 } 164 } 165 166 169 public float getValueAsFloat(float defaultValue) { 170 try { 171 return(this.getValueAsFloat()); 172 } catch (ConfigurationException e) { 173 return(defaultValue); 174 } 175 } 176 177 180 public boolean getValueAsBoolean(boolean defaultValue) { 181 try { 182 return(this.getValueAsBoolean()); 183 } catch (ConfigurationException e) { 184 return(defaultValue); 185 } 186 } 187 188 192 public int getAttributeAsInt(String name) 193 throws ConfigurationException { 194 String value=this.getAttribute(name); 195 try { 196 if (value.startsWith("0x")) 197 return(Integer.parseInt(value.substring(2),16)); 198 else if (value.startsWith("0o")) 199 return(Integer.parseInt(value.substring(2),8)); 200 else if (value.startsWith("0b")) 201 return(Integer.parseInt(value.substring(2),2)); 202 else return(Integer.parseInt(value)); 203 } catch (NumberFormatException e) { 204 throw new ConfigurationException("Cannot parse the value of the "+ 205 "attribute \""+name+"\" of the configuration element \""+ 206 this.getName()+"\" as an integer",this); 207 } 208 } 209 210 214 public long getAttributeAsLong(String name) 215 throws ConfigurationException { 216 String value=this.getAttribute(name); 217 try { 218 if (value.startsWith("0x")) 219 return(Long.parseLong(value.substring(2),16)); 220 else if (value.startsWith("0o")) 221 return(Long.parseLong(value.substring(2),8)); 222 else if (value.startsWith("0b")) 223 return(Long.parseLong(value.substring(2),2)); 224 else return(Integer.parseInt(value)); 225 } catch (NumberFormatException e) { 226 throw new ConfigurationException("Cannot parse the value of the "+ 227 "attribute \""+name+"\" of the configuration element \""+ 228 this.getName()+"\" as a long", this); 229 } 230 } 231 232 236 public float getAttributeAsFloat(String name) 237 throws ConfigurationException { 238 String value=this.getAttribute(name); 239 try { 240 return(Float.valueOf(value).floatValue()); 241 } catch (NumberFormatException e) { 242 throw new ConfigurationException("Cannot parse the value of the "+ 243 "attribute \""+name+"\" of the configuration element \""+ 244 this.getName()+"\" as a float", this); 245 } 246 } 247 248 252 public boolean getAttributeAsBoolean(String name) 253 throws ConfigurationException { 254 String value=this.getAttribute(name); 255 if (value.equals("true")) return(true); 256 if (value.equals("false")) return(false); 257 throw new ConfigurationException("Cannot parse the value of the "+ 258 "attribute \""+name+"\" of the configuration element \""+ 259 this.getName()+"\" as a boolean", this); 260 } 261 262 266 public String getAttribute(String name, String defaultValue) { 267 try { 268 return(this.getAttribute(name)); 269 } catch (ConfigurationException e) { 270 return(defaultValue); 271 } 272 } 273 274 278 public int getAttributeAsInt(String name, int defaultValue) { 279 try { 280 return(this.getAttributeAsInt(name)); 281 } catch (ConfigurationException e) { 282 return(defaultValue); 283 } 284 } 285 286 290 public long getAttributeAsLong(String name, long defaultValue) { 291 try { 292 return(this.getAttributeAsLong(name)); 293 } catch (ConfigurationException e) { 294 return(defaultValue); 295 } 296 } 297 298 302 public float getAttributeAsFloat(String name, float defaultValue) { 303 try { 304 return(this.getAttributeAsFloat(name)); 305 } catch (ConfigurationException e) { 306 return(defaultValue); 307 } 308 } 309 310 314 public boolean getAttributeAsBoolean(String name, boolean defaultValue) { 315 try { 316 return(this.getAttributeAsBoolean(name)); 317 } catch (ConfigurationException e) { 318 return(defaultValue); 319 } 320 } 321 322 326 public Configuration getConfiguration(String name) { 327 Enumeration e=this.getConfigurations(name); 328 if (e.hasMoreElements()) return((Configuration)e.nextElement()); 329 return(null); 330 } 331 332 336 public String getLocation() { 337 return(this.location); 338 } 339 } 340 | Popular Tags |