Search in sources :

Example 21 with PropertyResourceBundle

use of java.util.PropertyResourceBundle in project imageio-ext by geosolutions-it.

the class I18N method getString.

static String getString(String key) {
    PropertyResourceBundle bundle = null;
    try {
        InputStream stream = clazz.getResourceAsStream("properties");
        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 22 with PropertyResourceBundle

use of java.util.PropertyResourceBundle in project Nucleus by NucleusPowered.

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 23 with PropertyResourceBundle

use of java.util.PropertyResourceBundle in project Minigames by AddstarMC.

the class UTF8Control method newBundle.

@Override
public ResourceBundle newBundle(String baseName, Locale locale, String format, ClassLoader loader, boolean reload) throws 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, StandardCharsets.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 24 with PropertyResourceBundle

use of java.util.PropertyResourceBundle in project es6draft by anba.

the class PropertiesReaderControl method newBundle.

@Override
public ResourceBundle newBundle(String baseName, Locale locale, String format, ClassLoader loader, boolean reload) throws IOException {
    if ("java.properties".equals(format)) {
        String bundleName = toBundleName(baseName, locale);
        String resourceName = toResourceName(bundleName, "properties");
        InputStream stream = getInputStream(loader, resourceName, reload);
        if (stream == null) {
            return null;
        }
        try (Reader reader = new InputStreamReader(stream, charset)) {
            return new PropertyResourceBundle(reader);
        }
    }
    throw new IllegalArgumentException("unknown format: " + format);
}
Also used : InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) PropertyResourceBundle(java.util.PropertyResourceBundle)

Example 25 with PropertyResourceBundle

use of java.util.PropertyResourceBundle in project jmeter by apache.

the class ResourceKeyUsageTest method checkResourceReferences.

// Check that calls to getResString use a valid property key name
@Test
public void checkResourceReferences() throws Exception {
    String resourceName = "/org/apache/jmeter/resources/messages.properties";
    PropertyResourceBundle messagePRB = getRAS(resourceName);
    assertNotNull("Resource bundle " + resourceName + " was not found", resourceName);
    List<String> failures = new ArrayList<>();
    PackageTest.findFile(srcFiledir, null, new FilenameFilter() {

        @Override
        public boolean accept(File dir, String name) {
            final File file = new File(dir, name);
            // Look for calls to JMeterUtils.getResString()
            final Pattern pat = Pattern.compile(".*getResString\\(\"([^\"]+)\"\\).*");
            if (name.endsWith(".java")) {
                BufferedReader fileReader = null;
                try {
                    fileReader = new BufferedReader(new FileReader(file));
                    String s;
                    while ((s = fileReader.readLine()) != null) {
                        if (s.matches("\\s*//.*")) {
                            // leading comment
                            continue;
                        }
                        Matcher m = pat.matcher(s);
                        if (m.matches()) {
                            final String key = m.group(1);
                            // Resource keys cannot contain spaces, and are forced to lower case
                            // $NON-NLS-1$ // $NON-NLS-2$
                            String resKey = key.replace(' ', '_');
                            resKey = resKey.toLowerCase(java.util.Locale.ENGLISH);
                            if (!key.equals(resKey)) {
                                System.out.println(file + ": non-standard message key: '" + key + "'");
                            }
                            try {
                                messagePRB.getString(resKey);
                            } catch (MissingResourceException e) {
                                failures.add(file + ": missing message key: '" + key + "'");
                            }
                        }
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    JOrphanUtils.closeQuietly(fileReader);
                }
            }
            return file.isDirectory();
        }
    });
    if (failures.isEmpty()) {
        return;
    }
    fail(String.join("\n", failures));
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) MissingResourceException(java.util.MissingResourceException) ArrayList(java.util.ArrayList) IOException(java.io.IOException) PropertyResourceBundle(java.util.PropertyResourceBundle) FilenameFilter(java.io.FilenameFilter) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader) File(java.io.File) Test(org.junit.jupiter.api.Test)

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