Search in sources :

Example 6 with PropertiesEntityFactory

use of org.whole.lang.properties.factories.PropertiesEntityFactory in project whole by wholeplatform.

the class PropertiesUtils method translate.

@SuppressWarnings("unchecked")
public static Properties translate(java.util.Properties jProps) throws IOException {
    PropertiesEntityFactory lf = PropertiesEntityFactory.instance;
    Properties props = lf.create(PropertiesEntityDescriptorEnum.Properties);
    Entries entries = props.getEntries();
    if (entries.wSize() == 1)
        // workaround for removing the resolver
        entries.wRemove(0);
    Enumeration<String> names = (Enumeration<String>) jProps.propertyNames();
    while (names.hasMoreElements()) {
        String name = names.nextElement();
        String value = jProps.getProperty(name);
        entries.wAdd(lf.createProperty(lf.createPropertyName(name), // FIXME workaround for null valued property returned from System.getProperties()
        value != null ? lf.createPropertyValue(value) : CommonsEntityAdapterFactory.createResolver(PropertiesEntityDescriptorEnum.PropertyValue)));
    }
    return props;
}
Also used : PropertiesEntityFactory(org.whole.lang.properties.factories.PropertiesEntityFactory) Enumeration(java.util.Enumeration) Entries(org.whole.lang.properties.model.Entries) Properties(org.whole.lang.properties.model.Properties)

Example 7 with PropertiesEntityFactory

use of org.whole.lang.properties.factories.PropertiesEntityFactory in project whole by wholeplatform.

the class PropertiesUtils method getProperty.

public static String getProperty(Properties props, String name, String defaultValue) {
    if (EntityUtils.isResolver(props))
        return defaultValue;
    PropertiesEntityFactory lf = PropertiesEntityFactory.instance;
    Property pattern = lf.createProperty(lf.createPropertyName(name), createVariable(PropertiesEntityDescriptorEnum.PropertyValue, "value"));
    Property property = Matcher.findChild(pattern, props.getEntries());
    if (property == null)
        return getProperty(props.getDefaults(), name, defaultValue);
    else
        return property.getValue().wStringValue();
}
Also used : PropertiesEntityFactory(org.whole.lang.properties.factories.PropertiesEntityFactory) Property(org.whole.lang.properties.model.Property)

Example 8 with PropertiesEntityFactory

use of org.whole.lang.properties.factories.PropertiesEntityFactory in project whole by wholeplatform.

the class FactoriesTest method testCompositeFactoryMethods.

@Test
public void testCompositeFactoryMethods() {
    IEntityRegistry efi = ReflectionFactory.getLanguageKit(PropertiesLanguageKit.URI).getEntityRegistry(RegistryConfigurations.DEFAULT);
    PropertiesEntityFactory ef = PropertiesEntityFactory.instance;
    Entries p = ef.createEntries(ef.createProperty(), ef.create(PropertiesEntityDescriptorEnum.Property, "p2", "v2"));
    efi.put(p);
    Entries e1 = ef.createEntries();
    Assert.assertEquals(2, e1.wSize());
    Entries e2 = ef.createEntries(new Property[0]);
    Assert.assertEquals(0, e2.wSize());
    Entries e3 = ef.createEntries(ef.create(PropertiesEntityDescriptorEnum.Property, "p1", "v1"));
    Assert.assertEquals(1, e3.wSize());
}
Also used : PropertiesEntityFactory(org.whole.lang.properties.factories.PropertiesEntityFactory) Entries(org.whole.lang.properties.model.Entries) Test(org.junit.Test)

Aggregations

PropertiesEntityFactory (org.whole.lang.properties.factories.PropertiesEntityFactory)8 Test (org.junit.Test)5 Entries (org.whole.lang.properties.model.Entries)4 Property (org.whole.lang.properties.model.Property)3 Properties (org.whole.lang.properties.model.Properties)2 Enumeration (java.util.Enumeration)1 PropertyName (org.whole.lang.properties.model.PropertyName)1 PropertyValue (org.whole.lang.properties.model.PropertyValue)1