1 16 package org.apache.cocoon.faces.taglib; 17 18 import org.apache.cocoon.taglib.TagSupport; 19 20 import org.apache.cocoon.faces.FacesUtils; 21 import org.xml.sax.Attributes ; 22 import org.xml.sax.SAXException ; 23 24 import javax.faces.component.UIComponent; 25 import javax.faces.component.ValueHolder; 26 import javax.faces.convert.Converter; 27 import javax.faces.convert.ConverterException; 28 29 35 public class ConverterTag extends TagSupport { 36 37 private String converterId; 38 39 public String getConverterId() { 40 return this.converterId; 41 } 42 43 public void setConverterId(String converterId) { 44 this.converterId = converterId; 45 } 46 47 public int doStartTag(String namespaceURI, String localName, String qName, Attributes atts) 48 throws SAXException { 49 50 UIComponentTag tag = FacesUtils.findParentUIComponentTag(this); 51 if (tag == null) { 52 throw new SAXException ("Tag " + getClass().getName() + " have to be nested in a UIComponentTag"); 53 } 54 55 if (!tag.getCreated()) { 56 return 0; 57 } 58 59 Converter converter = createConverter(); 60 61 ValueHolder vh = (ValueHolder) tag.getComponentInstance(); 62 vh.setConverter(converter); 63 Object localValue = vh.getLocalValue(); 64 if (localValue instanceof String ) { 65 try { 66 localValue = converter.getAsObject(tag.getFacesContext(), (UIComponent) vh, (String ) localValue); 67 vh.setValue(localValue); 68 } catch (ConverterException ce) { 69 } 70 } 71 72 return SKIP_BODY; 73 } 74 75 78 protected Converter createConverter() { 79 final UIComponentTag tag = FacesUtils.findParentUIComponentTag(this); 80 String converterIdVal = (String ) tag.evaluate(converterId); 81 return tag.getApplication().createConverter(converterIdVal); 82 } 83 84 public void recycle() { 85 super.recycle(); 86 this.converterId = null; 87 } 88 } 89 | Popular Tags |