Search in sources :

Example 26 with PropertyResourceBundle

use of java.util.PropertyResourceBundle in project j2objc by google.

the class PropertyResourceBundleTest method test_access$0_Enumeration.

/**
 * {@link java.util.PropertyResourceBundle#Enumeration}
 */
public void test_access$0_Enumeration() throws IOException {
    class MockResourceBundle extends PropertyResourceBundle {

        MockResourceBundle(java.io.InputStream stream) throws IOException {
            super(stream);
        }

        @Override
        protected void setParent(ResourceBundle bundle) {
            super.setParent(bundle);
        }
    }
    java.io.InputStream localStream = new java.io.ByteArrayInputStream("p3=three\np4=four".getBytes());
    MockResourceBundle localPrb = new MockResourceBundle(localStream);
    localPrb.setParent(prb);
    Enumeration<String> keys = localPrb.getKeys();
    Vector<String> contents = new Vector<String>();
    while (keys.hasMoreElements()) {
        contents.add(keys.nextElement());
    }
    assertEquals("did not get the right number of properties", 4, contents.size());
    assertTrue("did not get the parent property p1", contents.contains("p1"));
    assertTrue("did not get the parent property p2", contents.contains("p2"));
    assertTrue("did not get the local property p3", contents.contains("p3"));
    assertTrue("did not get the local property p4", contents.contains("p4"));
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) PropertyResourceBundle(java.util.PropertyResourceBundle) ResourceBundle(java.util.ResourceBundle) Vector(java.util.Vector) PropertyResourceBundle(java.util.PropertyResourceBundle) InputStream(java.io.InputStream)

Example 27 with PropertyResourceBundle

use of java.util.PropertyResourceBundle in project j2objc by google.

the class PropertyResourceBundleTest method test_ConstructorLjava_io_InputStream.

/**
 * @throws IOException
 * java.util.PropertyResourceBundle#PropertyResourceBundle(java.io.InputStream)
 */
@SuppressWarnings("nls")
public void test_ConstructorLjava_io_InputStream() throws IOException {
    InputStream propertiesStream = new ByteArrayInputStream("p1=one\ncharset=iso-8859-1".getBytes("ISO-8859-1"));
    prb = new PropertyResourceBundle(propertiesStream);
    assertEquals(2, prb.keySet().size());
    assertEquals("one", prb.getString("p1"));
    assertEquals("iso-8859-1", prb.getString("charset"));
    propertiesStream = new ByteArrayInputStream("p1=one\ncharset=UTF-8".getBytes("UTF-8"));
    prb = new PropertyResourceBundle(propertiesStream);
    assertEquals(2, prb.keySet().size());
    assertEquals("UTF-8", prb.getString("charset"));
    try {
        new PropertyResourceBundle((InputStream) null);
        fail("Should throw NullPointerException");
    } catch (NullPointerException e) {
    // expected
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) PropertyResourceBundle(java.util.PropertyResourceBundle)

Example 28 with PropertyResourceBundle

use of java.util.PropertyResourceBundle in project java-swing-tips by aterai.

the class Utf8ResourceBundleControl method newBundle.

@Override
public ResourceBundle newBundle(String baseName, Locale locale, String format, ClassLoader loader, boolean reload) throws IOException {
    ResourceBundle bundle = null;
    if ("properties".equals(format)) {
        String bundleName = toBundleName(Objects.requireNonNull(baseName, "baseName must not be null"), Objects.requireNonNull(locale, "locale must not be null"));
        String resourceName = toResourceName(bundleName, Objects.requireNonNull(format, "format must not be null"));
        InputStream stream = null;
        ClassLoader cl = Objects.requireNonNull(loader, "loader must not be null");
        if (reload) {
            URL url = cl.getResource(resourceName);
            if (Objects.nonNull(url)) {
                URLConnection connection = url.openConnection();
                if (Objects.nonNull(connection)) {
                    connection.setUseCaches(false);
                    stream = connection.getInputStream();
                }
            }
        } else {
            stream = cl.getResourceAsStream(resourceName);
        }
        if (Objects.nonNull(stream)) {
            // BufferedInputStream bis = new BufferedInputStream(stream);
            try (Reader r = new BufferedReader(new InputStreamReader(stream, StandardCharsets.UTF_8))) {
                bundle = new PropertyResourceBundle(r);
            }
        }
    }
    return bundle;
}
Also used : InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) BufferedReader(java.io.BufferedReader) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) PropertyResourceBundle(java.util.PropertyResourceBundle) ResourceBundle(java.util.ResourceBundle) URL(java.net.URL) URLConnection(java.net.URLConnection) PropertyResourceBundle(java.util.PropertyResourceBundle)

