jstrachan 2003/03/03 11:09:04 Modified: jelly/jelly-tags/bean/src/test/org/apache/commons/jelly/tags/bean MyBeanTagLibrary.java suite.jelly jelly/jelly-tags/bean/src/java/org/apache/commons/jelly/tags/bean BeanTag.java Added: jelly/jelly-tags/bean/src/test/org/apache/commons/jelly/tags/bean MyContainerTag.java Log: Refactored the code so that the behaviour is more consistent when used in deeply nested scripts. Also added a demo/JellyUnit tests cas showing how to use CollectionTag interface for processing beans generically Revision Changes Path 1.2 +5 -4 jakarta-commons/jelly/jelly-tags/bean/src/test/org/apache/commons/jelly/tags/bean/MyBeanTagLibrary.java Index: MyBeanTagLibrary.java =================================================================== RCS file: /home/cvs/jakarta-commons/jelly/jelly-tags/bean/src/test/org/apache/commons/jelly/tags/bean/MyBeanTagLibrary.java,v retrieving revision 1.1 retrieving revision 1.2 diff -u -r1.1 -r1.2 --- MyBeanTagLibrary.java 21 Jan 2003 15:16:32 -0000 1.1 +++ MyBeanTagLibrary.java 3 Mar 2003 19:09:04 -0000 1.2 @@ -1,7 +1,7 @@ /* - * $Header$ - * $Revision$ - * $Date$ + * /home/cvs/jakarta-commons/jelly/jelly-tags/bean/src/test/org/apache/commons/jelly/tags/bean/MyBeanTagLibrary.java,v 1.1 2003/01/21 15:16:32 jstrachan Exp + * 1.1 + * 2003/01/21 15:16:32 * * ==================================================================== * @@ -57,7 +57,7 @@ * information on the Apache Software Foundation, please see * <http://www.apache.org/>. * - * $Id$ + * MyBeanTagLibrary.java,v 1.1 2003/01/21 15:16:32 jstrachan Exp */ package org.apache.commons.jelly.tags.bean; @@ -76,5 +76,6 @@ public MyBeanTagLibrary() { registerBean( "customer", Customer.class ); + registerTag( "myContainer", MyContainerTag.class ); } } 1.4 +45 -0 jakarta-commons/jelly/jelly-tags/bean/src/test/org/apache/commons/jelly/tags/bean/suite.jelly Index: suite.jelly =================================================================== RCS file: /home/cvs/jakarta-commons/jelly/jelly-tags/bean/src/test/org/apache/commons/jelly/tags/bean/suite.jelly,v retrieving revision 1.3 retrieving revision 1.4 diff -u -r1.3 -r1.4 --- suite.jelly 25 Feb 2003 22:54:23 -0000 1.3 +++ suite.jelly 3 Mar 2003 19:09:04 -0000 1.4 @@ -203,5 +203,50 @@ <test:assertTrue test="${size(foo.customers) == 2}"/> </test:case> + + <test:case name="testCollectionTag" xmlns="jelly:org.apache.commons.jelly.tags.bean.MyBeanTagLibrary"> + + <myContainer var="list"> + <customer name="James" location="London"> + <order amount="100" price="2.99"> + <product id="p1" name="Beer"/> + </order> + <order amount="200" price="4.99"> + <product id="p2" name="Pizza"/> + </order> + </customer> + <customer name="Bob" location="Atlanta"> + <order amount="200" price="2.99"> + <product id="p1" name="Beer"/> + </order> + </customer> + </myContainer> + + <test:assertTrue test="${size(list) == 2}"/> + + Created list: ${list} + + <test:assertEquals expected="James" actual="${list[0].name}"/> + <test:assertEquals expected="London" actual="${list[0].location}"/> + + <test:assertTrue test="${list[0].orders[0].amount == 100}"/> + <test:assertTrue test="${list[0].orders[0].price == 2.99}"/> + + <test:assertTrue test="${list[0].orders[1].amount == 200}"/> + <test:assertTrue test="${list[0].orders[1].price == 4.99}"/> + + <test:assertTrue test="${list[0].orders[0].product != null}"/> + <test:assertTrue test="${list[0].orders[1].product != null}"/> + + <test:assertEquals expected="p1" actual="${list[0].orders[0].product.id}"/> + <test:assertEquals expected="Beer" actual="${list[0].orders[0].product.name}"/> + + <test:assertEquals expected="p2" actual="${list[0].orders[1].product.id}"/> + <test:assertEquals expected="Pizza" actual="${list[0].orders[1].product.name}"/> + + + <test:assertEquals expected="Bob" actual="${list[1].name}"/> + <test:assertEquals expected="Atlanta" actual="${list[1].location}"/> + </test:case> </test:suite> 1.1 jakarta-commons/jelly/jelly-tags/bean/src/test/org/apache/commons/jelly/tags/bean/MyContainerTag.java Index: MyContainerTag.java =================================================================== /* * /home/cvs/jakarta-commons/jelly/jelly-tags/bean/src/test/org/apache/commons/jelly/tags/bean/CustomerTag.java,v 1.1 2003/01/21 15:16:32 jstrachan Exp * 1.1 * 2003/01/21 15:16:32 * * ==================================================================== * * 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/>. * * CustomerTag.java,v 1.1 2003/01/21 15:16:32 jstrachan Exp */ package org.apache.commons.jelly.tags.bean; import java.util.ArrayList; import java.util.List; import org.apache.commons.jelly.JellyTagException; import org.apache.commons.jelly.TagSupport; import org.apache.commons.jelly.XMLOutput; import org.apache.commons.jelly.impl.CollectionTag; /** * A simple tag which demonstrates how to process beans generically. * * @author <a href="mailto:[EMAIL PROTECTED]">James Strachan</a> * @version $Revision: 1.1 $ */ public class MyContainerTag extends TagSupport implements CollectionTag { private List list = new ArrayList(); private String var; public MyContainerTag() { } // Tag interface //------------------------------------------------------------------------- public void doTag(XMLOutput output) throws JellyTagException { invokeBody(output); context.setVariable(var, list); list = new ArrayList(); } // CollectionTag interface //------------------------------------------------------------------------- public void addItem(Object value) { list.add(value); } // Properties //------------------------------------------------------------------------- /** * @return String */ public String getVar() { return var; } /** * Sets the var. * @param var The var to set */ public void setVar(String var) { this.var = var; } } 1.7 +54 -36 jakarta-commons/jelly/jelly-tags/bean/src/java/org/apache/commons/jelly/tags/bean/BeanTag.java Index: BeanTag.java =================================================================== RCS file: /home/cvs/jakarta-commons/jelly/jelly-tags/bean/src/java/org/apache/commons/jelly/tags/bean/BeanTag.java,v retrieving revision 1.6 retrieving revision 1.7 diff -u -r1.6 -r1.7 --- BeanTag.java 25 Feb 2003 22:54:22 -0000 1.6 +++ BeanTag.java 3 Mar 2003 19:09:04 -0000 1.7 @@ -69,6 +69,7 @@ import org.apache.commons.beanutils.BeanUtils; import org.apache.commons.beanutils.MethodUtils; import org.apache.commons.jelly.JellyTagException; +import org.apache.commons.jelly.Tag; import org.apache.commons.jelly.impl.BeanSource; import org.apache.commons.jelly.impl.CollectionTag; import org.apache.commons.jelly.tags.core.UseBeanTag; @@ -135,47 +136,59 @@ // now lets try set the parent property via calling the adder or the setter method if (bean != null) { - Object parentObject = getParentObject(); - if (parentObject != null) { - if (parentObject instanceof Collection) { - Collection collection = (Collection) parentObject; - collection.add(bean); + Tag parent = this; + + while (true) { + parent = parent.getParent(); + if (parent == null) { + break; } - else { - // lets see if there's a setter method... - Method method = findAddMethod(parentObject.getClass(), bean.getClass()); - if (method != null) { - Object[] args = { bean }; - try { - method.invoke(parentObject, args); + + if (parent instanceof BeanSource) { + BeanSource source = (BeanSource) parent; + Object parentObject = source.getBean(); + if (parentObject != null) { + if (parentObject instanceof Collection) { + Collection collection = (Collection) parentObject; + collection.add(bean); } - catch (Exception e) { - throw new JellyTagException( "failed to invoke method: " + method + " on bean: " + parentObject + " reason: " + e, e ); + else { + // lets see if there's a setter method... + Method method = findAddMethod(parentObject.getClass(), bean.getClass()); + if (method != null) { + Object[] args = { bean }; + try { + method.invoke(parentObject, args); + } + catch (Exception e) { + throw new JellyTagException( "failed to invoke method: " + method + " on bean: " + parentObject + " reason: " + e, e ); + } + } + else { + try { + BeanUtils.setProperty(parentObject, tagName, bean); + } catch (IllegalAccessException e) { + throw new JellyTagException(e); + } catch (InvocationTargetException e) { + throw new JellyTagException(e); + } + } } } else { - try { - BeanUtils.setProperty(parentObject, tagName, bean); - } catch (IllegalAccessException e) { - throw new JellyTagException(e); - } catch (InvocationTargetException e) { - throw new JellyTagException(e); - } + log.warn("Cannot process null bean for tag: " + parent); } } - - } - else { - // lets try find a parent List to add this bean to - CollectionTag tag = (CollectionTag) findAncestorWithClass(CollectionTag.class); - if (tag != null) { + else if (parent instanceof CollectionTag) { + CollectionTag tag = (CollectionTag) parent; tag.addItem(bean); } - else if(var == null) { //warn if the bean gets lost in space - log.warn( "Could not add bean to parent for bean: " + bean ); + else { + continue; } + break; } - + if (invokeMethod != null) { Object[] args = { bean }; try { @@ -185,8 +198,13 @@ throw new JellyTagException( "failed to invoke method: " + invokeMethod + " on bean: " + bean + " reason: " + e, e ); } } + else { + if (parent == null && var == null) { + //warn if the bean gets lost in space + log.warn( "Could not add bean to parent for bean: " + bean ); + } + } } - } /**
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]