View Javadoc

1   package org.apache.velocity.tools.plugin.taglib.model;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.util.HashMap;
23  import java.util.Map;
24  
25  import org.apache.commons.digester.annotations.rules.BeanPropertySetter;
26  import org.apache.commons.digester.annotations.rules.ObjectCreate;
27  import org.apache.commons.digester.annotations.rules.SetTop;
28  
29  /**
30   * It represents a tag attribute, in a tag library descriptor.
31   */
32  @ObjectCreate(pattern = "taglib/tag/attribute")
33  public class Attribute
34  {
35  
36      /**
37       * Maps primitive types to their wrapped equivalents.
38       */
39      private static Map<String, String> primitive2wrapped;
40  
41      static {
42          primitive2wrapped = new HashMap<String, String>();
43          primitive2wrapped.put("byte", Byte.class.getName());
44          primitive2wrapped.put("short", Short.class.getName());
45          primitive2wrapped.put("int", Integer.class.getName());
46          primitive2wrapped.put("long", Long.class.getName());
47          primitive2wrapped.put("float", Float.class.getName());
48          primitive2wrapped.put("double", Double.class.getName());
49          primitive2wrapped.put("char", Character.class.getName());
50          primitive2wrapped.put("boolean", Boolean.class.getName());
51      }
52  
53      /**
54       * The name of the attribute.
55       */
56      @BeanPropertySetter(pattern = "taglib/tag/attribute/name")
57      private String name;
58  
59      /**
60       * The description of the attribute.
61       */
62      @BeanPropertySetter(pattern = "taglib/tag/attribute/description")
63      private String description;
64  
65      /**
66       * It tells if the attribute is required.
67       */
68      @BeanPropertySetter(pattern = "taglib/tag/attribute/required")
69      private boolean required = false;
70  
71      /**
72       * It tells if the attribute can have a value that is the result of runtime expression.
73       */
74      @BeanPropertySetter(pattern = "taglib/tag/attribute/rtexprvalue")
75      private boolean rtexprvalue = false;
76  
77      /**
78       * The type of the attribute.
79       */
80      @BeanPropertySetter(pattern = "taglib/tag/attribute/type")
81      private String type;
82  
83      /**
84       * The containing tag.
85       */
86      private Tag tag;
87  
88      /**
89       * Returns the name of the attribute.
90       *
91       * @return The name of the attribute.
92       */
93      public String getName()
94      {
95          return name;
96      }
97  
98      /**
99       * Sets the name of the attribute.
100      *
101      * @param name The name of the attribute.
102      */
103     public void setName(String name)
104     {
105         this.name = name;
106     }
107 
108     /**
109      * Returns the description of the attribute.
110      *
111      * @return The description of the attribute.
112      */
113     public String getDescription()
114     {
115         return description;
116     }
117 
118     /**
119      * Sets the description of the attribute.
120      *
121      * @param description The description of the attribute.
122      */
123     public void setDescription(String description)
124     {
125         this.description = description;
126     }
127 
128     /**
129      * Returns <code>true</code> if the attribute is required.
130      *
131      * @return <code>true</code> if the attribute is required.
132      */
133     public boolean isRequired()
134     {
135         return required;
136     }
137 
138     /**
139      * It tells if the attribute is required.
140      *
141      * @param required <code>true</code> if the attribute is required.
142      */
143     public void setRequired(boolean required)
144     {
145         this.required = required;
146     }
147 
148     /**
149      * Returns <code>true</code> if the attribute accepts runtime expression.
150      *
151      * @return <code>true</code> if the attribute accepts runtime expression.
152      */
153     public boolean isRtexprvalue()
154     {
155         return rtexprvalue;
156     }
157 
158     /**
159      * It tells ifthe attribute accepts runtime expression.
160      *
161      * @param rtexprvalue <code>true</code> if the attribute accepts runtime expression.
162      */
163     public void setRtexprvalue(boolean rtexprvalue)
164     {
165         this.rtexprvalue = rtexprvalue;
166     }
167 
168     /**
169      * Returns the type of the attribute.
170      *
171      * @return The type of the attribute.
172      */
173     public String getType()
174     {
175         return type;
176     }
177 
178     /**
179      * Sets the type of the attribute.
180      *
181      * @param type The type of the attribute.
182      */
183     public void setType(String type)
184     {
185         this.type = type;
186     }
187 
188     /**
189      * Sets the tag that contains this attribute.
190      *
191      * @param tag The container tag.
192      */
193     @SetTop(pattern="taglib/tag/attribute")
194     public void setJTag(Tag tag)
195     {
196         this.tag = tag;
197     }
198 
199     /**
200      * Returns the tag that contains this attribute.
201      *
202      * @return The container tag.
203      */
204     public Tag getTag()
205     {
206         return tag;
207     }
208 
209     /**
210      * Returns the type that should be used in the Velocity directive for this attribute.
211      *
212      * @return The type to use in Velocity directive.
213      */
214     public String getWrappedType() {
215         if (type == null) {
216             return String.class.getName();
217         }
218         String retValue = primitive2wrapped.get(type);
219         if (retValue == null) {
220             retValue = type;
221         }
222         return retValue;
223     }
224 
225     /**
226      * Returns the name of the setter method.
227      *
228      * @return The name of the setter method.
229      */
230     public String getSetterName() {
231         return "set" + name.substring(0, 1).toUpperCase() + name.substring(1);
232     }
233 
234     @Override
235     public String toString()
236     {
237         return "Attribute [name=" + name + ", description=" + description
238                 + ", required=" + required + ", rtexprvalue=" + rtexprvalue
239                 + ", type=" + type + "]";
240     }
241 
242 }