1 5 6 11 12 package com.raptus.owxv3; 13 14 import java.util.*; 15 16 import org.jdom.Element; 17 21 public class XMLConfiguration extends Configuration 22 { 23 protected void log(String msg) 24 { 25 try 26 { 27 LoggingManager.log("++++++++++++++++++++++++++++++"+this.getClass().getName()+" "+msg); 28 throw new RuntimeException (); 29 } 30 catch(RuntimeException ex) 31 { 32 ex.printStackTrace(); 33 } 34 } 35 36 37 public XMLConfiguration(Hashtable hash) 38 { 39 super(new Hashtable()); 40 } 41 42 43 46 public Object clone() throws CloneNotSupportedException 47 { 48 log("clone"); 49 return null; 50 } 52 53 59 public Hashtable getConfigHash() 60 { 61 log("getConfigHash()"); 62 return null; 64 } 65 66 72 public boolean isEmptyKey(String key) 73 { 74 log("isEmptyKey("+key+")"); 75 String str = (String ) myConfigHash.get(key); 76 77 return (str == null || str.length() == 0); 78 } 79 80 90 public boolean deleteByKey(String key) 91 { 92 log("deleteByKey("+key+")"); 93 myConfigHash.put(key,""); 94 95 return true; 96 } 97 98 99 100 109 public void setStringByKey(String key, String value) throws NullPointerException 110 { 111 log("setStringByKey("+key+","+value+")"); 112 myConfigHash.put(key, value); 113 } 114 115 124 public void setStringArrayByKey(String key, String [] value) throws NullPointerException 125 { 126 log("setStringArray("+key+")"); 127 if(value == null || value.length == 0) 128 throw new NullPointerException ("Empty array"); 129 130 String tmp = ""; 131 for(int i=0; i < value.length; i++) 132 { 133 tmp += value[i]; 134 if(i != value.length-1) 135 tmp += ARRAY_SEPARATOR; 136 } 137 myConfigHash.put(key,tmp); 138 } 139 140 149 public void setBooleanByKey(String key, boolean value) throws NullPointerException 150 { 151 log("setBooleanByKey("+key+","+value+")"); 152 if(value) 153 myConfigHash.put(key, "true"); 154 else 155 myConfigHash.put(key, "false"); 156 } 157 158 167 public void setIntByKey(String key, int value) throws NullPointerException 168 { 169 log("setIntByKey("+key+","+value); 170 myConfigHash.put(key, Integer.toString(value)); 171 } 172 173 174 175 180 public String getStringByKey(String key) 181 { 182 log("getStringByKey("+key+")"); 183 return getStringValue(XMLConfigManager.root,key); 185 } 186 187 195 public String [] getStringArrayByKey(String key) 196 { 197 log("getStringArrayByKey("+key+")"); 198 String value = getStringByKey(key); 199 if(value == null) 200 return null; 201 202 StringTokenizer st = new StringTokenizer(value, ARRAY_SEPARATOR); 203 int count = st.countTokens(); 204 if(count == 0) 205 return null; 206 207 int i = 0; 208 String [] array = new String [count]; 209 while(st.hasMoreTokens()) 210 { 211 array[i++] = st.nextToken(); 212 } 213 return array; 214 } 215 216 224 public boolean getBooleanByKey(String key) { 226 log("getBooleanByKey("+key+")"); 227 if(!isEmptyKey(key)) 228 { 229 String str = (String ) myConfigHash.get(key); 230 231 if(str.compareToIgnoreCase("true") == 0 || 232 str.compareToIgnoreCase("on") == 0 || 233 str.compareToIgnoreCase("1") == 0) 234 return true; 235 else if(str.compareToIgnoreCase("false") == 0 || 236 str.compareToIgnoreCase("off") == 0 || 237 str.compareToIgnoreCase("0") == 0) 238 return false; 239 } 240 return false; 241 } 243 244 250 public int getIntByKey(String key) throws NumberFormatException 251 { 252 log("getIntByKey("+key+")"); 253 256 return new Integer (getStringByKey(key)).intValue(); 257 } 258 259 public String getStringValue(Element e, String key) 260 { 261 log("checking for key "+key); 262 StringTokenizer st = new StringTokenizer(key, "."); 263 String k = st.nextToken(); 264 String rest=""; 265 while(st.hasMoreTokens()) 266 { 267 rest=rest.length()>0?rest+"."+st.nextToken():st.nextToken(); 268 } 269 270 271 if(rest.length()==0) 272 { 273 if(e.getAttribute(k)!=null) 275 { 276 return e.getAttribute(k).getValue(); 278 } 279 280 List l = e.getChildren(k); 282 Iterator i = l.iterator(); 283 284 while (i.hasNext()) 285 { 286 Element ee = (Element) i.next(); 287 288 if(ee!=null) 289 { 290 return ee.getText(); 291 } 292 else 293 { 294 return null; 295 } 296 } 297 } 298 299 List l = e.getChildren(k); 301 Iterator i = l.iterator(); 302 303 while (i.hasNext()) 304 { 305 Element ee = (Element) i.next(); 306 307 if(ee!=null) 308 { 309 310 return getStringValue(ee, rest); 311 } 312 else 313 { 314 return null; 315 } 316 } 317 318 return null; 319 320 } 321 } 322 | Popular Tags |