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.component.StateHolder;
24  import javax.faces.component.UIComponent;
25  import javax.faces.context.FacesContext;
26  import javax.faces.convert.Converter;
27  import javax.faces.convert.ConverterException;
28  
29  /**
30   * This class is used in conjunction with ConverterImplTag. 
31   * 
32   * When a tag like this is in a jsp page:
33   * 
34   * <f:converter binding="#{mybean}"/>
35   *  
36   *  or
37   *  
38   * <f:converter converterId="#{'anyid'}" binding="#{mybean}"/>
39   * 
40   * The value of mybean could be already on the context, so this
41   * converter avoid creating a new variable and use the previous one.
42   * 
43   * @author Leonardo Uribe (latest modification by $Author: jakobk $)
44   * @version $Revision: 922543 $ $Date: 2010-03-13 06:55:01 -0500 (Sat, 13 Mar 2010) $ 
45   */
46  public class DelegateConverter implements Converter, StateHolder
47  {
48  
49      private ValueExpression _converterId;
50      private ValueExpression _binding;
51      private String _converterIdString = null;
52      
53      public DelegateConverter(){
54          
55      }
56      
57      public DelegateConverter(ValueExpression id, ValueExpression binding, String converterIdString)
58      {
59          super();
60          _converterId = id;
61          _binding = binding;
62          _converterIdString = converterIdString;
63      }
64  
65      public boolean isTransient()
66      {
67          return false;
68      }
69  
70      public void restoreState(FacesContext facesContext, Object state)
71      {
72          Object[] values = (Object[]) state;
73          _converterId = (ValueExpression) values[0];
74          _binding = (ValueExpression) values[1];
75          _converterIdString = (String) values[2];
76      }
77  
78      public Object saveState(FacesContext facesContext)
79      {
80          Object[] values = new Object[3];
81          values[0] = _converterId;
82          values[1] = _binding;
83          values[2] = _converterIdString;
84          return values;
85      }
86  
87      public void setTransient(boolean arg0)
88      {
89          // Do nothing        
90      }
91  
92      public Object getAsObject(FacesContext facesContext, UIComponent component,
93              String value)
94      {
95          return _getDelegate().getAsObject(facesContext, component, value);
96      }
97  
98      public String getAsString(FacesContext facesContext, UIComponent component,
99              Object value)
100     {
101         return _getDelegate().getAsString(facesContext, component, value);
102     }
103 
104     private Converter _getDelegate()
105     {
106         return _createConverter();
107     }
108 
109     private Converter _createConverter()
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 ConverterException("Exception creating converter using binding", e);
132             }
133         }
134 
135         if ((_converterId != null) || (_converterIdString != null))
136         {
137             try
138             {
139                 if (null != _converterIdString)
140                 {
141                     converter = facesContext.getApplication().createConverter(_converterIdString);
142                 } else 
143                 {
144                     String converterId = (String) _converterId.getValue(elContext);
145                     converter = facesContext.getApplication().createConverter(converterId);
146                 }
147 
148                 // with binding no converter was created, set its value with the converter
149                 // created using the converterId
150                 if (converter != null && _binding != null)
151                 {
152                     _binding.setValue(elContext, converter);
153                 }
154             }
155             catch (Exception e)
156             {
157                 throw new ConverterException("Exception creating converter with converterId: " + _converterId, e);
158             }
159         }
160         
161         if (converter == null)
162         {
163             throw new IllegalStateException("Could not create converter. Please specify a valid converterId" +
164                     " or a non-null binding.");
165         }
166 
167         return converter;
168     }
169 
170 }