Package org.apache.commons.beanutils

Examples of org.apache.commons.beanutils.Converter


  public Object convert(Object srcFieldValue, Class destFieldClass, DateFormatContainer dateFormatContainer) {
    if (srcFieldValue == null || destFieldClass == null || (srcFieldValue.equals("") && !destFieldClass.equals(String.class))) {
      return null;
    }
    Converter converter = getPrimitiveOrWrapperConverter(destFieldClass, dateFormatContainer);
    try {
      return converter.convert(destFieldClass, srcFieldValue);
    } catch (org.apache.commons.beanutils.ConversionException e) {
      throw new org.dozer.converters.ConversionException(e);
    }
  }
View Full Code Here


  private Converter getPrimitiveOrWrapperConverter(Class destClass, DateFormatContainer dateFormatContainer) {
    if (String.class.equals(destClass)) {
      return new StringConverter(dateFormatContainer);
    }

    Converter result = CONVERTER_MAP.get(ClassUtils.primitiveToWrapper(destClass));

    if (result == null) {
      if (java.util.Date.class.isAssignableFrom(destClass)) {
        result = new DateConverter(dateFormatContainer.getDateFormat());
      } else if (Calendar.class.isAssignableFrom(destClass) ) {
View Full Code Here

  /**
   * 为BeanUtilsBean提供缺省的日期转换器
   */
  public void addDefaultDateConverter() {
    JRest4GuiceHelper.addBeanConvert(new Converter() {
      @Override
      public Object convert(Class type, Object value) {
        if (type != java.sql.Time.class || value == null
            || value.toString().trim().equals(""))
          return null;
        Object result = DatePatternConverter.this.format(value
            .toString());
        if (result == null) {
          try {
            result = new java.sql.Time(Long.parseLong(value
                .toString()));
          } catch (Exception e) {
          }
        }
        return result;
      }
    }, java.sql.Time.class);

    JRest4GuiceHelper.addBeanConvert(new Converter() {
      @Override
      public Object convert(Class type, Object value) {
        if (type != java.util.Date.class || value == null
            || value.toString().trim().equals(""))
          return null;
        Object result = DatePatternConverter.this.format(value
            .toString());
        if (result == null) {
          try {
            result = new java.util.Date(Long.parseLong(value
                .toString()));
          } catch (Exception e) {
          }
        }
        return result;
      }
    }, java.util.Date.class);

    JRest4GuiceHelper.addBeanConvert(new Converter() {
      @Override
      public Object convert(Class type, Object value) {
        if (type != java.sql.Date.class || value == null
            || value.toString().trim().equals(""))
          return null;
View Full Code Here

    ConvertUtils.register(new LongConverter(null), Long.class);
    assertNull(ConvertUtils.convert("", Long.class));
  }

  private void converToDate(String dateStr, int year, int month, int day) {
    Converter c = new SqlDateConverter();
    Date date = (Date) c.convert(Date.class, dateStr);
    Calendar calendar = new GregorianCalendar();
    calendar.setTime(date);
    assertEquals(calendar.get(Calendar.YEAR), year);
    assertEquals(calendar.get(Calendar.MONTH), month);
    assertEquals(calendar.get(Calendar.DAY_OF_MONTH), day);
View Full Code Here

    date1 = "1980-09-09";
    converToDate(date1, 1980, 8, 9);
  }

  private void converToDate(String dateStr, int year, int month, int day) {
    Converter c = new DateConverter();
    Date date = (Date) c.convert(Date.class, dateStr);
    Calendar calendar = new GregorianCalendar();
    calendar.setTime(date);
    assertEquals(calendar.get(Calendar.YEAR), year);
    assertEquals(calendar.get(Calendar.MONTH), month);
    assertEquals(calendar.get(Calendar.DAY_OF_MONTH), day);
View Full Code Here

        if (returnType.equals(Class.class)) {
            value = toQualifiedClassName(value, defaultPackage);
        }

        /* Converter lookup */
        Converter converter = ConvertUtils.lookup(returnType);
        if (converter == null && returnType.isEnum()) {
            converter = EnumerationConverter.getInstance();
        }

        if (converter != null) {
            return converter.convert(returnType, value);
        } else {
            return converter;
        }
    }
View Full Code Here

     * Helper method for testing a conversion with null as default.
     *
     * @param input the input string
     */
    private void checkConversionWithNullDefault(String input) {
        Converter converter = new IntegerLocaleConverter(null, Locale.US);
        assertNull("Wrong result", converter.convert(Integer.class, input));
    }
View Full Code Here

            }
        }
       
        if (destType != null)
        {
            Converter converter = ConvertUtils.lookup(destType);
            if (converter == null)
            {
                throw new UnsupportedOperationException("cant deal with " + destType);
            }

            // setting type to null, in fact the documentation is wrong here and this type is never used
            convertedValue = converter.convert(null, convertedValue);
        }
       
       
        return convertedValue;
    }
View Full Code Here

        ISODateConverter converter = new ISODateConverter();
        ConvertUtils.register(converter, java.util.Date.class);
        ISOToStringConverter tsConverter = new ISOToStringConverter();
        ConvertUtils.register(tsConverter, String.class);
       
        Converter dateConverter = ConvertUtils.lookup(java.util.Date.class);
        assertEquals("Date converter successfully registered", dateConverter, converter);
        Converter stringConverter = ConvertUtils.lookup(String.class);
        assertEquals("Date converter successfully registered", tsConverter, stringConverter);
       
        java.util.Date conversionResult = (java.util.Date)
                                ConvertUtils.convert("20030101", java.util.Date.class);
       
View Full Code Here

        ISODateConverter converter = new ISODateConverter();
        ConvertUtils.register(converter, java.util.Date.class);
        ISOToStringConverter tsConverter = new ISOToStringConverter();
        ConvertUtils.register(tsConverter, String.class);
       
        Converter dateConverter = ConvertUtils.lookup(java.util.Date.class);
        assertEquals("Date converter successfully registered", dateConverter, converter);
        Converter stringConverter = ConvertUtils.lookup(String.class);
        assertEquals("Date converter successfully registered", tsConverter, stringConverter);
       
        java.util.Date conversionResult = (java.util.Date)
                                ConvertUtils.convert("20030101", java.util.Date.class);
       
View Full Code Here

TOP

Related Classes of org.apache.commons.beanutils.Converter

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.