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 com.caucho.jsf.html;
030:
031: import java.io.*;
032: import java.util.*;
033:
034: import javax.el.*;
035: import javax.faces.*;
036: import javax.faces.component.*;
037: import javax.faces.component.html.*;
038: import javax.faces.context.*;
039: import javax.faces.convert.*;
040: import javax.faces.model.*;
041: import javax.faces.render.*;
042:
043: /**
044: * The base renderer
045: */
046: abstract class BaseRenderer extends Renderer {
047: /**
048: * True if the renderer is responsible for rendering the children.
049: */
050: @Override
051: public boolean getRendersChildren() {
052: return true;
053: }
054:
055: @Override
056: public Object getConvertedValue(FacesContext context,
057: UIComponent component, Object submittedValue)
058: throws ConverterException {
059: if (context == null || component == null)
060: throw new NullPointerException();
061:
062: if (component instanceof ValueHolder) {
063: Converter converter = ((ValueHolder) component)
064: .getConverter();
065:
066: if (converter != null)
067: return converter.getAsObject(context, component,
068: (String) submittedValue);
069: }
070:
071: ValueExpression valueExpr = component
072: .getValueExpression("value");
073:
074: if (valueExpr != null) {
075: Class type = valueExpr.getType(context.getELContext());
076:
077: if (type != null) {
078: Converter converter = context.getApplication()
079: .createConverter(type);
080:
081: if (converter != null) {
082: return converter.getAsObject(context, component,
083: (String) submittedValue);
084: }
085: }
086: }
087:
088: return submittedValue;
089: }
090:
091: protected String toString(FacesContext context,
092: UIComponent component, Object value) {
093: if (component instanceof ValueHolder) {
094: Converter converter = ((ValueHolder) component)
095: .getConverter();
096:
097: if (converter != null) {
098: String result = converter.getAsString(context,
099: component, value);
100:
101: return result;
102: }
103: }
104:
105: if (value != null)
106: return value.toString();
107: else
108: return "";
109: }
110:
111: public String toString() {
112: return getClass().getName() + "[]";
113: }
114: }
|