Example usage for org.apache.commons.configuration PropertiesConfiguration setEncoding

List of usage examples for org.apache.commons.configuration PropertiesConfiguration setEncoding

Introduction

In this page you can find the example usage for org.apache.commons.configuration PropertiesConfiguration setEncoding.

Prototype

public void setEncoding(String encoding) 

Source Link

Usage

From source file:net.sf.zekr.common.util.ConfigUtils.java

/**
 * Loads a configuration properties file (configStream) and close it.
 * /*  w  ww .ja v  a  2  s .c  o m*/
 * @param configStream
 * @param basePath
 * @param encoding
 * @return
 * @throws ConfigurationException
 * @throws IOException
 */
public static PropertiesConfiguration loadConfig(InputStream configStream, String basePath, String encoding)
        throws ConfigurationException, IOException {
    PropertiesConfiguration pc = new PropertiesConfiguration();
    pc.setThrowExceptionOnMissing(false); // this is the default behavior. just for MOHKAM KARI!
    pc.setEncoding("UTF-8");
    if (basePath != null) {
        pc.setBasePath(basePath);
    }
    pc.load(configStream);
    configStream.close();
    return pc;
}

From source file:com.ariht.maven.plugins.config.ConfigProcessorMojo.java

/**
 * Filter files contain the properties we wish to substitute in templates.
 *
 * Uses Apache Commons Configuration to load filters.
 *//* ww w .  j  a va2s.  co  m*/
private Properties readFilterIntoProperties(final FileInfo filter) throws ConfigurationException {
    final PropertiesConfiguration config = new PropertiesConfiguration(filter.getFile());
    config.setEncoding(encoding);
    // Add one more property:   filter.source=/relative/sub/dir/filenameNoExtension
    final String filterSource = filter.getRelativeSubDirectory() + filter.getNameWithoutExtension();
    config.setProperty("filter.source", FilenameUtils.separatorsToUnix(filterSource));
    return ConfigurationConverter.getProperties(config);
}

From source file:com.ariht.maven.plugins.config.generator.ConfigGeneratorImpl.java

/**
 * Filter io contain the properties we wish to substitute in templates.
 *
 * Uses Apache Commons Configuration to load filters.
 *//*from w w w .  j  av  a2  s. c om*/
private Properties readFilterIntoProperties(final FileInfo filter) throws ConfigurationException, IOException {
    final CompositeConfiguration composite = new CompositeConfiguration();
    final List<File> files = filter.getFiles();
    for (final File file : files) {
        final PropertiesConfiguration config = new PropertiesConfiguration(file);
        config.setEncoding(configGeneratorParameters.getEncoding());
        composite.addConfiguration(config);
    }
    if (StringUtils.isNotBlank(configGeneratorParameters.getFilterSourcePropertyName())) {
        composite.setProperty(configGeneratorParameters.getFilterSourcePropertyName(), filter.getAllSources());
    }
    return ConfigurationConverter.getProperties(composite);
}

From source file:com.qmetry.qaf.automation.util.PropertyUtil.java

private boolean loadFile(File file) {
    try {//w w w. j  a  v a 2  s  .com
        if (file.getName().endsWith("xml") || file.getName().contains(".xml.")) {
            super.load(new FileInputStream(file));
            XMLConfiguration xmlConfiguration = new XMLConfiguration(file);
            copy(xmlConfiguration);
            xmlConfiguration.clear();
        } else {
            PropertiesConfiguration propertiesConfiguration = new PropertiesConfiguration();
            propertiesConfiguration
                    .setEncoding(getString(ApplicationProperties.LOCALE_CHAR_ENCODING.key, "UTF-8"));
            propertiesConfiguration.load(new FileInputStream(file));
            copy(propertiesConfiguration);
            propertiesConfiguration.clear();
            // super.load(new FileInputStream(file));
        }
        return true;
    } catch (ConfigurationException e) {
        logger.error(e.getMessage());
    } catch (FileNotFoundException e) {
        logger.error(e.getMessage());
    }

    return false;
}

From source file:cz.cas.lib.proarc.common.config.AppConfiguration.java

