Author: michiel
Date: 2010-02-01 13:51:14 +0100 (Mon, 01 Feb 2010)
New Revision: 40758

Modified:
   
mmbase/branches/MMBase-1_9/core/src/main/java/org/mmbase/util/xml/DocumentReader.java
Log:
made a bit more resistant against NPE, or at least clearer show what was Null

Modified: 
mmbase/branches/MMBase-1_9/core/src/main/java/org/mmbase/util/xml/DocumentReader.java
===================================================================
--- 
mmbase/branches/MMBase-1_9/core/src/main/java/org/mmbase/util/xml/DocumentReader.java
       2010-02-01 11:48:14 UTC (rev 40757)
+++ 
mmbase/branches/MMBase-1_9/core/src/main/java/org/mmbase/util/xml/DocumentReader.java
       2010-02-01 12:51:14 UTC (rev 40758)
@@ -502,20 +502,24 @@
      * @return Leaf element of the path
      */
     public static Element getElementByPath(Element e, String path) {
-        StringTokenizer st = new StringTokenizer(path,".");
+        StringTokenizer st = new StringTokenizer(path, ".");
         if (!st.hasMoreTokens()) {
             // faulty path
             log.error("No tokens in path");
             return null;
         } else {
+            if (e == null) {
+                throw new NullPointerException("Cannot follow path on element 
which is NULL");
+            }
             String root = st.nextToken();
-            if (e.getLocalName().equals("error")) {
-                // path should start with document root element
+            final String localName = e.getLocalName();
+
+            if ("error".equals(localName)) { // WTF?
                 log.error("Error occurred : (" + getElementValue(e) + ")");
                 return null;
-            } else if (!e.getLocalName().equals(root)) {
+            } else if (! root.equals(localName)) {
                 // path should start with document root element
-                log.error("path [" + path + "] with root (" + root + ") 
doesn't start with root element (" + e.getLocalName() + "): incorrect xml file" 
+
+                log.error("path [" + path + "] with root (" + root + ") 
doesn't start with root element (" + localName + "): incorrect xml file" +
                           "(" + e.getOwnerDocument().getDocumentURI() + ")");
                 return null;
             }
@@ -524,10 +528,14 @@
                 String tag = st.nextToken();
                 NodeList nl = e.getChildNodes();
                 for(int i = 0; i < nl.getLength(); i++) {
-                    if (! (nl.item(i) instanceof Element)) continue;
+                    if (! (nl.item(i) instanceof Element)) {
+                        continue;
+                    }
                     e = (Element) nl.item(i);
                     String tagName = e.getLocalName();
-                    if (tagName == null || tagName.equals(tag) || 
"*".equals(tag)) continue OUTER;
+                    if (tagName == null || tagName.equals(tag) || 
"*".equals(tag)) {
+                        continue OUTER;
+                    }
                 }
                 // Handle error!
                 return null;

_______________________________________________
Cvs mailing list
[email protected]
http://lists.mmbase.org/mailman/listinfo/cvs

Reply via email to