Search in sources :

Example 1 with PropertiesClass

use of net.jangaroo.properties.model.PropertiesClass in project jangaroo-tools by CoreMedia.

the class PropertyClassGenerator method generate.

@Override
public File generate(File propertiesFile) {
    PropertiesConfiguration p = new PropertiesConfiguration();
    p.setDelimiterParsingDisabled(true);
    Reader r = null;
    try {
        r = new BufferedReader(new InputStreamReader(new FileInputStream(propertiesFile), "UTF-8"));
        p.load(r);
    } catch (IOException e) {
        throw new PropcException("Error while parsing properties file", propertiesFile, e);
    } catch (ConfigurationException e) {
        throw new PropcException("Error while parsing properties file", propertiesFile, e);
    } finally {
        try {
            if (r != null) {
                r.close();
            }
        } catch (IOException e) {
        //not really
        }
    }
    ResourceBundleClass bundle = new ResourceBundleClass(PropcHelper.computeBaseClassName(locations, propertiesFile));
    // Create properties class, which registers itself with the bundle.
    return generateJangarooClass(new PropertiesClass(bundle, PropcHelper.computeLocale(propertiesFile), p, propertiesFile));
}
Also used : InputStreamReader(java.io.InputStreamReader) ConfigurationException(org.apache.commons.configuration.ConfigurationException) ResourceBundleClass(net.jangaroo.properties.model.ResourceBundleClass) BufferedReader(java.io.BufferedReader) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) PropcException(net.jangaroo.properties.api.PropcException) PropertiesClass(net.jangaroo.properties.model.PropertiesClass) IOException(java.io.IOException) PropertiesConfiguration(org.apache.commons.configuration.PropertiesConfiguration) FileInputStream(java.io.FileInputStream)

Example 2 with PropertiesClass

use of net.jangaroo.properties.model.PropertiesClass in project jangaroo-tools by CoreMedia.

the class PropertyClassGeneratorTest method testSimplePropertySet.

@Test
public void testSimplePropertySet() throws Exception {
    FileLocations locations = new FileLocations();
    List<File> sourcePath = new ArrayList<File>();
    sourcePath.add(new File(getClass().getResource("/").toURI()));
    locations.setSourcePath(sourcePath);
    locations.addSourceFile("testPackage/subPackage/Properties.properties");
    locations.addSourceFile("testPackage/PropertiesTest.properties");
    locations.addSourceFile("testPackage/PropertiesTest_de.properties");
    locations.addSourceFile("testPackage/PropertiesTest_es_ES.properties");
    locations.addSourceFile("testPackage/PropertiesTest_it_VA_WIN.properties");
    PropertyClassGenerator generator = new PropertyClassGenerator(locations);
    StringWriter writer = new StringWriter();
    ResourceBundleClass rbc = new ResourceBundleClass("testPackage.PropertiesTest");
    PropertiesConfiguration p = new PropertiesConfiguration();
    p.setProperty("key", "Die Platte \"{1}\" enthält {0}.");
    p.setProperty("key2", "Die Platte \"{1}\" enthält {0}.");
    PropertiesClass pc = new PropertiesClass(rbc, null, p, null);
    generator.generatePropertiesClass(pc, writer);
    assertEquals(("package testPackage {\n" + "import joo.ResourceBundleAwareClassLoader;\n" + "import joo.JavaScriptObject;\n" + "\n" + "/**\n" + " * Properties class for ResourceBundle \"PropertiesTest\".\n" + " * @see PropertiesTest_properties#INSTANCE\n" + " */\n" + "[ResourceBundle('PropertiesTest')]\n" + "public class PropertiesTest_properties extends joo.JavaScriptObject {\n" + "\n" + "/**\n" + " * Singleton for the current user Locale's instance of ResourceBundle \"PropertiesTest\".\n" + " * @see PropertiesTest_properties\n" + " */\n" + "public static const INSTANCE:PropertiesTest_properties = ResourceBundleAwareClassLoader.INSTANCE.createSingleton(PropertiesTest_properties) as PropertiesTest_properties;\n" + "\n" + "[Resource(key='key',bundle='PropertiesTest')]\n" + "public native function get key():String;\n" + "[Resource(key='key2',bundle='PropertiesTest')]\n" + "public native function get key2():String;\n" + "\n" + "public function PropertiesTest_properties() {\n" + "  this[\"key\"] = \"Die Platte \\\"{1}\\\" enthält {0}.\";\n" + "  this[\"key2\"] = \"Die Platte \\\"{1}\\\" enthält {0}.\";\n" + "}\n" + "}\n" + "}").replaceAll("\n", LINE_SEPARATOR), writer.toString());
    PropertiesClass psc = new PropertiesClass(rbc, Locale.ENGLISH, p, null);
    writer = new StringWriter();
    generator.generatePropertiesClass(psc, writer);
    assertEquals(("package testPackage {\n" + "\n" + "/**\n" + " * Properties class for ResourceBundle \"PropertiesTest\" and Locale \"en\".\n" + " * @see PropertiesTest_properties#INSTANCE\n" + " */\n" + "[ResourceBundle('PropertiesTest_en')]\n" + "public class PropertiesTest_properties_en extends PropertiesTest_properties {\n" + "\n" + "\n" + "public function PropertiesTest_properties_en() {\n" + "  this[\"key\"] = \"Die Platte \\\"{1}\\\" enthält {0}.\";\n" + "  this[\"key2\"] = \"Die Platte \\\"{1}\\\" enthält {0}.\";\n" + "}\n" + "}\n" + "}").replaceAll("\n", LINE_SEPARATOR), writer.toString());
}
Also used : StringWriter(java.io.StringWriter) ResourceBundleClass(net.jangaroo.properties.model.ResourceBundleClass) ArrayList(java.util.ArrayList) PropertyClassGenerator(net.jangaroo.properties.PropertyClassGenerator) PropertiesClass(net.jangaroo.properties.model.PropertiesClass) File(java.io.File) PropertiesConfiguration(org.apache.commons.configuration.PropertiesConfiguration) FileLocations(net.jangaroo.utils.FileLocations) Test(org.junit.Test)

Aggregations

PropertiesClass (net.jangaroo.properties.model.PropertiesClass)2 ResourceBundleClass (net.jangaroo.properties.model.ResourceBundleClass)2 PropertiesConfiguration (org.apache.commons.configuration.PropertiesConfiguration)2 BufferedReader (java.io.BufferedReader)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1 InputStreamReader (java.io.InputStreamReader)1 Reader (java.io.Reader)1 StringWriter (java.io.StringWriter)1 ArrayList (java.util.ArrayList)1 PropertyClassGenerator (net.jangaroo.properties.PropertyClassGenerator)1 PropcException (net.jangaroo.properties.api.PropcException)1 FileLocations (net.jangaroo.utils.FileLocations)1 ConfigurationException (org.apache.commons.configuration.ConfigurationException)1 Test (org.junit.Test)1