%line | %branch | |||||||||
---|---|---|---|---|---|---|---|---|---|---|
org.apache.commons.jelly.tags.dynabean.PropertyTag |
|
|
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 | 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 | 21 | public PropertyTag() { |
42 | 21 | } |
43 | ||
44 | // Tag interface |
|
45 | //------------------------------------------------------------------------- |
|
46 | public void doTag (XMLOutput output) throws MissingAttributeException, JellyTagException { |
|
47 | ||
48 | // Check that this tag is used inside the body of |
|
49 | // a DynaClass tag, so that it can access the |
|
50 | // context of that tag |
|
51 | 27 | DynaclassTag parentTag = (DynaclassTag) findAncestorWithClass( DynaclassTag.class ); |
52 | 21 | if ( parentTag == null ) { |
53 | 0 | throw new JellyTagException( "This tag must be enclosed inside a <dynaclass> tag" ); |
54 | } |
|
55 | ||
56 | // Check property name |
|
57 | 21 | if (name == null) { |
58 | 0 | throw new MissingAttributeException( "name" ); |
59 | } |
|
60 | ||
61 | // Lookup appropriate property class |
|
62 | 21 | Class propClass = propertyClass; |
63 | 21 | if (propClass == null) { |
64 | ||
65 | // Check property type |
|
66 | 21 | if (type == null) { |
67 | 0 | throw new MissingAttributeException( "type" ); |
68 | } |
|
69 | ||
70 | 21 | if (type.equals("String")) { |
71 | 15 | propClass = String.class; |
72 | } |
|
73 | 6 | else if (type.equals("Integer")) { |
74 | 3 | propClass = Integer.TYPE; |
75 | } |
|
76 | 3 | else if (type.equals("Short")) { |
77 | 0 | propClass = Short.TYPE; |
78 | } |
|
79 | 3 | else if (type.equals("Long")) { |
80 | 0 | propClass = Long.TYPE; |
81 | } |
|
82 | 3 | else if (type.equals("Float")) { |
83 | 0 | propClass = Float.TYPE; |
84 | } |
|
85 | 3 | else if (type.equals("Double")) { |
86 | 0 | propClass = Double.TYPE; |
87 | } |
|
88 | 3 | else if (type.equals("Long")) { |
89 | 0 | propClass = Long.TYPE; |
90 | } |
|
91 | ||
92 | 21 | if (propClass == null) { |
93 | try { |
|
94 | 3 | propClass = Class.forName(type); |
95 | } |
|
96 | 0 | catch (Exception e) { |
97 | 0 | throw new JellyTagException |
98 | ("Class " + type + |
|
99 | " not found by Class.forName"); |
|
100 | 3 | } |
101 | } |
|
102 | } |
|
103 | ||
104 | // Create dynaproperty object with given name and type |
|
105 | 21 | prop = new DynaProperty (name, propClass); |
106 | ||
107 | // Add new property to dynaclass context |
|
108 | 21 | parentTag.addDynaProperty(prop); |
109 | 21 | } |
110 | ||
111 | // Properties |
|
112 | //------------------------------------------------------------------------- |
|
113 | ||
114 | /** |
|
115 | * Sets the name of this property |
|
116 | */ |
|
117 | public void setName(String name) { |
|
118 | 21 | this.name = name; |
119 | 21 | } |
120 | ||
121 | /** |
|
122 | * Sets the type name of this property |
|
123 | */ |
|
124 | public void setType(String type) { |
|
125 | 21 | this.type = type; |
126 | 21 | } |
127 | ||
128 | /** |
|
129 | * Returns the Class for this property |
|
130 | */ |
|
131 | public Class getPropertyClass() { |
|
132 | 0 | return propertyClass; |
133 | } |
|
134 | ||
135 | /** |
|
136 | * Sets the Class instance for this property |
|
137 | */ |
|
138 | public void setPropertyClass(Class propertyClass) { |
|
139 | 0 | this.propertyClass = propertyClass; |
140 | 0 | } |
141 | ||
142 | } |
This report is generated by jcoverage, Maven and Maven JCoverage Plugin. |