예제 #1
0
 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));
   }
 }
 @Before
 public void setupMocks() {
   SpringFacesContextSetter.setCurrentInstance(springFacesContext);
   given(springFacesContext.getFacesContext()).willReturn(facesContext);
   given(facesContext.getApplication()).willReturn(application);
   given(application.createConverter("example")).willReturn(facesConverter);
   given(application.createConverter(ClassWithConverter.class)).willReturn(facesConverter);
   given(facesConverter.getAsObject(facesContext, null, source)).willReturn(converted);
 }
 @Override
 public Object getConvertedValue(FacesContext context, UIComponent component, Object val)
     throws ConverterException {
   String s = (String) val;
   Converter converter = getConverterForValue(context, component);
   if (converter != null) {
     return converter.getAsObject(context, component, s);
   } else {
     return s;
   }
 }
 @Override
 public Object getConvertedValue(
     FacesContext context, UIComponent component, Object submittedValue)
     throws ConverterException {
   UIInputListOfValues inputDate = (UIInputListOfValues) component;
   Converter converter = inputDate.getConverter();
   if (converter != null) {
     return converter.getAsObject(context, component, String.valueOf(submittedValue));
   }
   if (submittedValue != null && String.valueOf(submittedValue).trim().isEmpty()) return null;
   return submittedValue;
 }
예제 #5
0
 public Object getAsObject(FacesContext context, UIComponent component, String value) {
   Converter delegate = getDelegate(context);
   if (delegate != null) {
     return delegate.getAsObject(context, component, value);
   } else {
     throw new ConverterException(
         MessageUtils.getExceptionMessage(
             MessageUtils.CANNOT_CONVERT_ID,
             converterId != null ? converterId.getExpressionString() : "",
             binding != null ? binding.getExpressionString() : ""));
   }
 }
예제 #6
0
  @Override
  public Object getConvertedValue(
      FacesContext context, UIComponent component, Object submittedValue)
      throws ConverterException {
    Converter converter = ComponentUtils.getConverter(context, component);

    if (converter != null) {
      String convertableValue = submittedValue == null ? null : submittedValue.toString();
      return converter.getAsObject(context, component, convertableValue);
    } else {
      return submittedValue;
    }
  }
예제 #7
0
 public static Object getConvertedUIOutputValue(
     final FacesContext context, final UIOutput output, final Object submittedValue)
     throws ConverterException {
   if (submittedValue == null) {
     return null;
   }
   final Converter converter = findConverter(context, output);
   if (converter == null) {
     return submittedValue;
   }
   return converter.getAsObject(
       context, output, (submittedValue instanceof String) ? (String) submittedValue : null);
 }
예제 #8
0
  @Override
  public Object getConvertedValue(
      FacesContext context, UIComponent component, Object submittedValue)
      throws ConverterException {
    Editor editor = (Editor) component;
    String value = (String) submittedValue;
    Converter converter = ComponentUtils.getConverter(context, component);

    if (converter != null) {
      return converter.getAsObject(context, editor, value);
    }

    return value;
  }
예제 #9
0
  public void testNumberConverter(UIViewRoot root)
      throws ConverterException, InstantiationException, IllegalAccessException,
          ClassNotFoundException {
    System.out.println("Tesing NumberConverter");
    UIInput text = new UIInput();
    text.setId("my_input_number");
    root.getChildren().add(text);

    Converter converter = application.createConverter("javax.faces.Number");

    String stringToConvert = "99.9";
    Object obj = converter.getAsObject(getFacesContext(), text, stringToConvert);
    assertTrue(obj instanceof java.lang.Number);
    String str = converter.getAsString(getFacesContext(), text, obj);
    assertTrue(str.equals(stringToConvert));
  }
  @Test
  public void testGetAsObject() {
    Converter converter = new AtomicIntegerConverter();
    assertNull(converter.getAsObject(null, null, null));
    assertNull(converter.getAsObject(null, null, ""));
    assertNull(converter.getAsObject(null, null, " "));
    assertTrue(8 == ((AtomicInteger) converter.getAsObject(null, null, " 8")).intValue());
    assertTrue(8 == ((AtomicInteger) converter.getAsObject(null, null, "8 ")).intValue());
    assertTrue(8 == ((AtomicInteger) converter.getAsObject(null, null, "8")).intValue());
    int over = Integer.MAX_VALUE + 1;
    assertTrue(over == ((AtomicInteger) converter.getAsObject(null, null, over + "")).intValue());
    int under = Integer.MIN_VALUE - 1;
    assertTrue(under == ((AtomicInteger) converter.getAsObject(null, null, under + "")).intValue());

    try {
      converter.getAsObject(null, null, "NaN");
      fail("should only take numbers");
    } catch (ConverterException c) {
    }
  }
