View Javadoc

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      public PropertyTag() {
42      }
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          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          // Check property name
57          if (name == null) {
58              throw new MissingAttributeException( "name" );
59          }
60  
61          // Lookup appropriate property class
62          Class propClass = propertyClass;
63          if (propClass == null) {
64  
65              // Check property type
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         // Create dynaproperty object with given name and type
105         prop = new DynaProperty (name, propClass);
106 
107         // Add new property to dynaclass context
108         parentTag.addDynaProperty(prop);
109     }
110 
111     // Properties
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 }