001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License version 2
011: * as published by the Free Software Foundation.
012: *
013: * Resin Open Source is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
016: * of NON-INFRINGEMENT. See the GNU General Public License for more
017: * details.
018: *
019: * You should have received a copy of the GNU General Public License
020: * along with Resin Open Source; if not, write to the
021: *
022: * Free Software Foundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package javax.faces.webapp;
030:
031: import java.io.*;
032: import java.util.*;
033:
034: import javax.el.*;
035:
036: import javax.faces.*;
037: import javax.faces.application.*;
038: import javax.faces.component.*;
039: import javax.faces.context.*;
040: import javax.faces.convert.*;
041: import javax.faces.el.*;
042:
043: import javax.servlet.jsp.*;
044: import javax.servlet.jsp.tagext.*;
045:
046: public class ConverterTag extends TagSupport {
047: private String _id;
048: private ValueExpression _binding;
049:
050: public void setConverterId(String id) {
051: _id = id;
052: }
053:
054: public void setBinding(String binding) throws JspException {
055: FacesContext context = FacesContext.getCurrentInstance();
056:
057: Application app = context.getApplication();
058: ExpressionFactory factory = app.getExpressionFactory();
059:
060: _binding = factory.createValueExpression(
061: context.getELContext(), binding, Converter.class);
062: }
063:
064: public int doStartTag() throws JspException {
065: UIComponentClassicTagBase parent;
066:
067: parent = UIComponentClassicTagBase
068: .getParentUIComponentClassicTagBase(this .pageContext);
069:
070: if (parent == null)
071: throw new JspException(
072: "ConverterTag must be nested inside a UIComponent tag.");
073:
074: UIComponent comp = parent.getComponentInstance();
075:
076: if (parent.getCreated()) {
077: if (!(comp instanceof ValueHolder))
078: throw new JspException(
079: "UIComponent parent of converter must be a ValueHolder.");
080:
081: ValueHolder valueHolder = (ValueHolder) comp;
082:
083: Converter converter = createConverter();
084:
085: valueHolder.setConverter(converter);
086: }
087:
088: return SKIP_BODY;
089: }
090:
091: protected Converter createConverter() throws JspException {
092: try {
093: FacesContext context = FacesContext.getCurrentInstance();
094: Converter converter;
095:
096: if (_binding != null) {
097: converter = (Converter) _binding.getValue(context
098: .getELContext());
099:
100: if (converter != null)
101: return converter;
102: }
103:
104: converter = context.getApplication().createConverter(_id);
105:
106: if (_binding != null)
107: _binding.setValue(context.getELContext(), converter);
108:
109: return converter;
110: } catch (Exception e) {
111: throw new JspException(e);
112: }
113: }
114: }
|