View Javadoc

1   /*
2    * $Id: NestedIterateTag.java 376843 2006-02-10 21:02:56Z husted $
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 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      // The current nesting
39      private String nesting = null;
40  
41      // original tag properties
42      private String originalName = null;
43      private String originalProperty = null;
44  
45      // original nesting environment
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          // original values
59          originalName = getName();
60          originalProperty = getProperty();
61  
62          // set the ID to make the super tag happy
63          if ((id == null) || (id.trim().length() == 0)) {
64              id = property;
65          }
66  
67          // the request object
68          HttpServletRequest request =
69              (HttpServletRequest) pageContext.getRequest();
70  
71          // original nesting details
72          originalNesting = NestedPropertyHelper.getCurrentProperty(request);
73          originalNestingName =
74              NestedPropertyHelper.getCurrentName(request, this);
75  
76          // set the bean if it's been provided
77          // (the bean that's been provided! get it!?... nevermind)
78          if (getName() == null) {
79              // the qualified nesting value
80              nesting =
81                  NestedPropertyHelper.getAdjustedProperty(request, getProperty());
82          } else {
83              // it's just the property
84              nesting = getProperty();
85          }
86  
87          // set the properties
88          NestedPropertyHelper.setNestedProperties(request, this);
89  
90          // get the original result
91          int temp = super.doStartTag();
92  
93          // set the new reference (including the index etc)
94          NestedPropertyHelper.setName(request, getName());
95          NestedPropertyHelper.setProperty(request, deriveNestedProperty());
96  
97          // return the result
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         // store original result
124         int temp = super.doAfterBody();
125         HttpServletRequest request =
126             (HttpServletRequest) pageContext.getRequest();
127 
128         if (temp != SKIP_BODY) {
129             // set the new reference
130             NestedPropertyHelper.setProperty(request, deriveNestedProperty());
131         }
132 
133         // return super result
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         // the super's thing
146         int i = super.doEndTag();
147 
148         // request
149         HttpServletRequest request =
150             (HttpServletRequest) pageContext.getRequest();
151 
152         // reset the original tag values
153         super.setName(originalName);
154         super.setProperty(originalProperty);
155 
156         // reset the original nesting values
157         if (originalNesting == null) {
158             NestedPropertyHelper.deleteReference(request);
159         } else {
160             NestedPropertyHelper.setProperty(request, originalNesting);
161             NestedPropertyHelper.setName(request, originalNestingName);
162         }
163 
164         // job done
165         return i;
166     }
167 
168     /***
169      * Release the tag's resources and reset the values.
170      */
171     public void release() {
172         // let the super release
173         super.release();
174 
175         // reset the original value place holders
176         originalName = null;
177         originalProperty = null;
178         originalNesting = null;
179         originalNestingName = null;
180     }
181 }