Search in sources :

Example 41 with PropertyResourceBundle

use of java.util.PropertyResourceBundle in project jdk8u_jdk by JetBrains.

the class I18NImpl method getString.

/**
     * Returns the message string with the specified key from the
     * "properties" file in the package containing the class with
     * the specified name.
     */
protected static final String getString(String className, String resource_name, String key) {
    PropertyResourceBundle bundle = null;
    try {
        InputStream stream = Class.forName(className).getResourceAsStream(resource_name);
        bundle = new PropertyResourceBundle(stream);
    } catch (Throwable e) {
        // Chain the exception.
        throw new RuntimeException(e);
    }
    return (String) bundle.handleGetObject(key);
}
Also used : InputStream(java.io.InputStream) PropertyResourceBundle(java.util.PropertyResourceBundle)

Example 42 with PropertyResourceBundle

use of java.util.PropertyResourceBundle in project pentaho-kettle by pentaho.

the class GlobalMessages method getBundle.

public static ResourceBundle getBundle(Locale locale, String packageName, Class<?> resourceClass) throws MissingResourceException {
    String filename = buildHashKey(locale, packageName);
    filename = "/" + filename.replace('.', '/') + ".properties";
    InputStream inputStream = null;
    try {
        ResourceBundle bundle = locales.get(filename);
        if (bundle == null) {
            inputStream = resourceClass.getResourceAsStream(filename);
            if (inputStream == null) {
                // Retry with the system class loader, just in case we are dealing with a messy plug-in.
                // 
                inputStream = ClassLoader.getSystemResourceAsStream(filename);
            }
            // 
            if (inputStream != null) {
                bundle = new PropertyResourceBundle(new InputStreamReader(inputStream, "UTF-8"));
                locales.put(filename, bundle);
            } else {
                throw new MissingResourceException("Unable to find properties file [" + filename + "]", locale.toString(), packageName);
            }
        }
        return bundle;
    } catch (IOException e) {
        throw new MissingResourceException("Unable to find properties file [" + filename + "] : " + e.toString(), locale.toString(), packageName);
    } finally {
        if (inputStream != null) {
            try {
                inputStream.close();
            } catch (Exception e) {
            // ignore this
            }
        }
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) MissingResourceException(java.util.MissingResourceException) PropertyResourceBundle(java.util.PropertyResourceBundle) ResourceBundle(java.util.ResourceBundle) IOException(java.io.IOException) KettleException(org.pentaho.di.core.exception.KettleException) MissingResourceException(java.util.MissingResourceException) IOException(java.io.IOException) PropertyResourceBundle(java.util.PropertyResourceBundle)

Example 43 with PropertyResourceBundle

use of java.util.PropertyResourceBundle in project eclipse.platform.text by eclipse.

the class ContributionTemplateStore method readIncludedTemplates.

private void readIncludedTemplates(Collection<TemplatePersistenceData> templates, IConfigurationElement element) throws IOException {
    String file = element.getAttribute(FILE);
    if (file != null) {
        Bundle plugin = Platform.getBundle(element.getContributor().getName());
        URL url = FileLocator.find(plugin, Path.fromOSString(file), null);
        if (url != null) {
            ResourceBundle bundle = null;
            String translations = element.getAttribute(TRANSLATIONS);
            if (translations != null) {
                URL bundleURL = FileLocator.find(plugin, Path.fromOSString(translations), null);
                if (bundleURL != null) {
                    try (InputStream bundleStream = bundleURL.openStream()) {
                        bundle = new PropertyResourceBundle(bundleStream);
                    }
                }
            }
            try (InputStream stream = new BufferedInputStream(url.openStream())) {
                TemplateReaderWriter reader = new TemplateReaderWriter();
                TemplatePersistenceData[] datas = reader.read(stream, bundle);
                for (int i = 0; i < datas.length; i++) {
                    TemplatePersistenceData data = datas[i];
                    if (data.isCustom()) {
                        if (data.getId() == null)
                            EditorsPlugin.logErrorMessage(NLSUtility.format(ContributionTemplateMessages.ContributionTemplateStore_ignore_no_id, data.getTemplate().getName()));
                        else
                            EditorsPlugin.logErrorMessage(NLSUtility.format(ContributionTemplateMessages.ContributionTemplateStore_ignore_deleted, data.getTemplate().getName()));
                    } else if (validateTemplate(data.getTemplate())) {
                        templates.add(data);
                    }
                }
            }
        }
    }
}
Also used : TemplatePersistenceData(org.eclipse.jface.text.templates.persistence.TemplatePersistenceData) BufferedInputStream(java.io.BufferedInputStream) PropertyResourceBundle(java.util.PropertyResourceBundle) ResourceBundle(java.util.ResourceBundle) Bundle(org.osgi.framework.Bundle) BufferedInputStream(java.io.BufferedInputStream) InputStream(java.io.InputStream) PropertyResourceBundle(java.util.PropertyResourceBundle) ResourceBundle(java.util.ResourceBundle) TemplateReaderWriter(org.eclipse.jface.text.templates.persistence.TemplateReaderWriter) URL(java.net.URL) PropertyResourceBundle(java.util.PropertyResourceBundle)

Example 44 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 45 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)

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