| %line | %branch | |||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| org.apache.commons.jelly.tags.define.DynaBeanTag |
|
|
| 1 | /* |
|
| 2 | * Copyright 2002,2004 The Apache Software Foundation. |
|
| 3 | * |
|
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
|
| 5 | * you may not use this file except in compliance with the License. |
|
| 6 | * You may obtain a copy of the License at |
|
| 7 | * |
|
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
|
| 9 | * |
|
| 10 | * Unless required by applicable law or agreed to in writing, software |
|
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
|
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
| 13 | * See the License for the specific language governing permissions and |
|
| 14 | * limitations under the License. |
|
| 15 | */ |
|
| 16 | ||
| 17 | package org.apache.commons.jelly.tags.define; |
|
| 18 | ||
| 19 | import java.util.HashMap; |
|
| 20 | import java.util.Map; |
|
| 21 | ||
| 22 | import org.apache.commons.beanutils.DynaClass; |
|
| 23 | import org.apache.commons.jelly.JellyTagException; |
|
| 24 | import org.apache.commons.jelly.MissingAttributeException; |
|
| 25 | import org.apache.commons.jelly.Tag; |
|
| 26 | import org.apache.commons.jelly.XMLOutput; |
|
| 27 | import org.apache.commons.jelly.impl.Attribute; |
|
| 28 | import org.apache.commons.jelly.impl.DynamicDynaBeanTag; |
|
| 29 | import org.apache.commons.jelly.impl.TagFactory; |
|
| 30 | import org.apache.commons.logging.Log; |
|
| 31 | import org.apache.commons.logging.LogFactory; |
|
| 32 | import org.xml.sax.Attributes; |
|
| 33 | ||
| 34 | /** |
|
| 35 | * Binds a Java bean to the given named Jelly tag so that the attributes of |
|
| 36 | * the tag set the bean properties.. |
|
| 37 | * |
|
| 38 | * @author <a href="mailto:jstrachan@apache.org">James Strachan</a> |
|
| 39 | * @version $Revision: 155420 $ |
|
| 40 | */ |
|
| 41 | 6 | public class DynaBeanTag extends DefineTagSupport { |
| 42 | ||
| 43 | /** The Log to which logging calls will be made. */ |
|
| 44 | 18 | private static final Log log = LogFactory.getLog(DynaBeanTag.class); |
| 45 | ||
| 46 | /** An empty Map as I think Collections.EMPTY_MAP is only JDK 1.3 onwards */ |
|
| 47 | 9 | private static final Map EMPTY_MAP = new HashMap(); |
| 48 | ||
| 49 | /** the name of the tag to create */ |
|
| 50 | private String name; |
|
| 51 | ||
| 52 | /** the DyanClass to bind to the tag */ |
|
| 53 | private DynaClass dynaClass; |
|
| 54 | ||
| 55 | /** the name of the attribute used for the variable name */ |
|
| 56 | 3 | private String varAttribute = "var"; |
| 57 | ||
| 58 | /** the attribute definitions for this dynamic tag */ |
|
| 59 | private Map attributes; |
|
| 60 | ||
| 61 | /** |
|
| 62 | * Adds a new attribute definition to this dynamic tag |
|
| 63 | */ |
|
| 64 | public void addAttribute(Attribute attribute) { |
|
| 65 | 0 | if ( attributes == null ) { |
| 66 | 0 | attributes = new HashMap(); |
| 67 | } |
|
| 68 | 0 | attributes.put( attribute.getName(), attribute ); |
| 69 | 0 | } |
| 70 | ||
| 71 | // Tag interface |
|
| 72 | //------------------------------------------------------------------------- |
|
| 73 | public void doTag(XMLOutput output) throws MissingAttributeException, JellyTagException { |
|
| 74 | 3 | invokeBody(output); |
| 75 | ||
| 76 | 3 | if (name == null) { |
| 77 | 0 | throw new MissingAttributeException("name"); |
| 78 | } |
|
| 79 | 3 | if (dynaClass == null) { |
| 80 | 0 | throw new MissingAttributeException("dynaClass"); |
| 81 | } |
|
| 82 | ||
| 83 | 3 | final DynaClass theDynaClass = dynaClass; |
| 84 | 3 | final Map beanAttributes = (attributes != null) ? attributes : EMPTY_MAP; |
| 85 | ||
| 86 | 3 | TagFactory factory = new TagFactory() { |
| 87 | public Tag createTag(String name, Attributes attributes) { |
|
| 88 | return new DynamicDynaBeanTag(theDynaClass, beanAttributes, varAttribute); |
|
| 89 | } |
|
| 90 | }; |
|
| 91 | 3 | getTagLibrary().registerBeanTag(name, factory); |
| 92 | ||
| 93 | // now lets clear the attributes for next invocation and help the GC |
|
| 94 | 3 | attributes = null; |
| 95 | 3 | } |
| 96 | ||
| 97 | ||
| 98 | // Properties |
|
| 99 | //------------------------------------------------------------------------- |
|
| 100 | ||
| 101 | /** |
|
| 102 | * Sets the name of the tag to create |
|
| 103 | */ |
|
| 104 | public void setName(String name) { |
|
| 105 | 3 | this.name = name; |
| 106 | 3 | } |
| 107 | ||
| 108 | /** |
|
| 109 | * Sets the name of the attribute used to define the bean variable that this dynamic |
|
| 110 | * tag will output its results as. This defaults to 'var' though this property |
|
| 111 | * can be used to change this if it conflicts with a bean property called 'var'. |
|
| 112 | */ |
|
| 113 | public void setVarAttribute(String varAttribute) { |
|
| 114 | 0 | this.varAttribute = varAttribute; |
| 115 | 0 | } |
| 116 | ||
| 117 | /** |
|
| 118 | * Returns the dynaClass. |
|
| 119 | * @return DynaClass |
|
| 120 | */ |
|
| 121 | public DynaClass getDynaClass() { |
|
| 122 | 0 | return dynaClass; |
| 123 | } |
|
| 124 | ||
| 125 | /** |
|
| 126 | * Sets the {@link DynaClass} which will be bound to this dynamic tag. |
|
| 127 | */ |
|
| 128 | public void setDynaClass(DynaClass dynaClass) { |
|
| 129 | 3 | this.dynaClass = dynaClass; |
| 130 | 3 | } |
| 131 | ||
| 132 | } |
| This report is generated by jcoverage, Maven and Maven JCoverage Plugin. |