~ubuntu-branches/ubuntu/trusty/libstruts1.2-java/trusty-proposed

« back to all changes in this revision

Viewing changes to src/share/org/apache/struts/tiles/ComponentContext.java

  • Committer: Bazaar Package Importer
  • Author(s): Arnaud Vandyck
  • Date: 2004-11-19 15:35:25 UTC
  • Revision ID: james.westby@ubuntu.com-20041119153525-mdu08a76z4zo67xt
Tags: upstream-1.2.4
ImportĀ upstreamĀ versionĀ 1.2.4

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
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 $
 
3
 * $Revision: 1.7 $
 
4
 * $Date: 2004/03/14 06:23:43 $
 
5
 *
 
6
 * Copyright 1999-2004 The Apache Software Foundation.
 
7
 * 
 
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
 
11
 * 
 
12
 *      http://www.apache.org/licenses/LICENSE-2.0
 
13
 * 
 
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.
 
19
 */
 
20
 
 
21
package org.apache.struts.tiles;
 
22
 
 
23
import java.io.Serializable;
 
24
import java.util.Collections;
 
25
import java.util.HashMap;
 
26
import java.util.Iterator;
 
27
import java.util.Map;
 
28
import java.util.Set;
 
29
 
 
30
import javax.servlet.ServletRequest;
 
31
import javax.servlet.jsp.PageContext;
 
32
 
 
33
import org.apache.struts.taglib.tiles.ComponentConstants;
 
34
 
 
35
/**
 
36
 * Component context.
 
37
 */
 
38
public class ComponentContext implements Serializable {
 
39
 
 
40
    /**
 
41
     * Component attributes.
 
42
     */
 
43
    private Map attributes=null;
 
44
 
 
45
    /**
 
46
     * Constructor.
 
47
     */
 
48
    public ComponentContext() {
 
49
        super();
 
50
    }
 
51
 
 
52
    /**
 
53
     * Constructor.
 
54
     * Create a context and set specified attributes.
 
55
     * @param attributes Attributes to initialize context.
 
56
     */
 
57
    public ComponentContext(Map attributes) {
 
58
        if (attributes != null) {
 
59
            this.attributes = new HashMap(attributes);
 
60
        }
 
61
    }
 
62
 
 
63
    /**
 
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.
 
69
     */
 
70
    public void addAll(Map newAttributes) {
 
71
        if (attributes == null) {
 
72
            attributes = new HashMap(newAttributes);
 
73
            return;
 
74
        }
 
75
        
 
76
        attributes.putAll(newAttributes);
 
77
    }
 
78
 
 
79
    /**
 
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
 
83
     * this context.
 
84
     * @param defaultAttributes Attributes to add.
 
85
     */
 
86
    public void addMissing(Map defaultAttributes) {
 
87
        if (defaultAttributes == null) {
 
88
            return;
 
89
        }
 
90
        
 
91
        if (attributes == null) {
 
92
            attributes = new HashMap(defaultAttributes);
 
93
            return;
 
94
        }
 
95
 
 
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());
 
102
            }
 
103
        }
 
104
    }
 
105
 
 
106
    /**
 
107
     * Get an attribute from context.
 
108
     * @param name Name of the attribute.
 
109
     * @return <{Object}>
 
110
     */
 
111
    public Object getAttribute(String name) {
 
112
        if (attributes == null){
 
113
            return null;
 
114
        }
 
115
        
 
116
        return attributes.get(name);
 
117
    }
 
118
 
 
119
    /**
 
120
     * Get names of all attributes.
 
121
     * @return <{Object}>
 
122
     */
 
123
    public Iterator getAttributeNames() {
 
124
        if (attributes == null) {
 
125
            return Collections.EMPTY_LIST.iterator();
 
126
        }
 
127
        
 
128
        return attributes.keySet().iterator();
 
129
    }
 
130
 
 
131
    /**
 
132
     * Put a new attribute to context.
 
133
     * @param name Name of the attribute.
 
134
     * @param value Value of the attribute.
 
135
     */
 
136
    public void putAttribute(String name, Object value) {
 
137
        if (attributes == null) {
 
138
            attributes = new HashMap();
 
139
        }
 
140
 
 
141
        attributes.put(name, value);
 
142
    }
 
143
 
 
144
    /**
 
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.
 
150
     */
 
151
    public Object findAttribute(String beanName, PageContext pageContext) {
 
152
        Object attribute = getAttribute(beanName);
 
153
        if (attribute == null) {
 
154
            attribute = pageContext.findAttribute(beanName);
 
155
        }
 
156
        
 
157
        return attribute;
 
158
    }
 
159
 
 
160
    /**
 
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.
 
167
     */
 
168
    public Object getAttribute(
 
169
        String beanName,
 
170
        int scope,
 
171
        PageContext pageContext) {
 
172
            
 
173
        if (scope == ComponentConstants.COMPONENT_SCOPE){
 
174
            return getAttribute(beanName);
 
175
        }
 
176
        
 
177
        return pageContext.getAttribute(beanName, scope);
 
178
    }
 
179
 
 
180
    /**
 
181
     * Get component context from request.
 
182
     * @param request ServletRequest.
 
183
     * @return ComponentContext
 
184
     */
 
185
    static public ComponentContext getContext(ServletRequest request) {
 
186
        return (ComponentContext) request.getAttribute(
 
187
            ComponentConstants.COMPONENT_CONTEXT);
 
188
    }
 
189
 
 
190
    /**
 
191
     * Store component context into request.
 
192
     * @param context ComponentContext to store.
 
193
     * @param request Request to store ComponentContext.
 
194
     */
 
195
    static public void setContext(
 
196
        ComponentContext context,
 
197
        ServletRequest request) {
 
198
            
 
199
        request.setAttribute(ComponentConstants.COMPONENT_CONTEXT, context);
 
200
    }
 
201
}