001: package com.mockrunner.tag;
002:
003: import java.io.IOException;
004: import java.util.HashMap;
005: import java.util.List;
006: import java.util.Map;
007:
008: import javax.servlet.jsp.JspContext;
009: import javax.servlet.jsp.JspException;
010: import javax.servlet.jsp.tagext.JspFragment;
011: import javax.servlet.jsp.tagext.JspTag;
012: import javax.servlet.jsp.tagext.SimpleTag;
013: import javax.servlet.jsp.tagext.SimpleTagSupport;
014: import javax.servlet.jsp.tagext.TagSupport;
015:
016: import com.mockrunner.base.NestedApplicationException;
017: import com.mockrunner.mock.web.MockJspFragment;
018:
019: /**
020: * Implementation of {@link NestedTag} wrapping tags of
021: * type <code>SimpleTag</code>. <code>NestedSimpleTag</code> instances
022: * are created with the help of {@link TagTestModule#createNestedTag}.
023: * You do not need to create them on your own in the tests.
024: */
025: public class NestedSimpleTag extends SimpleTagSupport implements
026: NestedTag {
027: private SimpleTag tag;
028: private JspContext jspContext;
029: private JspFragment jspBody;
030: private Map attributes;
031:
032: /**
033: * Constructor for a tag with an empty attribute map.
034: * @param tag the tag
035: * @param jspContext the corresponding <code>JspContext</code>
036: */
037: public NestedSimpleTag(SimpleTag tag, JspContext jspContext) {
038: this (tag, jspContext, new HashMap());
039: }
040:
041: /**
042: * Constructor for a tag with the specified attribute map.
043: * @param tag the tag
044: * @param jspContext the corresponding <code>JspContext</code>
045: * @param attributes the attribute map
046: */
047: public NestedSimpleTag(SimpleTag tag, JspContext jspContext,
048: Map attributes) {
049: this .tag = tag;
050: this .jspContext = jspContext;
051: jspBody = new MockJspFragment(jspContext, tag);
052: tag.setJspContext(jspContext);
053: tag.setJspBody(jspBody);
054: this .attributes = attributes;
055: }
056:
057: /**
058: * Constructor for a tag with an empty attribute map.
059: * @param tag the tag
060: * @param jspContext the corresponding <code>JspContext</code>
061: */
062: public NestedSimpleTag(SimpleTagSupport tag, JspContext jspContext) {
063: this (tag, jspContext, new HashMap());
064: }
065:
066: /**
067: * Constructor for a tag with the specified attribute map.
068: * @param tag the tag
069: * @param jspContext the corresponding <code>JspContext</code>
070: * @param attributes the attribute map
071: */
072: public NestedSimpleTag(SimpleTagSupport tag, JspContext jspContext,
073: Map attributes) {
074: this ((SimpleTag) tag, jspContext, attributes);
075: }
076:
077: /**
078: * Implementation of {@link NestedTag#setDoRelease}.
079: * Does nothing in this case.
080: */
081: public void setDoRelease(boolean doRelease) {
082:
083: }
084:
085: /**
086: * Implementation of {@link NestedTag#setDoReleaseRecursive}.
087: * Does nothing in this case.
088: */
089: public void setDoReleaseRecursive(boolean doRelease) {
090:
091: }
092:
093: /**
094: * @inheritDoc
095: */
096: public void populateAttributes() {
097: TagUtil.populateTag(tag, attributes);
098: }
099:
100: /**
101: * Implementation of {@link NestedTag#doLifecycle} for simple
102: * tags. Returns <code>-1</code> in this case, because
103: * <code>doTag()</code> does not have a return value.
104: */
105: public int doLifecycle() throws JspException {
106: try {
107: populateAttributes();
108: tag.doTag();
109: } catch (IOException exc) {
110: throw new NestedApplicationException(exc);
111: }
112: return -1;
113: }
114:
115: /**
116: * Implementation of {@link NestedTag#getTag}.
117: * Should not be called and throws a <code>RuntimeException</code>,
118: * because a simple tag is not an instance of <code>TagSupport</code>.
119: */
120: public TagSupport getTag() {
121: throw new RuntimeException(
122: "getTag() method cannot be called for simple tags.");
123: }
124:
125: /**
126: * @inheritDoc
127: */
128: public JspTag getWrappedTag() {
129: return tag;
130: }
131:
132: /**
133: * @inheritDoc
134: */
135: public void removeChilds() {
136: if (null != jspBody && jspBody instanceof MockJspFragment) {
137: ((MockJspFragment) jspBody).removeChilds();
138: }
139: }
140:
141: /**
142: * @inheritDoc
143: */
144: public List getChilds() {
145: if (null != jspBody && jspBody instanceof MockJspFragment) {
146: return ((MockJspFragment) jspBody).getChilds();
147: }
148: return null;
149: }
150:
151: /**
152: * @inheritDoc
153: */
154: public Object getChild(int index) {
155: if (null != jspBody && jspBody instanceof MockJspFragment) {
156: return ((MockJspFragment) jspBody).getChild(index);
157: }
158: return null;
159: }
160:
161: /**
162: * @inheritDoc
163: */
164: public void addTextChild(String text) {
165: if (null != jspBody && jspBody instanceof MockJspFragment) {
166: ((MockJspFragment) jspBody).addTextChild(text);
167: }
168: }
169:
170: /**
171: * @inheritDoc
172: */
173: public void addDynamicChild(DynamicChild child) {
174: if (null != jspBody && jspBody instanceof MockJspFragment) {
175: ((MockJspFragment) jspBody).addDynamicChild(child);
176: }
177: }
178:
179: /**
180: * @inheritDoc
181: */
182: public NestedTag addTagChild(Class tag) {
183: if (null != jspBody && jspBody instanceof MockJspFragment) {
184: return ((MockJspFragment) jspBody).addTagChild(tag);
185: }
186: return null;
187: }
188:
189: /**
190: * @inheritDoc
191: */
192: public NestedTag addTagChild(Class tag, Map attributeMap) {
193: if (null != jspBody && jspBody instanceof MockJspFragment) {
194: return ((MockJspFragment) jspBody).addTagChild(tag,
195: attributeMap);
196: }
197: return null;
198: }
199:
200: /**
201: * @inheritDoc
202: */
203: public NestedTag addTagChild(TagSupport tag) {
204: return addTagChild(tag, new HashMap());
205: }
206:
207: /**
208: * @inheritDoc
209: */
210: public NestedTag addTagChild(TagSupport tag, Map attributeMap) {
211: return addTagChild((JspTag) tag, attributeMap);
212: }
213:
214: /**
215: * @inheritDoc
216: */
217: public NestedTag addTagChild(JspTag tag) {
218: if (null != jspBody && jspBody instanceof MockJspFragment) {
219: return ((MockJspFragment) jspBody).addTagChild(tag);
220: }
221: return null;
222: }
223:
224: /**
225: * @inheritDoc
226: */
227: public NestedTag addTagChild(JspTag tag, Map attributeMap) {
228: if (null != jspBody && jspBody instanceof MockJspFragment) {
229: return ((MockJspFragment) jspBody).addTagChild(tag,
230: attributeMap);
231: }
232: return null;
233: }
234:
235: /**
236: * Delegates to wrapped tag.
237: */
238: public void doTag() throws JspException, IOException {
239: tag.doTag();
240: }
241:
242: /**
243: * Returns the body fragment.
244: * @return the body fragment
245: */
246: public JspFragment getJspBody() {
247: return jspBody;
248: }
249:
250: /**
251: * Returns the <code>JspContext</code>.
252: * @return the <code>JspContext</code>
253: */
254: public JspContext getJspContext() {
255: return jspContext;
256: }
257:
258: /**
259: * Delegates to wrapped tag.
260: */
261: public JspTag getParent() {
262: return tag.getParent();
263: }
264:
265: /**
266: * Delegates to wrapped tag.
267: */
268: public void setJspBody(JspFragment jspBody) {
269: this .jspBody = jspBody;
270: tag.setJspBody(jspBody);
271: }
272:
273: /**
274: * Delegates to wrapped tag. Also calls <code>setJspContext</code>
275: * on the body fragment, if the body fragment is an instance of
276: * {@link com.mockrunner.mock.web.MockJspFragment}
277: */
278: public void setJspContext(JspContext jspContext) {
279: this .jspContext = jspContext;
280: tag.setJspContext(jspContext);
281: if (null != jspBody && jspBody instanceof MockJspFragment) {
282: ((MockJspFragment) jspBody).setJspContext(jspContext);
283: }
284: }
285:
286: /**
287: * Delegates to wrapped tag.
288: */
289: public void setParent(JspTag parent) {
290: tag.setParent(parent);
291: }
292:
293: /**
294: * Dumps the content of this and the nested tags.
295: */
296: public String toString() {
297: return TagUtil.dumpTag(this , new StringBuffer(), 0);
298: }
299: }
|