1 14 15 package com.sun.facelets.tag; 16 17 import java.lang.reflect.Constructor ; 18 import java.lang.reflect.InvocationTargetException ; 19 import java.lang.reflect.Method ; 20 import java.net.URL ; 21 import java.util.HashMap ; 22 import java.util.Map ; 23 24 import javax.el.ELException; 25 import javax.faces.FacesException; 26 import javax.faces.convert.Converter; 27 import javax.faces.validator.Validator; 28 29 import com.sun.facelets.FaceletContext; 30 import com.sun.facelets.FaceletException; 31 import com.sun.facelets.FaceletHandler; 32 import com.sun.facelets.tag.jsf.ComponentConfig; 33 import com.sun.facelets.tag.jsf.ComponentHandler; 34 import com.sun.facelets.tag.jsf.ConvertHandler; 35 import com.sun.facelets.tag.jsf.ConverterConfig; 36 import com.sun.facelets.tag.jsf.ValidateHandler; 37 import com.sun.facelets.tag.jsf.ValidatorConfig; 38 39 45 public abstract class AbstractTagLibrary implements TagLibrary { 46 47 private static class ValidatorConfigWrapper implements ValidatorConfig { 48 49 private final TagConfig parent; 50 private final String validatorId; 51 52 public ValidatorConfigWrapper(TagConfig parent, String validatorId) { 53 this.parent = parent; 54 this.validatorId = validatorId; 55 } 56 57 public String getValidatorId() { 58 return this.validatorId; 59 } 60 61 public FaceletHandler getNextHandler() { 62 return this.parent.getNextHandler(); 63 } 64 65 public Tag getTag() { 66 return this.parent.getTag(); 67 } 68 69 public String getTagId() { 70 return this.parent.getTagId(); 71 } 72 } 73 74 private static class ConverterConfigWrapper implements ConverterConfig { 75 private final TagConfig parent; 76 private final String converterId; 77 78 public ConverterConfigWrapper(TagConfig parent, String converterId) { 79 this.parent = parent; 80 this.converterId = converterId; 81 } 82 83 public String getConverterId() { 84 return this.converterId; 85 } 86 public FaceletHandler getNextHandler() { 87 return this.parent.getNextHandler(); 88 } 89 public Tag getTag() { 90 return this.parent.getTag(); 91 } 92 public String getTagId() { 93 return this.parent.getTagId(); 94 } 95 } 96 97 private static class HandlerFactory implements TagHandlerFactory { 98 private final static Class [] CONSTRUCTOR_SIG = new Class [] { TagConfig.class }; 99 100 protected final Class handlerType; 101 102 public HandlerFactory(Class handlerType) { 103 this.handlerType = handlerType; 104 } 105 106 public TagHandler createHandler(TagConfig cfg) throws FacesException, 107 ELException { 108 try { 109 return (TagHandler) this.handlerType.getConstructor( 110 CONSTRUCTOR_SIG).newInstance(new Object [] { cfg }); 111 } catch (InvocationTargetException ite) { 112 Throwable t = ite.getCause(); 113 if (t instanceof FacesException) { 114 throw (FacesException) t; 115 } else if (t instanceof ELException) { 116 throw (ELException) t; 117 } else { 118 throw new FacesException("Error Instantiating: " 119 + this.handlerType.getName(), t); 120 } 121 } catch (Exception e) { 122 throw new FacesException("Error Instantiating: " 123 + this.handlerType.getName(), e); 124 } 125 } 126 } 127 128 private static class ComponentConfigWrapper implements ComponentConfig { 129 130 protected final TagConfig parent; 131 132 protected final String componentType; 133 134 protected final String rendererType; 135 136 public ComponentConfigWrapper(TagConfig parent, String componentType, 137 String rendererType) { 138 this.parent = parent; 139 this.componentType = componentType; 140 this.rendererType = rendererType; 141 } 142 143 public String getComponentType() { 144 return this.componentType; 145 } 146 147 public String getRendererType() { 148 return this.rendererType; 149 } 150 151 public FaceletHandler getNextHandler() { 152 return this.parent.getNextHandler(); 153 } 154 155 public Tag getTag() { 156 return this.parent.getTag(); 157 } 158 159 public String getTagId() { 160 return this.parent.getTagId(); 161 } 162 } 163 164 private static class UserTagFactory implements TagHandlerFactory { 165 protected final URL location; 166 167 public UserTagFactory(URL location) { 168 this.location = location; 169 } 170 171 public TagHandler createHandler(TagConfig cfg) throws FacesException, 172 ELException { 173 return new UserTagHandler(cfg, this.location); 174 } 175 } 176 177 private static class ComponentHandlerFactory implements TagHandlerFactory { 178 179 protected final String componentType; 180 181 protected final String renderType; 182 183 186 public ComponentHandlerFactory(String componentType, String renderType) { 187 this.componentType = componentType; 188 this.renderType = renderType; 189 } 190 191 public TagHandler createHandler(TagConfig cfg) throws FacesException, 192 ELException { 193 ComponentConfig ccfg = new ComponentConfigWrapper(cfg, 194 this.componentType, this.renderType); 195 return new ComponentHandler(ccfg); 196 } 197 } 198 199 private static class UserComponentHandlerFactory implements 200 TagHandlerFactory { 201 202 private final static Class [] CONS_SIG = new Class [] { ComponentConfig.class }; 203 204 protected final String componentType; 205 206 protected final String renderType; 207 208 protected final Class type; 209 210 protected final Constructor constructor; 211 212 215 public UserComponentHandlerFactory(String componentType, 216 String renderType, Class type) { 217 this.componentType = componentType; 218 this.renderType = renderType; 219 this.type = type; 220 try { 221 this.constructor = this.type.getConstructor(CONS_SIG); 222 } catch (Exception e) { 223 throw new FaceletException( 224 "Must have a Constructor that takes in a ComponentConfig", 225 e); 226 } 227 } 228 229 public TagHandler createHandler(TagConfig cfg) throws FacesException, 230 ELException { 231 try { 232 ComponentConfig ccfg = new ComponentConfigWrapper(cfg, 233 this.componentType, this.renderType); 234 return (TagHandler) this.constructor 235 .newInstance(new Object [] { ccfg }); 236 } catch (InvocationTargetException e) { 237 throw new FaceletException(e.getCause().getMessage(), e.getCause().getCause()); 238 } catch (Exception e) { 239 throw new FaceletException("Error Instantiating ComponentHandler: "+this.type.getName(), e); 240 } 241 } 242 } 243 244 private static class ValidatorHandlerFactory implements TagHandlerFactory { 245 246 protected final String validatorId; 247 248 public ValidatorHandlerFactory(String validatorId) { 249 this.validatorId = validatorId; 250 } 251 252 public TagHandler createHandler(TagConfig cfg) throws FacesException, 253 ELException { 254 return new ValidateHandler(new ValidatorConfigWrapper(cfg, this.validatorId)); 255 } 256 } 257 258 private static class ConverterHandlerFactory implements TagHandlerFactory { 259 260 protected final String converterId; 261 262 public ConverterHandlerFactory(String converterId) { 263 this.converterId = converterId; 264 } 265 266 public TagHandler createHandler(TagConfig cfg) throws FacesException, 267 ELException { 268 return new ConvertHandler(new ConverterConfigWrapper(cfg, this.converterId)); 269 } 270 } 271 272 private static class UserConverterHandlerFactory implements TagHandlerFactory { 273 private final static Class [] CONS_SIG = new Class [] { ConverterConfig.class }; 274 275 protected final String converterId; 276 277 protected final Class type; 278 279 protected final Constructor constructor; 280 281 public UserConverterHandlerFactory(String converterId, Class type) { 282 this.converterId = converterId; 283 this.type = type; 284 try { 285 this.constructor = this.type.getConstructor(CONS_SIG); 286 } catch (Exception e) { 287 throw new FaceletException( 288 "Must have a Constructor that takes in a ConverterConfig", 289 e); 290 } 291 } 292 293 public TagHandler createHandler(TagConfig cfg) throws FacesException, 294 ELException { 295 try { 296 ConverterConfig ccfg = new ConverterConfigWrapper(cfg, 297 this.converterId); 298 return (TagHandler) this.constructor 299 .newInstance(new Object [] { ccfg }); 300 } catch (InvocationTargetException e) { 301 throw new FaceletException(e.getCause().getMessage(), e.getCause().getCause()); 302 } catch (Exception e) { 303 throw new FaceletException("Error Instantiating ConverterHandler: "+this.type.getName(), e); 304 } 305 } 306 } 307 308 private static class UserValidatorHandlerFactory implements TagHandlerFactory { 309 private final static Class [] CONS_SIG = new Class [] { ValidatorConfig.class }; 310 311 protected final String validatorId; 312 313 protected final Class type; 314 315 protected final Constructor constructor; 316 317 public UserValidatorHandlerFactory(String validatorId, Class type) { 318 this.validatorId = validatorId; 319 this.type = type; 320 try { 321 this.constructor = this.type.getConstructor(CONS_SIG); 322 } catch (Exception e) { 323 throw new FaceletException( 324 "Must have a Constructor that takes in a ConverterConfig", 325 e); 326 } 327 } 328 329 public TagHandler createHandler(TagConfig cfg) throws FacesException, 330 ELException { 331 try { 332 ValidatorConfig ccfg = new ValidatorConfigWrapper(cfg, 333 this.validatorId); 334 return (TagHandler) this.constructor 335 .newInstance(new Object [] { ccfg }); 336 } catch (InvocationTargetException e) { 337 throw new FaceletException(e.getCause().getMessage(), e.getCause().getCause()); 338 } catch (Exception e) { 339 throw new FaceletException("Error Instantiating ValidatorHandler: "+this.type.getName(), e); 340 } 341 } 342 } 343 344 private final Map factories; 345 346 private final String namespace; 347 348 private final Map functions; 349 350 public AbstractTagLibrary(String namespace) { 351 this.namespace = namespace; 352 this.factories = new HashMap (); 353 this.functions = new HashMap (); 354 } 355 356 369 protected final void addComponent(String name, String componentType, 370 String rendererType) { 371 this.factories.put(name, new ComponentHandlerFactory(componentType, 372 rendererType)); 373 } 374 375 390 protected final void addComponent(String name, String componentType, 391 String rendererType, Class handlerType) { 392 this.factories.put(name, new UserComponentHandlerFactory(componentType, 393 rendererType, handlerType)); 394 } 395 396 406 protected final void addConverter(String name, String converterId) { 407 this.factories.put(name, new ConverterHandlerFactory(converterId)); 408 } 409 410 423 protected final void addConverter(String name, String converterId, Class type) { 424 this.factories.put(name, new UserConverterHandlerFactory(converterId, type)); 425 } 426 427 437 protected final void addValidator(String name, String validatorId) { 438 this.factories.put(name, new ValidatorHandlerFactory(validatorId)); 439 } 440 441 454 protected final void addValidator(String name, String validatorId, Class type) { 455 this.factories.put(name, new UserValidatorHandlerFactory(validatorId, type)); 456 } 457 458 468 protected final void addTagHandler(String name, Class handlerType) { 469 this.factories.put(name, new HandlerFactory(handlerType)); 470 } 471 472 480 protected final void addUserTag(String name, URL source) { 481 this.factories.put(name, new UserTagFactory(source)); 482 } 483 484 485 493 protected final void addFunction(String name, Method method) { 494 this.functions.put(name, method); 495 } 496 497 502 public boolean containsNamespace(String ns) { 503 return this.namespace.equals(ns); 504 } 505 506 512 public boolean containsTagHandler(String ns, String localName) { 513 if (this.namespace.equals(ns)) { 514 if (this.factories.containsKey(localName)) { 515 return true; 516 } 517 } 518 return false; 519 } 520 521 527 public TagHandler createTagHandler(String ns, String localName, 528 TagConfig tag) throws FacesException { 529 if (this.namespace.equals(ns)) { 530 TagHandlerFactory f = (TagHandlerFactory) this.factories 531 .get(localName); 532 if (f != null) { 533 return f.createHandler(tag); 534 } 535 } 536 return null; 537 } 538 539 545 public boolean containsFunction(String ns, String name) { 546 if (this.namespace.equals(ns)) { 547 return this.functions.containsKey(name); 548 } 549 return false; 550 } 551 552 558 public Method createFunction(String ns, String name) { 559 if (this.namespace.equals(ns)) { 560 return (Method ) this.functions.get(name); 561 } 562 return null; 563 } 564 565 570 public boolean equals(Object obj) { 571 return (obj instanceof TagLibrary && obj.hashCode() == this.hashCode()); 572 } 573 574 579 public int hashCode() { 580 return this.namespace.hashCode(); 581 } 582 583 public String getNamespace() { 584 return namespace; 585 } 586 } 587 | Popular Tags |