Return-Path: Delivered-To: apmail-struts-dev-archive@www.apache.org Received: (qmail 59046 invoked from network); 22 May 2006 06:04:00 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (209.237.227.199) by minotaur.apache.org with SMTP; 22 May 2006 06:04:00 -0000 Received: (qmail 58715 invoked by uid 500); 22 May 2006 06:03:57 -0000 Delivered-To: apmail-struts-dev-archive@struts.apache.org Received: (qmail 58678 invoked by uid 500); 22 May 2006 06:03:57 -0000 Mailing-List: contact commits-help@struts.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@struts.apache.org Delivered-To: mailing list commits@struts.apache.org Received: (qmail 58669 invoked by uid 99); 22 May 2006 06:03:57 -0000 Received: from asf.osuosl.org (HELO asf.osuosl.org) (140.211.166.49) by apache.org (qpsmtpd/0.29) with ESMTP; Sun, 21 May 2006 23:03:57 -0700 X-ASF-Spam-Status: No, hits=0.6 required=10.0 tests=NO_REAL_NAME X-Spam-Check-By: apache.org Received-SPF: pass (asf.osuosl.org: local policy) Received: from [140.211.166.113] (HELO eris.apache.org) (140.211.166.113) by apache.org (qpsmtpd/0.29) with ESMTP; Sun, 21 May 2006 23:03:56 -0700 Received: by eris.apache.org (Postfix, from userid 65534) id 437B31A983A; Sun, 21 May 2006 23:03:36 -0700 (PDT) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r408578 - in /struts/shale/trunk/clay-plugin/src: java/org/apache/shale/clay/parser/Node.java test/org/apache/shale/clay/parser/ParserTestCase.java Date: Mon, 22 May 2006 06:03:35 -0000 To: commits@struts.apache.org From: gvanmatre@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20060522060336.437B31A983A@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org X-Spam-Rating: minotaur.apache.org 1.6.2 0/1000/N 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; } + + /** + *

Finds matching nodes by name searching thru all the children.

+ */ + public List getNodesByName(String name) { + List nodes = new ArrayList(); + findNodesByName(this, name, nodes); + return nodes; + } + + /** + *

Recursively walks down the tree looking for nodes matching the name.

+ */ + 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); + } + + } + + /** + *

Walks up the tree looking for a uri namespace matching the prefix. + * A null prefix will search for the default uri namespace.

+ */ + 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("") + .append("") + .append("") + .append("") + .append("") + .append("Test") + .append("") + .append("") + .append("") + .append("
") + .append("") + .append("
") + .append("") + .append(""); + + + 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); + + + } }