Example usage for java.util ResourceBundle getKeys

List of usage examples for java.util ResourceBundle getKeys

Introduction

In this page you can find the example usage for java.util ResourceBundle getKeys.

Prototype

public abstract Enumeration<String> getKeys();

Source Link

Document

Returns an enumeration of the keys.

Usage

From source file:com.google.code.pentahoflashcharts.charts.PentahoOFC4JChartHelper.java

@SuppressWarnings("unchecked")
private static Map createChartFactoryMap() {
    Properties chartFactories = new Properties();
    // First, get known chart factories...
    try {//from   w ww.j  a v  a  2 s.  c o  m
        ResourceBundle pluginBundle = ResourceBundle.getBundle(PLUGIN_BUNDLE_NAME);
        if (pluginBundle != null) { // Copy the bundle here...
            Enumeration keyEnum = pluginBundle.getKeys();
            String bundleKey = null;
            while (keyEnum.hasMoreElements()) {
                bundleKey = (String) keyEnum.nextElement();
                chartFactories.put(bundleKey, pluginBundle.getString(bundleKey));
            }
        }
    } catch (Exception ex) {
        logger.warn(Messages.getString("PentahoOFC4JChartHelper.WARN_NO_CHART_FACTORY_PROPERTIES_BUNDLE")); //$NON-NLS-1$
    }
    // Get overrides...
    //
    // Note - If the override wants to remove an existing "known" plugin, 
    // simply adding an empty value will cause the "known" plugin to be removed.
    //
    if (PentahoSystem.getObjectFactory() == null
            || !PentahoSystem.getObjectFactory().objectDefined(ISolutionRepository.class.getSimpleName())) {
        // this is ok
        return chartFactories;
    }
    ISolutionRepository solutionRepository = PentahoSystem.get(ISolutionRepository.class,
            new StandaloneSession("system")); //$NON-NLS-1$
    try {
        if (solutionRepository.resourceExists(SOLUTION_PROPS)) {
            InputStream is = solutionRepository.getResourceInputStream(SOLUTION_PROPS, false);
            Properties overrideChartFactories = new Properties();
            overrideChartFactories.load(is);
            chartFactories.putAll(overrideChartFactories); // load over the top of the known properties
        }
    } catch (FileNotFoundException ignored) {
        logger.warn(Messages.getString("PentahoOFC4JChartHelper.WARN_NO_CHART_FACTORY_PROPERTIES")); //$NON-NLS-1$
    } catch (IOException ignored) {
        logger.warn(Messages.getString("PentahoOFC4JChartHelper.WARN_BAD_CHART_FACTORY_PROPERTIES"), ignored); //$NON-NLS-1$
    }

    return chartFactories;
}

From source file:net.openkoncept.vroom.VroomUtilities.java

public static JSONObject convertObjectToJSONObject(Object object) throws JSONException {
    JSONObject jo = new JSONObject();
    if (object instanceof Character || object instanceof String || object instanceof Boolean
            || object instanceof Short || object instanceof Integer || object instanceof Long
            || object instanceof Float || object instanceof Double || object instanceof java.util.Date
            || object instanceof java.math.BigDecimal || object instanceof java.math.BigInteger) {
        jo.put("value", getValueForJSONObject(object));
    } else if (object instanceof java.util.Map) {
        Map m = (Map) object;
        Iterator iter = m.keySet().iterator();
        while (iter.hasNext()) {
            String key = (String) iter.next();
            jo.put(key, getValueForJSONObject(m.get(key)));
        }/*  w w w. j  a  v  a2s.co m*/
    } else if (object instanceof Collection) {
        Collection c = (Collection) object;
        Iterator iter = c.iterator();
        JSONArray ja = new JSONArray();
        while (iter.hasNext()) {
            ja.put(getValueForJSONObject(iter.next()));
        }
        jo.put("array", ja);
    } else if (object != null && object.getClass().isArray()) {
        Object[] oa = (Object[]) object;
        JSONArray ja = new JSONArray();
        for (int i = 0; i < oa.length; i++) {
            ja.put(getValueForJSONObject(oa[i]));
        }
        jo.put("array", ja);
    } else if (object instanceof ResourceBundle) {
        ResourceBundle rb = (ResourceBundle) object;
        Enumeration e = rb.getKeys();
        while (e.hasMoreElements()) {
            String key = (String) e.nextElement();
            Object value = getValueForJSONObject(rb.getObject(key));
            jo.put(key, value);
        }
    } else if (object != null) {
        Class clazz = object.getClass();
        PropertyDescriptor[] pds = PropertyUtils.getPropertyDescriptors(object);
        for (int i = 0; i < pds.length; i++) {
            PropertyDescriptor pd = pds[i];
            String name = pd.getName();
            if (!"class".equals(name) && PropertyUtils.isReadable(object, pd.getName())) {
                try {
                    Object value = PropertyUtils.getProperty(object, name);
                    jo.put(name, getValueForJSONObject(value));
                } catch (Exception e) {
                    // Useless...
                }
            }
        }
    } else {
        jo.put("value", "");
    }
    return jo;
}

