KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > struts > tiles > ComponentContext


1 /*
2  * $Id: ComponentContext.java 55576 2004-10-26 06:31:13Z husted $
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 package org.apache.struts.tiles;
20
21 import java.io.Serializable JavaDoc;
22 import java.util.Collections JavaDoc;
23 import java.util.HashMap JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.Map JavaDoc;
26 import java.util.Set JavaDoc;
27
28 import javax.servlet.ServletRequest JavaDoc;
29 import javax.servlet.jsp.PageContext JavaDoc;
30
31 import org.apache.struts.taglib.tiles.ComponentConstants;
32
33 /**
34  * Component context.
35  */

36 public class ComponentContext implements Serializable JavaDoc {
37
38     /**
39      * Component attributes.
40      */

41     private Map JavaDoc attributes=null;
42
43     /**
44      * Constructor.
45      */

46     public ComponentContext() {
47         super();
48     }
49
50     /**
51      * Constructor.
52      * Create a context and set specified attributes.
53      * @param attributes Attributes to initialize context.
54      */

55     public ComponentContext(Map JavaDoc attributes) {
56         if (attributes != null) {
57             this.attributes = new HashMap JavaDoc(attributes);
58         }
59     }
60
61     /**
62      * Add all attributes to this context.
63      * Copies all of the mappings from the specified attribute map to this context.
64      * New attribute mappings will replace any mappings that this context had for any of the keys
65      * currently in the specified attribute map.
66      * @param newAttributes Attributes to add.
67      */

68     public void addAll(Map JavaDoc newAttributes) {
69         if (attributes == null) {
70             attributes = new HashMap JavaDoc(newAttributes);
71             return;
72         }
73         
74         attributes.putAll(newAttributes);
75     }
76
77     /**
78      * Add all missing attributes to this context.
79      * Copies all of the mappings from the specified attributes map to this context.
80      * New attribute mappings will be added only if they don't already exist in
81      * this context.
82      * @param defaultAttributes Attributes to add.
83      */

84     public void addMissing(Map JavaDoc defaultAttributes) {
85         if (defaultAttributes == null) {
86             return;
87         }
88         
89         if (attributes == null) {
90             attributes = new HashMap JavaDoc(defaultAttributes);
91             return;
92         }
93
94         Set JavaDoc entries = defaultAttributes.entrySet();
95         Iterator JavaDoc iterator = entries.iterator();
96         while (iterator.hasNext()) {
97             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) iterator.next();
98             if (!attributes.containsKey(entry.getKey())) {
99                 attributes.put(entry.getKey(), entry.getValue());
100             }
101         }
102     }
103
104     /**
105      * Get an attribute from context.
106      * @param name Name of the attribute.
107      * @return <{Object}>
108      */

109     public Object JavaDoc getAttribute(String JavaDoc name) {
110         if (attributes == null){
111             return null;
112         }
113         
114         return attributes.get(name);
115     }
116
117     /**
118      * Get names of all attributes.
119      * @return <{Object}>
120      */

121     public Iterator JavaDoc getAttributeNames() {
122         if (attributes == null) {
123             return Collections.EMPTY_LIST.iterator();
124         }
125         
126         return attributes.keySet().iterator();
127     }
128
129     /**
130      * Put a new attribute to context.
131      * @param name Name of the attribute.
132      * @param value Value of the attribute.
133      */

134     public void putAttribute(String JavaDoc name, Object JavaDoc value) {
135         if (attributes == null) {
136             attributes = new HashMap JavaDoc();
137         }
138
139         attributes.put(name, value);
140     }
141
142     /**
143      * Find object in one of the contexts.
144      * Order : component then pageContext.findAttribute()
145      * @param beanName Name of the bean to find.
146      * @param pageContext Page context.
147      * @return Requested bean or <code>null</code> if not found.
148      */

149     public Object JavaDoc findAttribute(String JavaDoc beanName, PageContext JavaDoc pageContext) {
150         Object JavaDoc attribute = getAttribute(beanName);
151         if (attribute == null) {
152             attribute = pageContext.findAttribute(beanName);
153         }
154         
155         return attribute;
156     }
157
158     /**
159      * Get object from requested context.
160      * Context can be 'component'.
161      * @param beanName Name of the bean to find.
162      * @param scope Search scope (see {@link PageContext}).
163      * @param pageContext Page context.
164      * @return requested bean or <code>null</code> if not found.
165      */

166     public Object JavaDoc getAttribute(
167         String JavaDoc beanName,
168         int scope,
169         PageContext JavaDoc pageContext) {
170             
171         if (scope == ComponentConstants.COMPONENT_SCOPE){
172             return getAttribute(beanName);
173         }
174         
175         return pageContext.getAttribute(beanName, scope);
176     }
177
178     /**
179      * Get component context from request.
180      * @param request ServletRequest.
181      * @return ComponentContext or null if context is not found or an
182      * jspException is present in the request.
183      */

184     static public ComponentContext getContext(ServletRequest JavaDoc request) {
185        if (request.getAttribute("javax.servlet.jsp.jspException") != null) {
186            return null;
187         } return (ComponentContext) request.getAttribute(
188             ComponentConstants.COMPONENT_CONTEXT);
189     }
190
191     /**
192      * Store component context into request.
193      * @param context ComponentContext to store.
194      * @param request Request to store ComponentContext.
195      */

196     static public void setContext(
197         ComponentContext context,
198         ServletRequest JavaDoc request) {
199             
200         request.setAttribute(ComponentConstants.COMPONENT_CONTEXT, context);
201     }
202 }
203
Popular Tags