1 16 17 package org.apache.velocity.tools.struts; 18 19 import java.util.Stack ; 20 import java.util.Map ; 21 import java.util.Iterator ; 22 23 import javax.servlet.http.HttpSession ; 24 25 import org.apache.struts.tiles.ComponentContext; 26 import org.apache.struts.tiles.ComponentDefinition; 27 import org.apache.struts.tiles.AttributeDefinition; 28 import org.apache.struts.tiles.DirectStringAttribute; 29 import org.apache.struts.tiles.DefinitionAttribute; 30 import org.apache.struts.tiles.DefinitionNameAttribute; 31 import org.apache.struts.tiles.PathAttribute; 32 import org.apache.struts.tiles.TilesUtil; 33 import org.apache.struts.tiles.DefinitionsFactoryException; 34 import org.apache.struts.tiles.Controller; 35 36 import org.apache.velocity.app.Velocity; 37 import org.apache.velocity.context.Context; 38 import org.apache.velocity.tools.view.ImportSupport; 39 import org.apache.velocity.tools.view.context.ViewContext; 40 import org.apache.velocity.tools.view.tools.ViewTool; 41 42 69 public class TilesTool extends ImportSupport implements ViewTool 70 { 71 static final String PAGE_SCOPE = "page"; 72 static final String REQUEST_SCOPE = "request"; 73 static final String SESSION_SCOPE = "session"; 74 static final String APPLICATION_SCOPE = "application"; 75 76 protected Context velocityContext; 77 78 82 protected Stack contextStack; 83 84 85 86 89 public TilesTool() {} 90 91 97 public void init(Object obj) 98 { 99 if (!(obj instanceof ViewContext)) 100 { 101 throw new IllegalArgumentException ("Tool can only be initialized with a ViewContext"); 102 } 103 104 ViewContext viewContext = (ViewContext)obj; 105 this.velocityContext = viewContext.getVelocityContext(); 106 this.request = viewContext.getRequest(); 107 this.response = viewContext.getResponse(); 108 this.application = viewContext.getServletContext(); 109 } 110 111 112 113 128 public String get(Object obj) 129 { 130 try 131 { 132 Object value = getCurrentContext().getAttribute(obj.toString()); 133 if (value != null) 134 { 135 return processObjectValue(value); 136 } 137 return processAsDefinitionOrURL(obj.toString()); 138 } 139 catch (Exception e) 140 { 141 Velocity.error("TilesTool: Exeption while rendering Tile " 142 + obj + ": " + e.getMessage()); 143 return null; 144 } 145 } 146 147 156 public Object getAttribute(String name) 157 { 158 Object value = getCurrentContext().getAttribute(name); 159 if (value == null) 160 { 161 Velocity.warn("TilesTool: Tile attribute '" 162 + name + "' was not found in context."); 163 } 164 return value; 165 } 166 167 176 public void importAttribute(String name) 177 { 178 this.importAttribute(name, PAGE_SCOPE); 179 } 180 181 191 public void importAttribute(String name, String scope) 192 { 193 Object value = getCurrentContext().getAttribute(name); 194 if (value == null) 195 { 196 Velocity.warn("TilesTool: Tile attribute '" 197 + name + "' was not found in context."); 198 } 199 200 if (scope.equals(PAGE_SCOPE)) 201 { 202 velocityContext.put(name, value); 203 } 204 else if (scope.equals(REQUEST_SCOPE)) 205 { 206 request.setAttribute(name, value); 207 } 208 else if (scope.equals(SESSION_SCOPE)) 209 { 210 request.getSession().setAttribute(name, value); 211 } 212 else if (scope.equals(APPLICATION_SCOPE)) 213 { 214 application.setAttribute(name, value); 215 } 216 } 217 218 225 public void importAttributes() 226 { 227 this.importAttributes(PAGE_SCOPE); 228 } 229 230 239 public void importAttributes(String scope) 240 { 241 ComponentContext context = getCurrentContext(); 242 Iterator names = context.getAttributeNames(); 243 244 if (scope.equals(PAGE_SCOPE)) 245 { 246 while (names.hasNext()) 247 { 248 String name = (String )names.next(); 249 velocityContext.put(name, context.getAttribute(name)); 250 } 251 } 252 else if (scope.equals(REQUEST_SCOPE)) 253 { 254 while (names.hasNext()) 255 { 256 String name = (String )names.next(); 257 request.setAttribute(name, context.getAttribute(name)); 258 } 259 } 260 else if (scope.equals(SESSION_SCOPE)) 261 { 262 HttpSession session = request.getSession(); 263 while (names.hasNext()) 264 { 265 String name = (String )names.next(); 266 session.setAttribute(name, context.getAttribute(name)); 267 } 268 } 269 else if (scope.equals(APPLICATION_SCOPE)) 270 { 271 while (names.hasNext()) 272 { 273 String name = (String )names.next(); 274 application.setAttribute(name, context.getAttribute(name)); 275 } 276 } 277 } 278 279 280 281 282 292 protected String processObjectValue(Object value) throws Exception 293 { 294 295 if (value instanceof AttributeDefinition) 296 { 297 298 return processTypedAttribute((AttributeDefinition)value); 299 } 300 else if (value instanceof ComponentDefinition) 301 { 302 return processDefinition((ComponentDefinition)value); 303 } 304 305 return processAsDefinitionOrURL(value.toString()); 306 } 307 308 315 protected String processTypedAttribute(AttributeDefinition value) 316 throws Exception 317 { 318 if (value instanceof DirectStringAttribute) 319 { 320 return (String )value.getValue(); 321 } 322 else if (value instanceof DefinitionAttribute) 323 { 324 return processDefinition((ComponentDefinition)value.getValue()); 325 } 326 else if (value instanceof DefinitionNameAttribute) 327 { 328 return processAsDefinitionOrURL((String )value.getValue()); 329 } 330 331 return doInsert((String )value.getValue(), null, null); 332 } 333 334 341 protected String processAsDefinitionOrURL(String name) throws Exception 342 { 343 try 344 { 345 ComponentDefinition definition = 346 TilesUtil.getDefinition(name, this.request, this.application); 347 if (definition != null) 348 { 349 return processDefinition(definition); 350 } 351 } 352 catch (DefinitionsFactoryException ex) 353 { 354 355 } 356 357 return processUrl(name); 358 } 359 360 367 protected String processDefinition(ComponentDefinition definition) throws 368 Exception 369 { 370 Controller controller = null; 371 try 372 { 373 controller = definition.getOrCreateController(); 374 375 String role = definition.getRole(); 376 String page = definition.getTemplate(); 377 378 return doInsert(definition.getAttributes(), 379 page, 380 role, 381 controller); 382 } 383 catch (InstantiationException ex) 384 { 385 throw new Exception (ex.getMessage()); 386 } 387 } 388 389 396 protected String processUrl(String url) throws Exception 397 { 398 return doInsert(url, null, null); 399 } 400 401 410 protected String doInsert(String page, String role, Controller controller) throws 411 Exception 412 { 413 if (role != null && !this.request.isUserInRole(role)) 414 { 415 return null; 416 } 417 418 ComponentContext subCompContext = new ComponentContext(); 419 return doInsert(subCompContext, page, role, controller); 420 } 421 422 432 protected String doInsert(Map attributes, 433 String page, 434 String role, 435 Controller controller) throws Exception 436 { 437 if (role != null && !this.request.isUserInRole(role)) 438 { 439 return null; 440 } 441 442 ComponentContext subCompContext = new ComponentContext(attributes); 443 return doInsert(subCompContext, page, role, controller); 444 } 445 446 457 protected String doInsert(ComponentContext subCompContext, 458 String page, 459 String role, 460 Controller controller) throws Exception 461 { 462 pushTilesContext(); 463 try 464 { 465 ComponentContext.setContext(subCompContext, this.request); 466 467 468 if (controller != null) 469 { 470 controller.perform(subCompContext, 471 this.request, 472 this.response, 473 this.application); 474 } 475 476 return this.acquireString(page); 477 } 478 finally 479 { 480 popTilesContext(); 481 } 482 } 483 484 488 protected ComponentContext getCurrentContext() 489 { 490 return ComponentContext.getContext(this.request); 491 } 492 493 498 protected void pushTilesContext() 499 { 500 if (this.contextStack == null) 501 { 502 this.contextStack = new Stack (); 503 } 504 contextStack.push(getCurrentContext()); 505 } 506 507 511 protected void popTilesContext() 512 { 513 ComponentContext context = (ComponentContext)this.contextStack.pop(); 514 ComponentContext.setContext(context, this.request); 515 } 516 517 } 518 | Popular Tags |