Author: gvanmatre
Date: Sun May 21 23:03:35 2006
New Revision: 408578

URL: http://svn.apache.org/viewvc?rev=408578&view=rev
Log:
Added simple namespace support to the clay parser.

Modified:
    
struts/shale/trunk/clay-plugin/src/java/org/apache/shale/clay/parser/Node.java
    
struts/shale/trunk/clay-plugin/src/test/org/apache/shale/clay/parser/ParserTestCase.java

Modified: 
struts/shale/trunk/clay-plugin/src/java/org/apache/shale/clay/parser/Node.java
URL: 
http://svn.apache.org/viewvc/struts/shale/trunk/clay-plugin/src/java/org/apache/shale/clay/parser/Node.java?rev=408578&r1=408577&r2=408578&view=diff
==============================================================================
--- 
struts/shale/trunk/clay-plugin/src/java/org/apache/shale/clay/parser/Node.java 
(original)
+++ 
struts/shale/trunk/clay-plugin/src/java/org/apache/shale/clay/parser/Node.java 
Sun May 21 23:03:35 2006
@@ -19,6 +19,7 @@
 package org.apache.shale.clay.parser;
 
 import java.util.ArrayList;
+import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
 import java.util.TreeMap;
@@ -260,5 +261,60 @@
         this.isComment = isComment;
     }
      
+ 
+    /**
+     * <p>Finds matching nodes by <code>name</code> searching thru all the 
children.</p>
+     */
+    public List getNodesByName(String name) {
+        List nodes = new ArrayList();
+        findNodesByName(this, name, nodes);
+        return nodes;
+    }
+    
+    /**
+     * <p>Recursively walks down the tree looking for nodes matching the 
<code>name</code>.</p>
+     */
+    private void findNodesByName(Node node, String name, List nodes) {
+        if (node.getName() != null && node.getName().equals(name))
+            nodes.add(node);
+        
+        Iterator ni = node.getChildren().iterator();
+        while (ni.hasNext()) {
+            Node child = (Node) ni.next();
+            if (child.getName() != null && child.getName().equals(name))
+                nodes.add(child);
+            if (!child.getChildren().isEmpty())
+                findNodesByName(child, name, nodes);
+        }
+        
+    }
+    
+    /**
+     * <p>Walks up the tree looking for a uri namespace matching the 
<code>prefix</code>.
+     * A <code>null</code> prefix will search for the default uri 
namespace.</p>
+     */
+    public String getNamespaceURI(String prefix) {
+        StringBuffer attributeName = new StringBuffer("xmlns");
+        if (prefix != null && prefix.length() > 0)
+            attributeName.append(":").append(prefix);
+        
+        String uri = null;
+        
+        if ((uri = (String) attributes.get(attributeName.toString())) != null) 
{
+            return uri;       
+        }
+        
+        Node parent = getParent();
+        while (parent != null) {
+            if ((uri = (String) 
parent.getAttributes().get(attributeName.toString())) != null) {
+                return uri;       
+            } else {
+                parent = parent.getParent();    
+            }
+        }
+        
+        
+        return null;
+    }
     
 }

Modified: 
struts/shale/trunk/clay-plugin/src/test/org/apache/shale/clay/parser/ParserTestCase.java
URL: 
http://svn.apache.org/viewvc/struts/shale/trunk/clay-plugin/src/test/org/apache/shale/clay/parser/ParserTestCase.java?rev=408578&r1=408577&r2=408578&view=diff
==============================================================================
--- 
struts/shale/trunk/clay-plugin/src/test/org/apache/shale/clay/parser/ParserTestCase.java
 (original)
+++ 
struts/shale/trunk/clay-plugin/src/test/org/apache/shale/clay/parser/ParserTestCase.java
 Sun May 21 23:03:35 2006
@@ -694,5 +694,63 @@
                 nodes.size() == 2);
 
     }
+    
+    
+    public void testNameSpace() {
+        Parser p = new Parser();
+        StringBuffer doc = new StringBuffer();
+        doc.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>")
+           .append("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 
Transitional//EN\" ") 
+           
.append("\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\";>")
+           .append("<html xmlns=\"http://www.w3.org/1999/xhtml\"; ")
+           .append("xmlns:clay=\"http://struts.apache.org/dtds/shale-clay\"; ")
+           .append("xml:lang=\"en\" ")  
+           .append("lang=\"en\">")
+           .append("<head>")
+           .append("<meta http-equiv=\"Content-Type\" content=\"text/html; 
charset=UTF-8\"/>")
+           .append("<title>Test</title>")
+           .append("</head>")
+           .append("<body>")
+           .append("<clay:component jsfid=\"outputText\" value=\"testing\"/>")
+           .append("<div 
xmlns:clay=\"http://apache.struts.org/dtds/shale-clay\";>")
+           .append("<clay:component jsfid=\"outputText\" value=\"testing\"/>") 
 
+           .append("</div>")
+           .append("</body>")
+           .append("</html>");
+        
+        
+        List roots = p.parse(doc);
+        assertEquals(3, roots.size());
+        Node root = (Node) roots.get(2);
+        assertNotNull(root);
+        
+        List nodes = root.getNodesByName("component");
+        assertEquals(2, nodes.size());
+        
+        Node clayComponent = (Node) nodes.get(0);
+        assertEquals("prefix", "clay", clayComponent.getQname());
+        
+        // clay namespace
+        String uri = clayComponent.getNamespaceURI(clayComponent.getQname());
+        assertEquals("uri", "http://struts.apache.org/dtds/shale-clay";, uri);
+        
+        // default namespace
+        uri = clayComponent.getNamespaceURI(null);
+        assertEquals("uri", "http://www.w3.org/1999/xhtml";, uri);
+
+        
+        clayComponent = (Node) nodes.get(1);
+        assertEquals("prefix", "clay", clayComponent.getQname());
+        
+        // clay namespace
+        uri = clayComponent.getNamespaceURI(clayComponent.getQname());
+        assertEquals("uri", "http://apache.struts.org/dtds/shale-clay";, uri);
+        
+        // default namespace
+        uri = clayComponent.getNamespaceURI(null);
+        assertEquals("uri", "http://www.w3.org/1999/xhtml";, uri);
+
+        
+    }
 
 }


Reply via email to