View Javadoc

1   /*
2    * $Id: ImportAttributeTag.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.util.Iterator;
23  
24  import javax.servlet.jsp.JspException;
25  import javax.servlet.jsp.PageContext;
26  import javax.servlet.jsp.tagext.TagSupport;
27  
28  import org.apache.struts.tiles.taglib.util.TagUtils;
29  import org.apache.struts.tiles.ComponentContext;
30  
31  
32  /***
33    *  Import attribute from component to requested scope.
34    *  Attribute name and scope are optional. If not specified, all component
35    *  attributes are imported in page scope.
36   */
37  
38  public class ImportAttributeTag extends TagSupport {
39  
40      /***
41       * Class name of object.
42       */
43      private String  name = 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       * Are errors ignored. This is the property for attribute
57       * <code>ignore</code>.
58       * Default value is <code>false</code>, which throws an exception.
59       * Only "attribute not found" - errors are ignored.
60       */
61    protected boolean isErrorIgnored = false;
62  
63  
64      /***
65       * Release all allocated resources.
66       */
67      public void release() {
68  
69          super.release();
70          name = null;
71          scopeName = null;
72          scope = PageContext.PAGE_SCOPE;
73          isErrorIgnored = false;
74      }
75  
76      /***
77       * Get the name.
78       * @return Name.
79       */
80      public String getName()
81       {
82       return (this.name);
83       }
84  
85  
86      /***
87       * Set the name.
88       * @param name The new name
89       */
90      public void setName(String name)
91       {
92       this.name = name;
93       }
94  
95      /***
96       * Set the scope.
97       * @param scope Scope.
98       */
99      public void setScope(String scope)
100       {
101       this.scopeName = scope;
102       }
103 
104     /***
105      * Get scope.
106      * @return Scope.
107      */
108   public String getScope()
109   {
110   return scopeName;
111   }
112 
113     /***
114      * Set ignore flag.
115      * @param ignore default: <code>false</code>: Exception is thrown when
116      * attribute is not found, set to <code>
117      * true</code> to ignore missing attributes silently
118      */
119   public void setIgnore(boolean ignore)
120     {
121     this.isErrorIgnored = ignore;
122     }
123 
124     /***
125      * Get ignore flag.
126      * @return default: <code>false</code>: Exception is thrown when attribute
127      * is not found, set to <code>
128      * true</code> to ignore missing attributes silently
129      */
130   public boolean getIgnore()
131   {
132   return isErrorIgnored;
133   }
134 
135     // --------------------------------------------------------- Public Methods
136 
137 
138     /***
139      * Expose the requested property from component context.
140      *
141      * @exception JspException On errors processing tag.
142      */
143 public int doStartTag() throws JspException
144     {
145       // retrieve component context
146     ComponentContext compContext =
147         (ComponentContext)pageContext.getAttribute(
148             ComponentConstants.COMPONENT_CONTEXT, PageContext.REQUEST_SCOPE);
149     if( compContext == null )
150         throw new JspException ( "Error - tag importAttribute : "
151             + "no tiles context found." );
152 
153       // set scope
154     scope = TagUtils.getScope( scopeName, PageContext.PAGE_SCOPE );
155 
156       // push attribute in requested context.
157     if( name != null )
158       {
159       Object value = compContext.getAttribute(name);
160         // Check if value exist and if we must send a runtime exception
161       if( value == null )
162         if(!isErrorIgnored) {
163           throw new JspException ( "Error - tag importAttribute : property '"+
164               name + "' not found in context. Check tag syntax" );
165         }
166 
167       pageContext.setAttribute(name, value, scope);
168       }
169      else
170       { // set all attributes
171       Iterator names = compContext.getAttributeNames();
172       while(names.hasNext())
173         {
174         String name = (String)names.next();
175         if(name == null ) {
176           if(!isErrorIgnored)
177             throw new JspException ( "Error - tag importAttribute : "
178                 + "encountered an attribute with key 'null'" );
179           else
180             return SKIP_BODY;
181         }
182 
183         Object value = compContext.getAttribute(name);
184         // Check if value exist and if we must send a runtime exception
185         if( value == null ) {
186           if(!isErrorIgnored) {
187             throw new JspException ( "Error - tag importAttribute : property '"
188                 + name + "' has a value of 'null'" );
189           }
190         }
191         pageContext.setAttribute(name, value, scope);
192         } // end loop
193       } // end else
194 
195       // Continue processing this page
196     return SKIP_BODY;
197     }
198 
199     /***
200      * Clean up after processing this enumeration.
201      *
202      * @exception JspException On errors processing tag.
203      */
204   public int doEndTag() throws JspException
205     {
206     return (EVAL_PAGE);
207     }
208 
209 }