public Object getProperty( final String name ) throws BeanException { return getPropertyForSpecification( new PropertySpecification( name ) ); }
public ExpressionPropertiesReadHandler( final Expression expression, final String originalExpressionClass, final String expressionClass ) throws IntrospectionException { this.originalExpressionClass = originalExpressionClass; this.expressionClass = expressionClass; this.beanUtility = new BeanUtility( expression ); this.expression = expression; }
public static synchronized ConverterRegistry getInstance() { if ( instance == null ) { instance = new ConverterRegistry(); } return instance; }
public Class getPropertyType( final String name ) throws BeanException { if ( name == null ) { throw new NullPointerException( "Name must not be null" ); } final PropertySpecification ps = new PropertySpecification( name ); final PropertyDescriptor pd = properties.get( ps.getName() ); if ( pd == null ) { throw new BeanException( "No such property:" + name ); } return BeanUtility.getPropertyType( pd ); }
public String getPropertyAsString( final String name ) throws BeanException { final PropertySpecification ps = new PropertySpecification( name ); final PropertyDescriptor pd = properties.get( ps.getName() ); if ( pd == null ) { throw new BeanException( "No such property:" + name ); } final Object o = getPropertyForSpecification( ps ); if ( o == null ) { return null; } final ValueConverter vc = ConverterRegistry.getInstance().getValueConverter( o.getClass() ); if ( vc == null ) { throw new BeanException( "Unable to handle property of type " + o.getClass().getName() ); } return vc.toAttributeValue( o ); }
public void testArrayConversion() throws BeanException { final String[] array = { " " }; final ArrayValueConverter c = new ArrayValueConverter( String.class, new StringValueConverter() ); final String s = c.toAttributeValue( array ); final Object o = c.toPropertyValue( s ); assertTrue( o instanceof String[] ); final String[] strings = (String[]) o; assertEquals( strings.length, 1 ); assertEquals( strings[0], " " ); } }
/** * Converts the attribute to a string. * * @param o * the attribute ({@link Integer} expected). * @return A string representing the {@link Integer} value. */ public String toAttributeValue( final Object o ) throws BeanException { if ( o == null ) { return null; } if ( !( o instanceof Color ) ) { throw new BeanException( "Failed to convert object of type " + o.getClass() + ": Not a Color." ); } final Color c = (Color) o; return colorToString( c ); }
public ValueConverter getValueConverter( final Class c ) { final ValueConverter plain = registeredClasses.get( c ); if ( plain != null ) { return plain; } if ( c.isArray() ) { final Class componentType = c.getComponentType(); final ValueConverter componentConverter = getValueConverter( componentType ); if ( componentConverter != null ) { return new ArrayValueConverter( componentType, componentConverter ); } } if ( c.isEnum() ) { return new EnumValueConverter( c ); } return null; }
/** * Converts a string to a property value. * * @param s * the string. * @param c * the target class. * @return the object converted from the given string into a object of the target class. * @throws BeanException * if there was an error during the conversion. */ public static Object toPropertyValue( final String s, final Class c ) throws BeanException { if ( s == null ) { return null; } if ( c == null ) { return null; } final ValueConverter vc = ConverterRegistry.getInstance().getValueConverter( c ); if ( vc == null ) { return null; } return vc.toPropertyValue( s ); }
public void setProperty( final String name, final Object o ) throws BeanException { if ( name == null ) { throw new NullPointerException( "Name must not be null" ); } setProperty( new PropertySpecification( name ), o ); }
private static String continueLookupVariable( final CSVTokenizer tokenizer, final Object parent ) throws BeanException { if ( tokenizer.hasMoreTokens() ) { final String name = tokenizer.nextToken(); final Object base = ConverterRegistry.toPropertyValue( name, parent.getClass() ); if ( tokenizer.hasMoreTokens() ) { return continueLookupVariable( tokenizer, base ); } else { return ConverterRegistry.toAttributeValue( base ); } } return null; }
protected String lookupVariable( final String entity ) { // first, split the entity into separate strings (separator is '.'). final CSVTokenizer tokenizer = new CSVTokenizer( entity, "." ); if ( tokenizer.hasMoreTokens() ) { final String name = tokenizer.nextToken(); final Object base = performInitialLookup( name ); try { if ( tokenizer.hasMoreTokens() ) { return BeanPropertyLookupParser.continueLookupVariable( tokenizer, base ); } else { return ConverterRegistry.toAttributeValue( base ); } } catch ( BeanException e ) { return entity; } } return entity; }
public String toAttributeValue( final Object o ) throws BeanException { if ( o instanceof Enum == false ) { throw new BeanException(); } final Enum e = (Enum) o; return e.name(); }
/** * Creates a new PropertySpecification object for the given property string. * * @param raw * the property string, posssibly with index specifications. */ private PropertySpecification( final String raw ) { this.raw = raw; this.name = getNormalizedName( raw ); this.index = getIndex( raw ); }
private String convertColorToString( final BorderEdge borderEdge ) { final Color o = borderEdge.getColor(); if ( o == null ) { return null; } return ColorValueConverter.colorToString( o ); }
public ContentStyleReadHandler() { colorValueConverter = new ColorValueConverter(); }
public void setPropertyAsString( final String name, final String txt ) throws BeanException { if ( name == null ) { throw new NullPointerException( "Name must not be null" ); } if ( txt == null ) { throw new NullPointerException( "Text must not be null" ); } final PropertySpecification ps = new PropertySpecification( name ); final PropertyDescriptor pd = properties.get( ps.getName() ); if ( pd == null ) { throw new BeanException( bean.getClass() + ": No such property:" + name ); } setPropertyAsString( name, BeanUtility.getPropertyType( pd ), txt ); }
private int findMaximumIndex( final PropertyDescriptor id ) { try { final Object o = getPropertyForSpecification( new PropertySpecification( id.getName() ) ); return Array.getLength( o ); } catch ( Exception e ) { // ignore, we run 'til we encounter an index out of bounds Ex. } return 0; }
public Object toPropertyValue( final String s ) throws BeanException { try { return Enum.valueOf( enumClass, s ); } catch ( Exception e ) { throw new BeanException( "Failed to convert enum from string " + s, e ); } } }
/** * Creates a new object description. */ public ColorObjectDescription() { super( Color.class ); valueConverter = new ColorValueConverter(); setParameterDefinition( "value", String.class ); }