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 java.util.ArrayList;
19  
20  import org.apache.commons.beanutils.BasicDynaClass;
21  import org.apache.commons.beanutils.DynaClass;
22  import org.apache.commons.beanutils.DynaProperty;
23  import org.apache.commons.jelly.JellyTagException;
24  import org.apache.commons.jelly.MissingAttributeException;
25  import org.apache.commons.jelly.TagSupport;
26  import org.apache.commons.jelly.XMLOutput;
27  
28  /***
29   * A tag which creates and defines and creates a DynaClass
30   * The DynaClass object is placed by name in the context,
31   * so that a DynaBean tag can use it by name to instantiate
32   * a DynaBean object
33   *
34   * @author Theo Niemeijer
35   * @version 1.0
36   */
37  public class DynaclassTag extends TagSupport {
38  
39      private ArrayList propList = new ArrayList();
40      private DynaProperty[] props = null;
41      private DynaClass dynaClass = null;
42  
43      private String name;
44      private String var;
45  
46      public DynaclassTag() {
47      }
48  
49      // Tag interface
50      //-------------------------------------------------------------------------
51      public void doTag(XMLOutput output) throws MissingAttributeException, JellyTagException {
52  
53          if (name == null) {
54              throw new MissingAttributeException("name");
55          }
56  
57          if (var == null) {
58              var = name;
59          }
60  
61          // Evaluate the body of the dynaclass definition
62          invokeBody(output);
63  
64          // Convert the list of properties into array
65          props =
66              (DynaProperty[]) propList.toArray(
67                  new DynaProperty[propList.size()]);
68  
69          if (props == null) {
70              throw new JellyTagException("No properties list");
71          }
72  
73          if (props.length == 0) {
74              throw new JellyTagException("No properties");
75          }
76  
77          // Create the dynaclass with name and properties
78          dynaClass = (DynaClass) new BasicDynaClass(name, null, props);
79  
80          // Place new dynaclass in context
81          context.setVariable(getVar(), dynaClass);
82      }
83  
84      // Properties
85      //-------------------------------------------------------------------------
86  
87      /***
88       * Sets the name of the new DynaClass
89       */
90      public void setName(String name) {
91          this.name = name;
92      }
93  
94      public String getVar() {
95          return var;
96      }
97  
98      /***
99       * Sets the name of the variable to export the DynaClass instance
100      */
101     public void setVar(String var) {
102         this.var = var;
103     }
104 
105     protected void addDynaProperty(DynaProperty prop) {
106         propList.add(prop);
107     }
108 }