commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jstrac...@apache.org
Subject cvs commit: jakarta-commons-sandbox/jelly/src/test/org/apache/commons/jelly TestTrim.java
Date Fri, 14 Jun 2002 06:53:03 GMT
jstrachan    2002/06/13 23:53:03

  Modified:    jelly    build.xml
               jelly/src/java/org/apache/commons/jelly TagSupport.java
               jelly/xdocs todo.xml
               jelly/src/java/org/apache/commons/jelly/impl TextScript.java
               jelly/src/test/org/apache/commons/jelly/xml TestXMLTags.java
               jelly/src/java/org/apache/commons/jelly/parser
                        XMLParser.java
  Added:       jelly/src/java/org/apache/commons/jelly/impl
                        CompositeTextScriptBlock.java ExpressionScript.java
               jelly/src/test/org/apache/commons/jelly/xml
                        testExpressions.jelly
               jelly/src/test/org/apache/commons/jelly TestTrim.java
  Log:
  Patched Jelly so that expressions can now be both embedded inside attribute text as well
as inside the body text.
  
  For example
  
  <foo value="text ${exp} text ${bean2.foo} text"/>
  
  or
  
  <foo>
  	some text ${foo} text ${bar}
  </foo>
  
  both work fine now.
  
  Added some unit test cases for some new whitespace trimming code.
  
  Revision  Changes    Path
  1.46      +6 -0      jakarta-commons-sandbox/jelly/build.xml
  
  Index: build.xml
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/jelly/build.xml,v
  retrieving revision 1.45
  retrieving revision 1.46
  diff -u -r1.45 -r1.46
  --- build.xml	12 Jun 2002 17:09:41 -0000	1.45
  +++ build.xml	14 Jun 2002 06:53:02 -0000	1.46
  @@ -242,6 +242,12 @@
       <maven-ant antfile="${maven.home}/plugins/test/build.xml" target="run-singletest"/>
      </target>
   
  +   <target name="test.xml"
  +      description="Runs the XML unit test">
  +    <property name="maven.testcase" value="org.apache.commons.jelly.xml.TestXMLTags"/>
  
  +    <maven-ant antfile="${maven.home}/plugins/test/build.xml" target="run-singletest"/>
  +   </target>
  +
   
   <!-- ========== Sample Program Targets ==================================== -->
   
  
  
  
  1.11      +31 -8     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.10
  retrieving revision 1.11
  diff -u -r1.10 -r1.11
  --- TagSupport.java	13 Jun 2002 14:07:25 -0000	1.10
  +++ TagSupport.java	14 Jun 2002 06:53:02 -0000	1.11
  @@ -61,6 +61,7 @@
    */
   package org.apache.commons.jelly;
   
  +import org.apache.commons.jelly.impl.CompositeTextScriptBlock;
   import org.apache.commons.jelly.impl.ScriptBlock;
   import org.apache.commons.jelly.impl.TextScript;
   
  @@ -209,6 +210,30 @@
        * if they are just whitespace then remove them
        */
       protected void trimBody() { 
  +        
  +        // #### should refactor this code into
  +        // #### trimWhitespace() methods on the Script objects
  +        
  +        if ( body instanceof CompositeTextScriptBlock ) {
  +            CompositeTextScriptBlock block = (CompositeTextScriptBlock) body;
  +            List list = block.getScriptList();
  +            int size = list.size();
  +            if ( size > 0 ) {
  +                Script script = (Script) list.get(0);
  +                if ( script instanceof TextScript ) {
  +                    TextScript textScript = (TextScript) script;
  +                    textScript.trimStartWhitespace();
  +                }
  +                if ( size > 1 ) {
  +                    script = (Script) list.get(size - 1);
  +	                if ( script instanceof TextScript ) {
  +	                    TextScript textScript = (TextScript) script;
  +	                    textScript.trimEndWhitespace();
  +	                }
  +                }
  +            }
  +        }
  +        else
           if ( body instanceof ScriptBlock ) {
               ScriptBlock block = (ScriptBlock) body;
               List list = block.getScriptList();
  @@ -229,9 +254,7 @@
           }
           else if ( body instanceof TextScript ) {
               TextScript textScript = (TextScript) body;
  -            String text = textScript.getText();
  -            text = text.trim();
  -            textScript.setText(text);
  +            textScript.trimWhitespace();
           }
   
           this.hasTrimmed = true;
  
  
  
  1.10      +0 -1      jakarta-commons-sandbox/jelly/xdocs/todo.xml
  
  Index: todo.xml
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/jelly/xdocs/todo.xml,v
  retrieving revision 1.9
  retrieving revision 1.10
  diff -u -r1.9 -r1.10
  --- todo.xml	12 Jun 2002 15:16:56 -0000	1.9
  +++ todo.xml	14 Jun 2002 06:53:02 -0000	1.10
  @@ -17,7 +17,6 @@
       <section name="Core tasks"> 
         <ul>
           <li>write a Cocoon JellyTransformer so that Jelly scripts can be used easily
inside Cocoon</li>
  -        <li>support the use of ${foo.bar} expressions within element text.</li>
           <li>autogenerate tag library documentation, in a kinda javadoc style, showing
all tag libraries, 
   			their tags and descriptions of their tags. 
   			We could also autogenerate XML Schemas or RelaxNG docs for tag libraries</li>
  
  
  
  1.6       +48 -6     jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/impl/TextScript.java
  
  Index: TextScript.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/impl/TextScript.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- TextScript.java	17 May 2002 15:18:10 -0000	1.5
  +++ TextScript.java	14 Jun 2002 06:53:03 -0000	1.6
  @@ -87,7 +87,47 @@
       public String toString() {
           return super.toString() + "[text=" + text + "]";
       }
  - 
  +    
  +    /**
  +     * Trims whitespace from the start and end of the text in this script
  +     */
  +    public void trimWhitespace() {
  +        this.text = text.trim();
  +    }
  +    
  +    /**
  +     * Trims whitespace from the start of the text
  +     */
  +    public void trimStartWhitespace() {
  +        int index = 0;        
  +        for ( int length = text.length(); index < length; index++ ) {
  +            char ch = text.charAt(index);
  +            if (!Character.isWhitespace(ch)) {
  +                break;
  +            }            
  +        }
  +        if ( index > 0 ) {
  +            this.text = text.substring(index);
  +        }
  +    }
  +    
  +    /**
  +     * Trims whitespace from the end of the text
  +     */
  +    public void trimEndWhitespace() {
  +        int index = text.length();
  +        while (--index > 0) {
  +            char ch = text.charAt(index);
  +            if (!Character.isWhitespace(ch)) {
  +                break;
  +            }
  +        }
  +        index++;
  +        if ( index < text.length() ) {
  +            this.text = text.substring(0,index);
  +        }
  +    }
  +
       /** @return the text output by this script */
       public String getText() {
           return text;
  @@ -108,4 +148,6 @@
       public void run(JellyContext context, XMLOutput output) throws Exception {
           output.write(text);
       }
  +
  +     
   }
  
  
  
  1.1                  jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/impl/CompositeTextScriptBlock.java
  
  Index: CompositeTextScriptBlock.java
  ===================================================================
  /*
   * $Header: /home/cvs/jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/impl/CompositeTextScriptBlock.java,v
1.7 2002/06/13 08:16:47 jstrachan Exp $
   * $Revision: 1.7 $
   * $Date: 2002/06/13 08:16:47 $
   *
   * ====================================================================
   *
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 1999-2002 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution, if
   *    any, must include the following acknowlegement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowlegement may appear in the software itself,
   *    if and wherever such third-party acknowlegements normally appear.
   *
   * 4. The names "The Jakarta Project", "Commons", and "Apache Software
   *    Foundation" must not be used to endorse or promote products derived
   *    from this software without prior written permission. For written
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Group.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   * 
   * $Id: CompositeTextScriptBlock.java,v 1.7 2002/06/13 08:16:47 jstrachan Exp $
   */
  package org.apache.commons.jelly.impl;
  
  import java.io.Writer;
  import java.util.ArrayList;
  import java.util.Iterator;
  import java.util.List;
  
  import org.apache.commons.jelly.JellyContext;
  import org.apache.commons.jelly.Script;
  import org.apache.commons.jelly.XMLOutput;
  
  /** 
   * <p><code>CompositeTextScriptBlock</code> represents a text body of
a
   * a tag which contains expressions, so that whitespace trimming
   * can be handled differently.</p>
   *
   * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
   * @version $Revision: 1.7 $
   */
  public class CompositeTextScriptBlock extends ScriptBlock {
  
      public CompositeTextScriptBlock() {
      }
  }
  
  
  
  1.1                  jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/impl/ExpressionScript.java
  
  Index: ExpressionScript.java
  ===================================================================
  /*
   * $Header: /home/cvs/jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/impl/ExpressionScript.java,v
1.5 2002/05/17 15:18:10 jstrachan Exp $
   * $Revision: 1.5 $
   * $Date: 2002/05/17 15:18:10 $
   *
   * ====================================================================
   *
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 1999-2002 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution, if
   *    any, must include the following acknowlegement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowlegement may appear in the software itself,
   *    if and wherever such third-party acknowlegements normally appear.
   *
   * 4. The names "The Jakarta Project", "Commons", and "Apache Software
   *    Foundation" must not be used to endorse or promote products derived
   *    from this software without prior written permission. For written
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Group.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   * 
   * $Id: ExpressionScript.java,v 1.5 2002/05/17 15:18:10 jstrachan Exp $
   */
  package org.apache.commons.jelly.impl;
  
  import org.apache.commons.jelly.JellyContext;
  import org.apache.commons.jelly.Script;
  import org.apache.commons.jelly.XMLOutput;
  import org.apache.commons.jelly.expression.Expression;
  
  /** 
   * <p><code>ExpressionScript</code> outputs the value of an expression
as text.</p>
   *
   * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
   * @version $Revision: 1.5 $
   */
  public class ExpressionScript implements Script {
   
      /** the expression evaluated as a String and output by this script */
      private Expression expression;
   
      public ExpressionScript() {
      }
  
      public ExpressionScript(Expression expression) {
          this.expression = expression;
      }
  
      public String toString() {
          return super.toString() + "[expression=" + expression + "]";
      }
   
      /** @return the expression evaluated as a String and output by this script */
      public Expression getExpression() {
          return expression;
      }
  
      /** Sets the expression evaluated as a String and output by this script */
      public void setExpression(Expression expression) {
          this.expression = expression;
      }
   
      // Script interface
      //-------------------------------------------------------------------------        
       
      public Script compile() {
          return this;
      }
  
      /** Evaluates the body of a tag */
      public void run(JellyContext context, XMLOutput output) throws Exception {
          String text = expression.evaluateAsString(context);
          if ( text != null ) {
              output.write(text);
          }
      }
  }
  
  
  
  1.4       +15 -6     jakarta-commons-sandbox/jelly/src/test/org/apache/commons/jelly/xml/TestXMLTags.java
  
  Index: TestXMLTags.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/jelly/src/test/org/apache/commons/jelly/xml/TestXMLTags.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- TestXMLTags.java	7 Jun 2002 09:36:11 -0000	1.3
  +++ TestXMLTags.java	14 Jun 2002 06:53:03 -0000	1.4
  @@ -111,6 +111,10 @@
           runUnitTest( "src/test/org/apache/commons/jelly/xml/testForEach.jelly" );
       }
       
  +    public void testExpressions() throws Exception {
  +        runUnitTest( "src/test/org/apache/commons/jelly/xml/testExpressions.jelly");
  +    }
  +    
       public void testParse() throws Exception {
           InputStream in = new FileInputStream("src/test/org/apache/commons/jelly/xml/example.jelly");
           XMLParser parser = new XMLParser();
  @@ -129,6 +133,16 @@
       }
       
       public void runUnitTest(String name) throws Exception {
  +        Document document = parseUnitTest(name);
  +        
  +        List failures = document.selectNodes( "/*/fail" );
  +        for ( Iterator iter = failures.iterator(); iter.hasNext(); ) {
  +            Node node = (Node) iter.next();
  +            fail( node.getStringValue() );
  +        }
  +    }
  +    
  +    public Document parseUnitTest(String name) throws Exception {
           // parse script
           InputStream in = new FileInputStream(name);
           XMLParser parser = new XMLParser();
  @@ -145,12 +159,7 @@
           }
           
           // now lets parse the output
  -        Document document = DocumentHelper.parseText( text );
  -        List failures = document.selectNodes( "/*/fail" );
  -        for ( Iterator iter = failures.iterator(); iter.hasNext(); ) {
  -            Node node = (Node) iter.next();
  -            fail( node.getStringValue() );
  -        }
  +        return DocumentHelper.parseText( text );
       }
       
   }
  
  
  
  1.1                  jakarta-commons-sandbox/jelly/src/test/org/apache/commons/jelly/xml/testExpressions.jelly
  
  Index: testExpressions.jelly
  ===================================================================
  <?xml version="1.0"?>
  <j:jelly trim="false" xmlns:j="jelly:core">
  <testcase>
  	
    <j:set var="topping">cheese</j:set>
    <j:set var="base">deep pan</j:set>
  
    <j:set var="test" value="pizza has ${topping} on top"/>
    
    <j:if test="${test != 'pizza has cheese on top'}">
    	<fail>attribute test failed. Found <j:expr value="${test}"/> and expected
