KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > jelly > tags > define > DynaBeanTag


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
17 package org.apache.commons.jelly.tags.define;
18
19 import java.util.HashMap JavaDoc;
20 import java.util.Map JavaDoc;
21
22 import org.apache.commons.beanutils.DynaClass;
23 import org.apache.commons.jelly.JellyTagException;
24 import org.apache.commons.jelly.MissingAttributeException;
25 import org.apache.commons.jelly.Tag;
26 import org.apache.commons.jelly.XMLOutput;
27 import org.apache.commons.jelly.impl.Attribute;
28 import org.apache.commons.jelly.impl.DynamicDynaBeanTag;
29 import org.apache.commons.jelly.impl.TagFactory;
30 import org.apache.commons.logging.Log;
31 import org.apache.commons.logging.LogFactory;
32 import org.xml.sax.Attributes JavaDoc;
33
34 /**
35  * Binds a Java bean to the given named Jelly tag so that the attributes of
36  * the tag set the bean properties..
37  *
38  * @author <a HREF="mailto:jstrachan@apache.org">James Strachan</a>
39  * @version $Revision: 155420 $
40  */

41 public class DynaBeanTag extends DefineTagSupport {
42
43     /** The Log to which logging calls will be made. */
44     private static final Log log = LogFactory.getLog(DynaBeanTag.class);
45
46     /** An empty Map as I think Collections.EMPTY_MAP is only JDK 1.3 onwards */
47     private static final Map JavaDoc EMPTY_MAP = new HashMap JavaDoc();
48
49     /** the name of the tag to create */
50     private String JavaDoc name;
51
52     /** the DyanClass to bind to the tag */
53     private DynaClass dynaClass;
54
55     /** the name of the attribute used for the variable name */
56     private String JavaDoc varAttribute = "var";
57
58     /** the attribute definitions for this dynamic tag */
59     private Map JavaDoc attributes;
60
61     /**
62      * Adds a new attribute definition to this dynamic tag
63      */

64     public void addAttribute(Attribute attribute) {
65         if ( attributes == null ) {
66             attributes = new HashMap JavaDoc();
67         }
68         attributes.put( attribute.getName(), attribute );
69     }
70
71     // Tag interface
72
//-------------------------------------------------------------------------
73
public void doTag(XMLOutput output) throws MissingAttributeException, JellyTagException {
74         invokeBody(output);
75
76         if (name == null) {
77             throw new MissingAttributeException("name");
78         }
79         if (dynaClass == null) {
80             throw new MissingAttributeException("dynaClass");
81         }
82
83         final DynaClass theDynaClass = dynaClass;
84         final Map JavaDoc beanAttributes = (attributes != null) ? attributes : EMPTY_MAP;
85
86         TagFactory factory = new TagFactory() {
87             public Tag createTag(String JavaDoc name, Attributes JavaDoc attributes) {
88                 return new DynamicDynaBeanTag(theDynaClass, beanAttributes, varAttribute);
89             }
90         };
91         getTagLibrary().registerBeanTag(name, factory);
92
93         // now lets clear the attributes for next invocation and help the GC
94
attributes = null;
95     }
96
97
98     // Properties
99
//-------------------------------------------------------------------------
100

101     /**
102      * Sets the name of the tag to create
103      */

104     public void setName(String JavaDoc name) {
105         this.name = name;
106     }
107
108     /**
109      * Sets the name of the attribute used to define the bean variable that this dynamic
110      * tag will output its results as. This defaults to 'var' though this property
111      * can be used to change this if it conflicts with a bean property called 'var'.
112      */

113     public void setVarAttribute(String JavaDoc varAttribute) {
114         this.varAttribute = varAttribute;
115     }
116
117     /**
118      * Returns the dynaClass.
119      * @return DynaClass
120      */

121     public DynaClass getDynaClass() {
122         return dynaClass;
123     }
124
125     /**
126      * Sets the {@link DynaClass} which will be bound to this dynamic tag.
127      */

128     public void setDynaClass(DynaClass dynaClass) {
129         this.dynaClass = dynaClass;
130     }
131
132 }
133
Popular Tags