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
33   * associates it with the nearest parent UIComponent.
34   * 
35   * @author Leonardo Uribe (latest modification by $Author: lu4242 $)
36   * @version $Revision: 810211 $ $Date: 2009-09-01 15:16:03 -0500 (Tue, 01 Sep 2009) $
37   *
38   */
39  @JSFJspTag(
40          name="f:converter",
41          bodyContent="empty")
42  public class ConverterImplTag extends ConverterELTag
43  {
44  
45      private static final long serialVersionUID = -4506829108081L;
46      private ValueExpression _converterId;
47      private ValueExpression _binding;
48      private String _converterIdString = null;
49  
50      public ConverterImplTag()
51      {
52          super();
53      }
54  
55      /**
56       * The converter's registered ID.
57       */
58      @JSFJspAttribute(
59              rtexprvalue=true,
60              className="java.lang.String")
61      public void setConverterId(ValueExpression converterId)
62      {
63          _converterId = converterId;
64      }
65  
66      /**
67       * A ValueExpression that evaluates to a Converter.
68       */
69      @JSFJspAttribute(
70              rtexprvalue=true,
71              className="javax.faces.convert.Converter")
72      public void setBinding(ValueExpression binding)
73      {
74          _binding = binding;
75      }
76  
77      /**
78       * Use this method to specify the converterId programmatically.
79       *
80       * @param converterIdString
81       */
82      public void setConverterIdString(String converterIdString)
83      {
84          _converterIdString = converterIdString;
85      }
86  
87      public void release()
88      {
89          super.release();
90          _converterId = null;
91          _binding = null;
92          _converterIdString = null;
93      }
94  
95      protected Converter createConverter() throws JspException
96      {
97          if (_converterId != null && _converterId.isLiteralText())
98          {
99              return this.createClassicConverter();
100         }
101         if (_converterIdString != null){
102             return this.createClassicConverter();
103         }
104         
105         return new DelegateConverter(_converterId, _binding,
106                 _converterIdString);
107     }
108 
109     protected Converter createClassicConverter() throws JspException
110     {
111         Converter converter = null;
112 
113         FacesContext facesContext = FacesContext.getCurrentInstance();
114         ELContext elContext = facesContext.getELContext();
115 
116         // try to create the converter from the binding expression first, and then from
117         // the converterId
118         if (_binding != null)
119         {
120             try
121             {
122                 converter = (Converter) _binding.getValue(elContext);
123 
124                 if (converter != null)
125                 {
126                     return converter;
127                 }
128             }
129             catch (Exception e)
130             {
131                 throw new JspException(
132                         "Exception creating converter using binding", e);
133             }
134         }
135 
136         if ((_converterId != null) || (_converterIdString != null))
137         {
138             try
139             {
140                 if (null != _converterIdString)
141                 {
142                     converter = facesContext.getApplication().createConverter(
143                             _converterIdString);
144                 }
145                 else
146                 {
147                     String converterId = (String) _converterId
148                             .getValue(elContext);
149                     converter = facesContext.getApplication().createConverter(
150                             converterId);
151                 }
152 
153                 // with binding no converter was created, set its value with the converter
154                 // created using the converterId
155                 if (converter != null && _binding != null)
156                 {
157                     _binding.setValue(elContext, converter);
158                 }
159             }
160             catch (Exception e)
161             {
162                 throw new JspException(
163                         "Exception creating converter with converterId: "
164                                 + _converterId, e);
165             }
166         }
167 
168         return converter;
169     }
170 }