Java Properties stringToProperties(String str)

Here you can find the source of stringToProperties(String str)

Description

This method converts a comma-separated String (with whitespace optionally allowed after the comma) representing properties to a Properties object.

License

Apache License

Declaration

public static Properties stringToProperties(String str) 

Method Source Code

//package com.java2s;
/*//from   ww  w .  j  a  v a2 s.c o m
 *  * Copyright 2016 Skymind, Inc.
 *  *
 *  *    Licensed under the Apache License, Version 2.0 (the "License");
 *  *    you may not use this file except in compliance with the License.
 *  *    You may obtain a copy of the License at
 *  *
 *  *        http://www.apache.org/licenses/LICENSE-2.0
 *  *
 *  *    Unless required by applicable law or agreed to in writing, software
 *  *    distributed under the License is distributed on an "AS IS" BASIS,
 *  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  *    See the License for the specific language governing permissions and
 *  *    limitations under the License.
 */

import java.util.*;

public class Main {
    /**
     * This method converts a comma-separated String (with whitespace
     * optionally allowed after the comma) representing properties
     * to a Properties object.  Each property is "property=value".  The value
     * for properties without an explicitly given value is applyTransformToDestination to "true".
     */
    public static Properties stringToProperties(String str) {
        Properties result = new Properties();
        String[] props = str.trim().split(",\\s*");
        for (String term : props) {
            int divLoc = term.indexOf("=");
            String key;
            String value;
            if (divLoc >= 0) {
                key = term.substring(0, divLoc);
                value = term.substring(divLoc + 1);
            } else {
                key = term;
                value = "true";
            }
            result.setProperty(key, value);
        }
        return result;
    }

    /**
     * Splits on whitespace (\\s+).
     */
    public static List split(String s) {
        return (split(s, "\\s+"));
    }

    /**
     * Splits the given string using the given regex as delimiters.
     * This method is the same as the String.split() method (except it throws
     * the results in a List),
     * and is included just to give a call that is parallel to the other
     * static regex methods in this class.
     *
     * @param str   String to split up
     * @param regex String to compile as the regular expression
     * @return List of Strings resulting from splitting on the regex
     */
    public static List split(String str, String regex) {
        return (Arrays.asList(str.split(regex)));
    }

    /**
     * Returns s if it's at most maxWidth chars, otherwise chops right side to fit.
     */
    public static String trim(String s, int maxWidth) {
        if (s.length() <= maxWidth) {
            return (s);
        }
        return (s.substring(0, maxWidth));
    }

    public static String trim(Object obj, int maxWidth) {
        return trim(obj.toString(), maxWidth);
    }
}

Related

  1. putPrefixToProperties(String prefix, Properties pro)
  2. removeDots(Properties props)
  3. search(List properties, String term)
  4. setProperty(Properties props, String keyword, Object value)
  5. stringPropertyNames(Properties props, String prefix)
  6. toProperties(Map parse)
  7. transformPropertiesToParams(Map properties)