KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > jelly > TagLibrary


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;
18
19 import java.io.File JavaDoc;
20 import java.util.HashMap JavaDoc;
21 import java.util.Map JavaDoc;
22
23 import org.apache.commons.beanutils.ConvertUtils;
24 import org.apache.commons.beanutils.Converter;
25
26 import org.apache.commons.jelly.expression.CompositeExpression;
27 import org.apache.commons.jelly.expression.ConstantExpression;
28 import org.apache.commons.jelly.expression.Expression;
29 import org.apache.commons.jelly.expression.ExpressionFactory;
30 import org.apache.commons.jelly.impl.TagFactory;
31 import org.apache.commons.jelly.impl.TagScript;
32
33 import org.xml.sax.Attributes JavaDoc;
34
35 /** <p><code>Taglib</code> represents the metadata for a Jelly custom tag library.</p>
36   *
37   * @author <a HREF="mailto:jstrachan@apache.org">James Strachan</a>
38   * @version $Revision: 155420 $
39   */

40
41 public abstract class TagLibrary {
42
43     private Map JavaDoc tags = new HashMap JavaDoc();
44
45     static {
46
47         // register standard converters
48

49         ConvertUtils.register(
50             new Converter() {
51                 public Object JavaDoc convert(Class JavaDoc type, Object JavaDoc value) {
52                     if ( value instanceof File JavaDoc ) {
53                         return (File JavaDoc) value;
54                     }
55                     else if ( value != null ) {
56                         String JavaDoc text = value.toString();
57                         return new File JavaDoc( text );
58                     }
59                     return null;
60                 }
61             },
62             File JavaDoc.class
63         );
64     }
65
66     public TagLibrary() {
67     }
68
69     /** Creates a new script to execute the given tag name and attributes */
70     public TagScript createTagScript(String JavaDoc name, Attributes JavaDoc attributes)
71         throws JellyException {
72
73         Object JavaDoc value = tags.get(name);
74         if (value instanceof Class JavaDoc) {
75             Class JavaDoc type = (Class JavaDoc) value;
76             return TagScript.newInstance(type);
77         }
78         else if (value instanceof TagFactory) {
79             return new TagScript( (TagFactory) value );
80         }
81         return null;
82
83     }
84
85     /** Creates a new Tag for the given tag name and attributes */
86     public Tag createTag(String JavaDoc name, Attributes JavaDoc attributes)
87         throws JellyException {
88
89         Object JavaDoc value = tags.get(name);
90         if (value instanceof Class JavaDoc) {
91             Class JavaDoc type = (Class JavaDoc) value;
92             try {
93                 return (Tag) type.newInstance();
94             } catch (InstantiationException JavaDoc e) {
95                 throw new JellyException(e.toString());
96             } catch (IllegalAccessException JavaDoc e) {
97                 throw new JellyException(e.toString());
98             }
99         }
100         else if (value instanceof TagFactory) {
101             TagFactory factory = (TagFactory) value;
102             return factory.createTag(name, attributes);
103         }
104         return null;
105     }
106
107     /** Allows taglibs to use their own expression evaluation mechanism */
108     public Expression createExpression(
109         ExpressionFactory factory,
110         TagScript tagScript,
111         String JavaDoc attributeName,
112         String JavaDoc attributeValue)
113         throws JellyException {
114
115         ExpressionFactory myFactory = getExpressionFactory();
116         if (myFactory == null) {
117             myFactory = factory;
118         }
119         if (myFactory != null) {
120             return CompositeExpression.parse(attributeValue, myFactory);
121         }
122
123         // will use a constant expression instead
124
return new ConstantExpression(attributeValue);
125     }
126
127
128     // Implementation methods
129
//-------------------------------------------------------------------------
130

131     /**
132      * Registers a tag implementation Class for a given tag name
133      */

134     protected void registerTag(String JavaDoc name, Class JavaDoc type) {
135         tags.put(name, type);
136     }
137
138     /**
139      * Registers a tag factory for a given tag name
140      */

141     protected void registerTagFactory(String JavaDoc name, TagFactory tagFactory) {
142         tags.put(name, tagFactory);
143     }
144
145     /** Allows derived tag libraries to use their own factory */
146     protected ExpressionFactory getExpressionFactory() {
147         return null;
148     }
149
150     protected Map JavaDoc getTagClasses() {
151         return tags;
152     }
153
154 }
155
Popular Tags