Package: SimpleTagSupport
SimpleTagSupport
name | instruction | branch | complexity | line | method | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
SimpleTagSupport() |
|
|
|
|
|
||||||||||||||||||||
doTag() |
|
|
|
|
|
||||||||||||||||||||
findAncestorWithClass(JspTag, Class) |
|
|
|
|
|
||||||||||||||||||||
getJspBody() |
|
|
|
|
|
||||||||||||||||||||
getJspContext() |
|
|
|
|
|
||||||||||||||||||||
getParent() |
|
|
|
|
|
||||||||||||||||||||
setJspBody(JspFragment) |
|
|
|
|
|
||||||||||||||||||||
setJspContext(JspContext) |
|
|
|
|
|
||||||||||||||||||||
setParent(JspTag) |
|
|
|
|
|
Coverage
1: /*
2: * Copyright (c) 1997, 2020 Oracle and/or its affiliates and others.
3: * All rights reserved.
4: * Copyright 2004 The Apache Software Foundation
5: *
6: * Licensed under the Apache License, Version 2.0 (the "License");
7: * you may not use this file except in compliance with the License.
8: * You may obtain a copy of the License at
9: *
10: * http://www.apache.org/licenses/LICENSE-2.0
11: *
12: * Unless required by applicable law or agreed to in writing, software
13: * distributed under the License is distributed on an "AS IS" BASIS,
14: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15: * See the License for the specific language governing permissions and
16: * limitations under the License.
17: */
18:
19: package jakarta.servlet.jsp.tagext;
20:
21: import jakarta.servlet.jsp.JspContext;
22: import jakarta.servlet.jsp.JspException;
23: import java.io.IOException;
24:
25: /**
26: * A base class for defining tag handlers implementing SimpleTag.
27: * <p>
28: * The SimpleTagSupport class is a utility class intended to be used as the base class for new simple tag handlers. The
29: * SimpleTagSupport class implements the SimpleTag interface and adds additional convenience methods including getter
30: * methods for the properties in SimpleTag.
31: *
32: * @since JSP 2.0
33: */
34: public class SimpleTagSupport implements SimpleTag {
35: /** Reference to the enclosing tag. */
36: private JspTag parentTag;
37:
38: /** The JSP context for the upcoming tag invocation. */
39: private JspContext jspContext;
40:
41: /** The body of the tag. */
42: private JspFragment jspBody;
43:
44: /**
45: * Sole constructor. (For invocation by subclass constructors, typically implicit.)
46: */
47: public SimpleTagSupport() {
48: }
49:
50: /**
51: * Default processing of the tag does nothing.
52: *
53: * @throws JspException Subclasses can throw JspException to indicate an error occurred while processing this tag.
54: * @throws jakarta.servlet.jsp.SkipPageException If the page that (either directly or indirectly) invoked
55: * this tag is to cease evaluation. A Simple Tag Handler generated from a tag file must throw
56: * this exception if an invoked Classic Tag Handler returned SKIP_PAGE or if an invoked Simple
57: * Tag Handler threw SkipPageException or if an invoked Jsp Fragment threw a SkipPageException.
58: * @throws IOException Subclasses can throw IOException if there was an error writing to the output stream
59: * @see SimpleTag#doTag()
60: */
61: @Override
62: public void doTag() throws JspException, IOException {
63: }
64:
65: /**
66: * Sets the parent of this tag, for collaboration purposes.
67: * <p>
68: * The container invokes this method only if this tag invocation is nested within another tag invocation.
69: *
70: * @param parent the tag that encloses this tag
71: */
72: @Override
73: public void setParent(JspTag parent) {
74: this.parentTag = parent;
75: }
76:
77: /**
78: * Returns the parent of this tag, for collaboration purposes.
79: *
80: * @return the parent of this tag
81: */
82: @Override
83: public JspTag getParent() {
84: return this.parentTag;
85: }
86:
87: /**
88: * Stores the provided JSP context in the private jspContext field. Subclasses can access the
89: * <code>JspContext</code> via <code>getJspContext()</code>.
90: *
91: * @param pc the page context for this invocation
92: * @see SimpleTag#setJspContext
93: */
94: @Override
95: public void setJspContext(JspContext pc) {
96: this.jspContext = pc;
97: }
98:
99: /**
100: * Returns the page context passed in by the container via setJspContext.
101: *
102: * @return the page context for this invocation
103: */
104: protected JspContext getJspContext() {
105: return this.jspContext;
106: }
107:
108: /**
109: * Stores the provided JspFragment.
110: *
111: * @param jspBody The fragment encapsulating the body of this tag. If the action element is empty in the page, this
112: * method is not called at all.
113: * @see SimpleTag#setJspBody
114: */
115: @Override
116: public void setJspBody(JspFragment jspBody) {
117: this.jspBody = jspBody;
118: }
119:
120: /**
121: * Returns the body passed in by the container via setJspBody.
122: *
123: * @return the fragment encapsulating the body of this tag, or null if the action element is empty in the page.
124: */
125: protected JspFragment getJspBody() {
126: return this.jspBody;
127: }
128:
129: /**
130: * Find the instance of a given class type that is closest to a given instance. This method uses the getParent
131: * method from the Tag and/or SimpleTag interfaces. This method is used for coordination among cooperating tags.
132: *
133: * <p>
134: * For every instance of TagAdapter encountered while traversing the ancestors, the tag handler returned by
135: * <tt>TagAdapter.getAdaptee()</tt> - instead of the TagAdpater itself - is compared to <tt>klass</tt>. If the tag
136: * handler matches, it - and not its TagAdapter - is returned.
137: *
138: * <p>
139: * The current version of the specification only provides one formal way of indicating the observable type of a tag
140: * handler: its tag handler implementation class, described in the tag-class subelement of the tag element. This is
141: * extended in an informal manner by allowing the tag library author to indicate in the description subelement an
142: * observable type. The type should be a subtype of the tag handler implementation class or void. This addititional
143: * constraint can be exploited by a specialized container that knows about that specific tag library, as in the case
144: * of the JSP standard tag library.
145: *
146: * <p>
147: * When a tag library author provides information on the observable type of a tag handler, client programmatic code
148: * should adhere to that constraint. Specifically, the Class passed to findAncestorWithClass should be a subtype of
149: * the observable type.
150: *
151: *
152: * @param from The instance from where to start looking.
153: * @param klass The subclass of JspTag or interface to be matched
154: * @return the nearest ancestor that implements the interface or is an instance of the class specified
155: */
156: public static final JspTag findAncestorWithClass(JspTag from, Class<?> klass) {
157: boolean isInterface = false;
158:
159:• if (from == null || klass == null
160:• || (!JspTag.class.isAssignableFrom(klass) && !(isInterface = klass.isInterface()))) {
161: return null;
162: }
163:
164: for (;;) {
165: JspTag parent = null;
166:• if (from instanceof SimpleTag) {
167: parent = ((SimpleTag) from).getParent();
168:• } else if (from instanceof Tag) {
169: parent = ((Tag) from).getParent();
170: }
171:• if (parent == null) {
172: return null;
173: }
174:
175:• if (parent instanceof TagAdapter) {
176: parent = ((TagAdapter) parent).getAdaptee();
177: }
178:
179:• if ((isInterface && klass.isInstance(parent)) || klass.isAssignableFrom(parent.getClass())) {
180: return parent;
181: }
182:
183: from = parent;
184: }
185: }
186: }