001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one
003     * or more contributor license agreements.  See the NOTICE file
004     * distributed with this work for additional information
005     * regarding copyright ownership.  The ASF licenses this file
006     * to you under the Apache License, Version 2.0 (the
007     * "License"); you may not use this file except in compliance
008     * with the License.  You may obtain a copy of the License at
009     *
010     *   http://www.apache.org/licenses/LICENSE-2.0
011     *
012     * Unless required by applicable law or agreed to in writing,
013     * software distributed under the License is distributed on an
014     * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015     * KIND, either express or implied.  See the License for the
016     * specific language governing permissions and limitations
017     * under the License.
018     */
019    
020    package org.apache.myfaces.tobago.renderkit;
021    
022    import org.apache.myfaces.tobago.config.ThemeConfig;
023    import org.apache.commons.logging.Log;
024    import org.apache.commons.logging.LogFactory;
025    
026    import javax.faces.render.Renderer;
027    import javax.faces.context.FacesContext;
028    import javax.faces.component.UIComponent;
029    import javax.faces.component.UIInput;
030    import javax.faces.component.ValueHolder;
031    import javax.faces.convert.Converter;
032    import javax.faces.convert.ConverterException;
033    import javax.faces.el.ValueBinding;
034    import javax.faces.FacesException;
035    import java.util.Locale;
036    
037    /**
038     * Date: Apr 21, 2007
039     * Time: 8:04:25 PM
040     */
041    public class RendererBase extends Renderer {
042      protected static final Log LOG = LogFactory.getLog(LayoutableRendererBase.class);
043    
044      public void decode(FacesContext facesContext, UIComponent component) {
045        // nothing to do
046    
047        // FIXME later:
048        if (component instanceof UIInput) {
049          LOG.warn("decode() should be overwritten! Renderer: "
050              + this.getClass().getName());
051        }
052      }
053    
054      public String getRendererName(String rendererType) {
055        String name;
056        if (LOG.isDebugEnabled()) {
057          LOG.debug("rendererType = '" + rendererType + "'");
058        }
059        /* if ("javax.faces.Text".equals(rendererType)) { // TODO: find a better way
060        name = RENDERER_TYPE_OUT;
061      } else {*/
062        name = rendererType;
063        /*}
064        if (name.startsWith("javax.faces.")) { // FIXME: this is a hotfix from jsf1.0beta to jsf1.0fr
065          LOG.warn("patching renderer from " + name);
066          name = name.substring("javax.faces.".length());
067          LOG.warn("patching renderer to   " + name);
068        }*/
069        name = name.substring(0, 1).toLowerCase(Locale.ENGLISH) + name.substring(1);
070        return name;
071      }
072    
073      public int getConfiguredValue(FacesContext facesContext,
074          UIComponent component, String key) {
075        try {
076          return ThemeConfig.getValue(facesContext, component, key);
077        } catch (Exception e) {
078          LOG.error("Can't take '" + key + "' for " + getClass().getName()
079              + " from config-file: " + e.getMessage(), e);
080        }
081        return 0;
082      }
083    
084      protected String getCurrentValue(
085          FacesContext facesContext, UIComponent component) {
086    
087        if (component instanceof UIInput) {
088          Object submittedValue = ((UIInput) component).getSubmittedValue();
089          if (submittedValue != null) {
090            return (String) submittedValue;
091          }
092        }
093        String currentValue = null;
094        Object currentObj = getValue(component);
095        if (currentObj != null) {
096          currentValue = RenderUtil.getFormattedValue(facesContext, component, currentObj);
097        }
098        return currentValue;
099      }
100    
101      protected Object getValue(UIComponent component) {
102        if (component instanceof ValueHolder) {
103          return ((ValueHolder) component).getValue();
104        } else {
105          return null;
106        }
107      }
108    
109      public Converter getConverter(FacesContext context, UIComponent component) {
110        Converter converter = null;
111        if (component instanceof ValueHolder) {
112          converter = ((ValueHolder) component).getConverter();
113        }
114        if (converter == null) {
115          ValueBinding valueBinding = component.getValueBinding("value");
116          if (valueBinding != null) {
117            Class converterType = valueBinding.getType(context);
118            if (converterType == null || converterType == String.class
119                || converterType == Object.class) {
120              return null;
121            }
122            try {
123              converter = context.getApplication().createConverter(converterType);
124            } catch (FacesException e) {
125              LOG.error("No Converter found for type " + converterType);
126            }
127          }
128        }
129        return converter;
130      }
131    
132      public Object getConvertedValue(FacesContext context,
133          UIComponent component, Object submittedValue)
134          throws ConverterException {
135        if (!(submittedValue instanceof String)) {
136          return submittedValue;
137        }
138        Converter converter = getConverter(context, component);
139        if (converter != null) {
140          return converter.getAsObject(context, component, (String) submittedValue);
141        } else {
142          return submittedValue;
143        }
144      }
145    }