1 31 package org.objectweb.proactive.core.xml; 32 33 34 58 public class XMLPropertiesStore { 59 60 64 68 69 private String targetURI; 70 71 72 private org.w3c.dom.Document targetDocument; 73 74 75 private org.w3c.dom.Element rootElement; 76 77 78 79 83 89 public XMLPropertiesStore(String uri) throws java.io.IOException { 90 this.targetURI = uri; 91 this.targetDocument = parseFromURI(uri); 92 this.rootElement = targetDocument.getDocumentElement(); 93 } 94 95 96 97 98 102 108 public static org.w3c.dom.Document parseFromURI(String uri) throws java.io.IOException { 109 try { 110 javax.xml.parsers.DocumentBuilderFactory factory = javax.xml.parsers.DocumentBuilderFactory.newInstance(); 111 javax.xml.parsers.DocumentBuilder documentBuilder = factory.newDocumentBuilder(); 112 return documentBuilder.parse(uri); 113 } catch (org.xml.sax.SAXException e) { 114 throw new java.io.IOException (e.toString()); 115 } catch (javax.xml.parsers.ParserConfigurationException e) { 116 throw new java.io.IOException (e.toString()); 117 } 118 } 119 120 121 122 123 127 134 public String getValueAsString(String path) { 135 return getValueAsString(path, rootElement); 136 } 137 138 139 147 public int getValueAsInt(String path, int defaultValue) { 148 return getValueAsInt(path, rootElement, defaultValue); 149 } 150 151 152 159 public org.w3c.dom.Node getValueAsNode(String path) { 160 return getValueAsNode(path, rootElement); 161 } 162 163 164 171 public org.w3c.dom.Node [] getAllNodes(String path) { 172 return getAllNodes(path, rootElement); 173 } 174 175 176 184 public String getValueAsString(String path, org.w3c.dom.Node context) { 185 if (context.getOwnerDocument() != targetDocument) return null; 186 org.w3c.dom.Node node = findNodeFromXPath(path,context); 187 if (node == null) return null; 188 int type = node.getNodeType(); 189 if (type == org.w3c.dom.Node.ELEMENT_NODE) { 190 org.w3c.dom.Node child = node.getFirstChild(); 191 if (child == null) return null; 192 return child.getNodeValue(); 193 } else { 194 return node.getNodeValue(); 195 } 196 } 197 198 199 208 public int getValueAsInt(String path, org.w3c.dom.Node context, int defaultValue) { 209 String s = getValueAsString(path,context); 210 if (s == null) return defaultValue; 211 try { 212 return Integer.parseInt(s); 213 } catch (NumberFormatException e) { 214 return defaultValue; 215 } 216 } 217 218 219 227 public org.w3c.dom.Node getValueAsNode(String path, org.w3c.dom.Node context) { 228 if (context.getOwnerDocument() != targetDocument) return null; 229 return findNodeFromXPath(path, context); 230 } 231 232 233 241 public org.w3c.dom.Node [] getAllNodes(String path, org.w3c.dom.Node context) { 242 if (context.getOwnerDocument() != targetDocument) return null; 243 return findNodesFromXPath(path, context); 244 } 245 246 247 294 295 296 300 307 private org.w3c.dom.Node findNodeFromXPath(String path, org.w3c.dom.Node node) { 308 if (path == null || path.length() == 0 || path.equals(".")) return node; 310 311 int n = path.indexOf('@'); 313 String attributeName = null; 314 if (n > -1 && n < (path.length()-1)) { 315 attributeName = path.substring(n+1); 316 path = path.substring(0,n); 317 } 318 319 if (path.length() == 0) 321 return findNamedAttribute(attributeName,node); 322 323 java.util.StringTokenizer st = new java.util.StringTokenizer (path,"/"); 324 if (path.charAt(0) == '/') { 325 node = rootElement; 328 if (st.hasMoreTokens()) { 329 String t = st.nextToken(); 330 if (! t.equals(node.getNodeName())) return null; 331 } 332 } 333 334 while (st.hasMoreTokens() && node != null) { 336 String t = st.nextToken(); 337 if (t.equals(".")) break; 338 node = findNamedChild(t,node); 339 } 340 return findNamedAttribute(attributeName,node); 341 } 342 343 344 351 private org.w3c.dom.Node [] findNodesFromXPath(String path, org.w3c.dom.Node node) { 352 if (path == null || path.length() == 0 || path.equals(".")) return null; 354 355 int n = path.indexOf('@'); 357 if (n > -1 && n < (path.length()-1)) { 358 path = path.substring(0,n); 359 } 360 if (path.length() == 0) return null; 362 363 java.util.StringTokenizer st = new java.util.StringTokenizer (path,"/"); 364 if (path.charAt(0) == '/') { 365 node = rootElement; 368 if (st.hasMoreTokens()) { 369 String t = st.nextToken(); 370 if (! t.equals(node.getNodeName())) return null; 371 } 372 } 373 374 if (! st.hasMoreTokens()) return null; 376 while (node != null) { 377 String t = st.nextToken(); 378 if (t.equals(".")) return null; 379 if (st.hasMoreTokens()) { 380 node = findNamedChild(t,node); 381 } else { 382 return findNamedChilds(t, node); 383 } 384 } 385 return null; 386 } 387 388 389 397 private org.w3c.dom.Node findNamedAttribute(String attributeName, org.w3c.dom.Node node) { 398 if (attributeName == null || attributeName.length() == 0) return node; 399 org.w3c.dom.NamedNodeMap attributes = node.getAttributes(); 400 if (attributes == null) return null; 401 return attributes.getNamedItem(attributeName); 402 } 403 404 412 private org.w3c.dom.Node findNamedChild(String name, org.w3c.dom.Node node) { 413 org.w3c.dom.Node child = node.getFirstChild(); 414 while (child != null) { 415 if (name.equals(child.getNodeName())) { 416 return child; 417 } 418 child = child.getNextSibling(); 419 } 420 return child; 421 } 422 423 430 private org.w3c.dom.Node [] findNamedChilds(String name, org.w3c.dom.Node node) { 431 org.w3c.dom.Node child = node.getFirstChild(); 432 java.util.ArrayList result = new java.util.ArrayList (); 433 while (child != null) { 434 if (name.equals(child.getNodeName())) { 435 result.add(child); 436 } 437 child = child.getNextSibling(); 438 } 439 int n = result.size(); 440 if (n == 0) return null; 441 org.w3c.dom.Node [] resultArray = new org.w3c.dom.Node [n]; 442 return (org.w3c.dom.Node []) result.toArray(resultArray); 443 } 444 } | Popular Tags |