Package org.apache.commons.beanutils

Examples of org.apache.commons.beanutils.Converter


    /**
     * Test Array Conversion
     */
    public void testArray() {
        Converter converter = new ClassConverter();

        // Test Array Class to String
        assertEquals("Array to String", "[Ljava.lang.Boolean;", converter.convert(String.class, Boolean[].class));

        // *** N.B. for some reason the following works on m1, but not m2
        // Test String to Array Class
        // assertEquals("String to Array", Boolean[].class, converter.convert(Class.class, "[Ljava.lang.Boolean;"));
    }
View Full Code Here


    /**
     * Test Invalid
     */
    public void testInvalid() {
        Converter converter = new ClassConverter();

        // Test invalid class name
        try {
            converter.convert(Class.class, "foo.bar");
            fail("Invalid class name, expected ConversionException");
        } catch (ConversionException e) {
            // expected result
        }
    }
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

    private static Object convert(Class klass, Object value) throws JellyTagException {
        if(null == value) {
            return null;
        } else if(!klass.isInstance(value)) {
            Converter converter = (Converter)(converterMap.get(klass));
            if(null == converter) {
                throw new JellyTagException("Can't convert " + value + " to " + klass);
            } else {
                try {
                    return converter.convert(klass,value);
                } catch(ConversionException e) {
                    throw new JellyTagException("Can't convert " + value + " to " + klass + " (" + e.toString() + ")",e);
                }
            }
        } else {
View Full Code Here

      for (Iterator<Element> i2 = modE.getChild(DATA_LIST,ns).getChildren(PARAM,ns).iterator();i2.hasNext();) {
        Element parE = i2.next();
        String key = parE.getAttributeValue(KEY,ns);
        String valueString = parE.getAttributeValue(VALUE,ns);
        DataDescriptor dd = DataHolderUtils.getDescriptorForKey(am,key);
        Converter toValueConverter = DataHolderUtils.getToValueConverter(dd);
        if (toValueConverter == null) {
          throw new IllegalArgumentException("No converter found for " + dd.getClass()); //$NON-NLS-1$
        }
        am.setData(key,toValueConverter.convert(dd.getDataType(), valueString));
      }     
      for (Iterator<Element> i2 = modE.getChild(TEST_LIST,ns).getChildren(TEST,ns).iterator();i2.hasNext();) {
        Element parE = i2.next();
        String name = parE.getAttributeValue(NAME,ns);
        String perform = parE.getAttributeValue(DO_TEST,ns);
View Full Code Here

  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) CONVERTER_MAP.get(ClassUtils.primitiveToWrapper(destClass));

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

      if (value instanceof String) {
        Object result = null;
        if (context == null)
          result = ConvertUtils.convert((String) value,clazz);
        else {
          Converter contextConverter = null;
          HashMap<Class,Converter> contextMap = contextMaps.get(context);
          if (contextMap != null)
            contextConverter = contextMap.get(clazz);
          if (contextConverter != null) {
            contextConverter.convert(clazz,value);
          } else {
            System.out.println("no context converter found ");
            result = ConvertUtils.convert((String) value,clazz);
          }
        }
  //      if (result instanceof java.util.Date) { //  dates need to be normalized
  //        result = new Date(DateTime.gmt((Date) result));
  //      }
        if (result == null) {
          throw new FieldParseException("Invalid type");
        }
        return result;
     
 
      // Because of stupidity of beanutils which assumes type string, I implement this by hand
      Converter converter = ConvertUtils.lookup(clazz);                      
      if (converter == null) {                        
        System.out.println("converter is null for class " + clazz + " instance " + instance.hashCode() + " resetting") ;
        instance = new FieldConverter();
        converter = ConvertUtils.lookup(String.class)
      }
      return converter.convert(clazz, value);
    } catch (ConversionException conversionException) {
      throw new FieldParseException(conversionException);
    }
  }
View Full Code Here

    ConvertUtils.register(new DateConverter(), Date.class);   // Wrapper class
    ConvertUtils.register(new CalendarConverter(), GregorianCalendar.class);   // Wrapper class
    ConvertUtils.register(new DurationConverter(), Duration.class);   // Wrapper class
    ConvertUtils.register(new WorkConverter(), Work.class);   // Wrapper class
    ConvertUtils.register(new MoneyConverter(), Money.class);   // Wrapper class
    Converter longConverter = new LongConverter();
    ConvertUtils.register(longConverter, Long.TYPE);    // Native type
    ConvertUtils.register(longConverter, Long.class);   // Wrapper class
    Converter doubleConverter = new DoubleConverter();
    ConvertUtils.register(doubleConverter, Double.TYPE);    // Native type
    ConvertUtils.register(doubleConverter, Double.class);   // Wrapper class
   

    // short context converters
View Full Code Here

            digester.addBeanPropertySetter(prefix + "/" + TAG_VOLATILITY, "volatility");
            digester.addRule(prefix + "/" + TAG_MISFIRE_INSTRUCTION, new MisfireInstructionRule("misfireInstruction"));
            digester.addBeanPropertySetter(prefix + "/" + TAG_CALENDAR_NAME, "calendarName");
            digester.addBeanPropertySetter(prefix + "/" + TAG_JOB_NAME, "jobName");
            digester.addBeanPropertySetter(prefix + "/" + TAG_JOB_GROUP, "jobGroup");
            Converter converter = new DateConverter(new String[] { XSD_DATE_FORMAT, DTD_DATE_FORMAT });
            digester.addRule(prefix + "/" + TAG_START_TIME, new SimpleConverterRule("startTime", converter, Date.class));
            digester.addRule(prefix + "/" + TAG_END_TIME, new SimpleConverterRule("endTime", converter, 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.