rwaldhoff    2002/11/29 07:53:10

  Modified:    jelly/src/java/org/apache/commons/jelly/tags/core
                        CoreTagLibrary.java
  Added:       jelly/src/java/org/apache/commons/jelly/tags/core
                        InvokeTag.java
               jelly/src/test/org/apache/commons/jelly/core
                        TestInvokeTag.java testInvokeTag.jelly
  Log:
  add invoke tag and tests
  
  Revision  Changes    Path
  1.24      +10 -9     
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.23
  retrieving revision 1.24
  diff -u -r1.23 -r1.24
  --- CoreTagLibrary.java       28 Nov 2002 00:22:23 -0000      1.23
  +++ CoreTagLibrary.java       29 Nov 2002 15:53:10 -0000      1.24
  @@ -104,17 +104,18 @@
           registerTag("import", ImportTag.class);
           
           // extensions to JSTL
  +        registerTag("arg", ArgTag.class);
           registerTag("break", BreakTag.class);
           registerTag("expr", ExprTag.class);
  +        registerTag("file", FileTag.class);
  +        registerTag("invoke", InvokeTag.class);
           registerTag("new", NewTag.class);
  -        registerTag("arg", ArgTag.class);
  +        registerTag("scope", ScopeTag.class);
           registerTag("setProperties", SetPropertiesTag.class);
  +        registerTag("thread", ThreadTag.class);
           registerTag("useBean", UseBeanTag.class);
           registerTag("useList", UseListTag.class);
           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/InvokeTag.java
  
  Index: InvokeTag.java
  ===================================================================
  /*
   * $Header: 
/home/cvs/jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/tags/core/InvokeTag.java,v
 1.1 2002/11/29 15:53:10 rwaldhoff Exp $
   * $Revision: 1.1 $
   * $Date: 2002/11/29 15:53:10 $
   *
   * ====================================================================
   *
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 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 [EMAIL PROTECTED]
   *
   * 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: InvokeTag.java,v 1.1 2002/11/29 15:53:10 rwaldhoff Exp $
   */
  package org.apache.commons.jelly.tags.core;
  
  import java.util.ArrayList;
  import java.util.List;
  
  import org.apache.commons.beanutils.MethodUtils;
  import org.apache.commons.jelly.MissingAttributeException;
  import org.apache.commons.jelly.TagSupport;
  import org.apache.commons.jelly.XMLOutput;
  
  /** A tag which creates a new object of the given type
    *
    * @author Rodney Waldhoff
    * @version $Revision: 1.1 $
    */
  public class InvokeTag extends TagSupport implements ArgTagParent {
  
      /** the variable exported */
      private String var;
      
      /** the method to invoke */
      private String methodName;
      
      /** the object to invoke the method on */
      private Object onInstance;
      
      private List paramTypes = new ArrayList();
      private List paramValues = new ArrayList();
      
      public InvokeTag() {
      }
  
      /** Sets the name of the variable exported by this tag */
      public void setVar(String var) {
          this.var = var;
      }
      
      public void setMethod(String method) {
          this.methodName = method;
      }
      
      public void setOn(Object instance) {
          this.onInstance = instance;
      }
      
      public void addArgument(Class type, Object value) {
          paramTypes.add(type);
          paramValues.add(value);
      }
      
      // Tag interface
      //------------------------------------------------------------------------- 
      public void doTag(XMLOutput output) throws Exception {
          if ( null == methodName) {
              throw new MissingAttributeException( "method" );
          }
          if ( null == onInstance ) {
              throw new MissingAttributeException( "on" );
          }
          
          invokeBody(output);
          
          Object[] values = paramValues.toArray();
          Class[] types = (Class[])(paramTypes.toArray(new Class[paramTypes.size()]));
          Object result = MethodUtils.invokeMethod(onInstance,methodName,values,types);
          paramTypes.clear();
          paramValues.clear();
          
          ArgTag parentArg = (ArgTag)(findAncestorWithClass(ArgTag.class));
          if(null != parentArg) {
              parentArg.setValueObject(result);
          }
          if(null != var) {
              context.setVariable(var, result);
          }
      }
  }
  
  
  
  1.1                  
