001: /*--
002: $Id: TagLibrary.java,v 1.1 2004/03/01 07:56:07 wolfpaulus Exp $
003:
004: Copyright (C) 2003-2007 Wolf Paulus.
005: All rights reserved.
006:
007: Redistribution and use in source and binary forms, with or without
008: modification, are permitted provided that the following conditions
009: are met:
010:
011: 1. Redistributions of source code must retain the above copyright
012: notice, this list of conditions, and the following disclaimer.
013:
014: 2. Redistributions in binary form must reproduce the above copyright
015: notice, this list of conditions, and the disclaimer that follows
016: these conditions in the documentation and/or other materials provided
017: with the distribution.
018:
019: 3. The end-user documentation included with the redistribution,
020: if any, must include the following acknowledgment:
021: "This product includes software developed by the
022: SWIXML Project (http://www.swixml.org/)."
023: Alternately, this acknowledgment may appear in the software itself,
024: if and wherever such third-party acknowledgments normally appear.
025:
026: 4. The name "Swixml" must not be used to endorse or promote products
027: derived from this software without prior written permission. For
028: written permission, please contact <info_AT_swixml_DOT_org>
029:
030: 5. Products derived from this software may not be called "Swixml",
031: nor may "Swixml" appear in their name, without prior written
032: permission from the Swixml Project Management.
033:
034: THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
035: WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
036: OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
037: DISCLAIMED. IN NO EVENT SHALL THE SWIXML PROJECT OR ITS
038: CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
039: SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
040: LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
041: USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
042: ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
043: OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
044: OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
045: SUCH DAMAGE.
046: ====================================================================
047:
048: This software consists of voluntary contributions made by many
049: individuals on behalf of the Swixml Project and was originally
050: created by Wolf Paulus <wolf_AT_swixml_DOT_org>. For more information
051: on the Swixml Project, please see <http://www.swixml.org/>.
052: */
053:
054: package org.swixml;
055:
056: import java.lang.reflect.Method;
057: import java.util.HashMap;
058: import java.util.Iterator;
059: import java.util.Map;
060:
061: /**
062: * A skeletal impementation of a TagLibrary<br>
063: * A TagLibrary has a collection of Factories.
064: * Every Tag is mapped to a Factory which is used to build the java object during document parsing.
065: * Date: Dec 9, 2002
066: *
067: * @author <a href="mailto:wolf@paulus.com">Wolf Paulus</a>
068: * @version $Revision: 1.1 $
069:
070: */
071: public abstract class TagLibrary {
072:
073: private Map tags = new HashMap();
074:
075: /**
076: * Constructs a new TagLibrary and regisiters all factories.
077: */
078: public TagLibrary() {
079: registerTags();
080: }
081:
082: /**
083: * Registers all factories for the TagLibrary.
084: */
085: abstract protected void registerTags();
086:
087: /**
088: * Registers a class for the given tag name
089: *
090: * @param name <code>String</code> the tag's name
091: * @param template <code>Class</code> the java class that represents the tag
092: */
093: public void registerTag(String name, Class template) {
094: registerTag(name, new DefaultFactory(template));
095: }
096:
097: /**
098: * Registers a factory for the given tag name
099: *
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 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 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 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 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 template) {
140: Factory factory = null;
141: Iterator 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 getSetter(Class template, String name) {
161: Method 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 guessSetter(Class template, String name) {
178: Method method = null;
179: Factory factory = getFactory(template.getName());
180: if (factory != null) {
181: method = factory.guessSetter(name);
182: }
183: return method;
184: }
185:
186: }
|