2
* $Header: /home/cvs/jakarta-struts/src/share/org/apache/struts/tiles/ComponentContext.java,v 1.7 2004/03/14 06:23:43 sraeburn Exp $
4
* $Date: 2004/03/14 06:23:43 $
6
* Copyright 1999-2004 The Apache Software Foundation.
8
* Licensed under the Apache License, Version 2.0 (the "License");
9
* you may not use this file except in compliance with the License.
10
* You may obtain a copy of the License at
12
* http://www.apache.org/licenses/LICENSE-2.0
14
* Unless required by applicable law or agreed to in writing, software
15
* distributed under the License is distributed on an "AS IS" BASIS,
16
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
* See the License for the specific language governing permissions and
18
* limitations under the License.
21
package org.apache.struts.tiles;
23
import java.io.Serializable;
24
import java.util.Collections;
25
import java.util.HashMap;
26
import java.util.Iterator;
30
import javax.servlet.ServletRequest;
31
import javax.servlet.jsp.PageContext;
33
import org.apache.struts.taglib.tiles.ComponentConstants;
38
public class ComponentContext implements Serializable {
41
* Component attributes.
43
private Map attributes=null;
48
public ComponentContext() {
54
* Create a context and set specified attributes.
55
* @param attributes Attributes to initialize context.
57
public ComponentContext(Map attributes) {
58
if (attributes != null) {
59
this.attributes = new HashMap(attributes);
64
* Add all attributes to this context.
65
* Copies all of the mappings from the specified attribute map to this context.
66
* New attribute mappings will replace any mappings that this context had for any of the keys
67
* currently in the specified attribute map.
68
* @param newAttributes Attributes to add.
70
public void addAll(Map newAttributes) {
71
if (attributes == null) {
72
attributes = new HashMap(newAttributes);
76
attributes.putAll(newAttributes);
80
* Add all missing attributes to this context.
81
* Copies all of the mappings from the specified attributes map to this context.
82
* New attribute mappings will be added only if they don't already exist in
84
* @param defaultAttributes Attributes to add.
86
public void addMissing(Map defaultAttributes) {
87
if (defaultAttributes == null) {
91
if (attributes == null) {
92
attributes = new HashMap(defaultAttributes);
96
Set entries = defaultAttributes.entrySet();
97
Iterator iterator = entries.iterator();
98
while (iterator.hasNext()) {
99
Map.Entry entry = (Map.Entry) iterator.next();
100
if (!attributes.containsKey(entry.getKey())) {
101
attributes.put(entry.getKey(), entry.getValue());
107
* Get an attribute from context.
108
* @param name Name of the attribute.
111
public Object getAttribute(String name) {
112
if (attributes == null){
116
return attributes.get(name);
120
* Get names of all attributes.
123
public Iterator getAttributeNames() {
124
if (attributes == null) {
125
return Collections.EMPTY_LIST.iterator();
128
return attributes.keySet().iterator();
132
* Put a new attribute to context.
133
* @param name Name of the attribute.
134
* @param value Value of the attribute.
136
public void putAttribute(String name, Object value) {
137
if (attributes == null) {
138
attributes = new HashMap();
141
attributes.put(name, value);
145
* Find object in one of the contexts.
146
* Order : component then pageContext.findAttribute()
147
* @param beanName Name of the bean to find.
148
* @param pageContext Page context.
149
* @return Requested bean or <code>null</code> if not found.
151
public Object findAttribute(String beanName, PageContext pageContext) {
152
Object attribute = getAttribute(beanName);
153
if (attribute == null) {
154
attribute = pageContext.findAttribute(beanName);
161
* Get object from requested context.
162
* Context can be 'component'.
163
* @param beanName Name of the bean to find.
164
* @param scope Search scope (see {@link PageContext}).
165
* @param pageContext Page context.
166
* @return requested bean or <code>null</code> if not found.
168
public Object getAttribute(
171
PageContext pageContext) {
173
if (scope == ComponentConstants.COMPONENT_SCOPE){
174
return getAttribute(beanName);
177
return pageContext.getAttribute(beanName, scope);
181
* Get component context from request.
182
* @param request ServletRequest.
183
* @return ComponentContext
185
static public ComponentContext getContext(ServletRequest request) {
186
return (ComponentContext) request.getAttribute(
187
ComponentConstants.COMPONENT_CONTEXT);
191
* Store component context into request.
192
* @param context ComponentContext to store.
193
* @param request Request to store ComponentContext.
195
static public void setContext(
196
ComponentContext context,
197
ServletRequest request) {
199
request.setAttribute(ComponentConstants.COMPONENT_CONTEXT, context);