All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.n3r.eql.parser.BlockOptionsParser Maven / Gradle / Ivy

There is a newer version: 0.1.19
Show newest version
package org.n3r.eql.parser;

import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class BlockOptionsParser {
    private static Pattern OPTION_PATTERN = Pattern.compile("\\b(\\w+)\\b\\s*(=?)\\s*");

    public static Map parseOptions(
            String optionsStr, OptionValueParser... optionValueParsers) {
        HashMap options = new HashMap();
        if (optionsStr == null) return options;

        Matcher matcher = OPTION_PATTERN.matcher(optionsStr);
        int pos = 0;
        while (matcher.find(pos)) {
            String key = matcher.group(1);
            String option = "";
            pos = matcher.end();
            boolean haveOptionValue = "=".equals(matcher.group(2));

            if (haveOptionValue) {
                OptionValueParser valueParser = null;
                for (OptionValueParser optionValueParser : optionValueParsers) {
                    if (optionValueParser.getKey().equals(key)) {
                        valueParser = optionValueParser;
                        break;
                    }
                }
                if (valueParser == null) valueParser = WordOptionValueParser.instance;

                OffsetAndOptionValue oo = valueParser.parseValueOption(optionsStr.substring(pos));
                option = oo.getOptionValue();
                pos += oo.getOffset();
            }

            options.put(key, option);

        }
        return options;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy