Java - Write code to Parse out a list of Name Value pairs that are delimited together.

Requirements

Write code to Parse out a list of Name Value pairs that are delimited together.

Demo

//package com.book2s;

import java.util.HashMap;
import java.util.Map;

public class Main {
    public static void main(String[] argv) {
        String allNameValuePairs = "&abc=book2s.com";
        String listDelimiter = ".";
        String nameValueSeparator = "=";
        System.out.println(delimitedToMap(allNameValuePairs, listDelimiter,
                nameValueSeparator));/* w ww . j a v a2s  . com*/
    }

    /**
     * Parses out a list of Name Value pairs that are delimited together. Will
     * always return a StringMap. If allNameValuePairs is null, or no name
     * values can be parsed out an empty StringMap is returned.
     * 
     * @param allNameValuePairs
     *            the entire string to be parsed.
     * @param listDelimiter
     *            (typically ';') the delimited between the list
     * @param nameValueSeparator
     *            (typically '=') the separator between the name and value
     */
    public static Map<String, String> delimitedToMap(
            String allNameValuePairs, String listDelimiter,
            String nameValueSeparator) {

        HashMap<String, String> params = new HashMap<String, String>();
        if ((allNameValuePairs == null)
                || (allNameValuePairs.length() == 0)) {
            return params;
        }
        // trim off any leading listDelimiter...
        allNameValuePairs = trimFront(allNameValuePairs, listDelimiter);
        return getKeyValue(params, 0, allNameValuePairs, listDelimiter,
                nameValueSeparator);
    }

    /**
     * Trims off recurring strings from the front of a string.
     * 
     * @param source
     *            the source string
     * @param trim
     *            the string to trim off the front
     */
    public static String trimFront(String source, String trim) {
        if (source == null) {
            return null;
        }
        if (source.indexOf(trim) == 0) {
            // dp("trim ...");
            return trimFront(source.substring(trim.length()), trim);
        } else {
            return source;
        }
    }

    private static HashMap<String, String> getKeyValue(
            HashMap<String, String> map, int pos, String allNameValuePairs,
            String listDelimiter, String nameValueSeparator) {

        if (pos >= allNameValuePairs.length()) {
            // dp("end as "+pos+" >= "+allNameValuePairs.length() );
            return map;
        }

        int equalsPos = allNameValuePairs.indexOf(nameValueSeparator, pos);
        int delimPos = allNameValuePairs.indexOf(listDelimiter, pos);

        if (delimPos == -1) {
            delimPos = allNameValuePairs.length();
        }
        if (equalsPos == -1) {
            // dp("no more equals...");
            return map;
        }
        if (delimPos == (equalsPos + 1)) {
            // dp("Ignoring as nothing between delim and equals...
            // delim:"+delimPos+" eq:"+equalsPos);
            return getKeyValue(map, delimPos + 1, allNameValuePairs,
                    listDelimiter, nameValueSeparator);
        }
        if (equalsPos > delimPos) {
            // there is a key without a value?
            String key = allNameValuePairs.substring(pos, delimPos);
            key = key.trim();
            if (key.length() > 0) {
                map.put(key, null);
            }
            return getKeyValue(map, delimPos + 1, allNameValuePairs,
                    listDelimiter, nameValueSeparator);

        }
        String key = allNameValuePairs.substring(pos, equalsPos);

        if (delimPos > -1) {
            String value = allNameValuePairs.substring(equalsPos + 1,
                    delimPos);
            // dp("cont "+key+","+value+" pos:"+pos+"
            // len:"+allNameValuePairs.length());
            key = key.trim();

            map.put(key, value);
            pos = delimPos + 1;

            // recurse the rest of the values...
            return getKeyValue(map, pos, allNameValuePairs, listDelimiter,
                    nameValueSeparator);
        } else {
            // dp("ERROR: delimPos < 0 ???");
            return map;
        }
    }
}

Related Exercise