1 14 15 package com.sun.facelets.impl; 16 17 import java.io.IOException ; 18 import java.net.URL ; 19 import java.util.ArrayList ; 20 import java.util.HashMap ; 21 import java.util.HashSet ; 22 import java.util.Iterator ; 23 import java.util.LinkedList ; 24 import java.util.List ; 25 import java.util.Map ; 26 import java.util.Set ; 27 28 import javax.el.ELContext; 29 import javax.el.ELException; 30 import javax.el.ELResolver; 31 import javax.el.ExpressionFactory; 32 import javax.el.FunctionMapper; 33 import javax.el.ValueExpression; 34 import javax.el.VariableMapper; 35 import javax.faces.FacesException; 36 import javax.faces.component.UIComponent; 37 import javax.faces.context.FacesContext; 38 39 import com.sun.facelets.Facelet; 40 import com.sun.facelets.FaceletContext; 41 import com.sun.facelets.FaceletException; 42 import com.sun.facelets.FaceletHandler; 43 import com.sun.facelets.TemplateClient; 44 import com.sun.facelets.el.DefaultVariableMapper; 45 import com.sun.facelets.el.ELAdaptor; 46 47 59 final class DefaultFaceletContext extends FaceletContext { 60 61 private final FacesContext faces; 62 63 private final ELContext ctx; 64 65 private final DefaultFacelet facelet; 66 67 private VariableMapper varMapper; 68 69 private FunctionMapper fnMapper; 70 71 private final Map ids; 72 73 public DefaultFaceletContext(DefaultFaceletContext ctx, 74 DefaultFacelet facelet) { 75 this.ctx = ctx.ctx; 76 this.facelet = facelet; 77 this.clients = ctx.clients; 78 this.faces = ctx.faces; 79 this.fnMapper = ctx.fnMapper; 80 this.ids = ctx.ids; 81 this.varMapper = ctx.varMapper; 82 } 83 84 public DefaultFaceletContext(FacesContext faces, DefaultFacelet facelet) { 85 this.ctx = ELAdaptor.getELContext(faces); 86 this.ids = new HashMap (); 87 this.clients = new ArrayList (5); 88 this.faces = faces; 89 this.facelet = facelet; 90 this.varMapper = this.ctx.getVariableMapper(); 91 if (this.varMapper == null) { 92 this.varMapper = new DefaultVariableMapper(); 93 } 94 this.fnMapper = this.ctx.getFunctionMapper(); 95 } 96 97 102 public FacesContext getFacesContext() { 103 return this.faces; 104 } 105 106 111 public ExpressionFactory getExpressionFactory() { 112 return this.facelet.getExpressionFactory(); 113 } 114 115 120 public void setVariableMapper(VariableMapper varMapper) { 121 this.varMapper = varMapper; 123 } 124 125 130 public void setFunctionMapper(FunctionMapper fnMapper) { 131 this.fnMapper = fnMapper; 133 } 134 135 141 public void includeFacelet(UIComponent parent, String relativePath) 142 throws IOException , FaceletException, FacesException, ELException { 143 this.facelet.include(this, parent, relativePath); 144 } 145 146 151 public FunctionMapper getFunctionMapper() { 152 return this.fnMapper; 153 } 154 155 160 public VariableMapper getVariableMapper() { 161 return this.varMapper; 162 } 163 164 169 public Object getContext(Class key) { 170 return this.ctx.getContext(key); 171 } 172 173 178 public void putContext(Class key, Object contextObject) { 179 this.ctx.putContext(key, contextObject); 180 } 181 182 187 public String generateUniqueId(String base) { 188 Integer cnt = (Integer ) this.ids.get(base); 189 if (cnt == null) { 190 this.ids.put(base, new Integer (0)); 191 return base; 192 } else { 193 int i = cnt.intValue() + 1; 194 this.ids.put(base, new Integer (i)); 195 return base + "_" + i; 196 } 197 } 198 199 204 public Object getAttribute(String name) { 205 if (this.varMapper != null) { 206 ValueExpression ve = this.varMapper.resolveVariable(name); 207 if (ve != null) { 208 return ve.getValue(this); 209 } 210 } 211 return null; 212 } 213 214 220 public void setAttribute(String name, Object value) { 221 if (this.varMapper != null) { 222 if (value == null) { 223 this.varMapper.setVariable(name, null); 224 } else { 225 this.varMapper.setVariable(name, this.facelet 226 .getExpressionFactory().createValueExpression(value, 227 Object .class)); 228 } 229 } 230 } 231 232 238 public void includeFacelet(UIComponent parent, URL absolutePath) 239 throws IOException , FaceletException, FacesException, ELException { 240 this.facelet.include(this, parent, absolutePath); 241 } 242 243 public ELResolver getELResolver() { 244 return this.ctx.getELResolver(); 245 } 246 247 private final List clients; 248 249 public void popClient(TemplateClient client) { 250 if (!this.clients.isEmpty()) { 251 Iterator itr = this.clients.iterator(); 252 while (itr.hasNext()) { 253 if (itr.next().equals(client)) { 254 itr.remove(); 255 return; 256 } 257 } 258 } 259 throw new IllegalStateException (client + " not found"); 260 } 261 262 public void pushClient(final TemplateClient client) { 263 this.clients.add(0, new TemplateManager(this.facelet, client)); 264 } 265 266 public void extendClient(final TemplateClient client) { 267 this.clients.add(new TemplateManager(this.facelet, client)); 268 } 269 270 public boolean includeDefinition(UIComponent parent, String name) 271 throws IOException , FaceletException, FacesException, ELException { 272 boolean found = false; 273 TemplateClient client; 274 275 for (int i = 0; i < this.clients.size() && found == false; i++) { 276 client = ((TemplateClient) this.clients.get(i)); 277 if (client.equals(this.facelet)) 278 continue; 279 found = client.apply(this, parent, name); 280 } 281 282 return found; 283 } 284 285 private final static class TemplateManager implements TemplateClient { 286 private final DefaultFacelet owner; 287 288 private final TemplateClient target; 289 290 private final Set names = new HashSet (); 291 292 public TemplateManager(DefaultFacelet owner, TemplateClient target) { 293 this.owner = owner; 294 this.target = target; 295 } 296 297 public boolean apply(FaceletContext ctx, UIComponent parent, String name) 298 throws IOException , FacesException, FaceletException, 299 ELException { 300 String testName = (name != null) ? name : "facelets._NULL_DEF_"; 301 if (this.names.contains(testName)) { 302 return false; 303 } else { 304 this.names.add(testName); 305 boolean found = false; 306 found = this.target.apply(new DefaultFaceletContext( 307 (DefaultFaceletContext) ctx, this.owner), parent, name); 308 this.names.remove(testName); 309 return found; 310 } 311 } 312 313 public boolean equals(Object o) { 314 return this.owner == o || this.target == o; 317 } 318 } 319 } 320 | Popular Tags |