Search in sources :

Example 46 with PropertyResourceBundle

use of java.util.PropertyResourceBundle in project ORCID-Source by ORCID.

the class UTF8Control method newBundle.

public ResourceBundle newBundle(String baseName, Locale locale, String format, ClassLoader loader, boolean reload) throws IllegalAccessException, InstantiationException, IOException {
    // The below is a copy of the default implementation.
    String bundleName = toBundleName(baseName, locale);
    String resourceName = toResourceName(bundleName, "properties");
    ResourceBundle bundle = null;
    InputStream stream = null;
    if (reload) {
        URL url = loader.getResource(resourceName);
        if (url != null) {
            URLConnection connection = url.openConnection();
            if (connection != null) {
                connection.setUseCaches(false);
                stream = connection.getInputStream();
            }
        }
    } else {
        stream = loader.getResourceAsStream(resourceName);
    }
    if (stream != null) {
        try {
            // Only this line is changed to make it to read properties files as UTF-8.
            bundle = new PropertyResourceBundle(new InputStreamReader(stream, "UTF-8"));
        } finally {
            stream.close();
        }
    }
    return bundle;
}
Also used : InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) ResourceBundle(java.util.ResourceBundle) PropertyResourceBundle(java.util.PropertyResourceBundle) URL(java.net.URL) URLConnection(java.net.URLConnection) PropertyResourceBundle(java.util.PropertyResourceBundle)

Example 47 with PropertyResourceBundle

use of java.util.PropertyResourceBundle in project TranskribusCore by Transkribus.

the class UTF8Control method newBundle.

public ResourceBundle newBundle(String baseName, Locale locale, String format, ClassLoader loader, boolean reload) throws IllegalAccessException, InstantiationException, IOException {
    // The below is a copy of the default implementation.
    String bundleName = toBundleName(baseName, locale);
    String resourceName = toResourceName(bundleName, "properties");
    ResourceBundle bundle = null;
    InputStream stream = null;
    if (reload) {
        URL url = loader.getResource(resourceName);
        if (url != null) {
            URLConnection connection = url.openConnection();
            if (connection != null) {
                connection.setUseCaches(false);
                stream = connection.getInputStream();
            }
        }
    } else {
        stream = loader.getResourceAsStream(resourceName);
    }
    if (stream != null) {
        try {
            // Only this line is changed to make it to read properties files as UTF-8.
            bundle = new PropertyResourceBundle(new InputStreamReader(stream, "UTF-8"));
        } finally {
            stream.close();
        }
    }
    return bundle;
}
Also used : InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) ResourceBundle(java.util.ResourceBundle) PropertyResourceBundle(java.util.PropertyResourceBundle) URL(java.net.URL) URLConnection(java.net.URLConnection) PropertyResourceBundle(java.util.PropertyResourceBundle)

Example 48 with PropertyResourceBundle

use of java.util.PropertyResourceBundle in project netxms by netxms.

the class NXFindAndReplaceAction method getResourceBundle.

/**
 * Get resource bundle
 * @return
 * @throws IOException
 */
private static ResourceBundle getResourceBundle(ViewPart wp) throws IOException {
    InputStream in = null;
    // $NON-NLS-1$
    String resource = "resource.properties";
    ClassLoader loader = Activator.getDefault().getClass().getClassLoader();
    if (loader != null) {
        in = loader.getResourceAsStream(resource);
    } else {
        in = ClassLoader.getSystemResourceAsStream(resource);
    }
    return new PropertyResourceBundle(in);
}
Also used : InputStream(java.io.InputStream) PropertyResourceBundle(java.util.PropertyResourceBundle)

Example 49 with PropertyResourceBundle

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

the class Variant method getProperty.

/**
 * Returns the value of a property, for a given variant. The default resource will be used, in case the variant resource is not found or if the variant does not have the given property.
 * @param variant the name of the variant. If a null value is passed, then the default will be used.
 * @param key the name of the property.
 * @throws MissingResourceException if the property is not found in both the variant and the default resource files.
 * @return the value of a property.
 */
public static String getProperty(String variant, String key) throws MissingResourceException {
    String value = null;
    PropertyResourceBundle resource = null;
    if (variant != null) {
        variant = VARIANT_RESOURCE_PREFIX + variant;
        // no need to find the resource if 'default' is passed in
        if (!variant.equals(DEFAULT_RESOURCE_NAME)) {
            if (c_cache.containsKey(variant))
                resource = (PropertyResourceBundle) c_cache.get(variant);
            else
                resource = loadResource(variant);
        }
        if (resource == null) {
            resource = c_defaultResource;
            if (log.isDebugEnabled())
                log.debug("Resource file not found for the variant: " + variant + ". Will use the default resource");
        }
    } else {
        resource = c_defaultResource;
    }
    // Now get the property from the resource
    try {
        value = resource.getString(key);
    } catch (MissingResourceException e) {
        // check if the key is present in the default resource
        if (resource != c_defaultResource) {
            if (log.isDebugEnabled())
                log.debug("Key '" + key + "' not found in the variant resource file '" + variant + "'. Will check the default resource");
            try {
                value = c_defaultResource.getString(key);
            } catch (MissingResourceException e1) {
                String str = "Key '" + key + "' not found in the default resource file";
                log.error(str, e1);
                throw e1;
            }
        } else {
            String str = "Key '" + key + "' not found in the default resource file";
            log.error(str, e);
            throw e;
        }
    }
    return value;
}
Also used : MissingResourceException(java.util.MissingResourceException) PropertyResourceBundle(java.util.PropertyResourceBundle)

