001: /*
002: * Copyright 2006 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.apache.myfaces.taglib.core;
017:
018: import javax.el.ELContext;
019: import javax.el.ValueExpression;
020: import javax.faces.context.FacesContext;
021: import javax.faces.convert.Converter;
022: import javax.faces.webapp.ConverterELTag;
023: import javax.servlet.jsp.JspException;
024:
025: /**
026: * Implementation of ConverterELTag
027: *
028: * @author Bruno Aranda (latest modification by $Author: dennisbyrne $)
029: * @version $Revision: 552338 $ $Date: 2007-07-01 19:17:29 +0200 (So, 01 Jul 2007) $
030: */
031: public class ConverterTag extends ConverterELTag {
032:
033: private static final long serialVersionUID = -4506829108081L;
034: private ValueExpression _converterId;
035: private ValueExpression _binding;
036: private String _converterIdString = null;
037:
038: public ConverterTag() {
039: super ();
040: }
041:
042: public void setConverterId(ValueExpression converterId) {
043: _converterId = converterId;
044: }
045:
046: public void setBinding(ValueExpression binding) {
047: _binding = binding;
048: }
049:
050: /**
051: * Use this method to specify the converterId programmatically.
052: *
053: * @param converterIdString
054: */
055: public void setConverterIdString(String converterIdString) {
056: _converterIdString = converterIdString;
057: }
058:
059: public void release() {
060: super .release();
061: _converterId = null;
062: _binding = null;
063: _converterIdString = null;
064: }
065:
066: protected Converter createConverter() throws JspException {
067: Converter converter = null;
068:
069: FacesContext facesContext = FacesContext.getCurrentInstance();
070: ELContext elContext = facesContext.getELContext();
071:
072: // try to create the converter from the binding expression first, and then from
073: // the converterId
074: if (_binding != null) {
075: try {
076: converter = (Converter) _binding.getValue(elContext);
077:
078: if (converter != null) {
079: return converter;
080: }
081: } catch (Exception e) {
082: throw new JspException(
083: "Exception creating converter using binding", e);
084: }
085: }
086:
087: if ((_converterId != null) || (_converterIdString != null)) {
088: try {
089: if (null != _converterIdString) {
090: converter = facesContext.getApplication()
091: .createConverter(_converterIdString);
092: } else {
093: String converterId = (String) _converterId
094: .getValue(elContext);
095: converter = facesContext.getApplication()
096: .createConverter(converterId);
097: }
098:
099: // with binding no converter was created, set its value with the converter
100: // created using the converterId
101: if (converter != null && _binding != null) {
102: _binding.setValue(elContext, converter);
103: }
104: } catch (Exception e) {
105: throw new JspException(
106: "Exception creating converter with converterId: "
107: + _converterId, e);
108: }
109: }
110:
111: return converter;
112: }
113:
114: }
|