KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > taglib > TagSupport


1 /*
2  * Copyright 1999-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 package org.apache.cocoon.taglib;
17
18 import java.util.Map JavaDoc;
19 import java.io.IOException JavaDoc;
20
21 import org.apache.avalon.excalibur.pool.Recyclable;
22 import org.apache.avalon.framework.logger.AbstractLogEnabled;
23 import org.apache.avalon.framework.parameters.Parameters;
24 import org.apache.cocoon.environment.Context;
25 import org.apache.cocoon.environment.ObjectModelHelper;
26 import org.apache.cocoon.environment.Request;
27 import org.apache.cocoon.environment.Session;
28 import org.apache.cocoon.environment.SourceResolver;
29
30 import org.xml.sax.Attributes JavaDoc;
31 import org.xml.sax.SAXException JavaDoc;
32
33 /**
34  * Abstract implementation for all Tags
35  *
36  * @author <a HREF="mailto:volker.schmitt@basf-it-services.com">Volker Schmitt</a>
37  * @version CVS $Id: TagSupport.java 158423 2005-03-21 09:15:22Z cziegeler $
38  */

39 public abstract class TagSupport extends AbstractLogEnabled implements Tag, Recyclable {
40     protected Tag parent;
41     protected SourceResolver resolver;
42     protected Map JavaDoc objectModel;
43     protected Parameters parameters;
44     private Request request;
45
46     /**
47      * Find the instance of a given class type that is closest to a given
48      * instance.
49      * This method uses the getParent method from the Tag
50      * interface.
51      * This method is used for coordination among cooperating tags.
52      *
53      * @param from The instance from where to start looking.
54      * @param klass The subclass of Tag or interface to be matched
55      * @return the nearest ancestor that implements the interface
56      * or is an instance of the class specified
57      */

58     public static final Tag findAncestorWithClass(Tag from, Class JavaDoc klass) {
59         boolean isInterface = false;
60
61         if (from == null || klass == null || (!Tag.class.isAssignableFrom(klass) && !(isInterface = klass.isInterface()))) {
62             return null;
63         }
64
65         for (;;) {
66             Tag tag = from.getParent();
67
68             if (tag == null) {
69                 return null;
70             }
71
72             if ((isInterface && klass.isInstance(tag)) || klass.isAssignableFrom(tag.getClass())) {
73                 return tag;
74             }
75             from = tag;
76         }
77     }
78
79     /**
80      * Process the end tag for this instance.
81      *
82      * @return EVAL_PAGE.
83      * @throws SAXException
84      */

85     public int doEndTag(String JavaDoc namespaceURI, String JavaDoc localName, String JavaDoc qName)
86     throws SAXException JavaDoc {
87         return EVAL_PAGE;
88     }
89
90     /**
91      * Process the start tag for this instance.
92      * <p>
93      * The doStartTag method assumes that pageContext and
94      * parent have been set. It also assumes that any properties exposed as
95      * attributes have been set too. When this method is invoked, the body
96      * has not yet been evaluated.
97      *
98      * @return EVAL_BODY or SKIP_BODY.
99      */

100     public int doStartTag(String JavaDoc namespaceURI, String JavaDoc localName, String JavaDoc qName, Attributes JavaDoc atts)
101     throws SAXException JavaDoc {
102         return EVAL_BODY;
103     }
104
105     /**
106      * Searches for the named attribute in request, session (if valid),
107      * and application scope(s) in order and returns the value associated or
108      * null.
109      *
110      * @return the value associated or null
111      */

112     public final Object JavaDoc findAttribute(String JavaDoc name) {
113         if (request == null)
114             request = ObjectModelHelper.getRequest(objectModel);
115         Object JavaDoc o = request.getAttribute(name);
116         if (o != null)
117             return o;
118
119         Session session = request.getSession(false);
120         if (session != null) {
121             o = session.getAttribute(name);
122             if (o != null)
123                 return o;
124         }
125         Context context = ObjectModelHelper.getContext(objectModel);
126         return context.getAttribute(name);
127     }
128
129     /**
130      * Get the parent (closest enclosing tag handler) for this tag handler.
131      *
132      * @return the current parent, or null if none.
133      */

134     public final Tag getParent() {
135         return parent;
136     }
137
138     public void recycle() {
139         getLogger().debug("recycle");
140         this.parent = null;
141         this.resolver = null;
142         this.objectModel = null;
143         this.parameters = null;
144         this.request = null;
145     }
146
147     /**
148      * Set the parent (closest enclosing tag handler) of this tag handler.
149      * Invoked by the TagTransformer prior to doStartTag().
150      * <p>
151      * This value is *not* reset by doEndTag() and must be explicitly reset
152      * by a Tag implementation.
153      *
154      * @param parent the parent tag, or null.
155      */

156     public final void setParent(Tag parent) {
157         this.parent = parent;
158     }
159
160     /**
161      * Set the <code>SourceResolver</code>, objectModel <code>Map</code>
162      * and sitemap <code>Parameters</code> used to process the request.
163      */

164     public void setup(SourceResolver resolver, Map JavaDoc objectModel, Parameters parameters)
165     throws SAXException JavaDoc, IOException JavaDoc {
166         this.resolver = resolver;
167         this.objectModel = objectModel;
168         this.parameters = parameters;
169     }
170 }
171
Popular Tags