Example usage for java.util Properties propertyNames

List of usage examples for java.util Properties propertyNames

Introduction

In this page you can find the example usage for java.util Properties propertyNames.

Prototype

public Enumeration<?> propertyNames() 

Source Link

Document

Returns an enumeration of all the keys in this property list, including distinct keys in the default property list if a key of the same name has not already been found from the main properties list.

Usage

From source file:org.openlaszlo.sc.ScriptCompiler.java

/**
 * @return a cache key for the given properties
 *//*w  ww.j  a  v  a2  s  .  co m*/
static String sortedPropertiesList(Properties props) {
    TreeSet<String> sorted = new TreeSet<String>();
    for (java.util.Enumeration<?> e = props.propertyNames(); e.hasMoreElements();) {
        String key = (String) e.nextElement();

        String value = props.getProperty(key);
        StringBuilder buf = new StringBuilder();
        buf.append(key);
        buf.append(' ');
        buf.append(value);

        sorted.add(buf.toString());
    }
    StringBuilder buf = new StringBuilder();
    for (String str : sorted) {
        buf.append(str);
    }
    String propstring = buf.toString();
    return propstring;
}

From source file:org.esigate.DriverFactory.java

/**
 * Loads all instances according to the properties parameter.
 * //w  w  w .ja  va 2s  . co  m
 * @param props
 *            properties to use for configuration
 */
public static void configure(Properties props) {
    Properties defaultProperties = new Properties();

    HashMap<String, Properties> driversProps = new HashMap<String, Properties>();
    for (Enumeration<?> enumeration = props.propertyNames(); enumeration.hasMoreElements();) {
        String propertyName = (String) enumeration.nextElement();
        String value = props.getProperty(propertyName);
        int idx = propertyName.lastIndexOf('.');
        if (idx < 0) {
            defaultProperties.put(propertyName, value);
        } else {
            String prefix = propertyName.substring(0, idx);
            String name = propertyName.substring(idx + 1);
            Properties driverProperties = driversProps.get(prefix);
            if (driverProperties == null) {
                driverProperties = new Properties();
                driversProps.put(prefix, driverProperties);
            }
            driverProperties.put(name, value);
        }
    }

    // Merge with default properties
    Map<String, Driver> newInstances = new HashMap<String, Driver>();
    for (Entry<String, Properties> entry : driversProps.entrySet()) {
        String name = entry.getKey();
        Properties properties = new Properties();
        properties.putAll(defaultProperties);
        properties.putAll(entry.getValue());
        newInstances.put(name, createDriver(name, properties));
    }
    if (newInstances.get(DEFAULT_INSTANCE_NAME) == null
            && Parameters.REMOTE_URL_BASE.getValue(defaultProperties) != null) {

        newInstances.put(DEFAULT_INSTANCE_NAME, createDriver(DEFAULT_INSTANCE_NAME, defaultProperties));
    }

    instances = new IndexedInstances(newInstances);
}

From source file:net.sourceforge.jaulp.lang.PropertiesUtils.java

/**
 * Finds all keys with the same key prefixes from the given Properties and saves them to a Map
 * with the prefix as a key and holds a List with the whole keys the starts with the same key
 * prefix./* w  ww  . ja  va2  s  .  com*/
 * 
 * @param enProperties
 *            the en properties
 * @return the matched prefix lists
 */
public static Map<String, List<String>> getMatchedPrefixLists(Properties enProperties) {
    Enumeration<?> e = enProperties.propertyNames();
    Map<String, List<String>> matchedPrefixes = new LinkedHashMap<>();
    while (e.hasMoreElements()) {
        String key = (String) e.nextElement();
        int lastIndex = key.lastIndexOf(".");
        String subKey = null;
        if (0 < lastIndex) {
            subKey = key.substring(0, lastIndex);
        } else {
            subKey = key;
        }
        if (matchedPrefixes.containsKey(subKey)) {
            List<String> fullKeys = matchedPrefixes.get(subKey);
            fullKeys.add(key);
        } else {
            List<String> fullKeys = new ArrayList<>();
            fullKeys.add(key);
            matchedPrefixes.put(subKey, fullKeys);
        }
    }
    return matchedPrefixes;
}

From source file:jp.terasoluna.fw.util.PropertyUtil.java

/**
 * ????? ??/*from  w w  w. jav a 2 s.  c  o m*/
 * @param localProps 
 * @param keyPrefix 
 * @return ???
 */
public static Enumeration<String> getPropertyNames(Properties localProps, String keyPrefix) {

    if (localProps == null || keyPrefix == null) {
        return null;
    }

    Collection<String> matchedNames = new ArrayList<String>();
    Enumeration<?> propNames = localProps.propertyNames();
    while (propNames.hasMoreElements()) {
        String name = (String) propNames.nextElement();
        if (name.startsWith(keyPrefix)) {
            matchedNames.add(name);
        }
    }
    return Collections.enumeration(matchedNames);
}

From source file:org.apache.directory.fortress.core.util.VUtil.java

/**
 * Perform simple length and safe text validations on collection of name-value pairs.
 *
 * @param props contains name-value pairs in {@code name:value} format.
 * @throws ValidationException in the event of failure.
 *///  ww  w.  j  av a  2s.  co  m
public static void properties(Properties props) throws ValidationException {
    if (PropUtil.isNotEmpty(props)) {
        for (Enumeration<?> e = props.propertyNames(); e.hasMoreElements();) {
            String key = (String) e.nextElement();
            String val = props.getProperty(key);
            safeText(key, GlobalIds.PROP_LEN);
            safeText(val, GlobalIds.PROP_LEN);
        }
    }
}

From source file:org.imsglobal.lti2.LTI2Util.java