jakarta-commons-sandbox/jelly/src/test/org/apache/commons/jelly/core/TestInvokeTag.java
  
  Index: TestInvokeTag.java
  ===================================================================
  /*
   * $Header: 
/home/cvs/jakarta-commons-sandbox/jelly/src/test/org/apache/commons/jelly/core/TestInvokeTag.java,v
 1.1 2002/11/29 15:53:10 rwaldhoff Exp $
   * $Revision: 1.1 $
   * $Date: 2002/11/29 15:53:10 $
   *
   * ====================================================================
   *
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 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 [EMAIL PROTECTED]
   *
   * 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: TestInvokeTag.java,v 1.1 2002/11/29 15:53:10 rwaldhoff Exp $
   */
  package org.apache.commons.jelly.core;
  
  import junit.framework.TestSuite;
  
  import org.apache.commons.jelly.Script;
  import org.apache.commons.jelly.bean.Customer;
  
  /**
   * @author Rodney Waldhoff
   * @version $Revision: 1.1 $ $Date: 2002/11/29 15:53:10 $
   */
  public class TestInvokeTag extends BaseJellyTest {
  
      public TestInvokeTag(String name) {
          super(name);
      }
  
      public static TestSuite suite() throws Exception {
          return new TestSuite(TestInvokeTag.class);        
      }
  
      public void setUp() throws Exception {
          super.setUp();
      }    
      
      public void tearDown() throws Exception {
          super.tearDown();
      }    
  
      public void testSimpleInvoke() throws Exception {
          setUpScript("testInvokeTag.jelly");
          Script script = getJelly().compileScript();
          getJellyContext().setVariable("test.simpleInvoke",Boolean.TRUE);
          script.run(getJellyContext(),getXMLOutput());
          assertNotNull(getJellyContext().getVariable("foo"));
          assertTrue(getJellyContext().getVariable("foo") instanceof Customer);
          Customer customer = (Customer)(getJellyContext().getVariable("foo"));
          assertEquals("Jane Doe",customer.getName());
          assertEquals("Chicago",customer.getCity());
          assertNotNull(customer.getOrders());
          assertEquals(1,customer.getOrders().size());
          assertNotNull(customer.getOrders().get(0));
      }
  
      public void testInvokeWithVar() throws Exception {
          setUpScript("testInvokeTag.jelly");
          Script script = getJelly().compileScript();
          getJellyContext().setVariable("test.invokeWithVar",Boolean.TRUE);
          script.run(getJellyContext(),getXMLOutput());
          assertNotNull(getJellyContext().getVariable("size"));
          assertTrue(getJellyContext().getVariable("size") instanceof Integer);
          Integer size = (Integer)(getJellyContext().getVariable("size"));
          assertEquals(3,size.intValue());
      }
  
      public void testInvokeWithReturnedValueAsArg() throws Exception {
          setUpScript("testInvokeTag.jelly");
          Script script = getJelly().compileScript();
          
getJellyContext().setVariable("test.invokeWithReturnedValueAsArg",Boolean.TRUE);
          script.run(getJellyContext(),getXMLOutput());
          assertNotNull(getJellyContext().getVariable("customer"));
          assertTrue(getJellyContext().getVariable("customer") instanceof Customer);
          Customer customer = (Customer)(getJellyContext().getVariable("customer"));
          assertEquals("Jane Doe",customer.getName());
          assertEquals("Chicago",customer.getCity());
      }
  
      public void testInvokeWithReturnedValueAsArgAndVar() throws Exception {
          setUpScript("testInvokeTag.jelly");
          Script script = getJelly().compileScript();
          
getJellyContext().setVariable("test.invokeWithReturnedValueAsArgAndVar",Boolean.TRUE);
          script.run(getJellyContext(),getXMLOutput());
          assertNotNull(getJellyContext().getVariable("customer"));
          assertTrue(getJellyContext().getVariable("customer") instanceof Customer);
          Customer customer = (Customer)(getJellyContext().getVariable("customer"));
          assertEquals("Jane Doe",customer.getName());
          assertEquals("Chicago",customer.getCity());
          assertNotNull(getJellyContext().getVariable("argtwo"));
          assertEquals("Chicago",getJellyContext().getVariable("argtwo"));
      }
  
  }
  
  
  
  1.1                  
jakarta-commons-sandbox/jelly/src/test/org/apache/commons/jelly/core/testInvokeTag.jelly
  
  Index: testInvokeTag.jelly
  ===================================================================
  <j:jelly xmlns:j="jelly:core">
  
      <j:if test="${test.simpleInvoke}">
          <j:set var="namearg" value="Jane Doe"/>
          <j:new var="foo" className="org.apache.commons.jelly.bean.Customer"/>
          <j:invoke method="setName" on="${foo}">
              <j:arg value="${namearg}"/>
          </j:invoke>
          <j:invoke method="setCity" on="${foo}">
              <j:arg value="Chicago"/>
          </j:invoke>
          <j:invoke method="addOrder" on="${foo}">
              <j:arg><j:new className="org.apache.commons.jelly.bean.Order"/></j:arg>
          </j:invoke>
      </j:if>
  
      <j:if test="${test.invokeWithVar}">
          <j:new var="list" className="java.util.ArrayList"/>
          <j:invoke on="${list}" method="add"><j:arg value="One"/></j:invoke>
          <j:invoke on="${list}" method="add"><j:arg value="Two"/></j:invoke>
          <j:invoke on="${list}" method="add"><j:arg value="Three"/></j:invoke>
          <j:invoke on="${list}" method="size" var="size"/>
      </j:if>
  
      <j:if test="${test.invokeWithReturnedValueAsArg}">
          <j:new var="list" className="java.util.ArrayList"/>
          <j:invoke on="${list}" method="add"><j:arg value="Jane Doe"/></j:invoke>
          <j:invoke on="${list}" method="add"><j:arg value="Chicago"/></j:invoke>
          
          <j:new var="customer" className="org.apache.commons.jelly.bean.Customer">
              <j:arg><j:invoke on="${list}" method="get"><j:arg type="int" 
value="0"/></j:invoke></j:arg>
              <j:arg><j:invoke on="${list}" method="get"><j:arg type="int" 
value="1"/></j:invoke></j:arg>
          </j:new>
      </j:if>
  
      <j:if test="${test.invokeWithReturnedValueAsArgAndVar}">
          <j:new var="list" className="java.util.ArrayList"/>
          <j:invoke on="${list}" method="add"><j:arg value="Jane Doe"/></j:invoke>
          <j:invoke on="${list}" method="add"><j:arg value="Chicago"/></j:invoke>
          
          <j:new var="customer" className="org.apache.commons.jelly.bean.Customer">
              <j:arg><j:invoke on="${list}" method="get"><j:arg type="int" 
value="0"/></j:invoke></j:arg>
              <j:arg><j:invoke on="${list}" method="get" var="argtwo"><j:arg 
type="int" value="1"/></j:invoke></j:arg>
          </j:new>
      </j:if>
  
  </j:jelly>
  
  
  

--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to