KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > struts > taglib > nested > logic > NestedIterateTag


1 /*
2  * $Id: NestedIterateTag.java 54929 2004-10-16 16:38:42Z germuska $
3  *
4  * Copyright 1999-2004 The Apache Software Foundation.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18 package org.apache.struts.taglib.nested.logic;
19
20 import java.util.*;
21 import javax.servlet.http.HttpServletRequest JavaDoc;
22 import javax.servlet.jsp.JspException JavaDoc;
23
24 import org.apache.struts.taglib.logic.IterateTag;
25 import org.apache.struts.taglib.nested.NestedNameSupport;
26 import org.apache.struts.taglib.nested.NestedPropertyHelper;
27
28 /**
29  * NestedIterateTag.
30  * Slightly more complex that the other extensions. This one has to yield a
31  * proper index property. Very taxing.
32  *
33  * @since Struts 1.1
34  * @version $Rev: 54929 $ $Date: 2004-10-16 17:38:42 +0100 (Sat, 16 Oct 2004) $
35  */

36 public class NestedIterateTag extends IterateTag implements NestedNameSupport {
37
38   /**
39    * Overriding method of the heart of the matter. Gets the relative property
40    * and leaves the rest up to the original tag implementation. Sweet.
41    * @return int JSP continuation directive.
42    * This is in the hands of the super class.
43    */

44   public int doStartTag() throws JspException JavaDoc {
45     // original values
46
originalName = getName();
47     originalProperty = getProperty();
48
49     // set the ID to make the super tag happy
50
if (id == null || id.trim().length() == 0) { id = property; }
51
52     // the request object
53
HttpServletRequest JavaDoc request = (HttpServletRequest JavaDoc)pageContext.getRequest();
54
55     // original nesting details
56
originalNesting = NestedPropertyHelper.getCurrentProperty(request);
57     originalNestingName = NestedPropertyHelper.getCurrentName(request, this);
58
59     // set the bean if it's been provided
60
// (the bean that's been provided! get it!?... nevermind)
61
if (getName() == null) {
62       // the qualified nesting value
63
nesting = NestedPropertyHelper.getAdjustedProperty(request, getProperty());
64     } else {
65       // it's just the property
66
nesting = getProperty();
67     }
68
69     // set the properties
70
NestedPropertyHelper.setNestedProperties(request, this);
71
72     // get the original result
73
int temp = super.doStartTag();
74
75     // set the new reference (including the index etc)
76
NestedPropertyHelper.setName(request, getName());
77     NestedPropertyHelper.setProperty(request, deriveNestedProperty());
78
79     // return the result
80
return temp;
81   }
82
83   /**
84    * The only added property to the class. For use in proper nesting.
85    * @return String value of the property and the current index or mapping.
86    */

87   private String JavaDoc deriveNestedProperty() {
88     Object JavaDoc idObj = pageContext.getAttribute(id);
89     if (idObj instanceof Map.Entry) {
90       return nesting + "("+ ((Map.Entry)idObj).getKey() +")";
91     } else {
92       return nesting + "["+ this.getIndex() +"]";
93     }
94   }
95
96   /**
97    * This is only overriden as the include reference will need it's index
98    * updated.
99    *
100    * @return int JSP continuation directive.
101    */

102   public int doAfterBody() throws JspException JavaDoc {
103     // store original result
104
int temp = super.doAfterBody();
105     HttpServletRequest JavaDoc request = (HttpServletRequest JavaDoc)pageContext.getRequest();
106     if (temp != SKIP_BODY) {
107       // set the new reference
108
NestedPropertyHelper.setProperty(request, deriveNestedProperty());
109     }
110     // return super result
111
return temp;
112   }
113
114   /**
115    * Complete the processing of the tag. The nested tags here will restore
116    * all the original value for the tag itself and the nesting context.
117    * @return int to describe the next step for the JSP processor
118    * @throws JspException for the bad things JSP's do
119    */

120   public int doEndTag() throws JspException JavaDoc {
121     // the super's thing
122
int i = super.doEndTag();
123
124     // request
125
HttpServletRequest JavaDoc request = (HttpServletRequest JavaDoc)pageContext.getRequest();
126     // reset the original tag values
127
super.setName(originalName);
128     super.setProperty(originalProperty);
129
130     // reset the original nesting values
131
if (originalNesting == null) {
132       NestedPropertyHelper.deleteReference(request);
133     } else {
134       NestedPropertyHelper.setProperty(request, originalNesting);
135       NestedPropertyHelper.setName(request, originalNestingName);
136     }
137     // job done
138
return i;
139   }
140
141   /**
142    * Release the tag's resources and reset the values.
143    */

144   public void release() {
145     // let the super release
146
super.release();
147     // reset the original value place holders
148
originalName = null;
149     originalProperty = null;
150     originalNesting = null;
151     originalNestingName = null;
152   }
153
154
155   // The current nesting
156
private String JavaDoc nesting = null;
157
158   // original tag properties
159
private String JavaDoc originalName = null;
160   private String JavaDoc originalProperty = null;
161
162   // original nesting environment
163
private String JavaDoc originalNesting = null;
164   private String JavaDoc originalNestingName = null;
165 }
166
Popular Tags