1 5 6 7 package com.raptus.owxv3; 8 9 10 11 import java.util.*; 12 import java.text.ParseException ; 13 14 15 54 public class Configuration extends Object implements Cloneable 55 { 56 57 58 63 protected static final String ARRAY_SEPARATOR = ","; 64 65 66 67 70 protected Hashtable myConfigHash; 71 72 73 74 80 public Configuration(Hashtable hash) 81 { 82 this.myConfigHash = hash; 83 } 84 85 86 89 public Object clone() throws CloneNotSupportedException 90 { 91 return super.clone(); 92 } 93 94 100 public Hashtable getConfigHash() 101 { 102 return this.myConfigHash; 103 } 104 105 111 public boolean isEmptyKey(String key) 112 { 113 String str = (String ) myConfigHash.get(key); 114 115 return (str == null || str.length() == 0); 116 } 117 118 128 public boolean deleteByKey(String key) 129 { 130 myConfigHash.put(key,""); 131 132 return true; 133 } 134 135 136 137 146 public void setStringByKey(String key, String value) throws NullPointerException 147 { 148 myConfigHash.put(key, value); 149 } 150 151 160 public void setStringArrayByKey(String key, String [] value) throws NullPointerException 161 { 162 if(value == null || value.length == 0) 163 throw new NullPointerException ("Empty array"); 164 165 String tmp = ""; 166 for(int i=0; i < value.length; i++) 167 { 168 tmp += value[i]; 169 if(i != value.length-1) 170 tmp += ARRAY_SEPARATOR; 171 } 172 myConfigHash.put(key,tmp); 173 } 174 175 184 public void setBooleanByKey(String key, boolean value) throws NullPointerException 185 { 186 if(value) 187 myConfigHash.put(key, "true"); 188 else 189 myConfigHash.put(key, "false"); 190 } 191 192 201 public void setIntByKey(String key, int value) throws NullPointerException 202 { 203 myConfigHash.put(key, Integer.toString(value)); 204 } 205 206 207 208 213 public String getStringByKey(String key) 214 { 215 return (String ) myConfigHash.get(key); 216 } 217 218 226 public String [] getStringArrayByKey(String key) 227 { 228 String value = getStringByKey(key); 229 if(value == null) 230 return null; 231 232 StringTokenizer st = new StringTokenizer(value, ARRAY_SEPARATOR); 233 int count = st.countTokens(); 234 if(count == 0) 235 return null; 236 237 int i = 0; 238 String [] array = new String [count]; 239 while(st.hasMoreTokens()) 240 { 241 array[i++] = st.nextToken(); 242 } 243 return array; 244 } 245 246 254 public boolean getBooleanByKey(String key) throws ParseException 255 { 256 if(!isEmptyKey(key)) 257 { 258 String str = (String ) myConfigHash.get(key); 259 260 if(str.compareToIgnoreCase("true") == 0 || 261 str.compareToIgnoreCase("on") == 0 || 262 str.compareToIgnoreCase("1") == 0) 263 return true; 264 else if(str.compareToIgnoreCase("false") == 0 || 265 str.compareToIgnoreCase("off") == 0 || 266 str.compareToIgnoreCase("0") == 0) 267 return false; 268 } 269 throw new ParseException ("Key has an unknown value",0); 270 } 271 272 278 public int getIntByKey(String key) throws ParseException , NumberFormatException 279 { 280 if(isEmptyKey(key)) 281 throw new ParseException ("Key is empty",0); 282 283 return new Integer (getStringByKey(key)).intValue(); 284 } 285 } 286 | Popular Tags |