View Javadoc

1   /*
2    * $Id: GetAttributeTag.java 421151 2006-07-12 06:07:14Z wsmoak $
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  
19  
20  package org.apache.struts.tiles.taglib;
21  
22  import java.io.IOException;
23  
24  import javax.servlet.http.HttpServletRequest;
25  import javax.servlet.jsp.JspException;
26  import javax.servlet.jsp.PageContext;
27  import javax.servlet.jsp.tagext.TagSupport;
28  
29  import org.apache.struts.tiles.ComponentContext;
30  
31    /***
32     * Retrieve the value of the specified component/template attribute property,
33     * and render it to the current JspWriter as a String.
34     * The usual toString() conversion is applied on the found value.
35     */
36  public class GetAttributeTag extends TagSupport implements ComponentConstants {
37  
38    private String attribute = null;
39      /*** Role attribute */
40    private String role = null;
41      /***
42       * Do we ignore error if attribute is not found.
43       * Default value is <code>false</code>, which will throw an exception.
44       */
45    private boolean isErrorIgnored = false;
46  
47    /***
48     * Default constructor.
49     */
50    public GetAttributeTag() {
51      super();
52    }
53  
54      /***
55       * Release all allocated resources.
56       */
57      public void release() {
58  
59          super.release();
60          attribute = null;
61          role = null;
62          isErrorIgnored = false;
63      }
64  
65      /***
66       * Set attribute.
67       * @param attribute Attribute.
68       */
69    public void setAttribute(String attribute){
70      this.attribute = attribute;
71    }
72  
73      /***
74       * Get attribute.
75       * @return Attribute.
76       */
77    public String getAttribute()
78    {
79    return attribute;
80    }
81  
82      /***
83       * Set Name.
84       * Same as setAttribute().
85       * @param value Attribute.
86       */
87    public void setName(String value)
88      {
89      this.attribute = value;
90      }
91  
92      /***
93       * Get Name.
94       * Set as getAttribute().
95       * @return Attribute.
96       */
97    public String getName()
98    {
99    return attribute;
100   }
101 
102     /***
103      * Set ignoring flag when attribute is not found.
104      * @param ignore default: <code>false</code>: Exception is thrown when attribute is not found, set to <code>
105      * true</code> to ignore missing attributes silently
106      */
107   public void setIgnore(boolean ignore)
108     {
109     this.isErrorIgnored = ignore;
110     }
111 
112     /***
113      * Get ignore flag.
114      * @return <code>false</code>: Exception is thrown when attribute is not found, set to <code>
115      * true</code> to ignore missing attributes silently
116      */
117   public boolean getIgnore()
118   {
119   return isErrorIgnored;
120   }
121 
122     /***
123      * Set role.
124      * @param role The role the user must be in to store content.
125      */
126    public void setRole(String role) {
127       this.role = role;
128    }
129 
130     /***
131      * Get role.
132      * @return Role.
133      */
134   public String getRole()
135   {
136   return role;
137   }
138 
139     /***
140      * Close tag.
141      * @throws JspException On error processing tag.
142      */
143   public int doEndTag() throws JspException {
144 
145       // Check role
146     if(role != null && !((HttpServletRequest)pageContext.getRequest()).isUserInRole(role) )
147       {
148       return EVAL_PAGE;
149       } // end if
150 
151       // Get context
152     ComponentContext compContext = (ComponentContext)pageContext.getAttribute( ComponentConstants.COMPONENT_CONTEXT, PageContext.REQUEST_SCOPE);
153 
154     if( compContext == null )
155       throw new JspException ( "Error - tag.getAsString : component context is not defined. Check tag syntax" );
156 
157     Object value = compContext.getAttribute(attribute);
158     if( value == null)
159       { // no value : throw error or fail silently according to ignore
160       if(isErrorIgnored == false )
161         throw new JspException ( "Error - tag.getAsString : attribute '"+ attribute + "' not found in context. Check tag syntax" );
162        else
163         return EVAL_PAGE;
164       } // end if
165 
166 
167     try
168       {
169       pageContext.getOut().print( value );
170       }
171      catch( IOException ex )
172       {
173       ex.printStackTrace();
174       throw new JspException ( "Error - tag.getProperty : IOException ", ex);
175       }
176 
177     return EVAL_PAGE;
178   }
179 }