public static void addCustomToLaunch(Properties ltiProps, Properties custom) {
    Enumeration<?> e = custom.propertyNames();
    while (e.hasMoreElements()) {
        String keyStr = (String) e.nextElement();
        String value = custom.getProperty(keyStr);
        setProperty(ltiProps, "custom_" + keyStr, value);
    }/*from w w  w .  j av  a 2  s .  c  om*/
}

From source file:org.pentaho.dictionary.DictionaryHelper.java

/**
 * Creates an in-memory metaverse node from the provided parameters
 *
 * @param id         The id of the node. An IIdGenerator should be used to create this.
 * @param name       The name of the node
 * @param type       The type of the node
 * @param properties The properties of the node
 * @return The metaverse node/*from ww  w.j a  va2 s . c  om*/
 */
public static IMetaverseNode createMetaverseNode(String id, String name, String type, Properties properties) {
    MetaverseTransientNode node = new MetaverseTransientNode();
    node.setStringID(id);
    node.setType(type);
    node.setName(name);
    if (properties != null) {
        Enumeration<?> propertyNames = properties.propertyNames();
        while (propertyNames.hasMoreElements()) {
            Object propertyName = propertyNames.nextElement();
            Object value = properties.get(propertyName);
            node.setProperty(propertyName.toString(), value);
        }
    }
    return node;
}

From source file:org.sakaiproject.util.Xml.java

/**
 * Serialize the properties into XML, adding an element to the doc under the top of the stack element.
 * /*from   w ww .j  a v a  2s  .c  o  m*/
 * @param propsToSerialize
 *        The properties to serialize.
 * @param doc
 *        The DOM doc to contain the XML (or null for a string return).
 * @param stack
 *        The DOM elements, the top of which is the containing element of the new "resource" element.
 * @return The newly added element.
 */
public static Element propertiesToXml(Properties propsToSerialize, Document doc, Stack<Element> stack) {
    Element properties = doc.createElement("properties");
    ((Element) stack.peek()).appendChild(properties);
    Enumeration<?> props = propsToSerialize.propertyNames();
    while (props.hasMoreElements()) {
        String name = (String) props.nextElement();
        String value = propsToSerialize.getProperty(name);
        Element propElement = doc.createElement("property");
        properties.appendChild(propElement);
        propElement.setAttribute("name", name);

        // encode to allow special characters in the value
        Xml.encodeAttribute(propElement, "value", (String) value);
        propElement.setAttribute("enc", "BASE64");
    }

    return properties;
}

From source file:de.alpharogroup.lang.PropertiesUtils.java

/**
 * Finds all keys with the same key prefixes from the given Properties and saves them to a Map
 * with the prefix as a key and holds a List with the whole keys the starts with the same key
 * prefix./*  w  w w  . ja v a2  s.c o m*/
 * 
 * @param enProperties
 *            the en properties
 * @return the matched prefix lists
 */
public static Map<String, List<String>> getMatchedPrefixLists(final Properties enProperties) {
    final Enumeration<?> e = enProperties.propertyNames();
    final Map<String, List<String>> matchedPrefixes = new LinkedHashMap<>();
    while (e.hasMoreElements()) {
        final String key = (String) e.nextElement();
        final int lastIndex = key.lastIndexOf(".");
        String subKey = null;
        if (0 < lastIndex) {
            subKey = key.substring(0, lastIndex);
        } else {
            subKey = key;
        }
        if (matchedPrefixes.containsKey(subKey)) {
            final List<String> fullKeys = matchedPrefixes.get(subKey);
            fullKeys.add(key);
        } else {
            final List<String> fullKeys = new ArrayList<>();
            fullKeys.add(key);
            matchedPrefixes.put(subKey, fullKeys);
        }
    }
    return matchedPrefixes;
}

From source file:Main.java

/**
 * Sorts property list and print out each key=value pair prepended with 
 * specific indentation.  If indent is null, do not prepend with
 * indentation. /*from w  w  w .jav a  2  s  .c o m*/
 *
 * The output string shows up in two styles, style 1 looks like
 * { key1=value1, key2=value2, key3=value3 }
 *
 * style 2 looks like
 *    key1=value1
 *    key2=value2
 *    key3=value3
 * where indent goes between the new line and the keys
 *
 * To get style 1, pass in a null indent
 * To get sytle 2, pass in non-null indent (whatever you want to go before
 * the key value)
 */
public static String sortProperties(Properties list, String indent) {
    int size = list == null ? 0 : list.size();
    int count = 0;
    String[] array = new String[size];
    String key;
    String value;
    StringBuffer buffer;

    // Calculate the number of properties in the property list and
    // build an array of all the property names.
    // We need to go thru the enumeration because Properties has a
    // recursive list of defaults.
    if (list != null) {
        for (Enumeration propertyNames = list.propertyNames(); propertyNames.hasMoreElements();) {
            if (count == size) {
                // need to expand the array
                size = size * 2;
                String[] expandedArray = new String[size];
                System.arraycopy(array, 0, expandedArray, 0, count);
                array = expandedArray;
            }
            key = (String) propertyNames.nextElement();
            array[count++] = key;
        }

        // now sort the array
        java.util.Arrays.sort(array, 0, count);
    }

    // now stringify the array
    buffer = new StringBuffer();
    if (indent == null)
        buffer.append("{ ");

    for (int ictr = 0; ictr < count; ictr++) {
        if (ictr > 0 && indent == null)
            buffer.append(", ");

        key = array[ictr];

        if (indent != null)
            buffer.append(indent);

        buffer.append(key);
        buffer.append("=");

        value = list.getProperty(key, "MISSING_VALUE");
        buffer.append(value);

        if (indent != null)
            buffer.append("\n");

    }
    if (indent == null)
        buffer.append(" }");

    return buffer.toString();
}