private void buildConfiguration(CompositeConfiguration cc, File cfgFile) {
    try {/* w w  w  . ja v  a 2s  . co m*/
        // envConfig contains interpolated properties
        PropertiesConfiguration envConfig = new PropertiesConfiguration();
        envConfig.addProperty(PROPERTY_APP_HOME, configHome.getPath());
        cc.addConfiguration(envConfig);
        // external configuration editable by users; UTF-8 expected
        PropertiesConfiguration external = new PropertiesConfiguration();
        external.setEncoding("UTF-8");
        FileChangedReloadingStrategy reloading = new FileChangedReloadingStrategy();
        external.setReloadingStrategy(reloading);
        external.setFile(cfgFile);
        cc.addConfiguration(external);
        try {
            // bundled default configurations
            Enumeration<URL> resources = AppConfiguration.class.getClassLoader()
                    .getResources(DEFAULT_PROPERTIES_RESOURCE);
            for (URL resource; resources.hasMoreElements();) {
                resource = resources.nextElement();
                LOG.log(Level.FINE, "classpath config: {0}", resource);
                cc.addConfiguration(new PropertiesConfiguration(resource));
            }
        } catch (IOException ex) {
            LOG.log(Level.SEVERE, null, ex);
        }
    } catch (ConfigurationException ex) {
        LOG.log(Level.SEVERE, null, ex);
    }
}

From source file:ninja.utils.SwissKnife.java

/**
 * This is important: We load stuff as UTF-8.
 *
 * We are using in the default Apache Commons loading mechanism.
 *
 * With two little tweaks: 1. We don't accept any delimimter by default 2.
 * We are reading in UTF-8/* w  w  w . j  a  v a2  s .c  o m*/
 *
 * More about that:
 * http://commons.apache.org/configuration/userguide/howto_filebased
 * .html#Loading
 *
 * From the docs: - If the combination from base path and file name is a
 * full URL that points to an existing file, this URL will be used to load
 * the file. - If the combination from base path and file name is an
 * absolute file name and this file exists, it will be loaded. - If the
 * combination from base path and file name is a relative file path that
 * points to an existing file, this file will be loaded. - If a file with
 * the specified name exists in the user's home directory, this file will be
 * loaded. - Otherwise the file name is interpreted as a resource name, and
 * it is checked whether the data file can be loaded from the classpath.
 *
 * @param fileOrUrlOrClasspathUrl Location of the file. Can be on file
 * system, or on the classpath. Will both work.
 * @return A PropertiesConfiguration or null if there were problems getting
 * it.
 */
public static PropertiesConfiguration loadConfigurationInUtf8(String fileOrUrlOrClasspathUrl) {

    PropertiesConfiguration propertiesConfiguration = new PropertiesConfiguration();
    propertiesConfiguration.setEncoding(NinjaConstant.UTF_8);
    propertiesConfiguration.setDelimiterParsingDisabled(true);
    propertiesConfiguration.setFileName(fileOrUrlOrClasspathUrl);
    propertiesConfiguration.getLayout().setSingleLine(NinjaConstant.applicationSecret, true);

    try {

        propertiesConfiguration.load(fileOrUrlOrClasspathUrl);

    } catch (ConfigurationException e) {

        logger.info("Could not load file " + fileOrUrlOrClasspathUrl
                + " (not a bad thing necessarily, but I am returing null)");

        return null;
    }

    return propertiesConfiguration;
}

From source file:org.betaconceptframework.astroboa.console.jsf.login.LoginBean.java

public List<SelectItem> getAvailableServers() {

    List<SelectItem> availableAstroboaServers = new ArrayList<SelectItem>();

    try {/*from w w  w . ja  v  a 2s.  c o m*/

        PropertiesConfiguration consoleConfiguration = new PropertiesConfiguration(
                "astroboa-console.properties");
        consoleConfiguration.setEncoding("UTF-8");

        @SuppressWarnings("unchecked")
        List<Object> astroboaServers = consoleConfiguration.getList(ASTROBOA_SERVER_KEY);

        if (CollectionUtils.isNotEmpty(astroboaServers)) {

            for (Object astroboaServer : astroboaServers) {
                //Get server host or ip
                String astroboaHostOrIp = consoleConfiguration.getString((String) astroboaServer + ".host");

                if (StringUtils.isBlank(astroboaHostOrIp)) {
                    astroboaHostOrIp = AstroboaClient.INTERNAL_CONNECTION;
                }

                //Get port
                String astroboaHostPort = consoleConfiguration.getString((String) astroboaServer + ".port");

                if (StringUtils.isBlank(astroboaHostPort)) {
                    astroboaHostPort = "1099";
                }

                //Get localized label
                String localizedLabel = consoleConfiguration
                        .getString((String) astroboaServer + "." + localeSelector.getLocaleString());

                if (StringUtils.isBlank(localizedLabel)) {
                    localizedLabel = (String) astroboaServer;
                }

                availableAstroboaServers
                        .add(new SelectItem(astroboaHostOrIp + ":" + astroboaHostPort, localizedLabel));
            }

            if (availableAstroboaServers.size() > 1) {
                SelectItemComparator selectItemComparator = new SelectItemComparator();
                selectItemComparator.setLocale(localeSelector.getLocaleString());
                Collections.sort(availableAstroboaServers, selectItemComparator);
            }
        }

    } catch (ConfigurationException e) {
        logger.error("", e);
    }

    return availableAstroboaServers;
}

