You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by js...@apache.org on 2002/10/03 09:52:37 UTC

cvs commit: jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly JellyContext.java

jstrachan    2002/10/03 00:52:37

  Modified:    jelly/src/test/org/apache/commons/jelly suite.jelly
               jelly/src/java/org/apache/commons/jelly/tags/core
                        CoreTagLibrary.java
               jelly/src/java/org/apache/commons/jelly JellyContext.java
  Added:       jelly/src/java/org/apache/commons/jelly/tags/core
                        ScopeTag.java RemoveTag.java
  Log:
  Added a <j:remove var="foo"/> tag to remove variables as well as a <j:scope> tag which creates a new variable scope for its body.
  So the following can be performed...
  
  <jelly>
   
      <scope>
          <set var="var1" />
          <useBean var="var2" />
      </scope>
   
      <!-- var1 and var2 no longer exist -->
   
  </jelly>
  
  Many thanks to Jason Horman for the idea.
  
  Revision  Changes    Path
  1.6       +77 -51    jakarta-commons-sandbox/jelly/src/test/org/apache/commons/jelly/suite.jelly
  
  Index: suite.jelly
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/jelly/src/test/org/apache/commons/jelly/suite.jelly,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- suite.jelly	2 Oct 2002 16:55:04 -0000	1.5
  +++ suite.jelly	3 Oct 2002 07:52:37 -0000	1.6
  @@ -4,65 +4,91 @@
   	xmlns:test="jelly:junit" 
   	xmlns:log="jelly:log">
   
  -  <test:case name="testChoose">
  +  <test:case name="testSetAndRemove">
  +	
  +		<j:set var="x" value="abc"/>
  +		
  +		<test:assertEquals expected="abc" actual="${x}"/>
  +		
  +		<j:remove var="x"/>
  +		
  +		<test:assert test="${x == null}"/>
  +		<test:assert test="${empty x}"/>
  +		
  +  </test:case>
   
  -	<j:set var="x" value="abc"/>
  +  <test:case name="testScope">
  +
  +		<j:scope>	
  +			<j:set var="x" value="abc"/>
  +			
  +			<test:assertEquals expected="abc" actual="${x}"/>
  +		</j:scope>			
  +			
  +		<test:assert test="${x == null}"/>
  +		<test:assert test="${empty x}"/>
  +		
  +  </test:case>
  +		
  +  <test:case name="testChoose">
   	
  -	<j:choose>
  -	  <j:when test="${x == 'abc'}">
  -	    this should always work
  -	  </j:when>
  -	  <j:when test="${x == 'xyz'}">
  -	    <fail>This should never fail</fail>
  -	  </j:when>
  -	  <j:otherwise>
  -	    <fail>This should never fail</fail>
  -	  </j:otherwise>
  -	</j:choose>
  -	  		  
  -	<j:choose>
  -	  <j:when test="${x == 'xyz'}">
  -	    <fail>This should never fail</fail>
  -	  </j:when>
  -	  <j:when test="${x == 'abc'}">
  -	    this should always work
  -	  </j:when>
  -	  <j:otherwise>
  -	    <fail>This should never fail</fail>
  -	  </j:otherwise>
  -	</j:choose>
  -	  		  
  -	  		  
  -	<j:choose>
  -	  <j:when test="${x == 'xyz'}">
  -	    <fail>This should never fail</fail>
  -	  </j:when>
  -	  <j:when test="${x == 'zzz'}">
  -	    <fail>This should never fail</fail>
  -	  </j:when>
  -	  <j:otherwise>
  -	    this should always work
  -	  </j:otherwise>
  -	</j:choose>
  +		<j:set var="x" value="abc"/>
  +		
  +		<j:choose>
  +		  <j:when test="${x == 'abc'}">
  +		    this should always work
  +		  </j:when>
  +		  <j:when test="${x == 'xyz'}">
  +		    <fail>This should never fail</fail>
  +		  </j:when>
  +		  <j:otherwise>
  +		    <fail>This should never fail</fail>
  +		  </j:otherwise>
  +		</j:choose>
  +		  		  
  +		<j:choose>
  +		  <j:when test="${x == 'xyz'}">
  +		    <fail>This should never fail</fail>
  +		  </j:when>
  +		  <j:when test="${x == 'abc'}">
  +		    this should always work
  +		  </j:when>
  +		  <j:otherwise>
  +		    <fail>This should never fail</fail>
  +		  </j:otherwise>
  +		</j:choose>
  +		  		  
  +		  		  
  +		<j:choose>
  +		  <j:when test="${x == 'xyz'}">
  +		    <fail>This should never fail</fail>
  +		  </j:when>
  +		  <j:when test="${x == 'zzz'}">
  +		    <fail>This should never fail</fail>
  +		  </j:when>
  +		  <j:otherwise>
  +		    this should always work
  +		  </j:otherwise>
  +		</j:choose>
   	  		  
     </test:case>
   
   
  -  <test:case name="testIf">
  -
  -	<j:set var="x" value="abc"/>
  -	<j:set var="worked" value="f"/>
  -	
  -	<j:if test="${x == 'abc'}">
  -	  this should always work
  -	  <j:set var="worked" value="t"/>
  -	</j:if>
  +	<test:case name="testIf">
   	
  -	<test:assertEquals expected="t" actual="${worked}"/>
  +		<j:set var="x" value="abc"/>
  +		<j:set var="worked" value="f"/>
  +		
  +		<j:if test="${x == 'abc'}">
  +		  this should always work
  +		  <j:set var="worked" value="t"/>
  +		</j:if>
  +		
  +		<test:assertEquals expected="t" actual="${worked}"/>
   	
  -	<j:if test="${x == 'zzz'}">
  -	  <fail>This should never fail</fail>
  -	</j:if>
  +		<j:if test="${x == 'zzz'}">
  +		  <fail>This should never fail</fail>
  +		</j:if>
   	  		  
     </test:case>
   
  
  
  
  1.18      +9 -5      jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/tags/core/CoreTagLibrary.java
  
  Index: CoreTagLibrary.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/tags/core/CoreTagLibrary.java,v
  retrieving revision 1.17
  retrieving revision 1.18
  diff -u -r1.17 -r1.18
  --- CoreTagLibrary.java	30 Sep 2002 17:40:16 -0000	1.17
  +++ CoreTagLibrary.java	3 Oct 2002 07:52:37 -0000	1.18
  @@ -87,11 +87,14 @@
           registerTag("catch", CatchTag.class);
           registerTag("forEach", ForEachTag.class);
           registerTag("set", SetTag.class);
  +        registerTag("remove", RemoveTag.class);
  +        
           // conditional tags
           registerTag("if", IfTag.class);
           registerTag("choose", ChooseTag.class);
           registerTag("when", WhenTag.class);
           registerTag("otherwise", OtherwiseTag.class);
  +        
           // other tags
           registerTag("include", IncludeTag.class);
           registerTag("import", ImportTag.class);
  @@ -105,6 +108,7 @@
           registerTag("whitespace", WhitespaceTag.class);
           registerTag("thread", ThreadTag.class);
           registerTag("file", FileTag.class);
  +        registerTag("scope", ScopeTag.class);
       }
       
   }
  
  
  
  1.1                  jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/tags/core/ScopeTag.java
  
  Index: ScopeTag.java
  ===================================================================
  /*
   * $Header: /home/cvs/jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/tags/core/ScopeTag.java,v 1.5 2002/05/17 15:18:08 jstrachan Exp $
   * $Revision: 1.5 $
   * $Date: 2002/05/17 15:18:08 $
   *
   * ====================================================================
   *
   * 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: ScopeTag.java,v 1.5 2002/05/17 15:18:08 jstrachan Exp $
   */
  package org.apache.commons.jelly.tags.core;
  
  import org.apache.commons.jelly.JellyContext;
  import org.apache.commons.jelly.TagSupport;
  import org.apache.commons.jelly.XMLOutput;
  
  /** 
   * A tag which creates a new child variable scope for its body.
   * So any variables defined within its body will no longer be in scope
   * after this tag.
   *
   * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
   * @version $Revision: 1.5 $
   */
  public class ScopeTag extends TagSupport {
  
      // Tag interface
      //------------------------------------------------------------------------- 
      public void doTag(XMLOutput output) throws Exception {
          JellyContext newContext = context.newJellyContext();
          getBody().run(newContext, output);
      }
  }
  
  
  
  1.1                  jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/tags/core/RemoveTag.java
  
  Index: RemoveTag.java
  ===================================================================
  /*
   * $Header: /home/cvs/jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/tags/core/RemoveTag.java,v 1.5 2002/05/17 15:18:08 jstrachan Exp $
   * $Revision: 1.5 $
   * $Date: 2002/05/17 15:18:08 $
   *
   * ====================================================================
   *
   * 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: RemoveTag.java,v 1.5 2002/05/17 15:18:08 jstrachan Exp $
   */
  package org.apache.commons.jelly.tags.core;
  
  import org.apache.commons.jelly.MissingAttributeException;
  import org.apache.commons.jelly.TagSupport;
  import org.apache.commons.jelly.XMLOutput;
  
  /** 
   * A tag which removes the variable of the given name from the current variable scope.
   *
   * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
   * @version $Revision: 1.5 $
   */
  public class RemoveTag extends TagSupport {
  
      private String var;
  
      public RemoveTag() {
      }
  
      // Tag interface
      //------------------------------------------------------------------------- 
      public void doTag(XMLOutput output) throws Exception {
          if (var != null) {
              context.removeVariable(var);
          }
          else {
              throw new MissingAttributeException("var");
          }
      }
  
      // Properties
      //------------------------------------------------------------------------- 
      
      /**
       * Sets the name of the variable which will be removed by this tag..
       */
      public void setVar(String var) {
          this.var = var;
      }
  }
  
  
  
  1.29      +8 -0      jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/JellyContext.java
  
  Index: JellyContext.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/JellyContext.java,v
  retrieving revision 1.28
  retrieving revision 1.29
  diff -u -r1.28 -r1.29
  --- JellyContext.java	27 Sep 2002 14:20:13 -0000	1.28
  +++ JellyContext.java	3 Oct 2002 07:52:37 -0000	1.29
  @@ -325,6 +325,14 @@
           return answer;
       }
   
  +    /**
  +     * A factory method to create a new child context of the
  +     * current context.
  +     */
  +    public JellyContext newJellyContext() {
  +        return createChildContext();
  +    }
  +
       /** Registers the given tag library against the given namespace URI.
        * This should be called before the parser is used.
        */
  
  
  

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>