1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.apache.commons.jelly.tags.dynabean;
17
18 import org.apache.commons.beanutils.DynaProperty;
19 import org.apache.commons.jelly.JellyTagException;
20 import org.apache.commons.jelly.MissingAttributeException;
21 import org.apache.commons.jelly.TagSupport;
22 import org.apache.commons.jelly.XMLOutput;
23
24 /***
25 * DynaProperty tag defines a property of a DynaClass
26 * It can only exist inside a DynaClass parent context
27 * The properties are added to the properties array
28 * of the parent context, and will be used to
29 * create the DynaClass object
30 *
31 * @author Theo Niemeijer
32 * @version 1.0
33 */
34 public class PropertyTag extends TagSupport {
35
36 private String name;
37 private String type;
38 private Class propertyClass;
39 private DynaProperty prop;
40
41 public PropertyTag() {
42 }
43
44
45
46 public void doTag (XMLOutput output) throws MissingAttributeException, JellyTagException {
47
48
49
50
51 DynaclassTag parentTag = (DynaclassTag) findAncestorWithClass( DynaclassTag.class );
52 if ( parentTag == null ) {
53 throw new JellyTagException( "This tag must be enclosed inside a <dynaclass> tag" );
54 }
55
56
57 if (name == null) {
58 throw new MissingAttributeException( "name" );
59 }
60
61
62 Class propClass = propertyClass;
63 if (propClass == null) {
64
65
66 if (type == null) {
67 throw new MissingAttributeException( "type" );
68 }
69
70 if (type.equals("String")) {
71 propClass = String.class;
72 }
73 else if (type.equals("Integer")) {
74 propClass = Integer.TYPE;
75 }
76 else if (type.equals("Short")) {
77 propClass = Short.TYPE;
78 }
79 else if (type.equals("Long")) {
80 propClass = Long.TYPE;
81 }
82 else if (type.equals("Float")) {
83 propClass = Float.TYPE;
84 }
85 else if (type.equals("Double")) {
86 propClass = Double.TYPE;
87 }
88 else if (type.equals("Long")) {
89 propClass = Long.TYPE;
90 }
91
92 if (propClass == null) {
93 try {
94 propClass = Class.forName(type);
95 }
96 catch (Exception e) {
97 throw new JellyTagException
98 ("Class " + type +
99 " not found by Class.forName");
100 }
101 }
102 }
103
104
105 prop = new DynaProperty (name, propClass);
106
107
108 parentTag.addDynaProperty(prop);
109 }
110
111
112
113
114 /***
115 * Sets the name of this property
116 */
117 public void setName(String name) {
118 this.name = name;
119 }
120
121 /***
122 * Sets the type name of this property
123 */
124 public void setType(String type) {
125 this.type = type;
126 }
127
128 /***
129 * Returns the Class for this property
130 */
131 public Class getPropertyClass() {
132 return propertyClass;
133 }
134
135 /***
136 * Sets the Class instance for this property
137 */
138 public void setPropertyClass(Class propertyClass) {
139 this.propertyClass = propertyClass;
140 }
141
142 }