You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jspwiki.apache.org by aj...@apache.org on 2008/10/08 05:40:48 UTC

svn commit: r702691 - in /incubator/jspwiki/branches/JSPWIKI_2_9_STRIPES_BRANCH/tests/com/ecyrd/jspwiki/ui/stripes: StripesJspTransformer.java StripesJspTransformerTest.java

Author: ajaquith
Date: Tue Oct  7 20:40:47 2008
New Revision: 702691

URL: http://svn.apache.org/viewvc?rev=702691&view=rev
Log:
Added code to migrate label tags to stripes:label.

Modified:
    incubator/jspwiki/branches/JSPWIKI_2_9_STRIPES_BRANCH/tests/com/ecyrd/jspwiki/ui/stripes/StripesJspTransformer.java
    incubator/jspwiki/branches/JSPWIKI_2_9_STRIPES_BRANCH/tests/com/ecyrd/jspwiki/ui/stripes/StripesJspTransformerTest.java

Modified: incubator/jspwiki/branches/JSPWIKI_2_9_STRIPES_BRANCH/tests/com/ecyrd/jspwiki/ui/stripes/StripesJspTransformer.java
URL: http://svn.apache.org/viewvc/incubator/jspwiki/branches/JSPWIKI_2_9_STRIPES_BRANCH/tests/com/ecyrd/jspwiki/ui/stripes/StripesJspTransformer.java?rev=702691&r1=702690&r2=702691&view=diff
==============================================================================
--- incubator/jspwiki/branches/JSPWIKI_2_9_STRIPES_BRANCH/tests/com/ecyrd/jspwiki/ui/stripes/StripesJspTransformer.java (original)
+++ incubator/jspwiki/branches/JSPWIKI_2_9_STRIPES_BRANCH/tests/com/ecyrd/jspwiki/ui/stripes/StripesJspTransformer.java Tue Oct  7 20:40:47 2008
@@ -49,8 +49,13 @@
                     migrated = migrateTextArea( tag ) || migrated;
                 }
                 
+                else if ( "label".equals( tag.getName() ) )
+                {
+                    migrated = migrateLabel( tag ) || migrated;
+                }
+
                 // Remove any <fmt:setLocale> tags (and their children)
-                else if ( "fmt:setLocale".equals( tag.getName() ) )
+                else if( "fmt:setLocale".equals( tag.getName() ) )
                 {
                     removeSetLocale( tag );
                 }
@@ -87,6 +92,72 @@
     }
 
     /**
+     * <p>
+     * Migrates an existing &lt;label&gt; element to &lt;stripes:label&gt;.
+     * Migration can happen in two ways:
+     * </p>
+     * <ul>
+     * <li>If the label tag has an a single <code>fmt:message</code> child
+     * element whose <code>key</code> attribute contains a value, that value
+     * will become the <code>name</code> attribute of the
+     * <code>stripes:label</code>element. </li>
+     * <li>In all other cases, the <code>label</code> element is simply re-named to
+     * <code>stripes:label</code>.</li>
+     * </ul>
+     * <p>
+     * For example, the ordinary HTML tag <code>&lt;label
+     * for="assertedName"&gt;&lt;fmt:message
+     * key="prefs.assertedname"/&gt;&lt;/label&gt;</code>
+     * will be migrated to &lt;stripes:label name="prefs.assertedname"
+     * for="assertedName"&gt;.
+     * </p>
+     * 
+     * @param tag
+     * @return
+     */
+    private boolean migrateLabel( Tag tag )
+    {
+        // Change the name to <stripes:label>
+        tag.setName( "stripes:label" );
+        
+        // Not a start tag, we're done
+        if ( tag.getType() != NodeType.START_TAG)
+        {
+            return false;
+        }
+        
+        // Do we have a single child <fmt:message>?
+        Node child = tag.getChildren().size() == 1 ? tag.getChildren().get( 0 ) : null;
+        if ( child != null && child.getType() == NodeType.EMPTY_ELEMENT_TAG )
+        {
+            if ( "fmt:message".equals( child.getName() ) )
+            {
+                // Move the fmt:message tag's key attribute to stripes:label name
+                Tag message = (Tag)child;
+                if ( message.hasAttribute( "key" ) )
+                {
+                    Attribute key = message.getAttribute( "key" );
+                    key.setName( "name" );
+                    message.removeAttribute( key );
+                    tag.addAttribute( key );
+                    tag.removeChild( message );
+                }
+                
+                // Change to an empty end tag
+                tag.setType( NodeType.EMPTY_ELEMENT_TAG );
+                
+                // Delete the matching end tag
+                int i = tag.getParent().getChildren().indexOf( tag );
+                Node endTag = tag.getParent().getChildren().get( i + 1 );
+                tag.getParent().removeChild( endTag );
+            }
+        }
+        message( tag, "Changed <label> to <stripes:label>." );
+        
+        return true;
+    }
+
+    /**
      * Migrates the &lt;form&gt; tag.
      * 
      * @param tag the Tag that represents the form tag being processed.
@@ -161,7 +232,7 @@
     private boolean migrateInputTag( Tag tag )
     {
         boolean migrated = false;
-        
+
         // Move 'type' attribute value to the localname
         Attribute attribute = tag.getAttribute( "type" );
         if( attribute != null )
@@ -190,7 +261,7 @@
     private boolean migrateTextArea( Tag tag )
     {
         boolean migrated = false;
-        
+
         // Only migrate textarea if 'name' attribute is present
         Attribute name = tag.getAttribute( "name" );
         if( name != null )
@@ -220,12 +291,13 @@
      * method leaves the attribute as-is.
      * 
      * @param tag the tag to migrate
-     * @return <code>true</code> if this method changed the JspDocument, and <code>false</code> if not
+     * @return <code>true</code> if this method changed the JspDocument, and
+     *         <code>false</code> if not
      */
     private boolean migrateValueAttribute( Tag tag )
     {
         boolean migrated = false;
-        
+
         // If embedded markup in "value" attribute, move to child
         // nodes
         Attribute attribute = tag.getAttribute( "value" );

Modified: incubator/jspwiki/branches/JSPWIKI_2_9_STRIPES_BRANCH/tests/com/ecyrd/jspwiki/ui/stripes/StripesJspTransformerTest.java
URL: http://svn.apache.org/viewvc/incubator/jspwiki/branches/JSPWIKI_2_9_STRIPES_BRANCH/tests/com/ecyrd/jspwiki/ui/stripes/StripesJspTransformerTest.java?rev=702691&r1=702690&r2=702691&view=diff
==============================================================================
--- incubator/jspwiki/branches/JSPWIKI_2_9_STRIPES_BRANCH/tests/com/ecyrd/jspwiki/ui/stripes/StripesJspTransformerTest.java (original)
+++ incubator/jspwiki/branches/JSPWIKI_2_9_STRIPES_BRANCH/tests/com/ecyrd/jspwiki/ui/stripes/StripesJspTransformerTest.java Tue Oct  7 20:40:47 2008
@@ -1,6 +1,7 @@
 package com.ecyrd.jspwiki.ui.stripes;
 
 import java.util.HashMap;
+import java.util.List;
 import java.util.Map;
 
 import junit.framework.Test;
@@ -85,6 +86,74 @@
         Node attribute = ((Tag) node).getAttributes().get( 0 );
         assertEquals( "Login.jsp", attribute.getValue() );
     }
