org/objectweb/proactive/core/xml/XMLPropertiesStore.java

00001 /* 
00002  * ################################################################
00003  * 
00004  * ProActive: The Java(TM) library for Parallel, Distributed, 
00005  *            Concurrent computing with Security and Mobility
00006  * 
00007  * Copyright (C) 1997-2007 INRIA/University of Nice-Sophia Antipolis
00008  * Contact: proactive@objectweb.org
00009  * 
00010  * This library is free software; you can redistribute it and/or
00011  * modify it under the terms of the GNU Lesser General Public
00012  * License as published by the Free Software Foundation; either
00013  * version 2.1 of the License, or any later version.
00014  *  
00015  * This library is distributed in the hope that it will be useful,
00016  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018  * Lesser General Public License for more details.
00019  * 
00020  * You should have received a copy of the GNU Lesser General Public
00021  * License along with this library; if not, write to the Free Software
00022  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
00023  * USA
00024  *  
00025  *  Initial developer(s):               The ProActive Team
00026  *                        http://www.inria.fr/oasis/ProActive/contacts.html
00027  *  Contributor(s): 
00028  * 
00029  * ################################################################
00030  */ 
00031 package org.objectweb.proactive.core.xml;
00032 
00033 
00058 public class XMLPropertiesStore {
00059     //
00060     //  ----- STATIC MEMBERS -----------------------------------------------------------------------------------
00061     //
00062     //
00063     //  ----- PRIVATE MEMBERS -----------------------------------------------------------------------------------
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     //  ----- CONSTRUCTORS -----------------------------------------------------------------------------------
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     //  ----- STATIC METHODS -----------------------------------------------------------------------------------
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     //  ----- PUBLIC METHODS -----------------------------------------------------------------------------------
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        // for testing purpose
00245        public static void main(String[] args) {
00246          String uri = null;
00247          if (args.length > 0) {
00248            uri = args[0];
00249          } else {
00250            uri = "file:///D:/cygwin/home/lmestre/ProActive/ProActiveDescriptor2.xml";
00251            System.out.println("uri="+uri);
00252          }
00253          try {
00254            XMLPropertiesStore p = new XMLPropertiesStore(uri);
00255            System.out.println("p="+p.toString());
00256            System.out.println("  ====  ");
00257          } catch (java.io.IOException e) {
00258            e.printStackTrace();
00259          }
00260        }
00261        // for testing purpose
00262        public String toString() {
00263          try {
00264            return serializeDocumentToString(targetDocument);
00265          } catch (java.io.IOException e) {
00266            e.printStackTrace();
00267            return null;
00268          }
00269        }
00270        public static String serializeDocumentToString(org.w3c.dom.Document doc) throws java.io.IOException {
00271          org.apache.xml.serialize.OutputFormat of = new org.apache.xml.serialize.OutputFormat(
00272              org.apache.xml.serialize.Method.XML,
00273              org.apache.xml.serialize.OutputFormat.Defaults.Encoding,true);
00274          of.setIndent(2);
00275          org.apache.xml.serialize.XMLSerializer ts = new org.apache.xml.serialize.XMLSerializer(of);
00276          java.io.ByteArrayOutputStream baos = new java.io.ByteArrayOutputStream();
00277          java.io.Writer writer = new java.io.PrintWriter(baos,false);
00278          ts.setOutputCharStream(writer);
00279          ts.serialize(doc);
00280          writer.flush();
00281          writer.close();
00282          return baos.toString();
00283        }
00284      */
00285 
00286     //
00287     //  ----- PRIVATE METHODS -----------------------------------------------------------------------------------
00288     //
00289 
00297     private org.w3c.dom.Node findNodeFromXPath(String path,
00298         org.w3c.dom.Node node) {
00299         // deals with special cases if path is empty or point to the current node
00300         if ((path == null) || (path.length() == 0) || path.equals(".")) {
00301             return node;
00302         }
00303 
00304         // find the possible attribute in the XPath expression
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         // deal with case where the XPath was just an attribute
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             // case of an absolute path (starting from the root node)
00320             // in this case we process the current node
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         // iterates through the /
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         // deals with special cases if path is empty or point to the current node
00351         if ((path == null) || (path.length() == 0) || path.equals(".")) {
00352             return null;
00353         }
00354 
00355         // remove possible attribute in the XPath expression
00356         int n = path.indexOf('@');
00357         if ((n > -1) && (n < (path.length() - 1))) {
00358             path = path.substring(0, n);
00359         }
00360 
00361         // deal with case where the XPath was just an attribute
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             // case of an absolute path (starting from the root node)
00369             // in this case we process the current node
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         // iterates through the /
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 }

Generated on Mon Jan 22 15:16:10 2007 for ProActive by  doxygen 1.5.1