struts-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From gvanma...@apache.org
Subject svn commit: r267521 - in /struts/shale/trunk/clay-plugin/src: java/org/apache/shale/clay/parser/AttributeTokenizer.java java/org/apache/shale/clay/parser/Node.java test/org/apache/shale/clay/parser/ParserTestCase.java
Date Sun, 04 Sep 2005 00:15:06 GMT
Author: gvanmatre
Date: Sat Sep  3 17:14:51 2005
New Revision: 267521

URL: http://svn.apache.org/viewcvs?rev=267521&view=rev
Log:
Bug#:  35839  [shale] Clay processes components inside HTML comments

Modified:
    struts/shale/trunk/clay-plugin/src/java/org/apache/shale/clay/parser/AttributeTokenizer.java
    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/AttributeTokenizer.java
URL: http://svn.apache.org/viewcvs/struts/shale/trunk/clay-plugin/src/java/org/apache/shale/clay/parser/AttributeTokenizer.java?rev=267521&r1=267520&r2=267521&view=diff
==============================================================================
--- struts/shale/trunk/clay-plugin/src/java/org/apache/shale/clay/parser/AttributeTokenizer.java
(original)
+++ struts/shale/trunk/clay-plugin/src/java/org/apache/shale/clay/parser/AttributeTokenizer.java
Sat Sep  3 17:14:51 2005
@@ -264,19 +264,21 @@
             }
             
             // looks for the key value delimiter
-            TokenOffset key = nextToken(currOffset, "=");
+            TokenOffset key = nextToken(currOffset, "=", " ");
             if (key == null)
                 break;
             
-            currOffset++;
+            currOffset++;     
             String delim = " "; // old school html color=red