From source file:com.edgenius.core.util.WebUtil.java

/**
 * @param agent // www.j a  v  a 2s .co m
 * @return
 */
public static boolean isPublicSearchEngineRobot(String agent) {
    if (!agentListInit) {
        agentListInit = true;
        try {
            ResourceBundle ua = ResourceBundle.getBundle(USER_AGENET_BROWSER);
            Enumeration<String> em = ua.getKeys();
            while (em.hasMoreElements()) {
                String regex = ua.getString(em.nextElement());
                try {
                    userAgentPatternList.add(Pattern.compile(regex));
                } catch (Exception e) {
                    log.error("Unable compile user agent pattern: " + regex);
                }
            }
        } catch (Throwable e) {
            log.error("Unable load user agent properties, use default instead");
        }
    }
    if (userAgentPatternList.size() > 0) {
        // use User-Agent to detect if current request is from browser, search engine robot, web crawler etc.
        for (Pattern pattern : userAgentPatternList) {
            //so far, browser is small amount than robot, so for performance reason, use browser agent list
            //See our issue http://bug.edgenius.com/issues/34
            //and SUN Java bug: http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6337993
            try {
                if (pattern.matcher(agent).matches())
                    return false;
            } catch (StackOverflowError e) {
                AuditLogger.error("StackOverflow Error in WebUtil.isPublicSearchEngineRobot. Input[" + agent
                        + "] Pattern [" + pattern.pattern() + "]");
            } catch (Throwable e) {
                AuditLogger.error("Unexpected error in WebUtil.isPublicSearchEngineRobot. Input[" + agent
                        + "] Pattern [" + pattern.pattern() + "]", e);
            }
        }
    } else {
        //default very rough check
        String user = agent.toLowerCase();
        if (user.indexOf("crawl") != -1 || user.indexOf("spider") != -1 || user.indexOf("check") != -1
                || user.indexOf("bot") != -1) {
            return true;
        } else if (user.indexOf("mozilla") != -1
                //               ||user.indexOf("") != -1
                || user.indexOf("opera") != -1) {
            return false;
        }
    }
    return true;
}

From source file:org.pentaho.platform.plugin.action.openflashchart.factory.PentahoOFC4JChartHelper.java

@SuppressWarnings("unchecked")
private static Map createChartFactoryMap() {
    Properties chartFactories = new Properties();
    // First, get known chart factories...
    try {/*w  w w .  ja  v a  2  s .  c  o  m*/
        ResourceBundle pluginBundle = ResourceBundle.getBundle(PLUGIN_BUNDLE_NAME);
        if (pluginBundle != null) { // Copy the bundle here...
            Enumeration keyEnum = pluginBundle.getKeys();
            String bundleKey = null;
            while (keyEnum.hasMoreElements()) {
                bundleKey = (String) keyEnum.nextElement();
                chartFactories.put(bundleKey, pluginBundle.getString(bundleKey));
            }
        }
    } catch (Exception ex) {
        logger.warn(Messages.getInstance()
                .getString("PentahoOFC4JChartHelper.WARN_NO_CHART_FACTORY_PROPERTIES_BUNDLE")); //$NON-NLS-1$
    }
    // Get overrides...
    //
    // Note - If the override wants to remove an existing "known" plugin,
    // simply adding an empty value will cause the "known" plugin to be removed.
    //
    if (PentahoSystem.getApplicationContext() == null) {
        return chartFactories;
    }
    File f = new File(PentahoSystem.getApplicationContext().getSolutionPath(SOLUTION_PROPS));
    if (!f.exists()) {
        // this is ok
        return chartFactories;
    }
    InputStream is = null;
    try {
        is = new FileInputStream(f);
        Properties overrideChartFactories = new Properties();
        overrideChartFactories.load(is);
        chartFactories.putAll(overrideChartFactories); // load over the top of the known properties
    } catch (FileNotFoundException ignored) {
        logger.warn(
                Messages.getInstance().getString("PentahoOFC4JChartHelper.WARN_NO_CHART_FACTORY_PROPERTIES")); //$NON-NLS-1$
    } catch (IOException ignored) {
        logger.warn(
                Messages.getInstance().getString("PentahoOFC4JChartHelper.WARN_BAD_CHART_FACTORY_PROPERTIES"), //$NON-NLS-1$
                ignored);
    } finally {
        try {
            if (is != null) {
                is.close();
            }
        } catch (Exception e) {
            //ignore
        }
    }

    return chartFactories;
}