Example 50 with PropertyResourceBundle

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

the class ResourceBundleWrapper method instantiateBundle.

private static ResourceBundleWrapper instantiateBundle(final String baseName, final String localeID, final String defaultID, final ClassLoader root, final boolean disableFallback) {
    final String name = localeID.isEmpty() ? baseName : baseName + '_' + localeID;
    String cacheKey = disableFallback ? name : name + '#' + defaultID;
    return BUNDLE_CACHE.getInstance(cacheKey, new Loader() {

        @Override
        public ResourceBundleWrapper load() {
            ResourceBundleWrapper parent = null;
            int i = localeID.lastIndexOf('_');
            boolean loadFromProperties = false;
            boolean parentIsRoot = false;
            if (i != -1) {
                String locName = localeID.substring(0, i);
                parent = instantiateBundle(baseName, locName, defaultID, root, disableFallback);
            } else if (!localeID.isEmpty()) {
                parent = instantiateBundle(baseName, "", defaultID, root, disableFallback);
                parentIsRoot = true;
            }
            ResourceBundleWrapper b = null;
            try {
                Class<? extends ResourceBundle> cls = root.loadClass(name).asSubclass(ResourceBundle.class);
                ResourceBundle bx = cls.newInstance();
                b = new ResourceBundleWrapper(bx);
                if (parent != null) {
                    b.setParent(parent);
                }
                b.baseName = baseName;
                b.localeID = localeID;
            } catch (ClassNotFoundException e) {
                loadFromProperties = true;
            } catch (NoClassDefFoundError e) {
                loadFromProperties = true;
            } catch (Exception e) {
                if (DEBUG)
                    System.out.println("failure");
                if (DEBUG)
                    System.out.println(e);
            }
            if (loadFromProperties) {
                try {
                    final String resName = name.replace('.', '/') + ".properties";
                    InputStream stream = java.security.AccessController.doPrivileged(new java.security.PrivilegedAction<InputStream>() {

                        @Override
                        public InputStream run() {
                            return root.getResourceAsStream(resName);
                        }
                    });
                    if (stream != null) {
                        // make sure it is buffered
                        stream = new java.io.BufferedInputStream(stream);
                        try {
                            b = new ResourceBundleWrapper(new PropertyResourceBundle(stream));
                            if (parent != null) {
                                b.setParent(parent);
                            }
                            b.baseName = baseName;
                            b.localeID = localeID;
                        } catch (Exception ex) {
                        // throw away exception
                        } finally {
                            try {
                                stream.close();
                            } catch (Exception ex) {
                            // throw away exception
                            }
                        }
                    }
                    // the default locale not the root locale!
                    if (b == null && !disableFallback && !localeID.isEmpty() && localeID.indexOf('_') < 0 && !localeIDStartsWithLangSubtag(defaultID, localeID)) {
                        // localeID is only a language subtag, different from the default language.
                        b = instantiateBundle(baseName, defaultID, defaultID, root, disableFallback);
                    }
                    // if still could not find the bundle then return the parent
                    if (b == null && (!parentIsRoot || !disableFallback)) {
                        b = parent;
                    }
                } catch (Exception e) {
                    if (DEBUG)
                        System.out.println("failure");
                    if (DEBUG)
                        System.out.println(e);
                }
            }
            if (b != null) {
                b.initKeysVector();
            } else {
                if (DEBUG)
                    System.out.println("Returning null for " + baseName + "_" + localeID);
            }
            return b;
        }
    });
}
Also used : InputStream(java.io.InputStream) MissingResourceException(java.util.MissingResourceException) PropertyResourceBundle(java.util.PropertyResourceBundle) UResourceBundle(android.icu.util.UResourceBundle) ResourceBundle(java.util.ResourceBundle) PropertyResourceBundle(java.util.PropertyResourceBundle)

Aggregations

PropertyResourceBundle (java.util.PropertyResourceBundle)71 InputStream (java.io.InputStream)35 ResourceBundle (java.util.ResourceBundle)33 IOException (java.io.IOException)30 URL (java.net.URL)24 InputStreamReader (java.io.InputStreamReader)23 FileInputStream (java.io.FileInputStream)15 URLConnection (java.net.URLConnection)14 MissingResourceException (java.util.MissingResourceException)13 File (java.io.File)12 FileNotFoundException (java.io.FileNotFoundException)8 Locale (java.util.Locale)7 ByteArrayInputStream (java.io.ByteArrayInputStream)5 HashMap (java.util.HashMap)5 MalformedURLException (java.net.MalformedURLException)4 Bundle (org.osgi.framework.Bundle)4 ArrayList (java.util.ArrayList)3 Properties (java.util.Properties)3 TemplatePersistenceData (org.eclipse.jface.text.templates.persistence.TemplatePersistenceData)3 TemplateReaderWriter (org.eclipse.jface.text.templates.persistence.TemplateReaderWriter)3