From source file:org.gss_project.gss.server.configuration.GSSConfigurationFactory.java

/**
 * @return the configuration object// w  w  w  .  j ava 2  s . com
 */
public synchronized static DataConfiguration getConfiguration() {
    try {
        if (configuration == null) {
            PropertiesConfiguration gssConfig = (PropertiesConfiguration) getClass(
                    PropertiesConfiguration.class.getCanonicalName()).newInstance();
            gssConfig.setBasePath("");
            gssConfig.setFileName(configFilename);
            gssConfig.setEncoding("ISO-8859-7");
            // Set automatic reloading.
            gssConfig.setReloadingStrategy(new VfsFileChangedReloadingStrategy());
            gssConfig.load();
            // Decorator.
            configuration = new DataConfiguration(gssConfig);
        }
        return configuration;
    } catch (ClassNotFoundException e) {
        // Use empty configuration, so we get no NPE but default values
        configuration = new DataConfiguration(new BaseConfiguration());
        logger.error("Error in ExternalAPI initialization! GSS is running with default values!", e);
        return configuration;
    } catch (InstantiationException e) {
        // Use empty configuration, so we get no NPE but default values
        configuration = new DataConfiguration(new BaseConfiguration());
        logger.error("Error in ExternalAPI initialization! GSS is running with default values!", e);
        return configuration;
    } catch (IllegalAccessException e) {
        // Use empty configuration, so we get no NPE but default values
        configuration = new DataConfiguration(new BaseConfiguration());
        logger.error("Error in ExternalAPI initialization! GSS is running with default values!", e);
        return configuration;
    } catch (ConfigurationException e) {
        // Use empty configuration, so we get no NPE but default values
        configuration = new DataConfiguration(new BaseConfiguration());
        logger.error("Error in ExternalAPI initialization! GSS is running with default values!", e);
        return configuration;
    }
}

From source file:org.jboss.forge.addon.configuration.ConfigurationFactoryImpl.java

private Configuration getConfiguration(File file) {
    try {//from  w w  w  .j a va  2s  . co  m
        PropertiesConfiguration commonsConfig = new PropertiesConfiguration(file);
        commonsConfig.setEncoding("UTF-8");
        commonsConfig.setReloadingStrategy(new FileChangedReloadingStrategy());
        commonsConfig.setAutoSave(true);
        return new ConfigurationAdapter(commonsConfig);
    } catch (org.apache.commons.configuration.ConfigurationException e) {
        throw new ConfigurationException("Error while creating configuration from " + file, e);
    }
}

From source file:org.kontalk.util.Tr.java

public static void init() {
    // get language
    String lang = Locale.getDefault().getLanguage();
    if (lang.equals(DEFAULT_LANG)) {
        return;//from   w  ww. j av  a  2 s . c om
    }

    LOGGER.info("Setting language: " + lang);

    // load string keys file
    String path = Kontalk.RES_PATH + I18N_DIR + STRING_FILE + PROP_EXT;
    PropertiesConfiguration stringKeys;
    try {
        stringKeys = new PropertiesConfiguration(ClassLoader.getSystemResource(path));
    } catch (ConfigurationException ex) {
        LOGGER.log(Level.WARNING, "can't load string key file", ex);
        return;
    }

    // load translation file
    path = Kontalk.RES_PATH + I18N_DIR + STRING_FILE + "_" + lang + PROP_EXT;
    URL url = ClassLoader.getSystemResource(path);
    if (url == null) {
        LOGGER.info("can't find translation file: " + path);
        return;
    }
    PropertiesConfiguration tr = new PropertiesConfiguration();
    tr.setEncoding("UTF-8");
    try {
        tr.load(url);
    } catch (ConfigurationException ex) {
        LOGGER.log(Level.WARNING, "can't load translation file", ex);
        return;
    }

    TR_MAP = new HashMap<>();
    Iterator<String> it = tr.getKeys();
    while (it.hasNext()) {
        String k = it.next();
        if (!stringKeys.containsKey(k)) {
            LOGGER.warning("key in translation but not in key file: " + k);
            continue;
        }
        TR_MAP.put(stringKeys.getString(k), tr.getString(k));
    }
}