KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: ImportAttributeTag.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.util.Iterator JavaDoc;
23
24 import javax.servlet.jsp.JspException JavaDoc;
25 import javax.servlet.jsp.PageContext JavaDoc;
26 import javax.servlet.jsp.tagext.TagSupport JavaDoc;
27
28 import org.apache.struts.taglib.tiles.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 JavaDoc {
39
40     /**
41      * Class name of object.
42      */

43     private String JavaDoc name = null;
44
45
46     /**
47      * The scope name.
48      */

49     private String JavaDoc 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 <code>ignore</code>.
57      * Default value is <code>false</code>, which throws an exception.
58      * Only "attribute not found" - errors are ignored.
59      */

60   protected boolean isErrorIgnored = false;
61
62
63     /**
64      * Release all allocated resources.
65      */

66     public void release() {
67
68         super.release();
69         name = null;
70         scopeName = null;
71         scope = PageContext.PAGE_SCOPE;
72         isErrorIgnored = false;
73     }
74
75     /**
76      * Get the name.
77      * @return Name.
78      */

79     public String JavaDoc getName()
80      {
81      return (this.name);
82      }
83
84
85     /**
86      * Set the name.
87      * @param name The new name
88      */

89     public void setName(String JavaDoc name)
90      {
91      this.name = name;
92      }
93
94     /**
95      * Set the scope.
96      * @param scope Scope.
97      */

98     public void setScope(String JavaDoc scope)
99       {
100       this.scopeName = scope;
101       }
102
103     /**
104      * Get scope.
105      * @return Scope.
106      */

107   public String JavaDoc getScope()
108   {
109   return scopeName;
110   }
111
112     /**
113      * Set ignore flag.
114      * @param ignore default: <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 void setIgnore(boolean ignore)
118     {
119     this.isErrorIgnored = ignore;
120     }
121
122     /**
123      * Get ignore flag.
124      * @return default: <code>false</code>: Exception is thrown when attribute is not found, set to <code>
125      * true</code> to ignore missing attributes silently
126      */

127   public boolean getIgnore()
128   {
129   return isErrorIgnored;
130   }
131
132     // --------------------------------------------------------- Public Methods
133

134
135     /**
136      * Expose the requested property from component context.
137      *
138      * @exception JspException On errors processing tag.
139      */

140 public int doStartTag() throws JspException JavaDoc
141     {
142       // retrieve component context
143
ComponentContext compContext = (ComponentContext)pageContext.getAttribute(ComponentConstants.COMPONENT_CONTEXT, PageContext.REQUEST_SCOPE);
144     if( compContext == null )
145         throw new JspException JavaDoc ( "Error - tag importAttribute : no tiles context found." );
146
147       // set scope
148
scope = TagUtils.getScope( scopeName, PageContext.PAGE_SCOPE );
149
150       // push attribute in requested context.
151
if( name != null )
152       {
153       Object JavaDoc value = compContext.getAttribute(name);
154         // Check if value exist and if we must send a runtime exception
155
if( value == null )
156         if(!isErrorIgnored)
157           throw new JspException JavaDoc ( "Error - tag importAttribute : property '"+ name + "' not found in context. Check tag syntax" );
158          else
159           return SKIP_BODY;
160
161       pageContext.setAttribute(name, value, scope);
162       }
163      else
164       { // set all attributes
165
Iterator JavaDoc names = compContext.getAttributeNames();
166       while(names.hasNext())
167         {
168         String JavaDoc name = (String JavaDoc)names.next();
169         if(name == null ) {
170           if(!isErrorIgnored)
171             throw new JspException JavaDoc ( "Error - tag importAttribute : encountered an attribute with key 'null'" );
172           else
173             return SKIP_BODY;
174         }
175
176         Object JavaDoc value = compContext.getAttribute(name);
177         // Check if value exist and if we must send a runtime exception
178
if( value == null ) {
179           if(!isErrorIgnored)
180             throw new JspException JavaDoc ( "Error - tag importAttribute : property '"+ name + "' has a value of 'null'" );
181           else
182             return SKIP_BODY;
183         }
184         pageContext.setAttribute(name, value, scope);
185         } // end loop
186
} // end else
187

188       // Continue processing this page
189
return SKIP_BODY;
190     }
191
192     /**
193      * Clean up after processing this enumeration.
194      *
195      * @exception JspException On errors processing tag.
196      */

197   public int doEndTag() throws JspException JavaDoc
198     {
199     return (EVAL_PAGE);
200     }
201
202 }
203
Popular Tags