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