View Javadoc
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  
20  package org.apache.myfaces.tobago.renderkit;
21  
22  import org.apache.myfaces.tobago.context.ResourceManager;
23  import org.apache.myfaces.tobago.internal.context.ResourceManagerFactory;
24  import org.apache.myfaces.tobago.layout.Measure;
25  import org.slf4j.Logger;
26  import org.slf4j.LoggerFactory;
27  
28  import javax.el.ValueExpression;
29  import javax.faces.FacesException;
30  import javax.faces.component.EditableValueHolder;
31  import javax.faces.component.UIComponent;
32  import javax.faces.component.UIInput;
33  import javax.faces.component.ValueHolder;
34  import javax.faces.context.FacesContext;
35  import javax.faces.convert.Converter;
36  import javax.faces.convert.ConverterException;
37  import javax.faces.render.Renderer;
38  
39  public class RendererBase extends Renderer {
40  
41    private static final Logger LOG = LoggerFactory.getLogger(RendererBase.class);
42  
43    private ResourceManager resourceManager;
44  
45    protected Object getCurrentValueAsObject(final UIInput input) {
46      final Object submittedValue = input.getSubmittedValue();
47      if (submittedValue != null) {
48        return submittedValue;
49      }
50      return getValue(input);
51    }
52  
53    protected String getCurrentValue(final FacesContext facesContext, final UIComponent component) {
54  
55      if (component instanceof EditableValueHolder) {
56        final EditableValueHolder editableValueHolder = (EditableValueHolder) component;
57        final Object submittedValue = editableValueHolder.getSubmittedValue();
58        if (submittedValue != null || !editableValueHolder.isValid()) {
59          return (String) submittedValue;
60        }
61      }
62      String currentValue = null;
63      final Object currentObj = getValue(component);
64      if (currentObj != null) {
65        currentValue = getFormattedValue(facesContext, component, currentObj);
66      }
67      return currentValue;
68    }
69  
70    protected String getFormattedValue(final FacesContext context, final UIComponent component, final Object currentValue)
71        throws ConverterException {
72  
73      if (currentValue == null) {
74        return "";
75      }
76  
77      if (!(component instanceof ValueHolder)) {
78        return currentValue.toString();
79      }
80  
81      Converter converter = ((ValueHolder) component).getConverter();
82  
83      if (converter == null) {
84        if (currentValue instanceof String) {
85          return (String) currentValue;
86        }
87        if (currentValue instanceof Measure) {
88          return ((Measure) currentValue).serialize();
89        }
90        final Class converterType = currentValue.getClass();
91        converter = context.getApplication().createConverter(converterType);
92      }
93  
94      if (converter == null) {
95        return currentValue.toString();
96      } else {
97        return converter.getAsString(context, component, currentValue);
98      }
99    }
100 
101   protected Object getValue(final UIComponent component) {
102     if (component instanceof ValueHolder) {
103       return ((ValueHolder) component).getValue();
104     } else {
105       return null;
106     }
107   }
108 
109   public Converter getConverter(final FacesContext context, final UIComponent component) {
110     Converter converter = null;
111     if (component instanceof ValueHolder) {
112       converter = ((ValueHolder) component).getConverter();
113     }
114     if (converter == null) {
115       final ValueExpression valueExpression = component.getValueExpression("value");
116       if (valueExpression != null) {
117         final Class converterType = valueExpression.getType(context.getELContext());
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 (final FacesException e) {
125           LOG.error("No Converter found for type " + converterType);
126         }
127       }
128     }
129     return converter;
130   }
131 
132   @Override
133   public Object getConvertedValue(final FacesContext context, final UIComponent component, final Object submittedValue)
134       throws ConverterException {
135     if (!(submittedValue instanceof String)) {
136       return submittedValue;
137     }
138     final 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 
146   public void onComponentCreated(
147       final FacesContext facesContext, final UIComponent component, final UIComponent parent) {
148   }
149   
150   protected synchronized ResourceManager getResourceManager() {
151     if (resourceManager == null) {
152       resourceManager = ResourceManagerFactory.getResourceManager(FacesContext.getCurrentInstance());
153     }
154     return resourceManager;
155   }
156 }