1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.apache.struts.taglib.nested.logic;
22
23 import org.apache.struts.taglib.logic.IterateTag;
24 import org.apache.struts.taglib.nested.NestedNameSupport;
25 import org.apache.struts.taglib.nested.NestedPropertyHelper;
26
27 import javax.servlet.http.HttpServletRequest;
28 import javax.servlet.jsp.JspException;
29
30 import java.util.Map;
31
32 /**
33 * NestedIterateTag. Slightly more complex that the other extensions. This one
34 * has to yield a proper index property. Very taxing.
35 *
36 * @version $Rev: 471754 $ $Date: 2004-10-16 12:38:42 -0400 (Sat, 16 Oct 2004)
37 * $
38 * @since Struts 1.1
39 */
40 public class NestedIterateTag extends IterateTag implements NestedNameSupport {
41
42 private String nesting = null;
43
44
45 private String originalName = null;
46 private String originalProperty = null;
47
48
49 private String originalNesting = null;
50 private String originalNestingName = null;
51
52 /**
53 * Overriding method of the heart of the matter. Gets the relative
54 * property and leaves the rest up to the original tag implementation.
55 * Sweet.
56 *
57 * @return int JSP continuation directive. This is in the hands of the
58 * super class.
59 */
60 public int doStartTag() throws JspException {
61
62 originalName = getName();
63 originalProperty = getProperty();
64
65
66 if ((id == null) || (id.trim().length() == 0)) {
67 id = property;
68 }
69
70
71 HttpServletRequest request =
72 (HttpServletRequest) pageContext.getRequest();
73
74
75 originalNesting = NestedPropertyHelper.getCurrentProperty(request);
76 originalNestingName =
77 NestedPropertyHelper.getCurrentName(request, this);
78
79
80
81 if (getName() == null) {
82
83 nesting =
84 NestedPropertyHelper.getAdjustedProperty(request, getProperty());
85 } else {
86
87 nesting = getProperty();
88 }
89
90
91 NestedPropertyHelper.setNestedProperties(request, this);
92
93
94 int temp = super.doStartTag();
95
96
97 NestedPropertyHelper.setName(request, getName());
98 NestedPropertyHelper.setProperty(request, deriveNestedProperty());
99
100
101 return temp;
102 }
103
104 /**
105 * The only added property to the class. For use in proper nesting.
106 *
107 * @return String value of the property and the current index or mapping.
108 */
109 private String deriveNestedProperty() {
110 Object idObj = pageContext.getAttribute(id);
111
112 if (idObj instanceof Map.Entry) {
113 return nesting + "(" + ((Map.Entry) idObj).getKey() + ")";
114 } else {
115 return nesting + "[" + this.getIndex() + "]";
116 }
117 }
118
119 /**
120 * This is only overriden as the include reference will need it's index
121 * updated.
122 *
123 * @return int JSP continuation directive.
124 */
125 public int doAfterBody() throws JspException {
126
127 int temp = super.doAfterBody();
128 HttpServletRequest request =
129 (HttpServletRequest) pageContext.getRequest();
130
131 if (temp != SKIP_BODY) {
132
133 NestedPropertyHelper.setProperty(request, deriveNestedProperty());
134 }
135
136
137 return temp;
138 }
139
140 /**
141 * Complete the processing of the tag. The nested tags here will restore
142 * all the original value for the tag itself and the nesting context.
143 *
144 * @return int to describe the next step for the JSP processor
145 * @throws JspException for the bad things JSP's do
146 */
147 public int doEndTag() throws JspException {
148
149 int i = super.doEndTag();
150
151
152 HttpServletRequest request =
153 (HttpServletRequest) pageContext.getRequest();
154
155
156 super.setName(originalName);
157 super.setProperty(originalProperty);
158
159
160 if (originalNesting == null) {
161 NestedPropertyHelper.deleteReference(request);
162 } else {
163 NestedPropertyHelper.setProperty(request, originalNesting);
164 NestedPropertyHelper.setName(request, originalNestingName);
165 }
166
167
168 return i;
169 }
170
171 /**
172 * Release the tag's resources and reset the values.
173 */
174 public void release() {
175
176 super.release();
177
178
179 originalName = null;
180 originalProperty = null;
181 originalNesting = null;
182 originalNestingName = null;
183 }
184 }