예제 #11
0
 public static Object getConvertedUIOutputValues(
     final FacesContext context, final UIOutput output, final Object submittedValue) {
   if (submittedValue == null) {
     return null;
   }
   final Converter converter = findConverter(context, output);
   if (converter == null) {
     return submittedValue;
   }
   final int length = Array.getLength(submittedValue);
   final Class valueType = getValueType(context, output);
   final Object ret = Array.newInstance(valueType, length);
   for (int i = 0; i < length; ++i) {
     final Object target = Array.get(submittedValue, i);
     final String value = (target instanceof String) ? (String) target : null;
     final Object o = converter.getAsObject(context, output, value);
     ArrayUtil.setArrayValue(ret, valueType, o, i);
   }
   return ret;
 }
예제 #12
0
파일: CarBean.java 프로젝트: kowsercse/jsf
  /**
   * 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);
      }
    }
  }
예제 #13
0
 public static Object getObjectFromRequestParameter(
     String requestParameterName, Converter converter, UIComponent component) {
   String theId = JsfUtil.getRequestParameter(requestParameterName);
   return converter.getAsObject(FacesContext.getCurrentInstance(), component, theId);
 }
예제 #14
0
 public static Object getConvertedValue(
     FacesContext facesContext, UIComponent component, Object val) throws ConverterException {
   String[] values = (val == null) ? new String[0] : (String[]) val;
   Converter converter = SelectManyHelper.getItemConverter(facesContext, component);
   ValueExpression ve = component.getValueExpression("value");
   Object targetForConvertedValues = null;
   if (ve != null) {
     // If the component has a ValueExpression for value, let modelType be the type of the value
     // expression
     Class<?> modelType = ve.getType(facesContext.getELContext());
     if (modelType.isArray()) {
       // If the component has a ValueExpression for value and the type of the expression is an
       // array, let targetForConvertedValues be a new array of the expected type.
       Class<?> arrayComponentType = modelType.getComponentType();
       targetForConvertedValues = Array.newInstance(arrayComponentType, values.length);
     } else if (Collection.class.isAssignableFrom(modelType) || Object.class.equals(modelType)) {
       // If modelType is a Collection, do the following to arrive at targetForConvertedValues:
       // Ask the component for its attribute under the key "collectionType"
       String collectionType = (String) component.getAttributes().get("collectionType");
       if (collectionType != null) {
         // Let targetForConvertedValues be a new instance of Collection implemented by the
         // concrete class specified in collectionType
         Class<?> collectionClass = getCollectionClass(collectionType);
         try {
           targetForConvertedValues = collectionClass.newInstance();
         } catch (Exception e) {
           throw new FacesException(e);
         }
       } else {
         // If there is no "collectionType" attribute, call getValue() on the component
         // The result will implement Collection.
         Collection value = (Collection) ((EditableValueHolder) component).getValue();
         if (value instanceof Cloneable) {
           // If the result also implements Cloneable, let targetForConvertedValues be the result
           // of calling its clone() method,
           // then calling clear() on the cloned Collection.
           try {
             targetForConvertedValues =
                 (Collection) value.getClass().getMethod("clone").invoke(value);
             ((Collection) targetForConvertedValues).clear();
           } catch (Exception e) {
             // If unable to clone the value for any reason, log a message
             LOG.log(Logger.Level.WARNING, "Unable to clone collection");
           }
         }
         if (targetForConvertedValues == null) {
           // and proceed to the next step
           Class<?> collectionClass = value == null ? modelType : value.getClass();
           try {
             // If modelType is a concrete class, let targetForConvertedValues be a new instance of
             // that class.
             targetForConvertedValues = collectionClass.newInstance();
             ((Collection) targetForConvertedValues).clear();
           } catch (Exception e) {
             // Otherwise, the concrete type for targetForConvertedValues is taken from the
             // following table
             if (Collection.class.isAssignableFrom(modelType)) {
               if (SortedSet.class.isAssignableFrom(modelType)) {
                 targetForConvertedValues = new TreeSet();
               } else if (Queue.class.isAssignableFrom(modelType)) {
                 targetForConvertedValues = new LinkedList();
               } else if (Set.class.isAssignableFrom(modelType)) {
                 targetForConvertedValues = new HashSet(values.length);
               } else {
                 targetForConvertedValues = new ArrayList(values.length);
               }
             }
           }
         }
       }
     } else {
       throw new FacesException("ValueExpression must be either an Array, or a Collection");
     }
   } else {
     // If the component does not have a ValueExpression for value, let targetForConvertedValues be
     // an array of type Object.
     targetForConvertedValues = new Object[values.length];
   }
   for (int i = 0; i < values.length; i++) {
     Object value;
     if (converter == null) {
       value = values[i];
     } else {
       value = converter.getAsObject(facesContext, component, values[i]);
     }
     if (targetForConvertedValues.getClass().isArray()) {
       Array.set(targetForConvertedValues, i, value);
     } else {
       ((Collection) targetForConvertedValues).add(value);
     }
   }
   return targetForConvertedValues;
 }
예제 #15
0
  public void testDateConverter(UIViewRoot root)
      throws ConverterException, InstantiationException, IllegalAccessException,
          ClassNotFoundException {
    System.out.println("Testing DateConverter");
    UIInput text = new UIInput();
    text.setId("my_input_date");
    root.getChildren().add(text);

    Converter converter = null;
    converter = application.createConverter("javax.faces.DateTime");

    // date
    String stringToConvert = "Jan 1, 1967";
    Object obj = converter.getAsObject(getFacesContext(), text, stringToConvert);
    assertTrue(obj instanceof java.util.Date);
    String str = converter.getAsString(getFacesContext(), text, obj);
    // make sure we end up with the same string we started with..
    assertTrue(str.equals(stringToConvert));

    // time
    converter = application.createConverter("javax.faces.DateTime");
    ((DateTimeConverter) converter).setType("time");
    text = new UIInput();
    text.setId("my_input_time");
    stringToConvert = "10:10:10 AM";
    obj = converter.getAsObject(getFacesContext(), text, stringToConvert);
    assertTrue(obj instanceof java.util.Date);
    str = converter.getAsString(getFacesContext(), text, obj);
    // make sure we end up with the same string we started with..
    assertTrue(str.equals(stringToConvert));

    // datetime
    converter = application.createConverter("javax.faces.DateTime");
    ((DateTimeConverter) converter).setType("both");
    text = new UIInput();
    text.setId("my_input_datetime");
    stringToConvert = "Jan 1, 1967 10:10:10 AM";
    obj = converter.getAsObject(getFacesContext(), text, stringToConvert);
    assertTrue(obj instanceof java.util.Date);
    str = converter.getAsString(getFacesContext(), text, obj);
    // make sure we end up with the same string we started with..
    assertTrue(str.equals(stringToConvert));

    // test bogus type....
    boolean exceptionThrown = false;
    try {
      ((DateTimeConverter) converter).setType("foobar");
      obj = converter.getAsObject(getFacesContext(), text, stringToConvert);
    } catch (Exception e) {
      exceptionThrown = true;
    }
    assertTrue(exceptionThrown);

    // test NullPointerException (if either context or component arg is null)
    exceptionThrown = false;
    try {
      obj = converter.getAsObject(null, text, stringToConvert);
    } catch (NullPointerException npe) {
      exceptionThrown = true;
    }
    assertTrue(exceptionThrown);
    exceptionThrown = false;
    try {
      obj = converter.getAsObject(getFacesContext(), null, stringToConvert);
    } catch (NullPointerException npe) {
      exceptionThrown = true;
    }
    assertTrue(exceptionThrown);

    exceptionThrown = false;
    try {
      str = converter.getAsString(null, text, obj);
    } catch (NullPointerException npe) {
      exceptionThrown = true;
    }
    assertTrue(exceptionThrown);

    exceptionThrown = false;
    try {
      str = converter.getAsString(getFacesContext(), null, obj);
    } catch (NullPointerException npe) {
      exceptionThrown = true;
    }
    assertTrue(exceptionThrown);
  }