public void applyAttachedObject(FacesContext context, UIComponent parent) { FaceletContext ctx = (FaceletContext) context.getAttributes().get(FaceletContext.FACELET_CONTEXT_KEY); // cast to a ValueHolder ValueHolder vh = (ValueHolder) parent; ValueExpression ve = null; Converter c = null; if (this.binding != null) { ve = this.binding.getValueExpression(ctx, Converter.class); c = (Converter) ve.getValue(ctx); } if (c == null) { c = this.createConverter(ctx); if (ve != null) { ve.setValue(ctx, c); } } if (c == null) { throw new TagException(this.tag, "No Converter was created"); } this.setAttributes(ctx, c); vh.setConverter(c); Object lv = vh.getLocalValue(); FacesContext faces = ctx.getFacesContext(); if (lv instanceof String) { vh.setValue(c.getAsObject(faces, parent, (String) lv)); } }
/** * populate the argument component with values, being sensitive to the possible multi-nature of * the values, and to the type of the values. * * @param context the <code>FacesContext</code> for the current request * @param component the <code>UIComponent</code> to populate * @param componentType the component type * @param value the value * @param valueType the value type */ private void populateComponentWithValue( FacesContext context, UIComponent component, String componentType, String value, String valueType) { Application application = context.getApplication(); Converter converter = null; // if we need a converter, and can have a converter if (!"java.lang.String".equals(valueType) && component instanceof ValueHolder) { // if so create it, try { converter = application.createConverter(CarStore.loadClass(valueType, this)); // add it to our component, ((ValueHolder) component).setConverter(converter); } catch (ClassNotFoundException cne) { FacesMessage errMsg = MessageFactory.getMessage(CONVERTER_ERROR_MESSAGE_ID, valueType); throw new IllegalStateException(errMsg.getSummary()); } } // if this component is a SelectOne or SelectMany, take special action if (isMultiValue(componentType)) { // create a UISelectItems instance UISelectItems items = new UISelectItems(); items.setValue(parseStringIntoArrayList(value, converter)); // add it to the component component.getChildren().add(items); } else { // we have a single value if (null != converter) { component.getAttributes().put("value", converter.getAsObject(context, component, value)); } else { component.getAttributes().put("value", value); } } }
public void applyMetadata(FaceletContext ctx, Object instance) { ((ValueHolder) instance) .setConverter(ctx.getFacesContext().getApplication().createConverter(this.converterId)); }
@SuppressWarnings("deprecation") public UIComponent processWidget( UIComponent component, String elementName, Map<String, String> attributes, UIMetawidget metawidget) { // Actions don't get converters if (ACTION.equals(elementName)) { return component; } // Recurse into stubs... if (component instanceof UIStub) { // ...whose children have the same value binding as us... // // (this is important because choice of Converter is based off the attributes Map, and // if the value binding is different then all bets are off as to the accuracy of the // attributes) javax.faces.el.ValueBinding valueBinding = component.getValueBinding("value"); if (valueBinding != null) { String expressionString = valueBinding.getExpressionString(); List<UIComponent> children = component.getChildren(); for (UIComponent componentChild : children) { javax.faces.el.ValueBinding childValueBinding = componentChild.getValueBinding("value"); if (childValueBinding == null) { continue; } if (!expressionString.equals(childValueBinding.getExpressionString())) { continue; } // ...and apply the Converter to them processWidget(componentChild, elementName, attributes, metawidget); } } return component; } // Ignore components that cannot have Converters if (!(component instanceof ValueHolder)) { return component; } // Defer evaluation of EL-based converters, else we will fail trying to store/restore them // from the ViewState ValueHolder valueHolder = (ValueHolder) component; String converterId = attributes.get(FACES_CONVERTER); if (converterId != null && FacesUtils.isExpression(converterId)) { FacesContext context = FacesContext.getCurrentInstance(); component.setValueBinding( "converter", context.getApplication().createValueBinding(converterId)); return component; } // Standard Converter valueHolder.setConverter(getConverter(valueHolder, attributes)); return component; }