commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From wer...@apache.org
Subject cvs commit: jakarta-commons-sandbox/jelly/src/test/org/apache/commons/jelly/werkz example.jelly
Date Wed, 12 Jun 2002 19:28:08 GMT
werken      2002/06/12 12:28:08

  Modified:    jelly/src/java/org/apache/commons/jelly TagSupport.java
               jelly/src/java/org/apache/commons/jelly/tags/ant
                        AntTagLibrary.java
               jelly/src/java/org/apache/commons/jelly/tags/core
                        ForEachTag.java
               jelly/src/java/org/apache/commons/jelly/tags/werkz
                        ProjectTag.java WerkzTagSupport.java
               jelly/src/test/org/apache/commons/jelly/werkz example.jelly
  Log:
  * Hacked in pertnear perfect whitespace trimming/handling.
    Any tag that inherits from TagSupport somewhere along the line
    now has the following properties:
  
      - A 'trim' attribute, taking 'true' or 'false'
  
      - If no 'trim' is set, then simply inherit from parent tag, or
        previous ancestor that has explicitly set a trim attribute.
  
      - TagSupport has a ctor that takes a boolean 'trim' parameter
        so that tags (or entire taglibs) may be marked as text-trimming
        or not.
  
      - The default response, if nothing set through ctor or trim attr
        is to *not trim*, which I think retains backwards compatibility.
  
  Revision  Changes    Path
  1.8       +94 -6     jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/TagSupport.java
  
  Index: TagSupport.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/TagSupport.java,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- TagSupport.java	25 May 2002 18:27:21 -0000	1.7
  +++ TagSupport.java	12 Jun 2002 19:28:08 -0000	1.8
  @@ -61,8 +61,12 @@
    */
   package org.apache.commons.jelly;
   
  +import org.apache.commons.jelly.impl.ScriptBlock;
  +import org.apache.commons.jelly.impl.TextScript;
  +
   import java.io.StringWriter;
   import java.io.Writer;
  +import java.util.List;
   
   /** <p><code>TagSupport</code> an abstract base class which is useful
to 
     * inherit from if developing your own tag.</p>
  @@ -79,6 +83,9 @@
       /** the body of the tag */  
       protected Script body;
       /** The current context */
  +
  +    protected Boolean shouldTrim;
  +    protected boolean hasTrimmed;
       
       protected JellyContext context;
   
  @@ -99,6 +106,44 @@
           }
           return null;
       }
  +
  +    public TagSupport()
  +    {
  +    }
  +
  +    public TagSupport(boolean shouldTrim)
  +    {
  +        setTrim( shouldTrim );
  +    }
  +
  +    public void setTrim(boolean shouldTrim)
  +    {
  +        if ( shouldTrim ) {
  +            this.shouldTrim = Boolean.TRUE;
  +        } else {
  +            this.shouldTrim = Boolean.FALSE;
  +        }
  +    }
  +
  +    public boolean getTrim()
  +    {
  +        if ( this.shouldTrim == null ) {
  +            Tag parent = getParent();
  +            if ( parent == null ) {
  +                return false;
  +            } else {
  +                if ( parent instanceof TagSupport ) {
  +                    TagSupport parentSupport = (TagSupport) parent;
  +
  +                    this.shouldTrim = ( parentSupport.getTrim() ? Boolean.TRUE : Boolean.FALSE
);
  +                } else {
  +                    this.shouldTrim = Boolean.FALSE;
  +                }
  +            }
  +        }
  +
  +        return this.shouldTrim.booleanValue();
  +    }
       
       /** @return the parent of this tag */
       public Tag getParent() {
  @@ -112,12 +157,19 @@
       
       /** @return the body of the tag */
       public Script getBody() {
  +        if ( getTrim()
  +             &&
  +             ! hasTrimmed )
  +        {
  +            trimBody();
  +        }
           return body;
       }
       
       /** Sets the body of the tag */
       public void setBody(Script body) {
           this.body = body;
  +        this.hasTrimmed = false;
       }
       
       /** @return the context in which the tag will be run */
  @@ -149,7 +201,43 @@
       protected String getBodyText() throws Exception {
           // XXX: could maybe optimise this later on by having a pool of buffers
           StringWriter writer = new StringWriter();
  -        body.run(context, XMLOutput.createXMLOutput(writer));
  +        getBody().run(context, XMLOutput.createXMLOutput(writer));
           return writer.toString();
  +    }
  +
  +    /** 
  +     * Find all text nodes inside the top level of this body and 
  +     * if they are just whitespace then remove them
  +     */
  +    protected void trimBody() { // throws Exception {
  +        // System.err.println( "trimBody() " + this.getClass().getName() );
  +        if ( body instanceof ScriptBlock ) {
  +            ScriptBlock block = (ScriptBlock) body;
  +            List list = block.getScriptList();
  +            for ( int i = list.size() - 1; i >= 0; i-- ) {
  +                Script script = (Script) list.get(i);
  +                if ( script instanceof TextScript ) {
  +                    TextScript textScript = (TextScript) script;
  +                    String text = textScript.getText();
  +                    text = text.trim();
  +                    // if ( text.length() == 0 ) {
  +                        // list.remove(i);
  +                        // System.err.println( "removeText(" + i + ")" );
  +                    // }
  +                    // else {
  +                        // System.err.println( "setText(" + text + ")" );
  +                        textScript.setText(text);
  +                    // }
  +                }
  +            }                
  +        }
  +        else if ( body instanceof TextScript ) {
  +            TextScript textScript = (TextScript) body;
  +            String text = textScript.getText();
  +            text = text.trim();
  +            textScript.setText(text);
  +        }
  +
  +        this.hasTrimmed = true;
       }
   }
  
  
  
  1.7       +2 -1      jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/tags/ant/AntTagLibrary.java
  
  Index: AntTagLibrary.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/tags/ant/AntTagLibrary.java,v
  retrieving revision 1.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- AntTagLibrary.java	12 Jun 2002 06:07:29 -0000	1.6
  +++ AntTagLibrary.java	12 Jun 2002 19:28:08 -0000	1.7
  @@ -173,7 +173,8 @@
               Task task = (Task) type.newInstance();
               task.setProject(project);
               task.setTaskName(name);
  -            Tag tag = new TaskTag( task );
  +            TaskTag tag = new TaskTag( task );
  +            tag.setTrim( true );
               return TagScript.newInstance(tag);
           }
           
  
  
  
  1.12      +6 -6      jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/tags/core/ForEachTag.java
  
  Index: ForEachTag.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/tags/core/ForEachTag.java,v
  retrieving revision 1.11
  retrieving revision 1.12
  diff -u -r1.11 -r1.12
  --- ForEachTag.java	4 Jun 2002 18:34:33 -0000	1.11
  +++ ForEachTag.java	12 Jun 2002 19:28:08 -0000	1.12
  @@ -145,6 +145,7 @@
                   if (indexVar != null) {
                       context.setVariable(indexVar, new Integer(index));
                   }
  +                // System.err.println( "body=" + getBody() );
                   getBody().run(context, output);
                   
                   // now we need to move to next index
  @@ -202,5 +203,4 @@
       public void setStep(int step) {
           this.step = step;
       }
  -
   }
  
  
  
  1.6       +2 -1      jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/tags/werkz/ProjectTag.java
  
  Index: ProjectTag.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/tags/werkz/ProjectTag.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- ProjectTag.java	12 Jun 2002 14:38:57 -0000	1.5
  +++ ProjectTag.java	12 Jun 2002 19:28:08 -0000	1.6
  @@ -74,12 +74,13 @@
    * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
    * @version $Revision$
    */
  -public class ProjectTag extends TagSupport {
  +public class ProjectTag extends WerkzTagSupport {
   
       /** the project */
       private Project project;
           
       public ProjectTag() {
  +        super( true );
       }
   
   
  
  
  
  1.4       +6 -0      jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/tags/werkz/WerkzTagSupport.java
  
  Index: WerkzTagSupport.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/tags/werkz/WerkzTagSupport.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- WerkzTagSupport.java	12 Jun 2002 14:00:40 -0000	1.3
  +++ WerkzTagSupport.java	12 Jun 2002 19:28:08 -0000	1.4
  @@ -72,8 +72,14 @@
    */
   public abstract class WerkzTagSupport extends TagSupport {
   
  +    public WerkzTagSupport(boolean trim) {
  +        super( trim );
  +    }
  +
       public WerkzTagSupport() {
       }
  +
  +    
   
       
       // Implementation methods
  
  
  
  1.5       +43 -42    jakarta-commons-sandbox/jelly/src/test/org/apache/commons/jelly/werkz/example.jelly
  
  Index: example.jelly
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/jelly/src/test/org/apache/commons/jelly/werkz/example.jelly,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- example.jelly	12 Jun 2002 17:09:41 -0000	1.4
  +++ example.jelly	12 Jun 2002 19:28:08 -0000	1.5
  @@ -1,42 +1,43 @@
  -<?xml version="1.0"?>
  -
  -<werkz:project xmlns:j="jelly:core" xmlns="jelly:ant" xmlns:werkz="jelly:werkz">
  -
  -  <!-- the following could be in the mediator of Maven -->
  -  <werkz:goal name="init">
  -  	<echo message="Running init goal's action"/>
  -  </werkz:goal>
  -  		
  -  <werkz:goal name="compile" prereqs="init">
  -  	<echo message="Running compile goal's action"/>
  -  </werkz:goal>
  -
  -  <werkz:goal name="test" prereqs="init,compile">
  -  	<echo message="Running test goal's action"/>
  -  </werkz:goal>
  -
  -
  - <!-- define some callbacks in the projects build.jelly -->
  - <werkz:preGoal name="compile">
  - 	<echo message="I am a callback: doing something before I compile"/> 	  		
  - </werkz:preGoal>
  -
  -
  -  <!-- call all the targets made on the command line -->	
  -  <werkz:attain>
  -  	   <echo>-------------------------------------</echo>
  -           <echo>jelly+werkz+ant-taskdefs</echo>
  -  	   <echo>-------------------------------------</echo>
  -      <j:forEach var="arg" items="${args}" begin="1">
  -  	   <echo>begin target [<j:expr value="${arg}"/>]</echo>
  -  	   <echo>-------------------------------------</echo>
  -           <werkz:attainGoal name="${arg}"/>
  -  	   <echo>end target [<j:expr value="${arg}"/>]</echo>
  -  	   <echo>-------------------------------------</echo>
  -      </j:forEach>
  -  </werkz:attain>
  -  	
  -</werkz:project>
  -
  -
  -
  +<?xml version="1.0"?>
  +<werkz:project xmlns:j="jelly:core" xmlns="jelly:ant" xmlns:werkz="jelly:werkz">
  +
  +  <!-- the following could be in the mediator of Maven -->
  +  <werkz:goal name="init">
  +    <echo message="Running init goal's action"/>
  +  </werkz:goal>
  +  		
  +  <werkz:goal name="compile" prereqs="init">
  +    <echo message="Running compile goal's action"/>
  +  </werkz:goal>
  +
  +  <werkz:goal name="test" prereqs="init,compile">
  +    <echo message="Running test goal's action"/>
  +  </werkz:goal>
  +
  + <!-- define some callbacks in the projects build.jelly -->
  + <werkz:preGoal name="compile">
  +    <echo message="I am a callback: doing something before I compile"/>
  + </werkz:preGoal>
  +
  +  <!-- call all the targets made on the command line -->	
  +  <werkz:attain>
  +
  +  	   <echo>+---------------------------------------</echo>
  +           <echo>| jelly+werkz+ant-taskdefs</echo>
  +  	   <echo>+---------------------------------------</echo>
  +
  +           <j:forEach var="arg" items="${args}" begin="1">
  +
  +  	       <echo>| begin target [<j:expr value="${arg}"/>]</echo>
  +  	       <echo>+----------------------------------------</echo>
  +
  +               <werkz:attainGoal name="${arg}"/> 
  +
  +  	       <echo>| end target [<j:expr value="${arg}"/>]</echo>
  +  	       <echo>+----------------------------------------</echo>
  +
  +           </j:forEach>
  +
  +  </werkz:attain>
  +
  +</werkz:project>
  
  
  

--
To unsubscribe, e-mail:   <mailto:commons-dev-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:commons-dev-help@jakarta.apache.org>


Mime
View raw message