From source file:net.openkoncept.vroom.VroomUtilities.java

/**
 * <p>/*from   www  .  j a  va  2  s  . c  o  m*/
 * This useful method converts a map to JSONObject.
 * </p>
 *
 * @param bundle - ResourceBundle (preferrably with key/value pairs)
 * @return - JSON object.
 */
public static JSONObject bundleToJSONObject(ResourceBundle bundle) {
    JSONObject jo = new JSONObject();
    if (bundle != null) {
        Enumeration e = bundle.getKeys();
        while (e.hasMoreElements()) {
            String key = (String) e.nextElement();
            try {
                jo.append(key, bundle.getString(key));
            } catch (JSONException ex) {
                // Not required!
            }
        }
    }
    return jo;
}

From source file:org.vulpe.commons.util.VulpeEmailUtil.java

/**
 *
 * @param resource/* www  . j  ava2s  .c o  m*/
 * @return
 */
public static Properties convertResourceBundleToProperties(final ResourceBundle resource) {
    final Properties properties = new Properties();
    final Enumeration<String> keys = resource.getKeys();
    while (keys.hasMoreElements()) {
        final String key = keys.nextElement();
        properties.put(key, resource.getString(key));
    }
    return properties;
}

From source file:org.soitoolkit.commons.mule.util.MiscUtil.java

/**
 * Convert ResourceBundle into a Properties object.
 *
 * @param resource a resource bundle to convert.
 * @return Properties a properties version of the resource bundle.
 *///from  w w  w.j  a v a2  s  . co m
static Properties convertResourceBundleToProperties(ResourceBundle resource) {
    Properties properties = new Properties();

    Enumeration<String> keys = resource.getKeys();
    while (keys.hasMoreElements()) {
        String key = keys.nextElement();
        properties.put(key, resource.getString(key));
    }

    return properties;
}

From source file:com.teamsun.framework.util.ConvertUtil.java

/**
 * ???MAP./*from  w  w w .j  av a2  s . com*/
 * 
 * @param rb
 *            a given resource bundle
 * @return Map a populated map
 */
public static Map convertBundleToMap(ResourceBundle rb) {
    Map map = new HashMap();

    for (Enumeration keys = rb.getKeys(); keys.hasMoreElements();) {
        String key = (String) keys.nextElement();
        map.put(key, rb.getString(key));
    }

    return map;
}

From source file:com.teamsun.framework.util.ConvertUtil.java

/**
 * Method to convert a ResourceBundle to a Properties object.
 * /*from w  ww  .j  a  va2 s  .com*/
 * @param rb
 *            a given resource bundle
 * @return Properties a populated properties object
 */
public static Properties convertBundleToProperties(ResourceBundle rb) {
    Properties props = new Properties();

    for (Enumeration keys = rb.getKeys(); keys.hasMoreElements();) {
        String key = (String) keys.nextElement();
        props.put(key, rb.getString(key));
    }

    return props;
}

From source file:I18NUtil.java

/**
 * Get the messages for a locale./*from ww  w  . j  a v a  2 s  .c o  m*/
 * <p>
 * Will use cache where available otherwise will load into cache from bundles.
 * 
 * @param locale    the locale
 * @return          message map
 */
private static Map<String, String> getLocaleProperties(Locale locale) {
    Set<String> loadedBundles = null;
    Map<String, String> props = null;
    int loadedBundleCount = 0;
    try {
        readLock.lock();
        loadedBundles = loadedResourceBundles.get(locale);
        props = cachedMessages.get(locale);
        loadedBundleCount = resouceBundleBaseNames.size();
    } finally {
        readLock.unlock();
    }

    if (loadedBundles == null) {
        try {
            writeLock.lock();
            loadedBundles = new HashSet<String>();
            loadedResourceBundles.put(locale, loadedBundles);
        } finally {
            writeLock.unlock();
        }
    }

    if (props == null) {
        try {
            writeLock.lock();
            props = new HashMap<String, String>();
            cachedMessages.put(locale, props);
        } finally {
            writeLock.unlock();
        }
    }

    if (loadedBundles.size() != loadedBundleCount) {
        try {
            writeLock.lock();
            for (String resourceBundleBaseName : resouceBundleBaseNames) {
                if (loadedBundles.contains(resourceBundleBaseName) == false) {
                    ResourceBundle resourcebundle = ResourceBundle.getBundle(resourceBundleBaseName, locale);
                    Enumeration<String> enumKeys = resourcebundle.getKeys();
                    while (enumKeys.hasMoreElements() == true) {
                        String key = enumKeys.nextElement();
                        props.put(key, resourcebundle.getString(key));
                    }
                    loadedBundles.add(resourceBundleBaseName);
                }
            }
        } finally {
            writeLock.unlock();
        }
    }

    return props;
}