KickJava   Java API By Example, From Geeks To Geeks.

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


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.lang.reflect.Method JavaDoc;
20 import java.util.HashMap JavaDoc;
21 import java.util.Map JavaDoc;
22
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.DynamicBeanTag;
29 import org.apache.commons.jelly.impl.TagFactory;
30 import org.apache.commons.jelly.util.ClassLoaderUtils;
31
32 import org.apache.commons.logging.Log;
33 import org.apache.commons.logging.LogFactory;
34
35 import org.xml.sax.Attributes JavaDoc;
36
37 /**
38  * Binds a Java bean to the given named Jelly tag so that the attributes of
39  * the tag set the bean properties..
40  *
41  * @author <a HREF="mailto:jstrachan@apache.org">James Strachan</a>
42  * @version $Revision: 155420 $
43  */

44 public class BeanTag extends DefineTagSupport {
45
46     /** The Log to which logging calls will be made. */
47     private static final Log log = LogFactory.getLog(BeanTag.class);
48
49     /** An empty Map as I think Collections.EMPTY_MAP is only JDK 1.3 onwards */
50     private static final Map JavaDoc EMPTY_MAP = new HashMap JavaDoc();
51
52     /** the name of the tag to create */
53     private String JavaDoc name;
54
55     /** the Java class name to use for the tag */
56     private String JavaDoc className;
57
58     /** the ClassLoader used to load beans */
59     private ClassLoader JavaDoc classLoader;
60
61     /** the name of the attribute used for the variable name */
62     private String JavaDoc varAttribute = "var";
63
64     /** the attribute definitions for this dynamic tag */
65     private Map JavaDoc attributes;
66
67     /**
68      * Adds a new attribute definition to this dynamic tag
69      */

70     public void addAttribute(Attribute attribute) {
71         if ( attributes == null ) {
72             attributes = new HashMap JavaDoc();
73         }
74         attributes.put( attribute.getName(), attribute );
75     }
76
77     // Tag interface
78
//-------------------------------------------------------------------------
79
public void doTag(XMLOutput output) throws MissingAttributeException, JellyTagException {
80         invokeBody(output);
81
82         if (name == null) {
83             throw new MissingAttributeException("name");
84         }
85         if (className == null) {
86             throw new MissingAttributeException("className");
87         }
88
89         Class JavaDoc theClass = null;
90         try {
91             ClassLoader JavaDoc classLoader = getClassLoader();
92             theClass = ClassLoaderUtils.loadClass(className, getClassLoader(), getContext().getUseContextClassLoader(), getClass());
93         }
94         catch (ClassNotFoundException JavaDoc e) {
95             log.error( "Could not load class: " + className + " exception: " + e, e );
96             throw new JellyTagException(
97                 "Could not find class: "
98                     + className
99                     + " using ClassLoader: "
100                     + classLoader);
101         }
102
103         final Class JavaDoc beanClass = theClass;
104         final Method JavaDoc invokeMethod = getInvokeMethod( theClass );
105         final Map JavaDoc beanAttributes = (attributes != null) ? attributes : EMPTY_MAP;
106
107         TagFactory factory = new TagFactory() {
108             public Tag createTag(String JavaDoc name, Attributes JavaDoc attributes) {
109                 return new DynamicBeanTag(beanClass, beanAttributes, varAttribute, invokeMethod);
110             }
111         };
112
113         getTagLibrary().registerBeanTag(name, factory);
114
115         // now lets clear the attributes for next invocation and help the GC
116
attributes = null;
117     }
118
119
120     // Properties
121
//-------------------------------------------------------------------------
122

123     /**
124      * Sets the name of the tag to create
125      */

126     public void setName(String JavaDoc name) {
127         this.name = name;
128     }
129
130     /**
131      * Sets the Java class name to use for the tag
132      */

133     public void setClassName(String JavaDoc className) {
134         this.className = className;
135     }
136
137     /**
138      * Sets the ClassLoader to use to load the class.
139      * If no value is set then the current threads context class
140      * loader is used.
141      */

142     public void setClassLoader(ClassLoader JavaDoc classLoader) {
143         this.classLoader = classLoader;
144     }
145
146     /**
147      * @return the ClassLoader to use to load classes specified by this object,
148      * the thread context loader if the context flag is set, or the class used to load this class.
149      */

150     public ClassLoader JavaDoc getClassLoader() {
151         return ClassLoaderUtils.getClassLoader(classLoader, getContext().getUseContextClassLoader(), getClass());
152     }
153
154     /**
155      * Sets the name of the attribute used to define the bean variable that this dynamic
156      * tag will output its results as. This defaults to 'var' though this property
157      * can be used to change this if it conflicts with a bean property called 'var'.
158      */

159     public void setVarAttribute(String JavaDoc varAttribute) {
160         this.varAttribute = varAttribute;
161     }
162
163
164     // Implementation methods
165
//-------------------------------------------------------------------------
166

167     /**
168      * Extracts the invoke method for the class if one is used.
169      */

170     protected Method JavaDoc getInvokeMethod( Class JavaDoc theClass ) {
171         return null;
172     }
173 }
174
Popular Tags