1 16 package javax.faces.webapp; 17 18 import javax.faces.application.Application; 19 import javax.faces.component.UIComponent; 20 import javax.faces.component.ValueHolder; 21 import javax.faces.context.FacesContext; 22 import javax.faces.convert.Converter; 23 import javax.faces.el.ValueBinding; 24 import javax.servlet.jsp.JspException ; 25 import javax.servlet.jsp.tagext.Tag ; 26 import javax.servlet.jsp.tagext.TagSupport ; 27 28 32 public class ConverterTag 33 extends TagSupport 34 { 35 private String _converterId; 36 37 public ConverterTag() 38 { 39 super(); 40 } 41 42 public void setConverterId(String converterId) 43 { 44 _converterId = converterId; 45 } 46 47 public int doStartTag() 48 throws JspException 49 { 50 UIComponentTag componentTag = UIComponentTag.getParentUIComponentTag(pageContext); 51 if (componentTag == null) 52 { 53 throw new JspException ("no parent UIComponentTag found"); 54 } 55 if (!componentTag.getCreated()) 56 { 57 return Tag.SKIP_BODY; 58 } 59 60 Converter converter = createConverter(); 61 62 UIComponent component = componentTag.getComponentInstance(); 63 if (component == null) 64 { 65 throw new JspException ("parent UIComponentTag has no UIComponent"); 66 } 67 if (!(component instanceof ValueHolder)) 68 { 69 throw new JspException ("UIComponent is no ValueHolder"); 70 } 71 ((ValueHolder)component).setConverter(converter); 72 73 return Tag.SKIP_BODY; 74 } 75 76 public void release() 77 { 78 super.release(); 79 _converterId = null; 80 } 81 82 protected Converter createConverter() 83 throws JspException 84 { 85 FacesContext facesContext = FacesContext.getCurrentInstance(); 86 Application application = facesContext.getApplication(); 87 if (UIComponentTag.isValueReference(_converterId)) 88 { 89 ValueBinding vb = facesContext.getApplication().createValueBinding(_converterId); 90 return application.createConverter((String )vb.getValue(facesContext)); 91 } 92 else 93 { 94 return application.createConverter(_converterId); 95 } 96 } 97 } 98 | Popular Tags |