Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: LGPL 

import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {
    /**
     * Pattern used to match variables in XML fragments.
     */
    private static Pattern variablePattern = Pattern.compile("\\$\\(([^\\)]+)\\)");

    /**
     * Extract default values of variables defined in the given string.
     * Default values are used to populate the variableDefs map.  Variables
     * already defined in this map will NOT be modified.
     *
     * @param string string to extract default values from.
     * @param variableDefs map which default values will be added to.
     */
    public static void extractVariableDefaultsFromString(String string, Map<String, String> variableDefs) {
        Matcher variableMatcher = variablePattern.matcher(string);
        while (variableMatcher.find()) {
            String varString = variableMatcher.group(1);

            int eqIdx = varString.indexOf("=");

            if (eqIdx > -1) {
                String varName = varString.substring(0, eqIdx).trim();

                if (!variableDefs.containsKey(varName))
                    variableDefs.put(varName, varString.substring(eqIdx + 1, varString.length()));
            }
        }
    }
}