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.converter;
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  /**
29   * Implementation of ConverterELTag
30   * 
31   * This is a copy of org.apache.myfaces.taglib.core.ConverterTag from 
32   * myfaces core impl 1.2. This is copied here because we need a base class
33   * where all custom converters must inherit.
34   *  
35   * @since 1.1.7
36   * @author Bruno Aranda (latest modification by $Author: lu4242 $)
37   * @version $Revision: 691871 $ $Date: 2008-09-03 23:32:08 -0500 (Wed, 03 Sep 2008) $
38   */
39  public class ConverterTag 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 ConverterTag()
48      {
49          super();
50      }
51  
52      public void setConverterId(ValueExpression converterId)
53      {
54          _converterId = converterId;
55      }
56  
57      public void setBinding(ValueExpression binding)
58      {
59          _binding = binding;
60      }
61  
62      /**
63       * Use this method to specify the converterId programmatically.
64       *
65       * @param converterIdString
66       */
67      public void setConverterIdString(String converterIdString)
68      {
69          _converterIdString = converterIdString;
70      }
71  
72      public void release()
73      {
74          super.release();
75          _converterId = null;
76          _binding = null;
77          _converterIdString = null;
78      }
79  
80      protected Converter createConverter()
81              throws JspException
82      {
83          Converter converter = null;
84  
85          FacesContext facesContext = FacesContext.getCurrentInstance();
86          ELContext elContext = facesContext.getELContext();
87  
88          // try to create the converter from the binding expression first, and then from
89          // the converterId
90          if (_binding != null)
91          {
92              try
93              {
94                  converter = (Converter) _binding.getValue(elContext);
95  
96                  if (converter != null)
97                  {
98                      return converter;
99                  }
100             }
101             catch (Exception e)
102             {
103                 throw new JspException("Exception creating converter using binding", e);
104             }
105         }
106 
107         if ((_converterId != null) || (_converterIdString != null))
108         {
109             try
110             {
111                 if (null != _converterIdString)
112                 {
113                     converter = facesContext.getApplication().createConverter(_converterIdString);
114                 } else 
115                 {
116                     String converterId = (String) _converterId.getValue(elContext);
117                     converter = facesContext.getApplication().createConverter(converterId);
118                 }
119 
120                 // with binding no converter was created, set its value with the converter
121                 // created using the converterId
122                 if (converter != null && _binding != null)
123                 {
124                     _binding.setValue(elContext, converter);
125                 }
126             }
127             catch (Exception e)
128             {
129                 throw new JspException("Exception creating converter with converterId: " + _converterId, e);
130             }
131         }
132 
133         return converter;
134     }
135 
136 }