1 5 6 11 12 package com.raptus.owxv3; 13 14 15 import java.io.File ; 16 import java.util.*; 17 18 import org.jdom.*; 19 import org.jdom.input.SAXBuilder; 20 21 22 26 public class XMLConfigManager extends Object implements ConfigManagerIFace 27 { 28 29 30 36 public static final String PARAM_PREFIX = "XMLConfigManager."; 37 38 43 protected static final String FILENAME_KEY = "FileName"; 44 45 46 47 50 protected String myFileName; 51 52 56 protected XMLConfiguration myConfiguration; 57 58 protected static XMLConfigManager _instance = null; 59 60 public static XMLConfigManager getInstance() 61 { 62 if(_instance == null) 63 { 64 return null; 65 } 66 67 return _instance; 68 } 69 70 71 public XMLConfigManager() 72 { 73 log("constructor()."); 74 } 75 76 protected void log(String msg) 77 { 78 } 80 81 87 public Configuration getConfiguration() 88 { 89 log("getConfiguration()"); 90 91 return (Configuration)myConfiguration; 92 } 93 94 100 public boolean saveConfiguration(Configuration cfg) 101 { 102 log("saveConfiguration()"); 103 return true; 104 } 105 106 110 public void refresh() 111 { 112 log("refresh"); 113 } 114 115 121 public boolean initialize(Hashtable hash) 122 { 123 _instance = this; 124 log("initialise()"); 125 myFileName = (String ) hash.get(ConfigManager.PARAM_PREFIX + PARAM_PREFIX + FILENAME_KEY); 126 log("Configuration file is "+myFileName); 127 myConfiguration = new XMLConfiguration(hash); 128 return load(); 129 } 130 131 132 public static Element root = null; 133 134 public boolean load() 135 { 136 log("load()"); 137 138 try 139 { 140 SAXBuilder builder = new SAXBuilder(false); 142 Document doc = builder.build(new File (myFileName)); 143 root = doc.getRootElement(); 144 log("XML Config file loaded!"); 145 146 } 163 catch(org.jdom.JDOMException ex) 164 { 165 ex.printStackTrace(); 166 } 167 return true; 169 } 170 171 public Element getElement(Element e, String child, String attrib, String value) 174 { 175 if(e == null) 176 { 177 return null; 178 } 179 180 List childs = e.getChildren(child); 181 Iterator i = childs.iterator(); 182 183 while (i.hasNext()) 184 { 185 Element ch = (Element) i.next(); 187 188 if(attrib == null && ch != null) 190 { 191 return ch; 192 } 193 194 if(attrib != null && ch!=null) 196 { 197 if(ch.getAttribute(attrib)!=null) 199 { 200 if(value == null) 203 { 204 return ch; 205 } 206 else 207 { 208 if(ch.getAttribute(attrib).getValue().equals(value)) 211 { 212 return ch; 213 } 214 } 215 } 216 } 217 } 218 219 return null; 220 } 221 222 public Element getElement(Element e, String child ) 223 { 224 return getElement(e, child, null, null); 225 } 226 227 public Element getElement(Element e, String child, String attribute ) 228 { 229 return getElement(e, child, attribute, null); 230 } 231 232 236 public Element getElementByTree(Element start, String tree) 237 { 238 if(start==null) 239 { 240 start=root; 241 } 242 243 if(tree==null) 244 { 245 return null; 246 } 247 248 if(tree.length()<=0) 249 { 250 return null; 251 } 252 253 StringTokenizer st = new StringTokenizer(tree, "/"); 254 255 if(!st.hasMoreTokens()) 257 { 258 return getElement(start, tree); 259 } 260 261 String current=st.nextToken(); 263 String rest=""; 264 265 while(st.hasMoreTokens()) 267 { 268 rest = rest.length()>0?rest+"/"+st.nextToken():st.nextToken(); 269 } 270 271 273 if(current.indexOf("?")==-1) 274 { 275 Element e = getElement(start, current); 276 if(e != null) 277 { 278 if(rest.length()>0) 279 { 280 return getElementByTree(e, rest); 281 } 282 else 283 { 284 return e; 285 } 286 } 287 else 288 { 289 log("current="+current+" was not found"); 291 return null; 292 } 293 } 294 else 295 { 296 String child=current.substring(0, current.indexOf("?")); 298 String attribute=current.substring(current.indexOf("?")+1, current.indexOf("=")); 300 302 String val=current.substring(current.indexOf("=")+1); 303 305 Element e = getElement(start, child, attribute, val); 306 if(e != null) 307 { 308 if(rest.length()>0) 309 { 310 return getElementByTree(e, rest); 311 } 312 else 313 { 314 return e; 315 } 316 } 317 else 318 { 319 log("current="+current+" was not found!"); 321 return null; 322 } 323 } 324 } 325 326 public String getPropertyByTree(String path, String attribute) 327 { 328 Element e = getElementByTree(null, path); 329 if(e == null) 330 { 331 return null; 332 } 333 else 334 { 335 if(e.getAttribute(attribute)!=null) 336 { 337 return e.getAttribute(attribute).getValue(); 338 } 339 else 340 { 341 if(e.getText().trim().length()<1) 342 { 343 return null; 344 } 345 else 346 return e.getText(); 347 } 348 } 349 } 350 351 public boolean existProperty(String path, String attribute) 352 { 353 String s = getPropertyByTree(path, attribute); 354 if(s == null) 355 { 356 return false; 357 } 358 else 359 { 360 return true; 361 } 362 } 363 public boolean getBooleanByTree(String path, String attribute) 364 { 365 String s = getPropertyByTree(path, attribute); 366 if(s == null) 367 { 368 return false; 369 } 370 if(s.equals("true")||s.equals("1")) 371 { 372 return true; 373 } 374 return false; 375 } 376 377 public int getIntegerByTree(String path, String attribute) 378 { 379 String s = getPropertyByTree(path, attribute); 380 if(s == null) 381 { 382 return 0; 383 } 384 return Integer.parseInt(s); 385 } 386 387 public String [] getStringArrayByTree(String path, String attribute) 388 { 389 Element e = getElementByTree(null, path); 390 String res; 391 if(e == null) 392 { 393 return null; 394 } 395 else 396 { 397 if(e.getAttribute(attribute)!=null) 398 { 399 res = e.getAttribute(attribute).getValue(); 400 } 401 else 402 { 403 res = e.getText(); 404 } 405 } 406 407 if(res.indexOf(",") == -1) 408 { 409 res = res+","; 410 } 411 StringTokenizer st = new StringTokenizer(res,","); 412 Vector v = new Vector(); 413 while(st.hasMoreTokens()) 414 { 415 String s = st.nextToken(); 416 log(s); 417 v.add(s.trim()); 418 } 419 log("Returning..."); 420 String vs[] = new String [v.size()]; 421 v.toArray(vs); 422 return vs; 423 } 424 425 } 426 | Popular Tags |