List of usage examples for java.util PropertyResourceBundle PropertyResourceBundle
@SuppressWarnings({ "unchecked", "rawtypes" }) public PropertyResourceBundle(Reader reader) throws IOException
From source file:fr.zcraft.zlib.components.i18n.translators.properties.ZLibResourceBundleControl.java
/** * <p>Loads the bundles from the file system instead of the JAR file, to allow modifications by * the end user.</p>//from ww w .j a va2 s . co m * * <hr /> * * <p> {@inheritDoc} */ @Override public ResourceBundle newBundle(String baseName, Locale locale, String format, ClassLoader loader, boolean reload) throws IllegalAccessException, InstantiationException, IOException { switch (format) { case "java.properties": ResourceBundle bundle = null; InputStream stream = null; try { stream = new FileInputStream(bundleFile); bundle = new PropertyResourceBundle(stream); } finally { if (stream != null) stream.close(); } return bundle; default: return super.newBundle(baseName, locale, format, loader, reload); } }
From source file:org.nuxeo.ecm.admin.runtime.RuntimeInstrospection.java
protected static SimplifiedBundleInfo getBundleSimplifiedInfo(Bundle bundle) { if (!(bundle instanceof BundleImpl)) { return null; }/*from www. jav a 2 s . c o m*/ BundleImpl nxBundle = (BundleImpl) bundle; BundleFile file = nxBundle.getBundleFile(); File jarFile = null; if (file instanceof JarBundleFile) { JarBundleFile jar = (JarBundleFile) file; jarFile = jar.getFile(); } if (jarFile == null || jarFile.isDirectory()) { return null; } SimplifiedBundleInfo result = null; try (ZipFile zFile = new ZipFile(jarFile)) { // Look for a pom.properties to extract its Maven version Enumeration<ZipEntry> entries = (Enumeration<ZipEntry>) zFile.entries(); while (entries.hasMoreElements()) { ZipEntry entry = entries.nextElement(); if (entry.getName().endsWith("pom.properties")) { try (InputStream pomStream = zFile.getInputStream(entry)) { PropertyResourceBundle prb = new PropertyResourceBundle(pomStream); String version = prb.getString("version"); result = new SimplifiedBundleInfo(bundle.getSymbolicName(), version); } break; } } } catch (IOException e) { log.debug(e.getMessage(), e); } if (result == null) { // Fall back on the filename to extract a version try { Version version = new Version(jarFile.getName()); result = new SimplifiedBundleInfo(bundle.getSymbolicName(), version.toString()); } catch (NumberFormatException e) { log.debug(e.getMessage()); } } if (result == null) { // Fall back on the MANIFEST Bundle-Version try { org.osgi.framework.Version version = bundle.getVersion(); result = new SimplifiedBundleInfo(bundle.getSymbolicName(), version.toString()); } catch (RuntimeException e) { log.debug(e.getMessage()); result = new SimplifiedBundleInfo(bundle.getSymbolicName(), "unknown"); } } return result; }
From source file:org.nuxeo.connect.NuxeoConnectClient.java
public static String getBuildVersion() { if (buildVersion == null) { try {//from w w w. ja v a 2 s.c om InputStream pomStream = NuxeoConnectClient.class.getClassLoader().getResourceAsStream( "META-INF/maven/org.nuxeo.connect/nuxeo-connect-client/pom.properties"); if (pomStream == null) { buildVersion = "Unknown (no pom)"; } else { PropertyResourceBundle prb = new PropertyResourceBundle(pomStream); buildVersion = prb.getString("version"); if (buildVersion == null) { buildVersion = "Unknown (not found)"; } } } catch (Throwable t) { log.error("Unable to find Nuxeo Client Build Version", t); buildVersion = "Unknown (error)"; } } return buildVersion; }
From source file:com.chilmers.configbootstrapper.ConfigHelper.java
public PropertyResourceBundle getApplicationConfiguration(String applicationConfigLocation) { InputStream is = null;/* www .j a v a2 s . co m*/ try { if (applicationConfigLocation.startsWith("classpath:")) { applicationConfigLocation = applicationConfigLocation.replaceFirst("classpath:", ""); is = Thread.currentThread().getContextClassLoader().getResourceAsStream(applicationConfigLocation); } else if (applicationConfigLocation.startsWith("file:")) { applicationConfigLocation = applicationConfigLocation.replaceFirst("file:", ""); is = new FileInputStream(applicationConfigLocation); } else { logToSystemOut("The application configuration location must start with file: or classpath:"); } return new PropertyResourceBundle(is); } catch (Exception e) { logToSystemOut("There was a problem reading the application configuration at location: " + applicationConfigLocation + "\n" + "Exception:" + e.getClass().toString() + "\n" + "Message:" + e.getMessage()); } finally { try { is.close(); } catch (Exception e) { logToSystemOut("WARNING! Exception while trying to close configuration file.\n" + "Exception:" + e.getClass().toString() + "\n" + "Message:" + e.getMessage()); } } return null; }
From source file:iqq.app.core.service.impl.I18nServiceImpl.java
private ResourceBundle getResourceBundle(Locale locale) { BufferedInputStream bis = null; try {//from w w w . ja v a2 s .c o m // ??_?_?.properties String flag = locale.getLanguage() + "_" + locale.getCountry(); String filename = I18N_DIR + I18N_BUNDLE + "_" + flag + ".properties"; ResourceBundle resourceBundle = resourceBundleMap.get(flag); // ??? if (resourceBundle != null) return resourceBundle; if (!new File(filename).exists()) { filename = I18N_DIR + I18N_BUNDLE + ".properties"; } LOG.debug(filename); bis = new BufferedInputStream(new FileInputStream(filename)); resourceBundle = new PropertyResourceBundle(bis); resourceBundleMap.put(flag, resourceBundle); return resourceBundle; } catch (FileNotFoundException e) { LOG.error("?", e); } catch (IOException e) { LOG.error("??", e); } finally { if (bis != null) try { bis.close(); } catch (IOException e) { e.printStackTrace(); } } return null; }
From source file:dk.statsbiblioteket.util.i18n.BundleCache.java
/** * Create a bundle from the resource specified by {@code localeBundleName} * and cache it. Return a reference to the newly created bundle. * * @param localeBundleName the full resource name as returned by * {@link #getLocaleBundleName} * @return the newly created and cached bundle */// w w w . j ava2 s . co m private ResourceBundle createBundle(String localeBundleName) { if (log.isDebugEnabled()) { log.debug("Loading '" + localeBundleName + "'"); } InputStream resourceStream = ClassLoader.getSystemResourceAsStream(localeBundleName); if (resourceStream == null) { throw new MissingResourceException("No such resource '" + localeBundleName + "'", localeBundleName, ""); } try { // The Java 1.6 way is much nicer: //ResourceBundle bundle = //new PropertyResourceBundle(new InputStreamReader(resourceStream)); ResourceBundle bundle = new PropertyResourceBundle(new EscapeUTF8Stream(resourceStream)); cache.put(localeBundleName, bundle); return bundle; } catch (IOException e) { throw new MissingResourceException("Error reading resource '" + localeBundleName + "'", localeBundleName, ""); } }
From source file:org.mybatisorm.EntityManager.java
protected void initDao() throws Exception { sqlSession = getSqlSession();/*from ww w . j a v a 2 s . c o m*/ configuration = sqlSession.getConfiguration(); sqlSourceBuilder = new SqlSourceBuilder(configuration); sqlSourceClassMap = new HashMap<String, Class<?>>(); ResourceBundle bundle = new PropertyResourceBundle( this.getClass().getClassLoader().getResourceAsStream("sql-sources.properties")); // ClassLoader.getSystemResourceAsStream("SqlSources.properties")); for (String source : bundle.getString(sourceType + ".sqlsources").split(",")) { sqlSourceClassMap.put(source, Class.forName(bundle.getString(sourceType + "." + source))); } valueGenerator = (ValueGeneratorImpl) getSourceTypeClass(SOURCE_GENERATOR).newInstance(); valueGenerator.setConfiguration(configuration); }
From source file:org.openmrs.web.filter.util.CustomResourceLoader.java
/** * This method is intended to load resource bundle from the file system by specified messages * properties file path and for specified locale * * @param path location of the resource on the file system * @param basename the name prefix for resource file * @param locale the location parameter// w ww . j av a 2 s .co m * @return resource bundle object if success, otherwise null. Error message is passed out * through <code>errorMsg</code> property */ protected ResourceBundle getFileSystemResource(String path, String basename, Locale locale) { File resourceFile = new File(path); try { FileInputStream fileInputStream = new FileInputStream(resourceFile); return new PropertyResourceBundle(new InputStreamReader(fileInputStream, "UTF-8")); } catch (Exception e) { log.warn("Unable to load bundle by path " + path + ", because of ", e); } return null; }
From source file:de.Keyle.MyPet.util.locale.Locales.java
public void loadLocale(String localeString) { ResourceBundle newLocale = null; if (jarFile != null) { try {// w ww.j a va 2 s .co m JarEntry jarEntry = jarFile.getJarEntry("locale/MyPet_" + localeString + ".properties"); if (jarEntry != null) { java.util.ResourceBundle defaultBundle = new PropertyResourceBundle( new InputStreamReader(jarFile.getInputStream(jarEntry), "UTF-8")); newLocale = new ResourceBundle(defaultBundle); } else { throw new IOException(); } } catch (UnsupportedEncodingException e) { e.printStackTrace(); DebugLogger.printThrowable(e); } catch (IOException ignored) { } } if (newLocale == null) { newLocale = new ResourceBundle(); } File localeFile = new File(MyPetPlugin.getPlugin().getDataFolder() + File.separator + "locale" + File.separator + "MyPet_" + localeString + ".properties"); if (localeFile.exists()) { try { java.util.ResourceBundle optionalBundle = new PropertyResourceBundle( new InputStreamReader(new FileInputStream(localeFile), "UTF-8")); newLocale.addExtensionBundle(optionalBundle); } catch (UnsupportedEncodingException e) { e.printStackTrace(); DebugLogger.printThrowable(e); } catch (IOException e) { e.printStackTrace(); DebugLogger.printThrowable(e); } } locales.put(localeString, newLocale); }
From source file:org.bizbundles.reverse.ResourcesToExcel.java
/** * This method will reverse resource bundle files into an excel spreadsheeet. * the spreadsheet will be created with the baseName parameter and the curnet date and time appended to it in 24 hour format. * There will be n number of tabs, one for each resource(property) file found. * * @param pathToResources Directory where all the resources files are located * @param baseName The basename for the resource bundles *//*from w w w . j ava2 s . c o m*/ public void generate(final File pathToResources, final String baseName) { assert pathToResources != null : "null file path entered"; assert pathToResources.exists() : "file does not exists."; assert pathToResources.isDirectory() : "File is not a directory"; assert pathToResources.canRead(); assert baseName != null || baseName.length() > 0 : "The base name cannot be empty."; File files[] = pathToResources.listFiles(new FilenameFilter() { public boolean accept(File file, String string) { return string.endsWith("properties") && string.startsWith(baseName); } }); assert null != files && files.length > 0 : "There were no resources found with the given baseName " + baseName; WritableWorkbook workbook = null; try { WritableCellFormat wrapFormat = new WritableCellFormat(); wrapFormat.setWrap(true); CellView cellView = new CellView(); cellView.setFormat(wrapFormat); WorkbookSettings settings = new WorkbookSettings(); settings.setGCDisabled(true); workbook = Workbook.createWorkbook(new File( pathToResources + File.separator + baseName + FileNameHelper.getDateTimeString() + ".xls"), settings); int col1MaxSize = 10; int col2MaxSize = 10; for (File file : files) { if (file.isFile()) { String filename = file.getName(); if (logger.isDebugEnabled()) { logger.debug("Procesing File " + file.getAbsoluteFile()); } int i = 0; WritableFont timesboldfont = new WritableFont(WritableFont.TIMES, 12, WritableFont.BOLD, true); WritableCellFormat timesBoldFormat = new WritableCellFormat(timesboldfont); WritableSheet sheet = workbook.createSheet(filename, i); sheet.getColumnView(1).setSize(60); sheet.addCell(new Label(0, 0, KEY, timesBoldFormat)); sheet.addCell(new Label(1, 0, VALUE, timesBoldFormat)); sheet.addCell(new Label(2, 0, ACTIVE, timesBoldFormat)); int row = 1; ResourceBundle bundle = new PropertyResourceBundle(new FileInputStream(file)); Enumeration enums = bundle.getKeys(); while (enums.hasMoreElements()) { String key = (String) enums.nextElement(); String val = bundle.getString(key); col1MaxSize = key.length() > col1MaxSize ? key.length() : col1MaxSize; col2MaxSize = val.length() > col1MaxSize ? val.length() : col1MaxSize; sheet.addCell(new Label(0, row, key)); sheet.addCell(new Label(1, row, val, wrapFormat)); sheet.addCell(new Label(2, row++, ACTIVE_INDCATOR)); } sheet.setColumnView(1, col2MaxSize + 4); sheet.setColumnView(0, col1MaxSize + 4); } } workbook.write(); } catch (Exception e) { e.printStackTrace(); } finally { if (null != workbook) { try { workbook.close(); } catch (Exception e) { e.printStackTrace(); } } } }