Example usage for org.apache.commons.collections ExtendedProperties entrySet

List of usage examples for org.apache.commons.collections ExtendedProperties entrySet

Introduction

In this page you can find the example usage for org.apache.commons.collections ExtendedProperties entrySet.

Prototype

public Set<Map.Entry<K, V>> entrySet() 

Source Link

Document

Returns a Set view of the mappings contained in this map.

Usage

From source file:org.opencms.setup.TestCmsSetupBean.java

/**
 * Tests the method saveProperties.<p>
 * //from w  w w  .j  a v a  2 s .  c om
 * @throws IOException if something goes wrong
 */
public void testSaveProperties() throws IOException {

    CmsSetupBean bean = new CmsSetupBean();
    bean.init("", null, null);

    String base = getTestDataPath(File.separator + "WEB-INF" + File.separator + "base");
    String inputFile = base + CmsSystemInfo.FILE_PROPERTIES;
    String outputFile = base + "output.properties";

    System.out.println("Reading properties from " + inputFile);
    ExtendedProperties oldProperties = bean.loadProperties(inputFile);

    System.out.println("Writing properties to " + outputFile);
    bean.copyFile(inputFile, outputFile);
    bean.saveProperties(oldProperties, outputFile, false);

    System.out.println("Checking properties from " + outputFile);
    ExtendedProperties newProperties = bean.loadProperties(outputFile);

    Iterator it = oldProperties.entrySet().iterator();
    while (it.hasNext()) {
        Map.Entry e = (Map.Entry) it.next();
        String key = (String) e.getKey();
        Object obj = e.getValue();

        String oldValue = "", newValue = "";
        if (obj instanceof Vector) {
            StringBuffer buf;

            buf = new StringBuffer();
            for (Iterator j = ((Vector) obj).iterator(); j.hasNext();) {
                buf.append("[" + (String) j.next() + "]");
            }
            oldValue = buf.toString();

            buf = new StringBuffer();
            for (Iterator j = ((Vector) newProperties.get(key)).iterator(); j.hasNext();) {
                buf.append("[" + (String) j.next() + "]");
            }
            newValue = buf.toString();

        } else {
            oldValue = (String) obj;
            newValue = (String) newProperties.get(key);
        }
        System.out.println(key);
        System.out.println(oldValue);
        System.out.println(newValue);
        System.out.println("---");
        assertEquals(oldValue, newValue);
    }

    // clean up - remove generated file
    File output = new File(outputFile);
    output.delete();
}

From source file:org.opencms.util.CmsPropertyUtils.java

/**
 * Unescapes the given properties, unescaping "," and "=" entries.<p>
 * /*from   www  . jav  a 2s . com*/
 * @param properties the properties to adjust
 */
public static void unescapeProperties(ExtendedProperties properties) {

    for (Iterator i = properties.entrySet().iterator(); i.hasNext();) {
        Map.Entry entry = (Map.Entry) i.next();
        String key = (String) entry.getKey();
        Object obj = entry.getValue();
        String[] value = {};

        if (obj instanceof Vector) {
            value = (String[]) ((Vector) obj).toArray(value);
        } else {
            String[] v = { (String) obj };
            value = v;
        }

        for (int j = 0; j < value.length; j++) {
            value[j] = CmsStringUtil.substitute(value[j], "\\,", ",");
            value[j] = CmsStringUtil.substitute(value[j], "\\=", "=");
        }

        if (value.length > 1) {
            properties.put(key, new Vector(Arrays.asList(value)));
        } else {
            properties.put(key, value[0]);
        }
    }
}