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.ArrayList;
23  import java.util.List;
24  
25  import javax.servlet.jsp.tagext.SimpleTag;
26  
27  import org.apache.commons.digester.annotations.rules.BeanPropertySetter;
28  import org.apache.commons.digester.annotations.rules.ObjectCreate;
29  import org.apache.commons.digester.annotations.rules.SetNext;
30  import org.apache.velocity.tools.view.jsp.jspimpl.VelocityToolsJspException;
31  
32  /**
33   * It represents a tag in a tag library descriptor.
34   */
35  @ObjectCreate(pattern = "taglib/tag")
36  public class Tag
37  {
38  
39      /**
40       * The name of the tag.
41       */
42      @BeanPropertySetter(pattern = "taglib/tag/name")
43      private String name;
44  
45      /**
46       * The description of the tag.
47       */
48      @BeanPropertySetter(pattern = "taglib/tag/description")
49      private String description;
50  
51      /**
52       * The Java class of the tag.
53       */
54      @BeanPropertySetter(pattern = "taglib/tag/tag-class")
55      private String tagClass;
56  
57      /**
58       * The type of the body content.
59       */
60      @BeanPropertySetter(pattern = "taglib/tag/body-content")
61      private String bodyContent;
62  
63      /**
64       * The list of supported attributes.
65       */
66      private List<Attribute> attributes = new ArrayList<Attribute>();
67  
68      /**
69       * Returns the name of the tag.
70       *
71       * @return The name of the tag.
72       */
73      public String getName()
74      {
75          return name;
76      }
77  
78      /**
79       * Sets the name of the tag.
80       *
81       * @param name The name of the tag.
82       */
83      public void setName(String name)
84      {
85          this.name = name;
86      }
87  
88      /**
89       * Returns the description of the tag.
90       *
91       * @return The description of the tag.
92       */
93      public String getDescription()
94      {
95          return description;
96      }
97  
98      /**
99       * Sets the description of the tag.
100      *
101      * @param description The description of the tag.
102      */
103     public void setDescription(String description)
104     {
105         this.description = description;
106     }
107 
108     /**
109      * Returns the class of the tag.
110      *
111      * @return The class of the tag.
112      */
113     public String getTagClass()
114     {
115         return tagClass;
116     }
117 
118     /**
119      * Sets the class of the tag.
120      *
121      * @param tagClass The class of the tag.
122      */
123     public void setTagClass(String tagClass)
124     {
125         this.tagClass = tagClass;
126     }
127 
128     /**
129      * Returns the body content of the tag.
130      *
131      * @return The body content of the tag.
132      */
133     public String getBodyContent()
134     {
135         return bodyContent;
136     }
137 
138     /**
139      * Sets the body content of the tag.
140      *
141      * @param bodyContent The body content of the tag.
142      */
143     public void setBodyContent(String bodyContent)
144     {
145         this.bodyContent = bodyContent;
146     }
147 
148     /**
149      * Returns the contained attributes.
150      *
151      * @return The attributes.
152      */
153     public List<Attribute> getAttributes()
154     {
155         return attributes;
156     }
157 
158     /**
159      * Adds an attribute to the supported attributes.
160      *
161      * @param attribute A new supported attribute.
162      */
163     @SetNext
164     public void addAttribute(Attribute attribute)
165     {
166         attributes.add(attribute);
167     }
168 
169     /**
170      * It tells if this tag implements {@link SimpleTag}.
171      *
172      * @return <code>true</code> if it is a SimpleTag.
173      */
174     public boolean isSimpleTag() {
175         return SimpleTag.class.isAssignableFrom(getReflectedTagClass());
176     }
177 
178     /**
179      * It tells if this tag has a body.
180      *
181      * @return <code>true</code> if this tag has a body.
182      */
183     public boolean hasBody() {
184         return !"empty".equals(bodyContent);
185     }
186 
187     /**
188      * Returns the reflected tag class.
189      *
190      * @return The real tag class.
191      */
192     public Class<?> getReflectedTagClass()
193     {
194         Class<?> clazz;
195         try
196         {
197             clazz = Class.forName(tagClass);
198         } catch (ClassNotFoundException e)
199         {
200             throw new VelocityToolsJspException("Problems obtaining class: " + tagClass, e);
201         }
202         return clazz;
203     }
204 
205     @Override
206     public String toString()
207     {
208         return "Tag [name=" + name + ", description=" + description
209                 + ", tagClass=" + tagClass + ", bodyContent=" + bodyContent
210                 + ", attributes=" + attributes + "]";
211     }
212 
213 }