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.DynaBean;
19  import org.apache.commons.beanutils.DynaClass;
20  import org.apache.commons.jelly.JellyTagException;
21  import org.apache.commons.jelly.MissingAttributeException;
22  import org.apache.commons.jelly.TagSupport;
23  import org.apache.commons.jelly.XMLOutput;
24  
25  
26  /*** A tag which conditionally evaluates its body based on some condition
27    *
28    * @author Theo Niemeijer
29    * @version $Revision: 155420 $
30    */
31  public class DynabeanTag extends TagSupport {
32  
33      private DynaClass dynaClass;
34      private String var;
35  
36      public DynabeanTag() {
37      }
38  
39      // Tag interface
40      //-------------------------------------------------------------------------
41      public void doTag(XMLOutput output) throws MissingAttributeException, JellyTagException {
42  
43          if (dynaClass == null) {
44              throw new MissingAttributeException( "dynaclass" );
45          }
46  
47          if (var == null) {
48              throw new MissingAttributeException( "var" );
49          }
50  
51          try {
52              // Create dynabean instance for this dynaclass
53              DynaBean dynaBean = dynaClass.newInstance();
54  
55              // Place new dynabean in context as a variable
56              context.setVariable(getVar(), dynaBean);
57          } catch (IllegalAccessException e) {
58              throw new JellyTagException(e);
59          } catch (InstantiationException e) {
60              throw new JellyTagException(e);
61          }
62      }
63  
64      // Properties
65      //-------------------------------------------------------------------------
66  
67      /***
68       * Sets the DynaClass of the new instance to create
69       */
70      public void setDynaclass(DynaClass dynaClass) {
71          this.dynaClass = dynaClass;
72      }
73  
74      public String getVar() {
75          return var;
76      }
77  
78      /***
79       * Sets the name of the variable to export the new DynaBean instance to
80       */
81      public void setVar(String var) {
82          this.var = var;
83      }
84  
85  
86  }