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.commons.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 * @author Bruno Aranda (latest modification by $Author: skitching $)
36 * @version $Revision: 676268 $ $Date: 2008-07-13 02:45:56 -0500 (Sun, 13 Jul 2008) $
37 */
38 public class ConverterTag extends ConverterELTag
39 {
40
41 private static final long serialVersionUID = -4506829108081L;
42 private ValueExpression _converterId;
43 private ValueExpression _binding;
44 private String _converterIdString = null;
45
46 public ConverterTag()
47 {
48 super();
49 }
50
51 public void setConverterId(ValueExpression converterId)
52 {
53 _converterId = converterId;
54 }
55
56 public void setBinding(ValueExpression binding)
57 {
58 _binding = binding;
59 }
60
61 /**
62 * Use this method to specify the converterId programmatically.
63 *
64 * @param converterIdString
65 */
66 public void setConverterIdString(String converterIdString)
67 {
68 _converterIdString = converterIdString;
69 }
70
71 public void release()
72 {
73 super.release();
74 _converterId = null;
75 _binding = null;
76 _converterIdString = null;
77 }
78
79 protected Converter createConverter()
80 throws JspException
81 {
82 Converter converter = null;
83
84 FacesContext facesContext = FacesContext.getCurrentInstance();
85 ELContext elContext = facesContext.getELContext();
86
87 // try to create the converter from the binding expression first, and then from
88 // the converterId
89 if (_binding != null)
90 {
91 try
92 {
93 converter = (Converter) _binding.getValue(elContext);
94
95 if (converter != null)
96 {
97 return converter;
98 }
99 }
100 catch (Exception e)
101 {
102 throw new JspException("Exception creating converter using binding", e);
103 }
104 }
105
106 if ((_converterId != null) || (_converterIdString != null))
107 {
108 try
109 {
110 if (null != _converterIdString)
111 {
112 converter = facesContext.getApplication().createConverter(_converterIdString);
113 } else
114 {
115 String converterId = (String) _converterId.getValue(elContext);
116 converter = facesContext.getApplication().createConverter(converterId);
117 }
118
119 // with binding no converter was created, set its value with the converter
120 // created using the converterId
121 if (converter != null && _binding != null)
122 {
123 _binding.setValue(elContext, converter);
124 }
125 }
126 catch (Exception e)
127 {
128 throw new JspException("Exception creating converter with converterId: " + _converterId, e);
129 }
130 }
131
132 return converter;
133 }
134
135 }