Example 29 with PropertyResourceBundle

use of java.util.PropertyResourceBundle in project jaffa-framework by jaffa-projects.

the class Variant method loadResource.

private static synchronized PropertyResourceBundle loadResource(String variant) {
    PropertyResourceBundle resource = null;
    if (c_cache.containsKey(variant))
        resource = (PropertyResourceBundle) c_cache.get(variant);
    else {
        try {
            resource = (PropertyResourceBundle) PropertyResourceBundle.getBundle(variant);
            c_cache.put(variant, resource);
        } catch (MissingResourceException e) {
            // Add a null object for the variant into the cache. This will avoid repeated searches for the variant resource.
            c_cache.put(variant, null);
        }
    }
    return resource;
}
Also used : MissingResourceException(java.util.MissingResourceException) PropertyResourceBundle(java.util.PropertyResourceBundle)

Example 30 with PropertyResourceBundle

use of java.util.PropertyResourceBundle in project jaffa-framework by jaffa-projects.

the class Factory method readTestTier.

/**
 * Read the tier setting from the middleware_test.properties file
 */
private static Byte readTestTier(String className) {
    PropertyResourceBundle prop = null;
    try {
        prop = (PropertyResourceBundle) PropertyResourceBundle.getBundle(MIDDLEWARE_RESOURCE);
    } catch (MissingResourceException e) {
        log.warn("Factory Set To Test Mode, But Config File Is Missing!, Mode 2 will be assumed! ", e);
        return new Byte(TIER2);
    }
    try {
        String tier = (String) prop.getObject(className);
        if (tier == null || "123".indexOf(tier) == -1) {
            log.info("Factory Test Mode For Interface '" + className + "' Not Valid. Value is " + tier + ", Mode 2 will be assumed! ");
            tier = "" + TIER2;
        }
        return new Byte(tier);
    } catch (MissingResourceException e) {
        log.info("Factory Set To Test Mode, But Definition For Interface '" + className + "' not found, Mode 2 will be assumed! ");
        return new Byte(TIER2);
    }
}
Also used : MissingResourceException(java.util.MissingResourceException) PropertyResourceBundle(java.util.PropertyResourceBundle)

Aggregations

PropertyResourceBundle (java.util.PropertyResourceBundle)66 ResourceBundle (java.util.ResourceBundle)32 InputStream (java.io.InputStream)31 IOException (java.io.IOException)27 URL (java.net.URL)24 InputStreamReader (java.io.InputStreamReader)20 URLConnection (java.net.URLConnection)14 MissingResourceException (java.util.MissingResourceException)13 FileInputStream (java.io.FileInputStream)12 File (java.io.File)9 Locale (java.util.Locale)7 FileNotFoundException (java.io.FileNotFoundException)5 MalformedURLException (java.net.MalformedURLException)4 Bundle (org.osgi.framework.Bundle)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 Properties (java.util.Properties)3 TemplatePersistenceData (org.eclipse.jface.text.templates.persistence.TemplatePersistenceData)3 TemplateReaderWriter (org.eclipse.jface.text.templates.persistence.TemplateReaderWriter)3