01: /**
02: * Licensed under the Common Development and Distribution License,
03: * you may not use this file except in compliance with the License.
04: * You may obtain a copy of the License at
05: *
06: * http://www.sun.com/cddl/
07: *
08: * Unless required by applicable law or agreed to in writing, software
09: * 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: */package com.sun.facelets.tag;
14:
15: import java.util.ArrayList;
16: import java.util.Iterator;
17: import java.util.List;
18:
19: import com.sun.facelets.FaceletHandler;
20:
21: /**
22: * Foundation class for FaceletHandlers associated with markup in a Facelet
23: * document.
24: *
25: * @author Jacob Hookom
26: * @version $Id: TagHandler.java,v 1.5 2005/09/02 19:25:57 jhook Exp $
27: */
28: public abstract class TagHandler implements FaceletHandler {
29:
30: protected final String tagId;
31:
32: protected final Tag tag;
33:
34: protected final FaceletHandler nextHandler;
35:
36: public TagHandler(TagConfig config) {
37: this .tagId = config.getTagId();
38: this .tag = config.getTag();
39: this .nextHandler = config.getNextHandler();
40: }
41:
42: /**
43: * Utility method for fetching the appropriate TagAttribute
44: *
45: * @param localName
46: * name of attribute
47: * @return TagAttribute if found, otherwise null
48: */
49: protected final TagAttribute getAttribute(String localName) {
50: return this .tag.getAttributes().get(localName);
51: }
52:
53: /**
54: * Utility method for fetching a required TagAttribute
55: *
56: * @param localName
57: * name of the attribute
58: * @return TagAttribute if found, otherwise error
59: * @throws TagException
60: * if the attribute was not found
61: */
62: protected final TagAttribute getRequiredAttribute(String localName)
63: throws TagException {
64: TagAttribute attr = this .getAttribute(localName);
65: if (attr == null) {
66: throw new TagException(this .tag, "Attribute '" + localName
67: + "' is required");
68: }
69: return attr;
70: }
71:
72: /**
73: * Searches child handlers, starting at the 'nextHandler' for all
74: * instances of the passed type. This process will stop searching
75: * a branch if an instance is found.
76: *
77: * @param type Class type to search for
78: * @return iterator over instances of FaceletHandlers of the matching type
79: */
80: protected final Iterator findNextByType(Class type) {
81: List found = new ArrayList();
82: if (type.isAssignableFrom(this .nextHandler.getClass())) {
83: found.add(this .nextHandler);
84: } else if (this .nextHandler instanceof CompositeFaceletHandler) {
85: FaceletHandler[] h = ((CompositeFaceletHandler) this .nextHandler)
86: .getHandlers();
87: for (int i = 0; i < h.length; i++) {
88: if (type.isAssignableFrom(h[i].getClass())) {
89: found.add(h[i]);
90: }
91: }
92: }
93: return found.iterator();
94: }
95:
96: public String toString() {
97: return this.tag.toString();
98: }
99: }
|