+    
+    public void testLabel() throws Exception
+    {
+        String s = "<label for=\"assertedName\"><fmt:message key=\"prefs.assertedname\"/></label>";
+        JspDocument doc = new JspParser().parse( s );
+        m_transformer.transform( m_sharedState, doc );
+
+        // Message tag is removed; 2 more added
+        List<Node> nodes = doc.getNodes();
+        assertEquals( 3, nodes.size() );
+        
+        // First node is the Stripes taglib
+        Node node = nodes.get( 0 );
+        assertEquals( NodeType.JSP_DIRECTIVE, node.getType() );
+
+        // Second node is the injected linebreak
+        node = nodes.get( 1 );
+        assertEquals( NodeType.TEXT, node.getType() );
+        
+        // Third node is the re-structured stripes:label
+        Tag tag = (Tag)nodes.get( 2 );
+        assertEquals( NodeType.EMPTY_ELEMENT_TAG, tag.getType() );
+        assertEquals( "stripes:label", tag.getName() );
+        assertEquals( 2, tag.getAttributes().size() );
+        assertEquals( "for", tag.getAttribute( "for" ).getName() );
+        assertEquals( "assertedName", tag.getAttribute( "for" ).getValue() );
+        assertEquals( "name", tag.getAttribute( "name" ).getName() );
+        assertEquals( "prefs.assertedname", tag.getAttribute( "name" ).getValue() );
+        
+        assertEquals( "<stripes:label for=\"assertedName\" name=\"prefs.assertedname\" />", tag.toString() );
+    }
+
+    public void testLabelNoFmtMessage() throws Exception
+    {
+        String s = "<label for=\"assertedName\">This is a test.</label>";
+        JspDocument doc = new JspParser().parse( s );
+        m_transformer.transform( m_sharedState, doc );
+
+        // Added 2 nodes...
+        List<Node> nodes = doc.getNodes();
+        assertEquals( 5, nodes.size() );
+        
+        // First node is the Stripes taglib
+        Node node = nodes.get( 0 );
+        assertEquals( NodeType.JSP_DIRECTIVE, node.getType() );
+
+        // Second node is the injected linebreak
+        node = nodes.get( 1 );
+        assertEquals( NodeType.TEXT, node.getType() );
+        
+        // Third node is the stripes:label start
+        Tag tag = (Tag)nodes.get( 2 );
+        assertEquals( NodeType.START_TAG, tag.getType() );
+        assertEquals( "stripes:label", tag.getName() );
+        assertEquals( 1, tag.getAttributes().size() );
+        assertEquals( "for", tag.getAttribute( "for" ).getName() );
+        assertEquals( "assertedName", tag.getAttribute( "for" ).getValue() );
+
+        // Fourth node is the label text
+        node = nodes.get( 3 );
+        assertEquals( NodeType.TEXT, node.getType() );
+        assertEquals( "This is a test.", node.getValue() );
+        
+        // Fifth node is the stripes:label end
+        tag = (Tag)nodes.get( 4 );
+        assertEquals( NodeType.END_TAG, tag.getType() );
+        assertEquals( "stripes:label", tag.getName() );
+    }
 
     public void testNoAddStripesTaglib() throws Exception
     {