1 18 19 package org.apache.struts.tiles; 20 21 import java.io.Serializable ; 22 import java.util.Collections ; 23 import java.util.HashMap ; 24 import java.util.Iterator ; 25 import java.util.Map ; 26 import java.util.Set ; 27 28 import javax.servlet.ServletRequest ; 29 import javax.servlet.jsp.PageContext ; 30 31 import org.apache.struts.taglib.tiles.ComponentConstants; 32 33 36 public class ComponentContext implements Serializable { 37 38 41 private Map attributes=null; 42 43 46 public ComponentContext() { 47 super(); 48 } 49 50 55 public ComponentContext(Map attributes) { 56 if (attributes != null) { 57 this.attributes = new HashMap (attributes); 58 } 59 } 60 61 68 public void addAll(Map newAttributes) { 69 if (attributes == null) { 70 attributes = new HashMap (newAttributes); 71 return; 72 } 73 74 attributes.putAll(newAttributes); 75 } 76 77 84 public void addMissing(Map defaultAttributes) { 85 if (defaultAttributes == null) { 86 return; 87 } 88 89 if (attributes == null) { 90 attributes = new HashMap (defaultAttributes); 91 return; 92 } 93 94 Set entries = defaultAttributes.entrySet(); 95 Iterator iterator = entries.iterator(); 96 while (iterator.hasNext()) { 97 Map.Entry entry = (Map.Entry ) iterator.next(); 98 if (!attributes.containsKey(entry.getKey())) { 99 attributes.put(entry.getKey(), entry.getValue()); 100 } 101 } 102 } 103 104 109 public Object getAttribute(String name) { 110 if (attributes == null){ 111 return null; 112 } 113 114 return attributes.get(name); 115 } 116 117 121 public Iterator getAttributeNames() { 122 if (attributes == null) { 123 return Collections.EMPTY_LIST.iterator(); 124 } 125 126 return attributes.keySet().iterator(); 127 } 128 129 134 public void putAttribute(String name, Object value) { 135 if (attributes == null) { 136 attributes = new HashMap (); 137 } 138 139 attributes.put(name, value); 140 } 141 142 149 public Object findAttribute(String beanName, PageContext pageContext) { 150 Object attribute = getAttribute(beanName); 151 if (attribute == null) { 152 attribute = pageContext.findAttribute(beanName); 153 } 154 155 return attribute; 156 } 157 158 166 public Object getAttribute( 167 String beanName, 168 int scope, 169 PageContext pageContext) { 170 171 if (scope == ComponentConstants.COMPONENT_SCOPE){ 172 return getAttribute(beanName); 173 } 174 175 return pageContext.getAttribute(beanName, scope); 176 } 177 178 184 static public ComponentContext getContext(ServletRequest 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 196 static public void setContext( 197 ComponentContext context, 198 ServletRequest request) { 199 200 request.setAttribute(ComponentConstants.COMPONENT_CONTEXT, context); 201 } 202 } 203 | Popular Tags |