Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
/**
 * Copyright (c) 2013, 2014 Denis Nikiforov.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *    Denis Nikiforov - initial API and implementation
 */

import java.util.ArrayList;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

public class Main {
    public static Map<String, String> convertFromString(String text) {
        Map<String, String> result = new LinkedHashMap<String, String>();
        List<String> keyValuePairs = decode(text, ';');
        for (String pair : keyValuePairs) {
            List<String> keyAndValue = decode(pair, '=');
            String key = keyAndValue.get(0);
            String value = keyAndValue.get(1);
            result.put(key, value);
        }
        return result;
    }

    public static List<String> decode(String text, char delimiter) {
        List<String> parts = new ArrayList<String>();

        boolean escapeMode = false;
        String part = "";
        for (int i = 0; i < text.length(); i++) {
            char c = text.charAt(i);
            if (c == delimiter) {
                if (escapeMode) {
                    part += delimiter;
                    escapeMode = false;
                } else {
                    // end of part
                    parts.add(part);
                    part = "";
                }
            } else if (c == '\\') {
                if (escapeMode) {
                    part += '\\';
                    escapeMode = false;
                } else {
                    escapeMode = true;
                }
            } else {
                part += c;
            }
        }
        return parts;
    }
}