-            if (buffer.charAt(currOffset) == '"') { // xmlish attribute
-                // delimiter
+            String otherDelim = "\"";
+            if (currOffset < buffer.length() && 
+                    buffer.charAt(currOffset) == '"') { // xmlish attribute
                 delim = "\"";
+                otherDelim = " ";
                 currOffset++;
             }
-            
-            TokenOffset value = nextToken(currOffset, delim);
+ 
+            TokenOffset value = nextToken(currOffset, delim, otherDelim);
             
             tokenIndex.add(new AttributeEntry(key, value));
             
@@ -298,23 +300,32 @@
      * <code>endDelim</code>.
      * </p>
      */
-    protected TokenOffset nextToken(int startOffset, String endDelim) {
+    protected TokenOffset nextToken(int startOffset, String endDelim, String otherDelim)
{
         // look for the value delimiter or the end of the parse fragment,
         // whichever comes first
         currOffset = Math.min(buffer.indexOf(endDelim, startOffset), endOffset);
-        // old school html <form name=old>
-        if ((currOffset == -1) && (endDelim.equals(" "))) {
+        // try another delimiter
+        if (currOffset == -1) 
+            currOffset = Math.min(buffer.indexOf(otherDelim, startOffset), endOffset);
+        
+        if (currOffset == -1) {
             currOffset = endOffset;
         }
         
-        if (currOffset > -1 && currOffset <= endOffset) {
+        if (currOffset > -1 && currOffset <= endOffset && startOffset
< currOffset) {
+            
+            int e = currOffset;
+            //forgive an attribute with Inconsistent delimiters, color=red" 
+            if (buffer.charAt(e - 1) == '"')
+              --e;
+            
             
-            TokenOffset value = new TokenOffset(startOffset, currOffset);
+            TokenOffset value = new TokenOffset(startOffset, e);
             
             if (log.isDebugEnabled()) {
                 log.debug(messages.getMessage("attribute.token.range",
                         new Object[] { new Integer(startOffset),
-                                new Integer(currOffset) }));
+                                new Integer(e) }));
             }
             
             return value;

Modified: struts/shale/trunk/clay-plugin/src/java/org/apache/shale/clay/parser/Node.java
URL: http://svn.apache.org/viewcvs/struts/shale/trunk/clay-plugin/src/java/org/apache/shale/clay/parser/Node.java?rev=267521&r1=267520&r2=267521&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 Sat Sep
 3 17:14:51 2005
@@ -235,8 +235,9 @@
     public String toString() {
         StringBuffer buff = new StringBuffer();
         buff.append("name=").append(name).append(" isStart=\"").append(isStart)
-        .append(" isEnd=\"").append(isEnd).append(" isWellFormed=\"")
-        .append(isWellFormed).append("\n").append(token).append("\n").append(attributes);
+        .append(" isEnd=\"").append(isEnd).append(" isWellFormed=")
+        .append(isWellFormed).append(" isComment=").append(isComment)
+        .append("\n").append(token).append("\n").append(attributes);
         return buff.toString();
     }
 

Modified: struts/shale/trunk/clay-plugin/src/test/org/apache/shale/clay/parser/ParserTestCase.java
URL: http://svn.apache.org/viewcvs/struts/shale/trunk/clay-plugin/src/test/org/apache/shale/clay/parser/ParserTestCase.java?rev=267521&r1=267520&r2=267521&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
Sat Sep  3 17:14:51 2005
@@ -120,8 +120,39 @@
         
         node = (Node) nodes1.get(0);
         assertTrue("node is a valid comment", node.isComment());
+   
         
+        // truncate the buffer
+        doc1.setLength(0);
+        doc1.append("<!-- <a attr=\"> -->");
         
+        nodes1 = p.parse(doc1);
+        assertTrue("one root node", nodes1.size() == 1);
+        
+        node = (Node) nodes1.get(0);
+        assertTrue("node is a valid comment", node.isComment());
+        
+        // truncate the buffer
+        doc1.setLength(0);
+        doc1.append("<!-- <a attr=\" size=4 class=\"test\"> -->");
+        
+        nodes1 = p.parse(doc1);
+        assertTrue("one root node", nodes1.size() == 1);
+        
+        node = (Node) nodes1.get(0);
+        assertTrue("node is a valid comment", node.isComment());
+
+
+        // truncate the buffer
+        doc1.setLength(0);
+        doc1.append("<!-- <a attr=> -->");
+        
+        nodes1 = p.parse(doc1);
+        assertTrue("one root node", nodes1.size() == 1);
+        
+        node = (Node) nodes1.get(0);
+        assertTrue("node is a valid comment", node.isComment());
+
     }
 
     /**
@@ -400,6 +431,122 @@
             }
 
         }
+    }
+    
+    /**
+     * <p>Test attribute parsing.</p>
+     */
+    public void testAttributes() {
+        Parser p = new Parser();
+        StringBuffer doc = new StringBuffer();
+        doc.append("<input type=text size=\"10\" name=date>");
+   
+        List nodes = p.parse(doc);
+        assertTrue("1 root node", nodes.size() == 1);
+
+        Node node = (Node) nodes.get(0);
+
+        String value = (String) node.getAttributes().get("type");
+        assertTrue("Attribute test type=text", value.equals("text"));
+        
+        value = (String) node.getAttributes().get("size");
+        assertTrue("Attribute test size=10", value.equals("10"));
+        
+        value = (String) node.getAttributes().get("name");
+        assertTrue("Attribute test name=date", value.equals("date"));
+        
+        //test bogus attribute with missing delimiter
+        doc.setLength(0);
+        doc.append("<input type=text size=\"10 name=date>");
+
+        nodes = p.parse(doc);
+        assertTrue("1 root node", nodes.size() == 1);
+        
+        node = (Node) nodes.get(0);
+
+        value = (String) node.getAttributes().get("type");
+        assertTrue("Attribute test type=text", value.equals("text"));
+
+        value = (String) node.getAttributes().get("size");
+        assertTrue("Attribute test size=10", value.equals("10"));
+
+        value = (String) node.getAttributes().get("name");
+        assertTrue("Attribute test name=date", value.equals("date"));
+
+      
+        //test bogus attribute with missing delimiter
+        doc.setLength(0);
+        doc.append("<input type=radio\" name=date checked>");
+
+        nodes = p.parse(doc);
+        assertTrue("1 root node", nodes.size() == 1);
+        
+        node = (Node) nodes.get(0);
+
+        value = (String) node.getAttributes().get("type");
+        assertTrue("Attribute test type=radio", value.equals("radio"));
+
+        value = (String) node.getAttributes().get("name");
+        assertTrue("Attribute test name=date", value.equals("date"));
+        
+        value = (String) node.getAttributes().get("checked");
+        assertTrue("Attribute checked=null", value == null);
+
+
+        doc.setLength(0);
+        doc.append("<option value=\"\"/>");
+
+        nodes = p.parse(doc);
+        assertTrue("1 root node", nodes.size() == 1);
+        
+        node = (Node) nodes.get(0);
+
+        value = (String) node.getAttributes().get("value");
+        assertTrue("Attribute value=null", value == null);
+ 
+    }
+    
+    public void testJSPTag() {
+        Parser p = new Parser();
+        StringBuffer doc = new StringBuffer();
+        doc.append("<table>")
+           .append("<tr><td>")
+           .append("<bean:message key=\"msg.username\"/><td><html:text property=\"userName\"
size=\"10\" maxlength=\"10\"/>")
+           .append("<tr><td>")
+           .append("<bean:message key=\"msg.password\"/><td><html:text property=\"password\"
size=\"10\" maxlength=10/><br>")
+           .append("</table>");
+        
+        List nodes = p.parse(doc);
+        assertTrue("Well-formed JSP, 1 root node", nodes.size() == 1);
+        
+        Node root = (Node) nodes.get(0);
+        assertNotNull("table node not null", root);
+
+        Node tr = (Node) root.getChildren().get(0);
+        assertNotNull("tr node not null", tr);
+
+        Node td = (Node) tr.getChildren().get(0);
+        assertNotNull("td node not null", td);
+
+        Node message = (Node) td.getChildren().get(0);
+        assertNotNull("bean:message node not null", message);
+        
+        assertEquals("node.name", message.getName(), "message");
+        assertEquals("node.qname", message.getQname(), "bean");
+        assertEquals("node.attributes.key", message.getAttributes().get("key"), "msg.username");
+       
+        td = (Node) tr.getChildren().get(1);
+        assertNotNull("td node not null", td);
+
+        Node text = (Node) td.getChildren().get(0);
+        assertNotNull("bean:message node not null", text);
+        
+        assertEquals("node.name", text.getName(), "text");
+        assertEquals("node.qname", text.getQname(), "html");
+        assertEquals("node.attributes.property", text.getAttributes().get("property"), "userName");
+        assertEquals("node.attributes.size", text.getAttributes().get("size"), "10");
+        assertEquals("node.attributes.maxlength", text.getAttributes().get("maxlength"),
"10");  
+
     }
     
     /**



---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@struts.apache.org
For additional commands, e-mail: dev-help@struts.apache.org


Mime
View raw message