KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > facelets > tag > TagHandler


1 /**
2  * Licensed under the Common Development and Distribution License,
3  * you may not use this file except in compliance with the License.
4  * You may obtain a copy of the License at
5  *
6  * http://www.sun.com/cddl/
7  *
8  * Unless required by applicable law or agreed to in writing, software
9  * distributed under the License is distributed on an "AS IS" BASIS,
10  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
11  * implied. See the License for the specific language governing
12  * permissions and limitations under the License.
13  */

14
15 package com.sun.facelets.tag;
16
17 import java.util.ArrayList JavaDoc;
18 import java.util.Iterator JavaDoc;
19 import java.util.List JavaDoc;
20
21 import com.sun.facelets.FaceletHandler;
22
23 /**
24  * Foundation class for FaceletHandlers associated with markup in a Facelet
25  * document.
26  *
27  * @author Jacob Hookom
28  * @version $Id: TagHandler.java,v 1.5 2005/09/02 19:25:57 jhook Exp $
29  */

30 public abstract class TagHandler implements FaceletHandler {
31
32     protected final String JavaDoc tagId;
33
34     protected final Tag tag;
35
36     protected final FaceletHandler nextHandler;
37
38     public TagHandler(TagConfig config) {
39         this.tagId = config.getTagId();
40         this.tag = config.getTag();
41         this.nextHandler = config.getNextHandler();
42     }
43
44     /**
45      * Utility method for fetching the appropriate TagAttribute
46      *
47      * @param localName
48      * name of attribute
49      * @return TagAttribute if found, otherwise null
50      */

51     protected final TagAttribute getAttribute(String JavaDoc localName) {
52         return this.tag.getAttributes().get(localName);
53     }
54
55     /**
56      * Utility method for fetching a required TagAttribute
57      *
58      * @param localName
59      * name of the attribute
60      * @return TagAttribute if found, otherwise error
61      * @throws TagException
62      * if the attribute was not found
63      */

64     protected final TagAttribute getRequiredAttribute(String JavaDoc localName)
65             throws TagException {
66         TagAttribute attr = this.getAttribute(localName);
67         if (attr == null) {
68             throw new TagException(this.tag, "Attribute '" + localName
69                     + "' is required");
70         }
71         return attr;
72     }
73     
74     /**
75      * Searches child handlers, starting at the 'nextHandler' for all
76      * instances of the passed type. This process will stop searching
77      * a branch if an instance is found.
78      *
79      * @param type Class type to search for
80      * @return iterator over instances of FaceletHandlers of the matching type
81      */

82     protected final Iterator JavaDoc findNextByType(Class JavaDoc type) {
83         List JavaDoc found = new ArrayList JavaDoc();
84         if (type.isAssignableFrom(this.nextHandler.getClass())) {
85             found.add(this.nextHandler);
86         } else if (this.nextHandler instanceof CompositeFaceletHandler) {
87             FaceletHandler[] h = ((CompositeFaceletHandler) this.nextHandler).getHandlers();
88             for (int i = 0; i < h.length; i++) {
89                 if (type.isAssignableFrom(h[i].getClass())) {
90                     found.add(h[i]);
91                 }
92             }
93         }
94         return found.iterator();
95     }
96
97     public String JavaDoc toString() {
98         return this.tag.toString();
99     }
100 }
101
Popular Tags