001: package com.mockrunner.tag;
002:
003: import java.util.ArrayList;
004: import java.util.Enumeration;
005: import java.util.HashMap;
006: import java.util.List;
007: import java.util.Map;
008:
009: import javax.servlet.jsp.JspException;
010: import javax.servlet.jsp.PageContext;
011: import javax.servlet.jsp.tagext.IterationTag; //import javax.servlet.jsp.tagext.JspTag;
012: //import javax.servlet.jsp.tagext.SimpleTag;
013: import javax.servlet.jsp.tagext.Tag;
014: import javax.servlet.jsp.tagext.TagSupport;
015:
016: /**
017: * Implementation of {@link NestedTag} wrapping tags of
018: * type <code>Tag</code>. <code>NestedStandardTag</code> instances
019: * are created with the help of {@link TagTestModule#createNestedTag}.
020: * You do not need to create them on your own in the tests.
021: */
022: public class NestedStandardTag extends TagSupport implements NestedTag {
023: private Tag tag;
024: private PageContext pageContext;
025: private Map attributes;
026: private List childs;
027: private boolean doRelease;
028:
029: /**
030: * Constructor for a tag with an empty attribute map.
031: * If the specified tag is not an instance of <code>TagSupport</code>,
032: * the methods that delegate to <code>TagSupport</code> specific methods
033: * throw an exception.
034: * @param tag the tag
035: * @param pageContext the corresponding <code>PageContext</code>
036: */
037: public NestedStandardTag(Tag tag, PageContext pageContext) {
038: this (tag, pageContext, new HashMap());
039: }
040:
041: /**
042: * Constructor for a tag with the specified attribute map.
043: * If the specified tag is not an instance of <code>TagSupport</code>,
044: * the methods that delegate to <code>TagSupport</code> specific methods
045: * throw an exception.
046: * @param tag the tag
047: * @param pageContext the corresponding <code>PageContext</code>
048: * @param attributes the attribute map
049: */
050: public NestedStandardTag(Tag tag, PageContext pageContext,
051: Map attributes) {
052: this .tag = tag;
053: this .pageContext = pageContext;
054: tag.setPageContext(pageContext);
055: childs = new ArrayList();
056: this .attributes = attributes;
057: doRelease = false;
058: }
059:
060: /**
061: * Constructor for a tag with an empty attribute map.
062: * @param tag the tag
063: * @param pageContext the corresponding <code>PageContext</code>
064: */
065: public NestedStandardTag(TagSupport tag, PageContext pageContext) {
066: this (tag, pageContext, new HashMap());
067: }
068:
069: /**
070: * Constructor for a tag with the specified attribute map.
071: * @param tag the tag
072: * @param pageContext the corresponding <code>PageContext</code>
073: * @param attributes the attribute map
074: */
075: public NestedStandardTag(TagSupport tag, PageContext pageContext,
076: Map attributes) {
077: this ((Tag) tag, pageContext, attributes);
078: }
079:
080: /**
081: * @inheritDoc
082: */
083: public void setDoRelease(boolean doRelease) {
084: this .doRelease = doRelease;
085: }
086:
087: /**
088: * @inheritDoc
089: */
090: public void setDoReleaseRecursive(boolean doRelease) {
091: this .doRelease = doRelease;
092: for (int ii = 0; ii < childs.size(); ii++) {
093: Object child = childs.get(ii);
094: if (child instanceof NestedTag) {
095: ((NestedTag) child).setDoReleaseRecursive(doRelease);
096: }
097: }
098: }
099:
100: /**
101: * @inheritDoc
102: */
103: public void populateAttributes() {
104: TagUtil.populateTag(tag, attributes);
105: }
106:
107: /**
108: * @inheritDoc
109: */
110: public int doLifecycle() throws JspException {
111: populateAttributes();
112: int returnValue = -1;
113: try {
114: int result = tag.doStartTag();
115: if (Tag.EVAL_BODY_INCLUDE == result) {
116: TagUtil.evalBody(childs, pageContext);
117: if (tag instanceof IterationTag) {
118: while (IterationTag.EVAL_BODY_AGAIN == doAfterBody()) {
119: TagUtil.evalBody(childs, pageContext);
120: }
121: }
122: }
123: returnValue = tag.doEndTag();
124: } catch (Throwable exc) {
125: TagUtil.handleException(tag, exc);
126: } finally {
127: TagUtil.handleFinally(tag);
128: }
129: if (doRelease)
130: tag.release();
131: return returnValue;
132: }
133:
134: /**
135: * @inheritDoc
136: * @throws <code>RuntimeException</code>, if the wrapped tag
137: * is not an instance of <code>TagSupport</code>
138: */
139: public TagSupport getTag() {
140: checkTagSupport();
141: return (TagSupport) tag;
142: }
143:
144: /**
145: * @inheritDoc
146: */
147: /*public JspTag getWrappedTag()
148: {
149: return tag;
150: }*/
151:
152: /**
153: * @inheritDoc
154: */
155: public void removeChilds() {
156: childs.clear();
157: }
158:
159: /**
160: * @inheritDoc
161: */
162: public List getChilds() {
163: return childs;
164: }
165:
166: /**
167: * @inheritDoc
168: */
169: public Object getChild(int index) {
170: return childs.get(index);
171: }
172:
173: /**
174: * @inheritDoc
175: */
176: public void addTextChild(String text) {
177: if (null == text)
178: text = "";
179: childs.add(text);
180: }
181:
182: /**
183: * @inheritDoc
184: */
185: public void addDynamicChild(DynamicChild child) {
186: if (null == child)
187: return;
188: childs.add(child);
189: }
190:
191: /**
192: * @inheritDoc
193: */
194: public NestedTag addTagChild(Class tag) {
195: return addTagChild(tag, new HashMap());
196: }
197:
198: /**
199: * @inheritDoc
200: */
201: public NestedTag addTagChild(Class tag, Map attributeMap) {
202: Object childTag = TagUtil.createNestedTagInstance(tag,
203: this .pageContext, attributeMap);
204: return addChild(childTag);
205: }
206:
207: /**
208: * @inheritDoc
209: */
210: public NestedTag addTagChild(TagSupport tag) {
211: return addTagChild(tag, new HashMap());
212: }
213:
214: /**
215: * @inheritDoc
216: */
217: public NestedTag addTagChild(TagSupport tag, Map attributeMap) {
218: Object childTag = TagUtil.createNestedTagInstance(tag,
219: this .pageContext, attributeMap);
220: return addChild(childTag);
221: }
222:
223: /**
224: * @inheritDoc
225: */
226: /*public NestedTag addTagChild(JspTag tag)
227: {
228: return addTagChild(tag, new HashMap());
229: }*/
230:
231: /**
232: * @inheritDoc
233: */
234: /*public NestedTag addTagChild(JspTag tag, Map attributeMap)
235: {
236: Object childTag = TagUtil.createNestedTagInstance(tag, this.pageContext, attributeMap);
237: return addChild(childTag);
238: }*/
239:
240: /**
241: * Delegates to wrapped tag.
242: * @throws <code>RuntimeException</code>, if the wrapped tag
243: * is not an instance of <code>IterationTag</code>
244: */
245: public int doAfterBody() throws JspException {
246: checkIterationTag();
247: return ((IterationTag) tag).doAfterBody();
248: }
249:
250: /**
251: * Delegates to wrapped tag.
252: */
253: public int doEndTag() throws JspException {
254: return tag.doEndTag();
255: }
256:
257: /**
258: * Delegates to wrapped tag.
259: */
260: public int doStartTag() throws JspException {
261: return tag.doStartTag();
262: }
263:
264: /**
265: * Delegates to wrapped tag.
266: * @throws <code>RuntimeException</code>, if the wrapped tag
267: * is not an instance of <code>TagSupport</code>
268: */
269: public String getId() {
270: checkTagSupport();
271: return ((TagSupport) tag).getId();
272: }
273:
274: /**
275: * Delegates to wrapped tag.
276: */
277: public Tag getParent() {
278: return tag.getParent();
279: }
280:
281: /**
282: * Delegates to wrapped tag.
283: * @throws <code>RuntimeException</code>, if the wrapped tag
284: * is not an instance of <code>TagSupport</code>
285: */
286: public Object getValue(String key) {
287: checkTagSupport();
288: return ((TagSupport) tag).getValue(key);
289: }
290:
291: /**
292: * Delegates to wrapped tag.
293: * @throws <code>RuntimeException</code>, if the wrapped tag
294: * is not an instance of <code>TagSupport</code>
295: */
296: public Enumeration getValues() {
297: checkTagSupport();
298: return ((TagSupport) tag).getValues();
299: }
300:
301: /**
302: * Delegates to wrapped tag.
303: */
304: public void release() {
305: tag.release();
306: }
307:
308: /**
309: * Delegates to wrapped tag.
310: * @throws <code>RuntimeException</code>, if the wrapped tag
311: * is not an instance of <code>TagSupport</code>
312: */
313: public void removeValue(String value) {
314: checkTagSupport();
315: ((TagSupport) tag).removeValue(value);
316: }
317:
318: /**
319: * Delegates to wrapped tag.
320: * @throws <code>RuntimeException</code>, if the wrapped tag
321: * is not an instance of <code>TagSupport</code>
322: */
323: public void setId(String id) {
324: checkTagSupport();
325: ((TagSupport) tag).setId(id);
326: }
327:
328: /**
329: * Delegates to wrapped tag. Also calls <code>setPageContext</code>
330: * for all child tags.
331: */
332: public void setPageContext(PageContext pageContext) {
333: this .pageContext = pageContext;
334: tag.setPageContext(pageContext);
335: for (int ii = 0; ii < childs.size(); ii++) {
336: Object child = childs.get(ii);
337: if (child instanceof Tag) {
338: ((Tag) child).setPageContext(pageContext);
339: }
340: /*else if(child instanceof SimpleTag)
341: {
342: ((SimpleTag)child).setJspContext(pageContext);
343: }*/
344: }
345: }
346:
347: /**
348: * Delegates to wrapped tag.
349: */
350: public void setParent(Tag parent) {
351: tag.setParent(parent);
352: }
353:
354: /**
355: * Delegates to wrapped tag.
356: */
357: public void setValue(String key, Object value) {
358: checkTagSupport();
359: ((TagSupport) tag).setValue(key, value);
360: }
361:
362: /**
363: * Dumps the content of this and the nested tags.
364: */
365: public String toString() {
366: return TagUtil.dumpTag(this , new StringBuffer(), 0);
367: }
368:
369: private NestedTag addChild(Object childTag) {
370: if (childTag instanceof Tag) {
371: ((Tag) childTag).setParent(this .tag);
372: }
373: /*else if(childTag instanceof SimpleTag)
374: {
375: ((SimpleTag)childTag).setParent(this.tag);
376: }*/
377: childs.add(childTag);
378: return (NestedTag) childTag;
379: }
380:
381: private void checkIterationTag() {
382: if (!(tag instanceof IterationTag)) {
383: throw new RuntimeException(
384: "This method can only be called if the wrapped tag is an instance of IterationTag.");
385: }
386: }
387:
388: private void checkTagSupport() {
389: if (!(tag instanceof TagSupport)) {
390: throw new RuntimeException(
391: "This method can only be called if the wrapped tag is an instance of TagSupport.");
392: }
393: }
394: }
|