View Javadoc
1 /* 2 * $Header: /home/cvs/jakarta-commons-sandbox/jelly/src/taglibs/beanshell/src/java/org/apache/commons/jelly/tags/beanshell/BeanShellExpressionFactory.java,v 1.1 2002/05/21 07:58:55 jstrachan Exp $ 3 * $Revision: 1.1 $ 4 * $Date: 2002/05/21 07:58:55 $ 5 * 6 * ==================================================================== 7 * 8 * The Apache Software License, Version 1.1 9 * 10 * Copyright (c) 1999-2002 The Apache Software Foundation. All rights 11 * reserved. 12 * 13 * Redistribution and use in source and binary forms, with or without 14 * modification, are permitted provided that the following conditions 15 * are met: 16 * 17 * 1. Redistributions of source code must retain the above copyright 18 * notice, this list of conditions and the following disclaimer. 19 * 20 * 2. Redistributions in binary form must reproduce the above copyright 21 * notice, this list of conditions and the following disclaimer in 22 * the documentation and/or other materials provided with the 23 * distribution. 24 * 25 * 3. The end-user documentation included with the redistribution, if 26 * any, must include the following acknowlegement: 27 * "This product includes software developed by the 28 * Apache Software Foundation (http://www.apache.org/)." 29 * Alternately, this acknowlegement may appear in the software itself, 30 * if and wherever such third-party acknowlegements normally appear. 31 * 32 * 4. The names "The Jakarta Project", "Commons", and "Apache Software 33 * Foundation" must not be used to endorse or promote products derived 34 * from this software without prior written permission. For written 35 * permission, please contact apache@apache.org. 36 * 37 * 5. Products derived from this software may not be called "Apache" 38 * nor may "Apache" appear in their names without prior written 39 * permission of the Apache Group. 40 * 41 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED 42 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 43 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 44 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR 45 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 46 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 47 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 48 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 49 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 50 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 51 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 52 * SUCH DAMAGE. 53 * ==================================================================== 54 * 55 * This software consists of voluntary contributions made by many 56 * individuals on behalf of the Apache Software Foundation. For more 57 * information on the Apache Software Foundation, please see 58 * <http://www.apache.org/>;. 59 * 60 * $Id: BeanShellExpressionFactory.java,v 1.1 2002/05/21 07:58:55 jstrachan Exp $ 61 */ 62 63 package org.apache.commons.jelly.tags.define; 64 65 import java.lang.reflect.Method; 66 import java.util.HashMap; 67 import java.util.Map; 68 69 import org.apache.commons.beanutils.DynaClass; 70 71 import org.apache.commons.jelly.JellyException; 72 import org.apache.commons.jelly.MissingAttributeException; 73 import org.apache.commons.jelly.Tag; 74 import org.apache.commons.jelly.XMLOutput; 75 import org.apache.commons.jelly.impl.Attribute; 76 import org.apache.commons.jelly.impl.DynamicDynaBeanTag; 77 import org.apache.commons.jelly.impl.TagFactory; 78 79 import org.apache.commons.logging.Log; 80 import org.apache.commons.logging.LogFactory; 81 82 83 /*** 84 * Binds a Java bean to the given named Jelly tag so that the attributes of 85 * the tag set the bean properties.. 86 * 87 * @author <a href="mailto:jstrachan@apache.org">James Strachan</a> 88 * @version $Revision: 1.1 $ 89 */ 90 public class DynaBeanTag extends DefineTagSupport { 91 92 /*** The Log to which logging calls will be made. */ 93 private static final Log log = LogFactory.getLog(DynaBeanTag.class); 94 95 /*** An empty Map as I think Collections.EMPTY_MAP is only JDK 1.3 onwards */ 96 private static final Map EMPTY_MAP = new HashMap(); 97 98 /*** the name of the tag to create */ 99 private String name; 100 101 /*** the DyanClass to bind to the tag */ 102 private DynaClass dynaClass; 103 104 /*** the name of the attribute used for the variable name */ 105 private String varAttribute = "var"; 106 107 /*** the attribute definitions for this dynamic tag */ 108 private Map attributes; 109 110 /*** 111 * Adds a new attribute definition to this dynamic tag 112 */ 113 public void addAttribute(Attribute attribute) { 114 if ( attributes == null ) { 115 attributes = new HashMap(); 116 } 117 attributes.put( attribute.getName(), attribute ); 118 } 119 120 // Tag interface 121 //------------------------------------------------------------------------- 122 public void doTag(XMLOutput output) throws Exception { 123 invokeBody(output); 124 125 if (name == null) { 126 throw new MissingAttributeException("name"); 127 } 128 if (dynaClass == null) { 129 throw new MissingAttributeException("dynaClass"); 130 } 131 132 final DynaClass theDynaClass = dynaClass; 133 final Map beanAttributes = (attributes != null) ? attributes : EMPTY_MAP; 134 135 TagFactory factory = new TagFactory() { 136 public Tag createTag() { 137 return new DynamicDynaBeanTag(theDynaClass, beanAttributes, varAttribute); 138 } 139 }; 140 getTagLibrary().registerBeanTag(name, factory); 141 142 // now lets clear the attributes for next invocation and help the GC 143 attributes = null; 144 } 145 146 147 // Properties 148 //------------------------------------------------------------------------- 149 150 /*** 151 * Sets the name of the tag to create 152 */ 153 public void setName(String name) { 154 this.name = name; 155 } 156 157 /*** 158 * Sets the name of the attribute used to define the bean variable that this dynamic 159 * tag will output its results as. This defaults to 'var' though this property 160 * can be used to change this if it conflicts with a bean property called 'var'. 161 */ 162 public void setVarAttribute(String varAttribute) { 163 this.varAttribute = varAttribute; 164 } 165 166 /*** 167 * Returns the dynaClass. 168 * @return DynaClass 169 */ 170 public DynaClass getDynaClass() { 171 return dynaClass; 172 } 173 174 /*** 175 * Sets the {@link DynaClass} which will be bound to this dynamic tag. 176 */ 177 public void setDynaClass(DynaClass dynaClass) { 178 this.dynaClass = dynaClass; 179 } 180 181 }

This page was automatically generated by Maven