All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.draagon.meta.attr.PropertiesAttribute Maven / Gradle / Ivy

The newest version!
package com.draagon.meta.attr;

import com.draagon.meta.DataTypes;

import java.io.IOException;
import java.io.StringReader;
import java.util.Properties;

/**
 * A Properties Attribute
 */
@SuppressWarnings("serial")
public class PropertiesAttribute extends MetaAttribute {

    public final static String SUBTYPE_PROPERTIES = "properties";

    public PropertiesAttribute(String name ) {
        super( SUBTYPE_PROPERTIES, name, DataTypes.CUSTOM);
    }

    /**
     * Manually create a Properties MetaAttribute with a value
     */
    public static PropertiesAttribute create(String name, Properties value ) {
        PropertiesAttribute a = new PropertiesAttribute( name );
        a.setValue( value );
        return a;
    }

    @Override
    public void setValueAsObject(Object value) {
        if ( value == null ) {
            setValue( null );
        } else if ( value instanceof String ) {
            setValueAsString( (String) value );
        }
        else if ( value instanceof Properties ) {
            super.setValue((Properties) value);
        }
        throw new InvalidAttributeValueException( "Can not set value with class [" + value.getClass() + "] for object: " + value );
    }

    @Override
    public void setValueAsString(String value) {
        try {
            Properties p = new Properties();
            p.load(new StringReader(value.toString()));
            setValue( p );
        } catch (IOException e) {
            throw new InvalidAttributeValueException("Could not load properties [" + value + "]: " + e.getMessage(), e);
        }
    }

    @Override
    public String getValueAsString() {
        return getValue().toString();
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy