View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.myfaces.taglib.core;
20  
21  import javax.el.ELContext;
22  import javax.el.ValueExpression;
23  import javax.faces.context.FacesContext;
24  import javax.faces.convert.Converter;
25  import javax.faces.webapp.ConverterELTag;
26  import javax.servlet.jsp.JspException;
27  
28  import org.apache.myfaces.buildtools.maven2.plugin.builder.annotation.JSFJspAttribute;
29  import org.apache.myfaces.buildtools.maven2.plugin.builder.annotation.JSFJspTag;
30  
31  /**
32   * This tag creates an instance of the specified Converter, and associates it with the nearest parent UIComponent.
33   * 
34   * @author Leonardo Uribe (latest modification by $Author: lu4242 $)
35   * @version $Revision: 823354 $ $Date: 2009-10-08 18:05:00 -0500 (Thu, 08 Oct 2009) $
36   * 
37   */
38  @JSFJspTag(name = "f:converter", bodyContent = "empty")
39  public class ConverterImplTag extends ConverterELTag
40  {
41  
42      private static final long serialVersionUID = -4506829108081L;
43      private ValueExpression _converterId;
44      private ValueExpression _binding;
45      private String _converterIdString = null;
46  
47      public ConverterImplTag()
48      {
49          super();
50      }
51  
52      /**
53       * The converter's registered ID.
54       */
55      @JSFJspAttribute(className="javax.el.ValueExpression",
56              deferredValueType="java.lang.String")
57      public void setConverterId(ValueExpression converterId)
58      {
59          _converterId = converterId;
60      }
61  
62      /**
63       * A ValueExpression that evaluates to a Converter.
64       */
65      @JSFJspAttribute(className="javax.el.ValueExpression",
66              deferredValueType="javax.faces.convert.Converter")
67      public void setBinding(ValueExpression binding)
68      {
69          _binding = binding;
70      }
71  
72      /**
73       * Use this method to specify the converterId programmatically.
74       * 
75       * @param converterIdString
76       */
77      public void setConverterIdString(String converterIdString)
78      {
79          _converterIdString = converterIdString;
80      }
81  
82      @Override
83      public void release()
84      {
85          super.release();
86          _converterId = null;
87          _binding = null;
88          _converterIdString = null;
89      }
90  
91      @Override
92      protected Converter createConverter() throws JspException
93      {
94          if (_converterId != null && _converterId.isLiteralText())
95          {
96              return this.createClassicConverter();
97          }
98          if (_converterIdString != null)
99          {
100             return this.createClassicConverter();
101         }
102 
103         return new DelegateConverter(_converterId, _binding, _converterIdString);
104     }
105 
106     protected Converter createClassicConverter() throws JspException
107     {
108         Converter converter = null;
109 
110         FacesContext facesContext = FacesContext.getCurrentInstance();
111         ELContext elContext = facesContext.getELContext();
112 
113         // try to create the converter from the binding expression first, and then from
114         // the converterId
115         if (_binding != null)
116         {
117             try
118             {
119                 converter = (Converter)_binding.getValue(elContext);
120 
121                 if (converter != null)
122                 {
123                     return converter;
124                 }
125             }
126             catch (Exception e)
127             {
128                 throw new JspException("Exception creating converter using binding", e);
129             }
130         }
131 
132         if ((_converterId != null) || (_converterIdString != null))
133         {
134             try
135             {
136                 if (null != _converterIdString)
137                 {
138                     converter = facesContext.getApplication().createConverter(_converterIdString);
139                 }
140                 else
141                 {
142                     String converterId = (String)_converterId.getValue(elContext);
143                     converter = facesContext.getApplication().createConverter(converterId);
144                 }
145 
146                 // with binding no converter was created, set its value with the converter
147                 // created using the converterId
148                 if (converter != null && _binding != null)
149                 {
150                     _binding.setValue(elContext, converter);
151                 }
152             }
153             catch (Exception e)
154             {
155                 throw new JspException("Exception creating converter with converterId: " + _converterId, e);
156             }
157         }
158 
159         return converter;
160     }
161 }