Source code: com/aendvari/tethys/tag/context/ContextTag.java
1 /*
2 * ContextTag.java
3 *
4 * Copyright (c) 2001, 2002 Aendvari, Ltd. All Rights Reserved.
5 *
6 * See the file LICENSE for terms of use.
7 *
8 */
9
10 package com.aendvari.tethys.tag.context;
11
12 import java.lang.reflect.Method;
13
14 import javax.servlet.jsp.JspTagException;
15 import javax.servlet.jsp.tagext.*;
16
17 import com.aendvari.tethys.context.*;
18
19 import com.aendvari.tethys.tag.*;
20 import com.aendvari.tethys.tag.data.*;
21
22 /**
23 * <p>A base class for all tags requiring context.</p>
24 *
25 * @author Scott Milne
26 *
27 */
28
29 public abstract class ContextTag extends DataTag implements ContextAncestorTag
30 {
31 /* Parameters. */
32
33 /** The relative context in which this tag resides. */
34 protected String context;
35
36
37 /* Constructors. */
38
39 /**
40 * Default constructor, all subclasses are required to only define a public
41 * constructor with the same signature, and to call the superclass constructor.
42 *
43 */
44
45 public ContextTag()
46 {
47 super();
48
49 context = null;
50 }
51
52
53 /* Accessors. */
54
55 public String getContext()
56 {
57 return context;
58 }
59
60 public void setContext(String context)
61 {
62 this.context = context;
63 }
64
65 /**
66 * Determines the {@link Context} of this tag.
67 * The <code>context</code> parameter is used to select the {@link Context}.
68 *
69 * @param contextMap The context map to search.
70 * @param ancestorClass The class type of parent context tags.
71 * @param contextMethod The {@link Method} to retrieve the tag {@link Context}.
72 *
73 * @return The {@link Context} object for this tag.
74 *
75 * @exception JspTagException The context could not be established.
76 *
77 */
78
79 protected Context determineContext(ContextMap contextMap, Class ancestorClass, Method contextMethod)
80 throws JspTagException
81 {
82 // determine the context for this tag
83 Context contextObject = ContextUtil.determineContext(
84 this, ancestorClass, contextMethod, contextMap, context, true);
85
86 // throw exception if context could not be found
87 if (contextObject == null)
88 {
89 throw new JspTagException(
90 this.getClass().getName() + ": The context \"" + context + "\" cannot be found.");
91 }
92
93 return contextObject;
94 }
95
96 /**
97 * Returns the parent {@link Context} for this tag.
98 *
99 * @param contextMap The context map to search.
100 * @param tagSupport The tag to get the parent of.
101 * @param ancestorClass The class type of the parent tag.
102 * @param contextMethod The {@link Method} to retrieve the tag {@link Context}.
103 *
104 * @return A {@link Context} instance representing the parent context,
105 * null if none found.
106 *
107 * @exception JspTagException If the context cannot be determined.
108 *
109 */
110
111 public Context getParentContext(
112 ContextMap contextMap, TagSupport tagSupport, Class ancestorClass, Method contextMethod)
113 throws JspTagException
114 {
115 // get parent tag
116 TagSupport parentTag = TagUtil.getParentTag(tagSupport, ancestorClass);
117
118 // use default if no parent tag
119 if (parentTag == null)
120 {
121 return contextMap.getDefaultContext();
122 }
123
124 // get parent context
125 Context parentContext = ContextUtil.getTagContext(parentTag, contextMethod);
126
127 return parentContext;
128 }
129
130 /**
131 * Release all allocated resources.
132 *
133 */
134
135 public void release()
136 {
137 super.release();
138
139 context = null;
140 }
141 }
142