1 14 15 package com.sun.facelets.tag.jsf; 16 17 import java.io.IOException ; 18 19 import javax.el.ELException; 20 import javax.el.ValueExpression; 21 import javax.faces.FacesException; 22 import javax.faces.component.UIComponent; 23 import javax.faces.component.ValueHolder; 24 import javax.faces.context.FacesContext; 25 import javax.faces.convert.Converter; 26 27 import com.sun.facelets.FaceletContext; 28 import com.sun.facelets.FaceletException; 29 import com.sun.facelets.tag.MetaTagHandler; 30 import com.sun.facelets.tag.TagAttribute; 31 import com.sun.facelets.tag.TagConfig; 32 import com.sun.facelets.tag.TagException; 33 import com.sun.facelets.tag.MetaRuleset; 34 35 48 public class ConvertHandler extends MetaTagHandler { 49 50 private final TagAttribute binding; 51 52 private String converterId; 53 54 58 public ConvertHandler(TagConfig config) { 59 super(config); 60 this.binding = this.getAttribute("binding"); 61 this.converterId = null; 62 } 63 64 public ConvertHandler(ConverterConfig config) { 65 this((TagConfig) config); 66 this.converterId = config.getConverterId(); 67 } 68 69 90 public final void apply(FaceletContext ctx, UIComponent parent) 91 throws IOException , FacesException, FaceletException, ELException { 92 if (parent == null || !(parent instanceof ValueHolder)) { 93 throw new TagException(this.tag, 94 "Parent not an instance of ValueHolder: " + parent); 95 } 96 97 if (parent.getParent() == null) { 99 ValueHolder vh = (ValueHolder) parent; 101 ValueExpression ve = null; 102 Converter c = null; 103 if (this.binding != null) { 104 ve = this.binding.getValueExpression(ctx, Converter.class); 105 c = (Converter) ve.getValue(ctx); 106 } 107 if (c == null) { 108 c = this.createConverter(ctx); 109 if (ve != null) { 110 ve.setValue(ctx, c); 111 } 112 } 113 if (c == null) { 114 throw new TagException(this.tag, "No Converter was created"); 115 } 116 this.setAttributes(ctx, c); 117 vh.setConverter(c); 118 Object lv = vh.getLocalValue(); 119 FacesContext faces = ctx.getFacesContext(); 120 if (lv instanceof String ) { 121 vh.setValue(c.getAsObject(faces, parent, (String ) lv)); 122 } 123 } 124 } 125 126 133 protected Converter createConverter(FaceletContext ctx) { 134 if (this.converterId == null) { 135 throw new TagException( 136 this.tag, 137 "Default behavior invoked of requiring a converter-id passed in the constructor, must override ConvertHandler(ConverterConfig)"); 138 } 139 return ctx.getFacesContext().getApplication().createConverter(this.converterId); 140 } 141 142 protected MetaRuleset createMetaRuleset(Class type) { 143 return super.createMetaRuleset(type).ignore("binding"); 144 } 145 } 146 | Popular Tags |