00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031 package org.objectweb.proactive.core.xml;
00032
00033
00058 public class XMLPropertiesStore {
00059
00060
00061
00062
00063
00064
00065
00067 private String targetURI;
00068
00070 private org.w3c.dom.Document targetDocument;
00071
00073 private org.w3c.dom.Element rootElement;
00074
00075
00076
00077
00078
00085 public XMLPropertiesStore(String uri) throws java.io.IOException {
00086 this.targetURI = uri;
00087 this.targetDocument = parseFromURI(uri);
00088 this.rootElement = targetDocument.getDocumentElement();
00089 }
00090
00091
00092
00093
00094
00101 public static org.w3c.dom.Document parseFromURI(String uri)
00102 throws java.io.IOException {
00103 try {
00104 javax.xml.parsers.DocumentBuilderFactory factory = javax.xml.parsers.DocumentBuilderFactory.newInstance();
00105 javax.xml.parsers.DocumentBuilder documentBuilder = factory.newDocumentBuilder();
00106 return documentBuilder.parse(uri);
00107 } catch (org.xml.sax.SAXException e) {
00108 throw new java.io.IOException(e.toString());
00109 } catch (javax.xml.parsers.ParserConfigurationException e) {
00110 throw new java.io.IOException(e.toString());
00111 }
00112 }
00113
00114
00115
00116
00117
00125 public String getValueAsString(String path) {
00126 return getValueAsString(path, rootElement);
00127 }
00128
00137 public int getValueAsInt(String path, int defaultValue) {
00138 return getValueAsInt(path, rootElement, defaultValue);
00139 }
00140
00148 public org.w3c.dom.Node getValueAsNode(String path) {
00149 return getValueAsNode(path, rootElement);
00150 }
00151
00159 public org.w3c.dom.Node[] getAllNodes(String path) {
00160 return getAllNodes(path, rootElement);
00161 }
00162
00171 public String getValueAsString(String path, org.w3c.dom.Node context) {
00172 if (context.getOwnerDocument() != targetDocument) {
00173 return null;
00174 }
00175 org.w3c.dom.Node node = findNodeFromXPath(path, context);
00176 if (node == null) {
00177 return null;
00178 }
00179 int type = node.getNodeType();
00180 if (type == org.w3c.dom.Node.ELEMENT_NODE) {
00181 org.w3c.dom.Node child = node.getFirstChild();
00182 if (child == null) {
00183 return null;
00184 }
00185 return child.getNodeValue();
00186 } else {
00187 return node.getNodeValue();
00188 }
00189 }
00190
00200 public int getValueAsInt(String path, org.w3c.dom.Node context,
00201 int defaultValue) {
00202 String s = getValueAsString(path, context);
00203 if (s == null) {
00204 return defaultValue;
00205 }
00206 try {
00207 return Integer.parseInt(s);
00208 } catch (NumberFormatException e) {
00209 return defaultValue;
00210 }
00211 }
00212
00221 public org.w3c.dom.Node getValueAsNode(String path, org.w3c.dom.Node context) {
00222 if (context.getOwnerDocument() != targetDocument) {
00223 return null;
00224 }
00225 return findNodeFromXPath(path, context);
00226 }
00227
00236 public org.w3c.dom.Node[] getAllNodes(String path, org.w3c.dom.Node context) {
00237 if (context.getOwnerDocument() != targetDocument) {
00238 return null;
00239 }
00240 return findNodesFromXPath(path, context);
00241 }
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289
00297 private org.w3c.dom.Node findNodeFromXPath(String path,
00298 org.w3c.dom.Node node) {
00299
00300 if ((path == null) || (path.length() == 0) || path.equals(".")) {
00301 return node;
00302 }
00303
00304
00305 int n = path.indexOf('@');
00306 String attributeName = null;
00307 if ((n > -1) && (n < (path.length() - 1))) {
00308 attributeName = path.substring(n + 1);
00309 path = path.substring(0, n);
00310 }
00311
00312
00313 if (path.length() == 0) {
00314 return findNamedAttribute(attributeName, node);
00315 }
00316
00317 java.util.StringTokenizer st = new java.util.StringTokenizer(path, "/");
00318 if (path.charAt(0) == '/') {
00319
00320
00321 node = rootElement;
00322 if (st.hasMoreTokens()) {
00323 String t = st.nextToken();
00324 if (!t.equals(node.getNodeName())) {
00325 return null;
00326 }
00327 }
00328 }
00329
00330
00331 while (st.hasMoreTokens() && (node != null)) {
00332 String t = st.nextToken();
00333 if (t.equals(".")) {
00334 break;
00335 }
00336 node = findNamedChild(t, node);
00337 }
00338 return findNamedAttribute(attributeName, node);
00339 }
00340
00348 private org.w3c.dom.Node[] findNodesFromXPath(String path,
00349 org.w3c.dom.Node node) {
00350
00351 if ((path == null) || (path.length() == 0) || path.equals(".")) {
00352 return null;
00353 }
00354
00355
00356 int n = path.indexOf('@');
00357 if ((n > -1) && (n < (path.length() - 1))) {
00358 path = path.substring(0, n);
00359 }
00360
00361
00362 if (path.length() == 0) {
00363 return null;
00364 }
00365
00366 java.util.StringTokenizer st = new java.util.StringTokenizer(path, "/");
00367 if (path.charAt(0) == '/') {
00368
00369
00370 node = rootElement;
00371 if (st.hasMoreTokens()) {
00372 String t = st.nextToken();
00373 if (!t.equals(node.getNodeName())) {
00374 return null;
00375 }
00376 }
00377 }
00378
00379
00380 if (!st.hasMoreTokens()) {
00381 return null;
00382 }
00383 while (node != null) {
00384 String t = st.nextToken();
00385 if (t.equals(".")) {
00386 return null;
00387 }
00388 if (st.hasMoreTokens()) {
00389 node = findNamedChild(t, node);
00390 } else {
00391 return findNamedChilds(t, node);
00392 }
00393 }
00394 return null;
00395 }
00396
00405 private org.w3c.dom.Node findNamedAttribute(String attributeName,
00406 org.w3c.dom.Node node) {
00407 if ((attributeName == null) || (attributeName.length() == 0)) {
00408 return node;
00409 }
00410 org.w3c.dom.NamedNodeMap attributes = node.getAttributes();
00411 if (attributes == null) {
00412 return null;
00413 }
00414 return attributes.getNamedItem(attributeName);
00415 }
00416
00425 private org.w3c.dom.Node findNamedChild(String name, org.w3c.dom.Node node) {
00426 org.w3c.dom.Node child = node.getFirstChild();
00427 while (child != null) {
00428 if (name.equals(child.getNodeName())) {
00429 return child;
00430 }
00431 child = child.getNextSibling();
00432 }
00433 return child;
00434 }
00435
00443 private org.w3c.dom.Node[] findNamedChilds(String name,
00444 org.w3c.dom.Node node) {
00445 org.w3c.dom.Node child = node.getFirstChild();
00446 java.util.ArrayList result = new java.util.ArrayList();
00447 while (child != null) {
00448 if (name.equals(child.getNodeName())) {
00449 result.add(child);
00450 }
00451 child = child.getNextSibling();
00452 }
00453 int n = result.size();
00454 if (n == 0) {
00455 return null;
00456 }
00457 org.w3c.dom.Node[] resultArray = new org.w3c.dom.Node[n];
00458 return (org.w3c.dom.Node[]) result.toArray(resultArray);
00459 }
00460 }