View Javadoc

1   /*
2    * $Id: CameliaConverterTag.java,v 1.1 2007/09/07 15:32:59 oeuillot Exp $
3    */
4   package org.rcfaces.core.internal.taglib;
5   
6   import javax.el.ELContext;
7   import javax.el.ValueExpression;
8   import javax.faces.context.FacesContext;
9   import javax.faces.convert.Converter;
10  import javax.faces.webapp.ConverterELTag;
11  import javax.servlet.jsp.JspException;
12  
13  public class CameliaConverterTag extends ConverterELTag {
14  
15      private static final long serialVersionUID = 1L;
16  
17      private ValueExpression binding = null;
18  
19      private ValueExpression converterId = null;
20  
21      private String defaultConverterId;
22  
23      public void setConverterId(ValueExpression converterId) {
24          this.converterId = converterId;
25      }
26  
27      public void setBinding(ValueExpression binding) {
28          this.binding = binding;
29      }
30  
31      // -------------------------------------------- Methods from ConverterELTag
32  
33      protected void setConverterId(String converterId) {
34          this.defaultConverterId = converterId;
35      }
36  
37      protected Converter createConverter() throws JspException {
38  
39          FacesContext facesContext = FacesContext.getCurrentInstance();
40          ELContext elContext = facesContext.getELContext();
41          Converter converter = null;
42  
43          // If "binding" is set, use it to create a converter instance.
44          if (binding != null) {
45              try {
46                  converter = (Converter) binding.getValue(elContext);
47                  if (converter != null) {
48                      return converter;
49                  }
50              } catch (Exception e) {
51                  throw new JspException(e);
52              }
53          }
54  
55          // If "converterId" is set, use it to create the converter
56          // instance. If "converterId" and "binding" are both set, store the
57          // converter instance in the value of the property represented by
58          // the ValueExpression 'binding'.
59  
60          String cid = null;
61          if (converterId != null) {
62              cid = (String) converterId.getValue(elContext);
63          }
64          
65          if (defaultConverterId != null) {
66              cid = defaultConverterId;
67          }
68  
69          if (cid != null) {
70              try {
71                  converter = facesContext.getApplication().createConverter(cid);
72                  if (converter != null && binding != null) {
73                      binding.setValue(elContext, converter);
74                  }
75              } catch (Exception e) {
76                  throw new JspException(e);
77              }
78          }
79  
80          return converter;
81      }
82  
83      public void release() {
84          super.release();
85  
86          binding = null;
87          converterId = null;
88          defaultConverterId = null;
89      }
90  
91  }