Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
License Agreement. Rich Faces - Natural Ajax for Java Server Faces (JSF) Copyright (C) 2007 Exadel, Inc. This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License version 2.1 as published by the Free Software Foundation. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
 package org.richfaces.component.util;
 
 import  javax.faces.component.UIComponent;
 import  javax.faces.component.UIInput;
 import  javax.faces.component.ValueHolder;
 import  javax.faces.context.FacesContext;
 import  javax.faces.convert.Converter;
 import  javax.faces.convert.ConverterException;
 
 
 import  com.google.common.base.Strings;

Author(s):
Maksim Kaszynski
Manfred Geiler
 
 public final class InputUtils {
         public Converter getConverterByValue(FacesContext context, UIComponent componentObject value) {
             Converter result = null;
 
             if (component instanceof ValueHolder) {
                 result = ((ValueHolder) component).getConverter();
             }
 
             if (result == null && value != null) {
                 result = getConverterForType(contextvalue.getClass());
             }
 
             return result;
         }
 
         public Converter getConverterByProperty(FacesContext context, UIComponent component) {
             return findConverter(contextcomponent"value");
         }
     };
 
     private InputUtils() {
     }
 
     public static interface ConverterLookupStrategy {
         Converter getConverterByProperty(FacesContext context, UIComponent component);
 
         Converter getConverterByValue(FacesContext context, UIComponent componentObject value);
     }
 
     public static boolean isDisabled(UIComponent component) {
         return RendererUtils.getInstance().isBooleanAttribute(component"disabled");
     }
 
     public static boolean isReadOnly(UIComponent component) {
         return RendererUtils.getInstance().isBooleanAttribute(component"readonly");
     }
 
     public static Converter getConverterForType(FacesContext contextClass<?> type) {
         // see getConvertedValue
         if (type == null || Object.class.equals(type)) {
             return null;
         }
 
         return context.getApplication().createConverter(type);
     }
 
     public static String getConvertedStringValue(FacesContext context, UIComponent componentObject value)
         throws ConverterException {
         return getConvertedStringValue(contextcomponentvalue);
     }
 
     public static String getConvertedStringValue(FacesContext context, UIComponent component,
         ConverterLookupStrategy converterLookupStrategyObject valuethrows ConverterException {
 
         Converter converter = converterLookupStrategy.getConverterByValue(contextcomponentvalue);
         if (converter != null) {
             return converter.getAsString(contextcomponentvalue);
         }
 
         if (value == null) {
            return "";
        }
        if (value instanceof String) {
            return (Stringvalue;
        }
        return value.toString();
    }
    public static Converter findConverter(FacesContext facesContext, UIComponent componentString property) {
        Converter converter = null;
        if (component instanceof ValueHolder) {
            converter = ((ValueHolder) component).getConverter();
        }
        if (converter == null) {
            ValueExpression ve = component.getValueExpression(property);
            if (ve != null) {
                Class<?> valueType = ve.getType(facesContext.getELContext());
                if (valueType == null || Object.class.equals(valueType)) {
                    // No converter needed
                } else {
                    converter = facesContext.getApplication().createConverter(valueType);
                }
            }
        }
        return converter;
    }
    public static Object getConvertedValue(FacesContext context, UIComponent componentObject valthrows ConverterException {
        return getConvertedValue(contextcomponentval);
    }
    public static Object getConvertedValue(FacesContext context, UIComponent component,
        ConverterLookupStrategy converterLookupStrategyObject valthrows ConverterException {
        String submittedString = (Stringval;
        if (Strings.isNullOrEmpty(submittedString)) {
            return null;
        }
        Converter converter = converterLookupStrategy.getConverterByProperty(context, (UIInput) component);
        if (converter != null) {
            return converter.getAsObject(contextcomponentsubmittedString);
        }
        return submittedString;
    }
    public static String getInputValue(FacesContext context, UIComponent componentthrows ConverterException {
        return getInputValue(contextcomponent);
    }
    public static String getInputValue(FacesContext context, UIComponent component,
        ConverterLookupStrategy converterLookupStrategythrows ConverterException {
        UIInput input = (UIInput) component;
        String submittedValue = (Stringinput.getSubmittedValue();
        if (submittedValue != null) {
            return submittedValue;
        }
        Object value = input.getValue();
        Converter converter = converterLookupStrategy.getConverterByValue(contextinputvalue);
        if (converter != null) {
            return converter.getAsString(contextinputvalue);
        } else {
            return value != null ? value.toString() : "";
        }
    }
New to GrepCode? Check out our FAQ X