You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pivot.apache.org by gb...@apache.org on 2009/11/29 15:45:38 UTC

svn commit: r885235 - in /incubator/pivot/trunk/core: src/org/apache/pivot/xml/XMLSerializer.java test/org/apache/pivot/xml/test/XMLSerializerTest.java

Author: gbrown
Date: Sun Nov 29 14:45:38 2009
New Revision: 885235

URL: http://svn.apache.org/viewvc?rev=885235&view=rev
Log:
Add path index support to XMLSerializer.

Modified:
    incubator/pivot/trunk/core/src/org/apache/pivot/xml/XMLSerializer.java
    incubator/pivot/trunk/core/test/org/apache/pivot/xml/test/XMLSerializerTest.java

Modified: incubator/pivot/trunk/core/src/org/apache/pivot/xml/XMLSerializer.java
URL: http://svn.apache.org/viewvc/incubator/pivot/trunk/core/src/org/apache/pivot/xml/XMLSerializer.java?rev=885235&r1=885234&r2=885235&view=diff
==============================================================================
--- incubator/pivot/trunk/core/src/org/apache/pivot/xml/XMLSerializer.java (original)
+++ incubator/pivot/trunk/core/src/org/apache/pivot/xml/XMLSerializer.java Sun Nov 29 14:45:38 2009
@@ -318,20 +318,43 @@
             throw new IllegalArgumentException("path is empty.");
         }
 
-        // TODO Parse into list of name/index structs
-
-        ArrayList<String> tagNames = new ArrayList<String>(path.split("/"));
+        ArrayList<String> pathComponents = new ArrayList<String>(path.split("/"));
         Element current = root;
 
-        for (int i = 0, n = tagNames.getLength(); i < n; i++) {
-            String tagName = tagNames.get(i);
+        for (int i = 0, n = pathComponents.getLength(); i < n; i++) {
+            String pathComponent = pathComponents.get(i);
+
+            String tagName;
+            int index;
+            int leadingBracketIndex = pathComponent.indexOf('[');
+            if (leadingBracketIndex == -1) {
+                tagName = pathComponent;
+                index = 0;
+            } else {
+                tagName = pathComponent.substring(0, leadingBracketIndex);
+
+                int trailingBracketIndex = pathComponent.lastIndexOf(']');
+                if (trailingBracketIndex == -1) {
+                    throw new IllegalArgumentException("Unterminated index identifier.");
+                }
+
+                index = Integer.parseInt(pathComponent.substring(leadingBracketIndex + 1,
+                    trailingBracketIndex));
+            }
+
 
             int j = 0;
+            int k = 0;
             for (Node node : current) {
                 if (node instanceof Element) {
                     Element element = (Element)node;
+
                     if (element.getName().equals(tagName)) {
-                        break;
+                        if (k == index) {
+                            break;
+                        }
+
+                        k++;
                     }
                 }
 

Modified: incubator/pivot/trunk/core/test/org/apache/pivot/xml/test/XMLSerializerTest.java
URL: http://svn.apache.org/viewvc/incubator/pivot/trunk/core/test/org/apache/pivot/xml/test/XMLSerializerTest.java?rev=885235&r1=885234&r2=885235&view=diff
==============================================================================
--- incubator/pivot/trunk/core/test/org/apache/pivot/xml/test/XMLSerializerTest.java (original)
+++ incubator/pivot/trunk/core/test/org/apache/pivot/xml/test/XMLSerializerTest.java Sun Nov 29 14:45:38 2009
@@ -76,5 +76,8 @@
 
         List<Element> is = XMLSerializer.getElements(e, "is", "i");
         assertEquals(is.getLength(), 3);
+
+        assertEquals(XMLSerializer.getText(root, "d[0]/e[0]/f[2]"), "3");
+        assertEquals(XMLSerializer.getText(root, "d[0]/e[1]"), null);
     }
 }