1/*2 * $Id: BasicAttributeContext.java 616498 2008-01-29 19:40:35Z apetrelli $3 *4 * Licensed to the Apache Software Foundation (ASF) under one5 * or more contributor license agreements. See the NOTICE file6 * distributed with this work for additional information7 * regarding copyright ownership. The ASF licenses this file8 * to you under the Apache License, Version 2.0 (the9 * "License"); you may not use this file except in compliance10 * with the License. You may obtain a copy of the License at11 *12 * http://www.apache.org/licenses/LICENSE-2.013 *14 * Unless required by applicable law or agreed to in writing,15 * software distributed under the License is distributed on an16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY17 * KIND, either express or implied. See the License for the18 * specific language governing permissions and limitations19 * under the License.20 */2122package org.apache.tiles.context;
2324import java.io.Serializable;
25import java.util.ArrayList;
26import java.util.HashMap;
27import java.util.Iterator;
28import java.util.Map;
29import java.util.Set;
30import java.util.Stack;
3132import org.apache.tiles.Attribute;
33import org.apache.tiles.AttributeContext;
3435/***36 * Basic implementation for <code>AttributeContext</code>.37 *38 * @version $Rev: 616498 $ $Date: 2008-01-29 20:40:35 +0100 (Tue, 29 Jan 2008) $39 */40publicclassBasicAttributeContext implements AttributeContext, Serializable {
4142/***43 * Name used to store attribute context stack.44 */45privatestaticfinal String ATTRIBUTE_CONTEXT_STACK =
46"org.apache.tiles.AttributeContext.STACK";
4748/***49 * Template attributes.50 */51private Map<String, Attribute> attributes = null;
5253/***54 * Constructor.55 */56publicBasicAttributeContext() {
57super();
58 }
5960/***61 * Constructor.62 * Create a context and set specified attributes.63 *64 * @param attributes Attributes to initialize context.65 */66publicBasicAttributeContext(Map<String, Attribute> attributes) {
67if (attributes != null) {
68this.attributes = new HashMap<String, Attribute>(attributes);
69 }
70 }
717273/***74 * Copy constructor.75 *76 * @param context The constructor to copy.77 */78publicBasicAttributeContext(AttributeContext context) {
79this.attributes = new HashMap<String, Attribute>();
80 Iterator<String> names = context.getAttributeNames();
81while (names.hasNext()) {
82 String name = names.next();
83 attributes.put(name, context.getAttribute(name));
84 }
85 }
8687/***88 * Add all attributes to this context.89 * Copies all of the mappings from the specified attribute map to this context.90 * New attribute mappings will replace any mappings that this context had for any of the keys91 * currently in the specified attribute map.92 *93 * @param newAttributes Attributes to add.94 */95publicvoid addAll(Map<String, Attribute> newAttributes) {
96if (newAttributes == null) {
97return;
98 }
99100if (attributes == null) {
101 attributes = new HashMap<String, Attribute>(newAttributes);
102return;
103 }
104105 attributes.putAll(newAttributes);
106 }
107108/***109 * Add all missing attributes to this context.110 * Copies all of the mappings from the specified attributes map to this context.111 * New attribute mappings will be added only if they don't already exist in112 * this context.113 *114 * @param defaultAttributes Attributes to add.115 */116publicvoid addMissing(Map<String, Attribute> defaultAttributes) {
117if (defaultAttributes == null) {
118return;
119 }
120121if (attributes == null) {
122 attributes = new HashMap<String, Attribute>(defaultAttributes);
123return;
124 }
125126 Set<Map.Entry<String, Attribute>> entries = defaultAttributes.entrySet();
127for (Map.Entry<String, Attribute> entry : entries) {
128if (!attributes.containsKey(entry.getKey())) {
129 attributes.put(entry.getKey(), entry.getValue());
130 }
131 }
132 }
133134/***135 * Get an attribute from context.136 *137 * @param name Name of the attribute.138 * @return <{Attribute}>139 */140public Attribute getAttribute(String name) {
141if (attributes == null) {
142returnnull;
143 }
144145return attributes.get(name);
146 }
147148/***149 * Get names of all attributes.150 *151 * @return <{Attribute}>152 */153public Iterator<String> getAttributeNames() {
154if (attributes == null) {
155returnnew ArrayList<String>().iterator();
156 }
157158return attributes.keySet().iterator();
159 }
160161/***162 * Put a new attribute to context.163 *164 * @param name Name of the attribute.165 * @param value Value of the attribute.166 */167publicvoid putAttribute(String name, Attribute value) {
168if (attributes == null) {
169 attributes = new HashMap<String, Attribute>();
170 }
171172 attributes.put(name, value);
173 }
174175/***176 * Get attribute context from request.177 *178 * @param tilesContext current Tiles application context.179 * @return BasicAttributeContext or null if context is not found or an180 * jspException is present in the request.181 * @deprecated Use {@link TilesContainer#getAttributeContext(Object...)}.182 */183 @Deprecated
184publicstatic AttributeContext getContext(TilesRequestContext tilesContext) {
185 Stack<AttributeContext> contextStack = getContextStack(tilesContext);
186if (!contextStack.isEmpty()) {
187return contextStack.peek();
188 } else {
189returnnull;
190 }
191 }
192193/***194 * Returns the context stack.195 *196 * @param tilesContext The Tiles context object to use.197 * @return The needed stack of contexts.198 * @deprecated Use {@link TilesContainer#getAttributeContext(Object...)},199 * {@link TilesContainer#startContext(Object...)} or200 * {@link TilesContainer#endContext(Object...)}.201 */202 @Deprecated
203 @SuppressWarnings("unchecked")
204publicstatic Stack<AttributeContext> getContextStack(TilesRequestContext tilesContext) {
205 Stack<AttributeContext> contextStack =
206 (Stack<AttributeContext>) tilesContext.getRequestScope().get(
207 BasicAttributeContext.ATTRIBUTE_CONTEXT_STACK);
208if (contextStack == null) {
209 contextStack = new Stack<AttributeContext>();
210 tilesContext.getRequestScope().put(BasicAttributeContext.ATTRIBUTE_CONTEXT_STACK,
211 contextStack);
212 }
213214return contextStack;
215 }
216217/***218 * Pushes a context object in the stack.219 *220 * @param context The context to push.221 * @param tilesContext The Tiles context object to use.222 * @deprecated Use {@link TilesContainer#startContext(Object...)}.223 */224 @Deprecated
225publicstaticvoid pushContext(AttributeContext context,
226TilesRequestContext tilesContext) {
227 Stack<AttributeContext> contextStack = getContextStack(tilesContext);
228 contextStack.push(context);
229 }
230231/***232 * Pops a context object out of the stack.233 *234 * @param tilesContext The Tiles context object to use.235 * @return The popped context object.236 * @deprecated Use {@link TilesContainer#endContext(Object...)}.237 */238publicstatic AttributeContext popContext(TilesRequestContext tilesContext) {
239 Stack<AttributeContext> contextStack = getContextStack(tilesContext);
240return contextStack.pop();
241 }
242243/*** {@inheritDoc} */244publicvoid clear() {
245 attributes.clear();
246 }
247 }