'pizza has cheese on top'</fail>
    </j:if>
    	
    <j:set var="test">pizza has ${topping} on top</j:set>
    
    <j:if test="${test != 'pizza has cheese on top'}">
    	<fail>element test failed. Found <j:expr value="${test}"/> and expected 'pizza
has cheese on top'</fail>
    </j:if>
    	
  </testcase>
  </j:jelly>
  
  
  
  
  1.21      +85 -25    jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/parser/XMLParser.java
  
  Index: XMLParser.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/parser/XMLParser.java,v
  retrieving revision 1.20
  retrieving revision 1.21
  diff -u -r1.20 -r1.21
  --- XMLParser.java	6 Jun 2002 07:13:41 -0000	1.20
  +++ XMLParser.java	14 Jun 2002 06:53:03 -0000	1.21
  @@ -60,6 +60,7 @@
    * $Id$
    */
   package org.apache.commons.jelly.parser;
  +
   import java.io.File;
   import java.io.FileReader;
   import java.io.InputStream;
  @@ -75,16 +76,21 @@
   import java.util.List;
   import java.util.Map;
   import java.util.Properties;
  +
   import javax.xml.parsers.SAXParser;
   import javax.xml.parsers.SAXParserFactory;
  +
   import org.apache.commons.collections.ArrayStack;
  +
   import org.apache.commons.jelly.JellyContext;
   import org.apache.commons.jelly.Script;
   import org.apache.commons.jelly.Tag;
   import org.apache.commons.jelly.TagLibrary;
  -import org.apache.commons.jelly.impl.ScriptBlock;
  +import org.apache.commons.jelly.impl.CompositeTextScriptBlock;
  +import org.apache.commons.jelly.impl.ExpressionScript;
   import org.apache.commons.jelly.impl.StaticTag;
   import org.apache.commons.jelly.impl.DynaTagScript;
  +import org.apache.commons.jelly.impl.ScriptBlock;
   import org.apache.commons.jelly.impl.TagScript;
   import org.apache.commons.jelly.impl.TextScript;
   import org.apache.commons.jelly.expression.CompositeExpression;
  @@ -92,10 +98,13 @@
   import org.apache.commons.jelly.expression.Expression;
   import org.apache.commons.jelly.expression.ExpressionFactory;
   import org.apache.commons.jelly.expression.jexl.JexlExpressionFactory;
  +
   import org.apache.commons.jelly.tags.core.CoreTagLibrary;
   import org.apache.commons.jelly.tags.xml.XMLTagLibrary;
  +
   import org.apache.commons.logging.Log;
   import org.apache.commons.logging.LogFactory;
  +
   import org.xml.sax.Attributes;
   import org.xml.sax.ContentHandler;
   import org.xml.sax.EntityResolver;
  @@ -580,7 +589,7 @@
                   parentTag = tag;                
                   
                   if (textBuffer.length() > 0) {
  -                    script.addScript(new TextScript(textBuffer.toString()));
  +                    addTextScript(textBuffer.toString());
                       textBuffer.setLength(0);
                   }
                   script.addScript(tagScript);
  @@ -613,7 +622,7 @@
               throw new SAXException( "Runtime Exception: " + e, e );            
           }
       }
  -
  +    
       /**
        * Process notification of character data received from the body of
        * an XML element.
  @@ -643,25 +652,34 @@
        */
       public void endElement(String namespaceURI, String localName, String qName)
           throws SAXException {
  -        tagScript = (TagScript) tagScriptStack.remove(tagScriptStack.size() - 1);
  -        if (tagScript != null) {
  -            if (textBuffer.length() > 0) {
  -                script.addScript(new TextScript(textBuffer.toString()));
  -                textBuffer.setLength(0);
  -            }
  -            script = (ScriptBlock) scriptStack.pop();
  +        try {            
  +	        tagScript = (TagScript) tagScriptStack.remove(tagScriptStack.size() - 1);
  +	        if (tagScript != null) {
  +	            if (textBuffer.length() > 0) {
  +	                addTextScript(textBuffer.toString());
  +	                textBuffer.setLength(0);
  +	            }
  +	            script = (ScriptBlock) scriptStack.pop();
  +	        }
  +	        else {
  +	            textBuffer.append("</");
  +	            textBuffer.append(qName);
  +	            textBuffer.append(">");
  +	        }
  +	        int size = tagStack.size();
  +	        if ( size <= 0 ) {
  +	            parentTag = null;
  +	        }
  +	        else {
  +	            parentTag = (Tag) tagStack.remove( size - 1 );
  +	        }
           }
  -        else {
  -            textBuffer.append("</");
  -            textBuffer.append(qName);
  -            textBuffer.append(">");
  -        }
  -        int size = tagStack.size();
  -        if ( size <= 0 ) {
  -            parentTag = null;
  +        catch (SAXException e) {
  +            throw e;
           }
  -        else {
  -            parentTag = (Tag) tagStack.remove( size - 1 );
  +        catch (Exception e) {
  +            log.error( "Caught exception: " + e, e );
  +            throw new SAXException( "Runtime Exception: " + e, e );            
           }
       }
   
  @@ -1003,6 +1021,48 @@
               throw createSAXException(e);
           }
       }
  +    
  +    /**
  +     * Adds the text to the current script block parsing any embedded
  +     * expressions inot ExpressionScript objects.
  +     */
  +    protected void addTextScript(String text) throws Exception {
  +        Expression expression =
  +            CompositeExpression.parse(text, getExpressionFactory());
  +
  +        addExpressionScript(script, expression);
  +    }
  +
  +
  +    /**
  +     * Adds the given Expression object to the current Script.
  +     */
  +    protected void addExpressionScript(ScriptBlock script, Expression expression) {
  +        if ( expression instanceof ConstantExpression ) {
  +            ConstantExpression constantExpression 
  +                = (ConstantExpression) expression;
  +            Object value = constantExpression.getValue();
  +            if ( value != null ) {
  +                script.addScript(new TextScript( value.toString() ));
  +            }
  +        }
  +        else 
  +        if ( expression instanceof CompositeExpression ) {
  +            CompositeTextScriptBlock newBlock = new CompositeTextScriptBlock();
  +            script.addScript(newBlock);
  +            
  +            CompositeExpression compositeExpression 
  +                = (CompositeExpression) expression;
  +            Iterator iter = compositeExpression.getExpressions().iterator();
  +            while (iter.hasNext()) {
  +                addExpressionScript( newBlock, (Expression) iter.next() );
  +            }
  +        }
  +        else {
  +            script.addScript(new ExpressionScript(expression));
  +        }
  +    }
  +
   
       protected Expression createConstantExpression(
           String tagName,
  
  
  
  1.1                  jakarta-commons-sandbox/jelly/src/test/org/apache/commons/jelly/TestTrim.java
  
  Index: TestTrim.java
  ===================================================================
  /*
   * $Header: /home/cvs/jakarta-commons-sandbox/jelly/src/test/org/apache/commons/jelly/TestTrim.java,v
1.9 2002/06/07 09:36:11 jstrachan Exp $
   * $Revision: 1.9 $
   * $Date: 2002/06/07 09:36:11 $
   *
   * ====================================================================
   *
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 1999-2002 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution, if
   *    any, must include the following acknowlegement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowlegement may appear in the software itself,
   *    if and wherever such third-party acknowlegements normally appear.
   *
   * 4. The names "The Jakarta Project", "Commons", and "Apache Software
   *    Foundation" must not be used to endorse or promote products derived
   *    from this software without prior written permission. For written
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Group.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   * 
   * $Id: TestTrim.java,v 1.9 2002/06/07 09:36:11 jstrachan Exp $
   */
  package org.apache.commons.jelly;
  
  import junit.framework.Test;
  import junit.framework.TestCase;
  import junit.framework.TestSuite;
  import junit.textui.TestRunner;
  
  import org.apache.commons.jelly.impl.TextScript;
  
  /** 
   * Tests the whitespace triming of scripts.
   *
   * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
   * @version $Revision: 1.9 $
   */
  public class TestTrim extends TestCase {
  
      public static void main(String[] args) {
          TestRunner.run(suite());
      }
  
      public static Test suite() {
          return new TestSuite(TestTrim.class);
      }
  
      public TestTrim(String testName) {
          super(testName);
      }
  
      public void testTrim() throws Exception {
          TextScript script = new TextScript( "   foo    " );
          script.trimWhitespace();
          
          assertEquals( "foo", script.getText() );
  
          script = new TextScript( " foo " );
          script.trimWhitespace();
          
          assertEquals( "foo", script.getText() );
          
          script = new TextScript( "foo" );
          script.trimWhitespace();
          
          assertEquals( "foo", script.getText() );
      }
      
      public void testTrimStart() throws Exception {
          TextScript script = new TextScript( "   foo    " );
          script.trimStartWhitespace();
          
          assertEquals( "foo    ", script.getText() );
  
          script = new TextScript( " foo " );
          script.trimStartWhitespace();
          
          assertEquals( "foo ", script.getText() );
          
          script = new TextScript( "foo" );
          script.trimStartWhitespace();
          
          assertEquals( "foo", script.getText() );
      }
      
      public void testTrimEnd() throws Exception {
          TextScript script = new TextScript( "   foo    " );
          script.trimEndWhitespace();
          
          assertEquals( "   foo", script.getText() );
  
          script = new TextScript( " foo " );
          script.trimEndWhitespace();
          
          assertEquals( " foo", script.getText() );
          
          script = new TextScript( "foo" );
          script.trimEndWhitespace();
          
          assertEquals( "foo", script.getText() );
      }
  }
  
  
  

--
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