1
2
3
4
5
6
7
8
9
10
11
12
13
14
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 public class DynaBeanTag extends DefineTagSupport {
42
43 /*** The Log to which logging calls will be made. */
44 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 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 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 if ( attributes == null ) {
66 attributes = new HashMap();
67 }
68 attributes.put( attribute.getName(), attribute );
69 }
70
71
72
73 public void doTag(XMLOutput output) throws MissingAttributeException, JellyTagException {
74 invokeBody(output);
75
76 if (name == null) {
77 throw new MissingAttributeException("name");
78 }
79 if (dynaClass == null) {
80 throw new MissingAttributeException("dynaClass");
81 }
82
83 final DynaClass theDynaClass = dynaClass;
84 final Map beanAttributes = (attributes != null) ? attributes : EMPTY_MAP;
85
86 TagFactory factory = new TagFactory() {
87 public Tag createTag(String name, Attributes attributes) {
88 return new DynamicDynaBeanTag(theDynaClass, beanAttributes, varAttribute);
89 }
90 };
91 getTagLibrary().registerBeanTag(name, factory);
92
93
94 attributes = null;
95 }
96
97
98
99
100
101 /***
102 * Sets the name of the tag to create
103 */
104 public void setName(String name) {
105 this.name = name;
106 }
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 this.varAttribute = varAttribute;
115 }
116
117 /***
118 * Returns the dynaClass.
119 * @return DynaClass
120 */
121 public DynaClass getDynaClass() {
122 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 this.dynaClass = dynaClass;
130 }
131
132 }