001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package javax.servlet.jsp.tagext;
019:
020: import javax.servlet.jsp.*;
021:
022: /**
023: * Wraps any SimpleTag and exposes it using a Tag interface. This is used
024: * to allow collaboration between classic Tag handlers and SimpleTag
025: * handlers.
026: * <p>
027: * Because SimpleTag does not extend Tag, and because Tag.setParent()
028: * only accepts a Tag instance, a classic tag handler (one
029: * that implements Tag) cannot have a SimpleTag as its parent. To remedy
030: * this, a TagAdapter is created to wrap the SimpleTag parent, and the
031: * adapter is passed to setParent() instead. A classic Tag Handler can
032: * call getAdaptee() to retrieve the encapsulated SimpleTag instance.
033: *
034: * @since 2.0
035: */
036: public class TagAdapter implements Tag {
037: /** The simple tag that's being adapted. */
038: private SimpleTag simpleTagAdaptee;
039:
040: /** The parent, of this tag, converted (if necessary) to be of type Tag. */
041: private Tag parent;
042:
043: // Flag indicating whether we have already determined the parent
044: private boolean parentDetermined;
045:
046: /**
047: * Creates a new TagAdapter that wraps the given SimpleTag and
048: * returns the parent tag when getParent() is called.
049: *
050: * @param adaptee The SimpleTag being adapted as a Tag.
051: */
052: public TagAdapter(SimpleTag adaptee) {
053: if (adaptee == null) {
054: // Cannot wrap a null adaptee.
055: throw new IllegalArgumentException();
056: }
057: this .simpleTagAdaptee = adaptee;
058: }
059:
060: /**
061: * Must not be called.
062: *
063: * @param pc ignored.
064: * @throws UnsupportedOperationException Must not be called
065: */
066: public void setPageContext(PageContext pc) {
067: throw new UnsupportedOperationException(
068: "Illegal to invoke setPageContext() on TagAdapter wrapper");
069: }
070:
071: /**
072: * Must not be called. The parent of this tag is always
073: * getAdaptee().getParent().
074: *
075: * @param parentTag ignored.
076: * @throws UnsupportedOperationException Must not be called.
077: */
078: public void setParent(Tag parentTag) {
079: throw new UnsupportedOperationException(
080: "Illegal to invoke setParent() on TagAdapter wrapper");
081: }
082:
083: /**
084: * Returns the parent of this tag, which is always
085: * getAdaptee().getParent().
086: *
087: * This will either be the enclosing Tag (if getAdaptee().getParent()
088: * implements Tag), or an adapter to the enclosing Tag (if
089: * getAdaptee().getParent() does not implement Tag).
090: *
091: * @return The parent of the tag being adapted.
092: */
093: public Tag getParent() {
094: if (!parentDetermined) {
095: JspTag adapteeParent = simpleTagAdaptee.getParent();
096: if (adapteeParent != null) {
097: if (adapteeParent instanceof Tag) {
098: this .parent = (Tag) adapteeParent;
099: } else {
100: // Must be SimpleTag - no other types defined.
101: this .parent = new TagAdapter(
102: (SimpleTag) adapteeParent);
103: }
104: }
105: parentDetermined = true;
106: }
107:
108: return this .parent;
109: }
110:
111: /**
112: * Gets the tag that is being adapted to the Tag interface.
113: * This should be an instance of SimpleTag in JSP 2.0, but room
114: * is left for other kinds of tags in future spec versions.
115: *
116: * @return the tag that is being adapted
117: */
118: public JspTag getAdaptee() {
119: return this .simpleTagAdaptee;
120: }
121:
122: /**
123: * Must not be called.
124: *
125: * @return always throws UnsupportedOperationException
126: * @throws UnsupportedOperationException Must not be called
127: * @throws JspException never thrown
128: */
129: public int doStartTag() throws JspException {
130: throw new UnsupportedOperationException(
131: "Illegal to invoke doStartTag() on TagAdapter wrapper");
132: }
133:
134: /**
135: * Must not be called.
136: *
137: * @return always throws UnsupportedOperationException
138: * @throws UnsupportedOperationException Must not be called
139: * @throws JspException never thrown
140: */
141: public int doEndTag() throws JspException {
142: throw new UnsupportedOperationException(
143: "Illegal to invoke doEndTag() on TagAdapter wrapper");
144: }
145:
146: /**
147: * Must not be called.
148: *
149: * @throws UnsupportedOperationException Must not be called
150: */
151: public void release() {
152: throw new UnsupportedOperationException(
153: "Illegal to invoke release() on TagAdapter wrapper");
154: }
155: }
|