KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > struts > taglib > tiles > GetAttributeTag


1 /*
2  * $Id: GetAttributeTag.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
19
20 package org.apache.struts.taglib.tiles;
21
22 import java.io.IOException JavaDoc;
23
24 import javax.servlet.http.HttpServletRequest JavaDoc;
25 import javax.servlet.jsp.JspException JavaDoc;
26 import javax.servlet.jsp.PageContext JavaDoc;
27 import javax.servlet.jsp.tagext.TagSupport JavaDoc;
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 JavaDoc implements ComponentConstants {
37
38   private String JavaDoc attribute = null;
39     /** Role attribute */
40   private String JavaDoc 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 JavaDoc attribute){
70     this.attribute = attribute;
71   }
72
73     /**
74      * Get attribute.
75      * @return Attribute.
76      */

77   public String JavaDoc 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 JavaDoc value)
88     {
89     this.attribute = value;
90     }
91
92     /**
93      * Get Name.
94      * Set as getAttribute().
95      * @return Attribute.
96      */

97   public String JavaDoc 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 JavaDoc role) {
127       this.role = role;
128    }
129
130     /**
131      * Get role.
132      * @return Role.
133      */

134   public String JavaDoc 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 JavaDoc {
144
145       // Check role
146
if(role != null && !((HttpServletRequest JavaDoc)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 JavaDoc ( "Error - tag.getAsString : component context is not defined. Check tag syntax" );
156
157     Object JavaDoc 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 JavaDoc ( "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 JavaDoc ex )
172       {
173       ex.printStackTrace();
174       throw new JspException JavaDoc ( "Error - tag.getProperty : IOException ");
175       }
176
177     return EVAL_PAGE;
178   }
179 }
180
Popular Tags