Skip to contentMethod: TagAdapter(SimpleTag)
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.JspException;
22: import jakarta.servlet.jsp.PageContext;
23:
24: /**
25: * Wraps any SimpleTag and exposes it using a Tag interface. This is used to allow collaboration between classic Tag
26: * handlers and SimpleTag handlers.
27: * <p>
28: * Because SimpleTag does not extend Tag, and because Tag.setParent() only accepts a Tag instance, a classic tag handler
29: * (one that implements Tag) cannot have a SimpleTag as its parent. To remedy this, a TagAdapter is created to wrap the
30: * SimpleTag parent, and the adapter is passed to setParent() instead. A classic Tag Handler can call getAdaptee() to
31: * retrieve the encapsulated SimpleTag instance.
32: *
33: * @since JSP 2.0
34: */
35: public class TagAdapter implements Tag {
36: /** The simple tag that's being adapted. */
37: private SimpleTag simpleTagAdaptee;
38:
39: /** The parent, of this tag, converted (if necessary) to be of type Tag. */
40: private Tag parent;
41:
42: // Flag indicating whether we have already determined the parent
43: private boolean parentDetermined;
44:
45: /**
46: * Creates a new TagAdapter that wraps the given SimpleTag and returns the parent tag when getParent() is called.
47: *
48: * @param adaptee The SimpleTag being adapted as a Tag.
49: */
50: public TagAdapter(SimpleTag adaptee) {
51:• if (adaptee == null) {
52: // Cannot wrap a null adaptee.
53: throw new IllegalArgumentException();
54: }
55: this.simpleTagAdaptee = adaptee;
56: }
57:
58: /**
59: * Must not be called.
60: *
61: * @param pc ignored.
62: * @throws UnsupportedOperationException Must not be called
63: */
64: @Override
65: public void setPageContext(PageContext pc) {
66: throw new UnsupportedOperationException("Illegal to invoke setPageContext() on TagAdapter wrapper");
67: }
68:
69: /**
70: * Must not be called. The parent of this tag is always getAdaptee().getParent().
71: *
72: * @param parentTag ignored.
73: * @throws UnsupportedOperationException Must not be called.
74: */
75: @Override
76: public void setParent(Tag parentTag) {
77: throw new UnsupportedOperationException("Illegal to invoke setParent() on TagAdapter wrapper");
78: }
79:
80: /**
81: * Returns the parent of this tag, which is always getAdaptee().getParent().
82: *
83: * This will either be the enclosing Tag (if getAdaptee().getParent() implements Tag), or an adapter to the
84: * enclosing Tag (if getAdaptee().getParent() does not implement Tag).
85: *
86: * @return The parent of the tag being adapted.
87: */
88: @Override
89: public Tag getParent() {
90: if (!parentDetermined) {
91: JspTag adapteeParent = simpleTagAdaptee.getParent();
92: if (adapteeParent != null) {
93: if (adapteeParent instanceof Tag) {
94: this.parent = (Tag) adapteeParent;
95: } else {
96: // Must be SimpleTag - no other types defined.
97: this.parent = new TagAdapter((SimpleTag) adapteeParent);
98: }
99: }
100: parentDetermined = true;
101: }
102:
103: return this.parent;
104: }
105:
106: /**
107: * Gets the tag that is being adapted to the Tag interface. This should be an instance of SimpleTag in JSP 2.0, but
108: * room is left for other kinds of tags in future spec versions.
109: *
110: * @return the tag that is being adapted
111: */
112: public JspTag getAdaptee() {
113: return this.simpleTagAdaptee;
114: }
115:
116: /**
117: * Must not be called.
118: *
119: * @return always throws UnsupportedOperationException
120: * @throws UnsupportedOperationException Must not be called
121: * @throws JspException never thrown
122: */
123: @Override
124: public int doStartTag() throws JspException {
125: throw new UnsupportedOperationException("Illegal to invoke doStartTag() on TagAdapter wrapper");
126: }
127:
128: /**
129: * Must not be called.
130: *
131: * @return always throws UnsupportedOperationException
132: * @throws UnsupportedOperationException Must not be called
133: * @throws JspException never thrown
134: */
135: @Override
136: public int doEndTag() throws JspException {
137: throw new UnsupportedOperationException("Illegal to invoke doEndTag() on TagAdapter wrapper");
138: }
139:
140: /**
141: * Must not be called.
142: *
143: * @throws UnsupportedOperationException Must not be called
144: */
145: @Override
146: public void release() {
147: throw new UnsupportedOperationException("Illegal to invoke release() on TagAdapter wrapper");
148: }
149: }