View Javadoc

1   /*
2    * $Id: NestedWriteNestingTag.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;
19  
20  import org.apache.struts.taglib.TagUtils;
21  
22  import javax.servlet.http.HttpServletRequest;
23  import javax.servlet.jsp.JspException;
24  import javax.servlet.jsp.tagext.BodyTagSupport;
25  
26  /***
27   * NestedWriteNestingTag.
28   *
29   * Created so developers could have a more elegant way of getting to the
30   * underlying nested property their tag properties are referencing.
31   *
32   * @version $Rev: 376843 $
33   * @since Struts 1.1
34   */
35  public class NestedWriteNestingTag extends BodyTagSupport {
36      /* the usual private member variables */
37      private boolean filter = false;
38      private String property = null;
39      private String id = null;
40      private String originalProperty = null;
41  
42      /***
43       * Getter method for the <i>property</i> property
44       *
45       * @return String value of the property property
46       */
47      public String getProperty() {
48          return this.property;
49      }
50  
51      /***
52       * Setter method for the <i>property</i> property
53       *
54       * @param newProperty new value for the property property
55       */
56      public void setProperty(String newProperty) {
57          this.property = newProperty;
58      }
59  
60      /***
61       * Getter method for the <i>id</i> property
62       *
63       * @return String value for the id property
64       */
65      public String getId() {
66          return id;
67      }
68  
69      /***
70       * Setter method for the <i>id</i> property
71       *
72       * @param id new value for the id property
73       */
74      public void setId(String id) {
75          this.id = id;
76      }
77  
78      /***
79       * Getter method for the <i>filter</i> property
80       *
81       * @return String value of the filter property
82       */
83      public boolean getFilter() {
84          return this.filter;
85      }
86  
87      /***
88       * Setter method for the <i>filter</i> property
89       *
90       * @param newFilter new value for the filter property
91       */
92      public void setFilter(boolean newFilter) {
93          this.filter = newFilter;
94      }
95  
96      /***
97       * Overriding method of the heart of the tag. Gets the relative property
98       * and tells the JSP engine to evaluate its body content.
99       *
100      * @return int JSP continuation directive.
101      */
102     public int doStartTag() throws JspException {
103         // set the original property
104         originalProperty = property;
105 
106         HttpServletRequest request =
107             (HttpServletRequest) pageContext.getRequest();
108         String nesting =
109             NestedPropertyHelper.getAdjustedProperty(request, property);
110 
111         if (id != null) {
112             // use it as a scripting variable instead
113             pageContext.setAttribute(id, nesting);
114         } else {
115             /* write output, filtering if required */
116             if (this.filter) {
117                 TagUtils.getInstance().write(pageContext,
118                     TagUtils.getInstance().filter(nesting));
119             } else {
120                 TagUtils.getInstance().write(pageContext, nesting);
121             }
122         }
123 
124         /* continue with page processing */
125         return (SKIP_BODY);
126     }
127 
128     public int doEndTag() throws JspException {
129         // do the super thing
130         int i = super.doEndTag();
131 
132         // reset the property
133         property = originalProperty;
134 
135         // complete
136         return i;
137     }
138 
139     /***
140      * JSP method to release all resources held by the tag.
141      */
142     public void release() {
143         super.release();
144         this.filter = false;
145         this.property = null;
146         this.originalProperty = null;
147     }
148 }