List of usage examples for javax.el ValueExpression getType
public abstract Class<?> getType(ELContext context);
From source file:com.flexive.faces.FxJsfComponentUtils.java
/** * Get the converted value of a component making sure that validators are processed. * Portions of the code are taken and adapted from HtmlBasicInputRenderer of the JSF RI 1.2 * * @param context faces context/*from w w w.j ava 2 s . co m*/ * @param component the component affected * @param submittedValue the submitted value * @return the converted value * @throws ConverterException on errors */ public static Object getConvertedValue(FacesContext context, UIComponent component, Object submittedValue) throws ConverterException { String newValue = (String) submittedValue; // if we have no local value, try to get the valueExpression. ValueExpression valueExpression = component.getValueExpression("value"); Converter converter = null; // If there is a converter attribute, use it to to ask application // instance for a converter with this identifer. if (component instanceof ValueHolder) { converter = ((ValueHolder) component).getConverter(); } if (null == converter && null != valueExpression) { Class converterType = valueExpression.getType(context.getELContext()); // if converterType is null, assume the modelType is "String". if (converterType == null || converterType == Object.class) { //no conversion necessary return newValue; } // If the converterType is a String, and we don't have a // converter-for-class for java.lang.String, assume the type is "String". if (converterType == String.class && !(null != context.getApplication().createConverter(String.class))) { //no conversion necessary return newValue; } // if getType returns a type for which we support a default try { Application application = context.getApplication(); converter = application.createConverter(converterType); } catch (Exception e) { LOG.error("Could not instantiate converter for type " + converterType + ": " + e.toString()); return (null); } } else if (converter == null) { // if there is no valueExpression and converter attribute set, // assume the modelType as "String" since we have no way of // figuring out the type. So for the selectOne and // selectMany, converter has to be set if there is no // valueExpression attribute set on the component. return newValue; } if (converter != null) { // If the conversion eventually falls to needing to use EL type coercion, // make sure our special ConverterPropertyEditor knows about this value. return converter.getAsObject(context, component, newValue); } else { // throw converter exception. throw new ConverterException("No converter for " + newValue + " available!"); } }
From source file:org.crank.javax.faces.component.MenuRenderer.java
public Object convertSelectManyValue(FacesContext context, UISelectMany uiSelectMany, String[] newValues) throws ConverterException { // if we have no local value, try to get the valueExpression. ValueExpression valueExpression = uiSelectMany.getValueExpression("value"); Object result = newValues; // default case, set local value Class<?> modelType = null; boolean throwException = false; // If we have a ValueExpression if (null != valueExpression) { modelType = valueExpression.getType(context.getELContext()); // Does the valueExpression resolve properly to something with // a type? if (modelType != null) { result = convertSelectManyValuesForModel(context, uiSelectMany, modelType, newValues); }//from w ww . j a v a2s .c o m // If it could not be converted, as a fall back try the type of // the valueExpression's current value covering some edge cases such // as where the current value came from a Map. if (result == null) { Object value = valueExpression.getValue(context.getELContext()); if (value != null) { result = convertSelectManyValuesForModel(context, uiSelectMany, value.getClass(), newValues); } } if (result == null) { throwException = true; } } else { // No ValueExpression, just use Object array. result = convertSelectManyValues(context, uiSelectMany, Object[].class, newValues); } if (throwException) { StringBuffer values = new StringBuffer(); if (null != newValues) { for (int i = 0; i < newValues.length; i++) { if (i == 0) { values.append(newValues[i]); } else { values.append(' ').append(newValues[i]); } } } Object[] params = { values.toString(), valueExpression.getExpressionString() }; throw new ConverterException( MessageUtils.getExceptionMessage(MessageUtils.CONVERSION_ERROR_MESSAGE_ID, params)); } // At this point, result is ready to be set as the value if (logger.isLoggable(Level.FINE)) { logger.fine("SelectMany Component " + uiSelectMany.getId() + " convertedValues " + result); } return result; }
From source file:com.flexive.faces.renderer.FxSelectRenderer.java
/** * Convert SelectManys value to the correct value classes * * @param context faces context//from ww w .j a v a 2 s .co m * @param uiSelectMany the select many component * @param newValues the new values to convert * @return converted values * @throws ConverterException on errors */ public Object convertSelectManyValue(FacesContext context, UISelectMany uiSelectMany, String[] newValues) throws ConverterException { // if we have no local value, try to get the valueExpression. ValueExpression valueExpression = uiSelectMany.getValueExpression("value"); Object result = newValues; // default case, set local value boolean throwException = false; // If we have a ValueExpression if (null != valueExpression) { Class modelType = valueExpression.getType(context.getELContext()); // Does the valueExpression resolve properly to something with // a type? if (modelType != null) result = convertSelectManyValuesForModel(context, uiSelectMany, modelType, newValues); // If it could not be converted, as a fall back try the type of // the valueExpression's current value covering some edge cases such // as where the current value came from a Map. if (result == null) { Object value = valueExpression.getValue(context.getELContext()); if (value != null) result = convertSelectManyValuesForModel(context, uiSelectMany, value.getClass(), newValues); } if (result == null) throwException = true; } else { // No ValueExpression, just use Object array. result = convertSelectManyValues(context, uiSelectMany, Object[].class, newValues); } if (throwException) { StringBuffer values = new StringBuffer(); if (null != newValues) { for (int i = 0; i < newValues.length; i++) { if (i == 0) values.append(newValues[i]); else values.append(' ').append(newValues[i]); } } throw new ConverterException("Error converting expression [" + valueExpression.getExpressionString() + "] of " + String.valueOf(values)); } return result; }
From source file:py.una.pol.karaku.util.ControllerHelper.java
private void updateModel(UIComponent formulario) { FacesContext context = getContext(); ELContext elContext = getContext().getELContext(); Iterator<UIComponent> iter = formulario.getFacetsAndChildren(); while (iter.hasNext()) { UIComponent component = iter.next(); // Si es un valor submiteable, o INPUTEaBLE if (component instanceof UISelectOne) { UISelectOne com = (UISelectOne) component; Object newValue = com.getSubmittedValue(); ValueExpression value = com.getValueExpression(EL_VALUE_PROPERTY); if (newValue != null && com.getConverter() != null) { // Si tiene un converter definido, entonces utilizamos ese // converter para obtener el valor newValue = com.getConverter().getAsObject(context, com, newValue.toString()); }/*from w w w. j a v a2 s . c o m*/ value.setValue(elContext, newValue); } else if (component instanceof UICalendar) { UICalendar com = (UICalendar) component; Object newValue = com.getSubmittedValue(); if (newValue != null) { ValueExpression value = com.getValueExpression(EL_VALUE_PROPERTY); newValue = getConverter().getAsObject(context, component, newValue.toString()); value.setValue(elContext, newValue); } } else if (component instanceof UIInput && !(component instanceof UICalendar)) { UIInput com = (UIInput) component; Object newValue = com.getSubmittedValue(); ValueExpression value = com.getValueExpression(EL_VALUE_PROPERTY); if (value.getType(elContext).equals(Quantity.class)) { if (StringUtils.isValid(newValue)) { newValue = new Quantity((String) newValue); } else { newValue = Quantity.ZERO; } } if (newValue instanceof String && !StringUtils.isValid(newValue)) { newValue = null; } value.setValue(elContext, newValue); } updateModel(component); } }
From source file:org.ajax4jsf.util.SelectUtils.java
/** * Converts UISelectMany submitted value to converted value * /*from w ww . java 2 s. c om*/ * @author Manfred Geiler * @param facesContext * @param component * @param submittedValue * @return * @throws ConverterException */ public static Object getConvertedUISelectManyValue(FacesContext facesContext, UISelectMany component, String[] submittedValue) throws ConverterException { // Attention! // This code is duplicated in jsfapi component package. // If you change something here please do the same in the other class! if (submittedValue == null) throw new NullPointerException("submittedValue"); ELContext elContext = facesContext.getELContext(); ValueExpression vb = component.getValueExpression("value"); Class<?> valueType = null; Class<?> arrayComponentType = null; if (vb != null) { valueType = vb.getType(elContext); if (valueType != null && valueType.isArray()) { arrayComponentType = valueType.getComponentType(); } } Converter converter = component.getConverter(); if (converter == null) { if (valueType == null) { // No converter, and no idea of expected type // --> return the submitted String array return submittedValue; } if (List.class.isAssignableFrom(valueType)) { // expected type is a List // --> according to javadoc of UISelectMany we assume that the // element type // is java.lang.String, and copy the String array to a new List List<String> lst = Arrays.asList(submittedValue); return lst; } if (arrayComponentType == null) { throw new IllegalArgumentException(Messages.getMessage(Messages.VALUE_BINDING_TYPE_ERROR)); } if (String.class.equals(arrayComponentType)) return submittedValue; // No conversion needed for String type if (Object.class.equals(arrayComponentType)) return submittedValue; // No conversion for Object class try { converter = facesContext.getApplication().createConverter(arrayComponentType); } catch (FacesException e) { log.error(Messages.getMessage(Messages.NO_CONVERTER_FOUND_ERROR, arrayComponentType.getName()), e); return submittedValue; } } // Now, we have a converter... if (valueType == null) { // ...but have no idea of expected type // --> so let's convert it to an Object array int len = submittedValue.length; Object[] convertedValues = (Object[]) Array .newInstance(arrayComponentType == null ? Object.class : arrayComponentType, len); for (int i = 0; i < len; i++) { convertedValues[i] = converter.getAsObject(facesContext, component, submittedValue[i]); } return convertedValues; } if (List.class.isAssignableFrom(valueType)) { // Curious case: According to specs we should assume, that the // element type // of this List is java.lang.String. But there is a Converter set // for this // component. Because the user must know what he is doing, we will // convert the values. int len = submittedValue.length; List<Object> lst = new ArrayList<Object>(len); for (int i = 0; i < len; i++) { lst.add(converter.getAsObject(facesContext, component, submittedValue[i])); } return lst; } if (arrayComponentType == null) { throw new IllegalArgumentException(Messages.getMessage(Messages.VALUE_BINDING_TYPE_ERROR)); } if (arrayComponentType.isPrimitive()) { // primitive array int len = submittedValue.length; Object convertedValues = Array.newInstance(arrayComponentType, len); for (int i = 0; i < len; i++) { Array.set(convertedValues, i, converter.getAsObject(facesContext, component, submittedValue[i])); } return convertedValues; } else { // Object array int len = submittedValue.length; ArrayList<Object> convertedValues = new ArrayList<Object>(len); for (int i = 0; i < len; i++) { convertedValues.add(i, converter.getAsObject(facesContext, component, submittedValue[i])); } return convertedValues.toArray((Object[]) Array.newInstance(arrayComponentType, len)); } }
From source file:org.ajax4jsf.util.SelectUtils.java
/** * /*from w w w. j a v a 2 s . com*/ * @param facesContext * @param component * @param property * @return converter for specified component attribute */ public static Converter getConverterForProperty(FacesContext facesContext, UIOutput component, String property) { Converter converter = component.getConverter(); if (converter == null) { ValueExpression ve = component.getValueExpression(property); if (ve != null) { Class<?> valueType = ve.getType(facesContext.getELContext()); if (valueType == null || String.class.equals(valueType) || Object.class.equals(valueType)) { //No converter needed } else { converter = facesContext.getApplication().createConverter(valueType); if (converter == null) { throw new ConverterException( Messages.getMessage(Messages.NO_CONVERTER_FOUND_ERROR, valueType.getName())); } } } } return converter; }
From source file:org.apache.myfaces.component.html.ext.AbstractHtmlDataTable.java
private void updateModelFromPreservedDataModel(FacesContext context) { ValueExpression vb = getValueExpression("value"); if (vb != null && !vb.isReadOnly(context.getELContext())) { DataModel qdm = getDataModel();/*w w w. j ava 2 s . c o m*/ if (qdm instanceof _SerializableDataModel) { _SerializableDataModel dm = (_SerializableDataModel) qdm; Class type = (getValueType() == null) ? vb.getType(context.getELContext()) : ClassUtils.simpleClassForName(getValueType()); Class dmType = dm.getClass(); if (DataModel.class.isAssignableFrom(type)) { vb.setValue(context.getELContext(), dm); } else if (List.class.isAssignableFrom(type) || _SerializableListDataModel.class.isAssignableFrom(dmType)) { vb.setValue(context.getELContext(), dm.getWrappedData()); } else if (OBJECT_ARRAY_CLASS.isAssignableFrom(type)) { List lst = (List) dm.getWrappedData(); vb.setValue(context.getELContext(), lst.toArray(new Object[lst.size()])); } else if (ResultSet.class.isAssignableFrom(type)) { throw new UnsupportedOperationException( this.getClass().getName() + " UnsupportedOperationException"); } else { //Assume scalar data model List lst = (List) dm.getWrappedData(); if (lst != null && lst.size() > 0) { vb.setValue(context.getELContext(), lst.get(0)); } else { vb.setValue(context.getELContext(), null); } } } } _preservedDataModel = null; }
From source file:org.jbuilt.components.html.raw.base.AbstractCommandComponent.java
public Object getConvertedValue(FacesContext context, UIComponent component, Object submittedValue) throws ConverterException { String newValue = (String) submittedValue; // if we have no local value, try to get the valueExpression. ValueExpression valueExpression = component.getValueExpression("value"); Converter converter = null;/*from w w w . j a v a2 s . c o m*/ // If there is a converter attribute, use it to to ask application // instance for a converter with this identifer. if (component instanceof ValueHolder) { converter = ((ValueHolder) component).getConverter(); } if (null == converter && null != valueExpression) { Class converterType = valueExpression.getType(context.getELContext()); // if converterType is null, assume the modelType is "String". if (converterType == null || converterType == Object.class) { if (logger.isLoggable(Level.FINE)) { logger.log(Level.FINE, "No conversion necessary for value {0} of component {1}", new Object[] { submittedValue, component.getId() }); } return newValue; } // If the converterType is a String, and we don't have a // converter-for-class for java.lang.String, assume the type is // "String". if (converterType == String.class && !hasStringConverter(context)) { // if (logger.isLoggable(Level.FINE)) { // logger.log(Level.FINE, // "No conversion necessary for value {0} of component {1}", // new Object[]{ // submittedValue, // component.getId()}); // } return newValue; } // if getType returns a type for which we support a default // conversion, acquire an appropriate converter instance. try { Application application = context.getApplication(); converter = application.createConverter(converterType); if (logger.isLoggable(Level.FINE)) { logger.log(Level.FINE, "Created converter ({0}) for type {1} for component {2}.", new Object[] { converter.getClass().getName(), converterType.getClass().getName(), component.getId() }); } } catch (Exception e) { // if (logger.isLoggable(Level.SEVERE)) { // logger.log(Level.SEVERE, // "Could not instantiate converter for type {0}: {1}", // new Object[] { // converterType, // e.toString() }); // } return null; } } else if (converter == null) { // if there is no valueExpression and converter attribute set, // assume the modelType as "String" since we have no way of // figuring out the type. So for the selectOne and // selectMany, converter has to be set if there is no // valueExpression attribute set on the component. if (logger.isLoggable(Level.FINE)) { logger.log(Level.FINE, "No conversion necessary for value {0} of component {1}", new Object[] { submittedValue, component.getId() }); logger.fine(" since there is no explicitly registered converter " + "and the component value is not bound to a model property"); } return newValue; } if (converter != null) { // If the conversion eventually falls to needing to use EL type coercion, // make sure our special ConverterPropertyEditor knows about this value. RequestStateManager.set(context, RequestStateManager.TARGET_COMPONENT_ATTRIBUTE_NAME, component); return converter.getAsObject(context, component, newValue); } else { // throw converter exception. Object[] params = { newValue, "null Converter" }; throw new ConverterException( MessageFactory.getMessage(context, MessageUtils.CONVERSION_ERROR_MESSAGE_ID, params)); } }
From source file:org.jbuilt.components.html.raw.base.AbstractInputComponent.java
public Object getConvertedValue(FacesContext context, UIComponent component, Object submittedValue) throws ConverterException { component = this; String newValue = (String) submittedValue; // if we have no local value, try to get the valueExpression. ValueExpression valueExpression = component.getValueExpression("value"); Converter converter = null;// w w w. ja v a 2 s . co m // If there is a converter attribute, use it to to ask application // instance for a converter with this identifer. if (component instanceof ValueHolder) { converter = ((ValueHolder) component).getConverter(); } if (null == converter && null != valueExpression) { Class converterType = valueExpression.getType(context.getELContext()); // if converterType is null, assume the modelType is "String". if (converterType == null || converterType == Object.class) { if (logger.isLoggable(Level.FINE)) { logger.log(Level.FINE, "No conversion necessary for value {0} of component {1}", new Object[] { submittedValue, component.getId() }); } return newValue; } // If the converterType is a String, and we don't have a // converter-for-class for java.lang.String, assume the type is // "String". if (converterType == String.class && !hasStringConverter(context)) { // if (logger.isLoggable(Level.FINE)) { // logger.log(Level.FINE, // "No conversion necessary for value {0} of component {1}", // new Object[]{ // submittedValue, // component.getId()}); // } return newValue; } // if getType returns a type for which we support a default // conversion, acquire an appropriate converter instance. try { Application application = context.getApplication(); converter = application.createConverter(converterType); if (logger.isLoggable(Level.FINE)) { logger.log(Level.FINE, "Created converter ({0}) for type {1} for component {2}.", new Object[] { converter.getClass().getName(), converterType.getClass().getName(), component.getId() }); } } catch (Exception e) { // if (logger.isLoggable(Level.SEVERE)) { // logger.log(Level.SEVERE, // "Could not instantiate converter for type {0}: {1}", // new Object[] { // converterType, // e.toString() }); // } return null; } } else if (converter == null) { // if there is no valueExpression and converter attribute set, // assume the modelType as "String" since we have no way of // figuring out the type. So for the selectOne and // selectMany, converter has to be set if there is no // valueExpression attribute set on the component. if (logger.isLoggable(Level.FINE)) { logger.log(Level.FINE, "No conversion necessary for value {0} of component {1}", new Object[] { submittedValue, component.getId() }); logger.fine(" since there is no explicitly registered converter " + "and the component value is not bound to a model property"); } return newValue; } if (converter != null) { // If the conversion eventually falls to needing to use EL type coercion, // make sure our special ConverterPropertyEditor knows about this value. RequestStateManager.set(context, RequestStateManager.TARGET_COMPONENT_ATTRIBUTE_NAME, component); return converter.getAsObject(context, component, newValue); } else { // throw converter exception. Object[] params = { newValue, "null Converter" }; throw new ConverterException( MessageFactory.getMessage(context, MessageUtils.CONVERSION_ERROR_MESSAGE_ID, params)); } }
From source file:org.jbuilt.utils.ValueClosure.java
@Override public boolean equals(Object other) { if (other == this) { return true; }// w w w .j a v a 2s . c om if (other instanceof ValueExpression) { ValueExpression otherVE = (ValueExpression) other; Class type = null; try { type = PropertyUtils.getPropertyType(this.bean, this.prop); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } catch (NoSuchMethodException e) { e.printStackTrace(); } if (type != null) { return type.equals(otherVE.getType(context.getELContext())); } } return false; }