KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > swixml > TagLibrary


1 /*--
2  $Id: TagLibrary.java,v 1.1 2004/03/01 07:56:07 wolfpaulus Exp $
3
4  Copyright (C) 2003-2004 Wolf Paulus.
5  All rights reserved.
6
7  Redistribution and use in source and binary forms, with or without
8  modification, are permitted provided that the following conditions
9  are met:
10
11  1. Redistributions of source code must retain the above copyright
12  notice, this list of conditions, and the following disclaimer.
13
14  2. Redistributions in binary form must reproduce the above copyright
15  notice, this list of conditions, and the disclaimer that follows
16  these conditions in the documentation and/or other materials provided
17  with the distribution.
18
19  3. The end-user documentation included with the redistribution,
20  if any, must include the following acknowledgment:
21         "This product includes software developed by the
22          SWIXML Project (http://www.swixml.org/)."
23  Alternately, this acknowledgment may appear in the software itself,
24  if and wherever such third-party acknowledgments normally appear.
25
26  4. The name "Swixml" must not be used to endorse or promote products
27  derived from this software without prior written permission. For
28  written permission, please contact <info_AT_swixml_DOT_org>
29
30  5. Products derived from this software may not be called "Swixml",
31  nor may "Swixml" appear in their name, without prior written
32  permission from the Swixml Project Management.
33
34  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
35  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
36  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
37  DISCLAIMED. IN NO EVENT SHALL THE SWIXML PROJECT OR ITS
38  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
39  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
40  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
41  USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
42  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
43  OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
44  OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
45  SUCH DAMAGE.
46  ====================================================================
47
48  This software consists of voluntary contributions made by many
49  individuals on behalf of the Swixml Project and was originally
50  created by Wolf Paulus <wolf_AT_swixml_DOT_org>. For more information
51  on the Swixml Project, please see <http://www.swixml.org/>.
52 */

53
54 package org.swixml;
55
56 import java.lang.reflect.Method JavaDoc;
57 import java.util.HashMap JavaDoc;
58 import java.util.Iterator JavaDoc;
59 import java.util.Map JavaDoc;
60
61 /**
62  * A skeletal impementation of a TagLibrary<br>
63  * A TagLibrary has a collection of Factories.
64  * Every Tag is mapped to a Factory which is used to build the java object during document parsing.
65  * Date: Dec 9, 2002
66  *
67  * @author <a HREF="mailto:wolf@paulus.com">Wolf Paulus</a>
68  * @version $Revision: 1.1 $
69
70  */

71 public abstract class TagLibrary {
72
73   private Map JavaDoc tags = new HashMap JavaDoc();
74
75   /**
76    * Constructs a new TagLibrary and regisiters all factories.
77    */

78   public TagLibrary() {
79     registerTags();
80   }
81
82   /**
83    * Registers all factories for the TagLibrary.
84    */

85   abstract protected void registerTags();
86
87   /**
88    * Registers a class for the given tag name
89    *
90    * @param name <code>String</code> the tag's name
91    * @param template <code>Class</code> the java class that represents the tag
92    */

93   public void registerTag( String JavaDoc name, Class JavaDoc template ) {
94     registerTag( name, new DefaultFactory( template ) );
95   }
96
97   /**
98    * Registers a factory for the given tag name
99    *
100    * @param name <code>String</code> the tag's name
101    * @param factory <code>FactoryFactory</code> factory to create an Instance of the tag
102    */

103   public void registerTag( String JavaDoc name, Factory factory ) {
104     tags.put( name.toLowerCase(), factory );
105   }
106
107   /**
108    * Un-registers (removes) a registered tag.
109    *
110    * @param name <code>String</code> the tag's name
111    * @return <code>boolean</code> true if tag was registered befoire and now successfuly removed.
112    */

113   public boolean unregisterTag( String JavaDoc name ) {
114     return (null != tags.remove( name ));
115   }
116
117   /**
118    * @return <code>Map</code> - all registered tags.
119    * <pre>Use athe tag names to get to the factories</pre>
120    */

121   public Map JavaDoc getTagClasses() {
122     return tags;
123   }
124
125   /**
126    * Returns the Factory that is currently registered for the given Tag name
127    * @param name <code>String</code>
128    * @return <code>Factory</code> - regsitered for the given tag name
129    */

130   public Factory getFactory( String JavaDoc name ) {
131     return (Factory) tags.get( name.toLowerCase() );
132   }
133
134   /**
135    * Returns the Factory that is currently registered for the given Tag name
136    * @param template <code>Class</code>
137    * @return <code>Factory</code> - regsitered for the given tag name
138    */

139   public Factory getFactory( Class JavaDoc template ) {
140     Factory factory = null;
141     Iterator JavaDoc it = tags.values().iterator();
142     while (it != null && it.hasNext()) {
143       Factory f = (Factory) it.next();
144       if (f.getTemplate().equals( template )) {
145         factory = f;
146         break;
147       }
148     }
149     return factory;
150   }
151
152   /**
153    * Returns a setter method by name for a specified template class
154    * @param template <code>Class</code> template class
155    * @param name <code>Sting</code> method name
156    * @return <code>Method</code> - a setter method for the given class.
157    * @see #guessSetter(Class, String)
158    * @see org.swixml.Factory#getSetter(String)
159    */

160   protected Method JavaDoc getSetter( Class JavaDoc template, String JavaDoc name ) {
161     Method JavaDoc method = null;
162     Factory factory = getFactory( template.getName() );
163     if (factory != null) {
164       method = factory.getSetter( name );
165     }
166     return method;
167   }
168
169   /**
170    * Returns a setter method by name for a specified template class
171    * @param template <code>Class</code> template class
172    * @param name <code>Sting</code> attribute name
173    * @return <code>Method</code> - a setter method for the given class, able to modify the property.
174    * @see #getSetter(Class, String)
175    * @see org.swixml.Factory#guessSetter(String)
176    */

177   protected Method JavaDoc guessSetter( Class JavaDoc template, String JavaDoc name ) {
178     Method JavaDoc method = null;
179     Factory factory = getFactory( template.getName() );
180     if (factory != null) {
181       method = factory.guessSetter( name );
182     }
183     return method;
184   }
185
186 }
187
Popular Tags