View Javadoc

1   /*
2    * $Id: UseAttributeTag.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 javax.servlet.jsp.JspException;
23  import javax.servlet.jsp.PageContext;
24  import javax.servlet.jsp.tagext.TagSupport;
25  
26  import org.apache.struts.tiles.taglib.util.TagUtils;
27  import org.apache.struts.tiles.ComponentContext;
28  
29  
30  /***
31   * Custom tag exposing a component attribute to page.
32   *
33   */
34  public class UseAttributeTag extends TagSupport {
35  
36  
37      // ----------------------------------------------------- Instance Variables
38  
39  
40      /***
41       * Class name of object.
42       */
43      private String  classname = null;
44  
45  
46      /***
47       * The scope name.
48       */
49      private String scopeName = null;
50  
51      /***
52       * The scope value.
53       */
54      private int scope = PageContext.PAGE_SCOPE;
55  
56  
57  
58      /***
59       * The attribute name to be exposed.
60       */
61      private String attributeName = null;
62  
63      /***
64       * Are errors ignored. This is the property for attribute 'ignore'.
65       * Default value is <code>false</code>, which throws an exception.
66       * Only "attribute not found" - errors are ignored.
67       */
68    protected boolean isErrorIgnored = false;
69  
70  
71      // ------------------------------------------------------------- Properties
72  
73  
74      /***
75       * Release all allocated resources.
76       */
77      public void release() {
78  
79          super.release();
80          attributeName = null;
81          classname = null;
82          scope = PageContext.PAGE_SCOPE;
83          scopeName = null;
84          isErrorIgnored = false;
85            // Parent doesn't clear id, so we do it
86            // bug reported by Heath Chiavettone on 18 Mar 2002
87          id = null;
88      }
89  
90      /***
91       * Get class name.
92       */
93      public String getClassname() {
94  
95    return (this.classname);
96  
97      }
98  
99  
100     /***
101      * Set the class name.
102      *
103      * @param name The new class name.
104      */
105     public void setClassname(String name) {
106 
107   this.classname = name;
108 
109     }
110 
111     /***
112      * Set name.
113      */
114   public void setName(String value){
115     this.attributeName = value;
116   }
117 
118     /***
119      * Get name.
120      */
121   public String getName()
122   {
123   return attributeName;
124   }
125 
126     /***
127      * Set the scope.
128      *
129      * @param scope The new scope.
130      */
131     public void setScope(String scope) {
132   this.scopeName = scope;
133     }
134 
135     /***
136      * Get scope.
137      */
138   public String getScope()
139   {
140   return scopeName;
141   }
142 
143     /***
144      * Set ignore.
145      */
146   public void setIgnore(boolean ignore)
147     {
148     this.isErrorIgnored = ignore;
149     }
150 
151     /***
152      * Get ignore.
153      */
154   public boolean getIgnore()
155   {
156   return isErrorIgnored;
157   }
158 
159     // --------------------------------------------------------- Public Methods
160 
161 
162     /***
163      * Expose the requested attribute from component context.
164      *
165      * @exception JspException if a JSP exception has occurred
166      */
167   public int doStartTag() throws JspException
168     {
169       // Do a local copy of id
170     String localId=this.id;
171     if( localId==null )
172       localId=attributeName;
173 
174     ComponentContext compContext = (ComponentContext)pageContext.getAttribute( ComponentConstants.COMPONENT_CONTEXT, PageContext.REQUEST_SCOPE);
175     if( compContext == null )
176       throw new JspException ( "Error - tag useAttribute : no tiles context found." );
177 
178     Object value = compContext.getAttribute(attributeName);
179         // Check if value exists and if we must send a runtime exception
180     if( value == null )
181       if(!isErrorIgnored)
182         throw new JspException ( "Error - tag useAttribute : attribute '"+ attributeName + "' not found in context. Check tag syntax" );
183        else
184         return SKIP_BODY;
185 
186     if( scopeName != null )
187       {
188       scope = TagUtils.getScope( scopeName, PageContext.PAGE_SCOPE );
189       if(scope!=ComponentConstants.COMPONENT_SCOPE)
190         pageContext.setAttribute(localId, value, scope);
191       }
192      else
193       pageContext.setAttribute(localId, value);
194 
195       // Continue processing this page
196     return SKIP_BODY;
197     }
198 
199 
200 
201 
202     /***
203      * Clean up after processing this enumeration.
204      *
205      * @exception JspException if a JSP exception has occurred
206      */
207   public int doEndTag() throws JspException
208     {
209     return (EVAL_PAGE);
210     }
211 
212 }