parses a String of the form name1='value1' name2='value2'. - Java java.lang

Java examples for java.lang:String Parse

Description

parses a String of the form name1='value1' name2='value2'.

Demo Code

//package com.java2s;

import java.util.HashMap;

public class Main {
    public static void main(String[] argv) {
        String tag = "abc=java2s.com";
        System.out.println(parseNameQuotedValue(tag));
    }/*from   w  ww. ja v a  2s  .com*/

    private static final char SINGLE_QUOTE = '\'';
    private static final char DOUBLE_QUOTE = '"';

    /**
     * parses a String of the form name1='value1' name2='value2'. Note that you
     * can use either single or double quotes for any particular name value pair
     * and the end quote must match the begin quote.
     */
    public static HashMap<String, String> parseNameQuotedValue(String tag)
            throws RuntimeException {

        if (tag == null || tag.length() < 1) {
            return null;
        }

        // make sure that the quotes are matched...
        // int remainer = countOccurances(tag, ""+quote) % 2;
        // if (remainer == 1) {
        // dp("remainder = "+remainer);
        // throw new StringParsingException("Unmatched quote in "+tag);
        // }

        // make sure that th last character is not an equals...
        // (check now so I don't need to check this every time..)
        if (tag.charAt(tag.length() - 1) == '=') {
            throw new RuntimeException(
                    "missing quoted value at the end of " + tag);
        }

        HashMap<String, String> map = new HashMap<String, String>();
        // recursively parse out the name value pairs...
        return parseNameQuotedValue(map, tag, 0);
    }

    /**
     * recursively parse out name value pairs (where the value is quoted, with
     * either single or double quotes).
     */
    private static HashMap<String, String> parseNameQuotedValue(
            HashMap<String, String> map, String tag, int pos)
            throws RuntimeException {

        int equalsPos = tag.indexOf("=", pos);
        if (equalsPos > -1) {
            // check for begin quote...
            char firstQuote = tag.charAt(equalsPos + 1);
            if (firstQuote != SINGLE_QUOTE && firstQuote != DOUBLE_QUOTE) {
                throw new RuntimeException("missing begin quote at "
                        + (equalsPos) + "[" + tag.charAt(equalsPos + 1)
                        + "] in [" + tag + "]");
            }

            // check for end quote...
            int endQuotePos = tag.indexOf(firstQuote, equalsPos + 2);
            if (endQuotePos == -1) {
                throw new RuntimeException("missing end quote ["
                        + firstQuote + "] after " + pos + " in [" + tag
                        + "]");
            }

            // we have a valid name and value...
            // dp("pos="+pos+" equalsPos="+equalsPos+"
            // endQuotePos="+endQuotePos);
            String name = tag.substring(pos, equalsPos);
            String value = tag.substring(equalsPos + 2, endQuotePos);
            // dp("name="+name+"; value="+value+";");

            // trim off any whitespace from the front of name...
            name = trimFront(name, " ");
            if ((name.indexOf(SINGLE_QUOTE) > -1)
                    || (name.indexOf(DOUBLE_QUOTE) > -1)) {
                throw new RuntimeException(
                        "attribute name contains a quote [" + name + "]");
            }
            map.put(name, value);

            return parseNameQuotedValue(map, tag, endQuotePos + 1);

        } else {
            // no more equals... stop parsing...
            return map;
        }
    }

    /**
     * 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;
        }
    }
}

Related Tutorials