Search in sources :

Example 1 with Pair

use of com.vladsch.flexmark.util.Pair in project flexmark-java by vsch.

the class OptionsParser method parseOption.

@Override
public Pair<T, List<ParsedOption<T>>> parseOption(BasedSequence optionsText, T options, MessageProvider provider) {
    ArrayList<ParserMessage> messages = null;
    BasedSequence[] optionsList = optionsText.split(myOptionDelimiter, 0, BasedSequence.SPLIT_TRIM_SKIP_EMPTY);
    T result = options;
    if (provider == null)
        provider = MessageProvider.DEFAULT;
    List<ParsedOption<T>> parsedOptions = new ArrayList<ParsedOption<T>>(optionsList.length);
    for (BasedSequence optionText : optionsList) {
        OptionParser<T> matched = null;
        DelimitedBuilder message = null;
        BasedSequence[] optionList = optionText.split(myOptionValueDelimiter, 2, BasedSequence.SPLIT_SKIP_EMPTY);
        if (optionList.length == 0)
            continue;
        BasedSequence optionName = optionList[0];
        BasedSequence optionValue = optionList.length > 1 ? optionList[1] : optionName.subSequence(optionName.length(), optionName.length());
        for (OptionParser<T> optionParser : myParsableOptions) {
            if (optionParser.getOptionName().equals(optionName.toString())) {
                matched = optionParser;
                message = null;
                break;
            }
            if (optionParser.getOptionName().startsWith(optionName.toString())) {
                if (matched == null) {
                    matched = optionParser;
                } else {
                    if (message == null) {
                        message = new DelimitedBuilder(", ");
                        message.append(provider.message(KEY_OPTION_0_IS_AMBIGUOUS, OPTION_0_IS_AMBIGUOUS, optionName));
                        message.append(matched.getOptionName()).mark();
                    }
                    message.append(optionParser.getOptionName()).mark();
                }
            }
        }
        // have our match
        if (matched != null) {
            if (message == null) {
                Pair<T, List<ParsedOption<T>>> pair = matched.parseOption(optionValue, result, provider);
                result = pair.getFirst();
                parsedOptions.add(new ParsedOption<T>(optionText, this, ParsedOptionStatus.VALID, null, pair.getSecond()));
            } else {
                parsedOptions.add(new ParsedOption<T>(optionText, this, ParsedOptionStatus.ERROR, new ParserMessage(optionName, ParsedOptionStatus.ERROR, message.toString())));
            }
        } else {
            message = new DelimitedBuilder(", ");
            message.append(provider.message(KEY_OPTION_0_DOES_NOT_MATCH, OPTION_0_DOES_NOT_MATCH, optionName));
            appendOptionNames(message);
            parsedOptions.add(new ParsedOption<T>(optionText, this, ParsedOptionStatus.ERROR, new ParserMessage(optionName, ParsedOptionStatus.ERROR, message.toString())));
        }
    }
    return new Pair<T, List<ParsedOption<T>>>(result, parsedOptions);
}
Also used : BasedSequence(com.vladsch.flexmark.util.sequence.BasedSequence) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList) Pair(com.vladsch.flexmark.util.Pair)

Example 2 with Pair

use of com.vladsch.flexmark.util.Pair in project flexmark-java by vsch.

the class TocLevelsOptionParser method parseOption.

@SuppressWarnings("unchecked")
@Override
public Pair<TocOptions, List<ParsedOption<TocOptions>>> parseOption(BasedSequence optionText, TocOptions options, MessageProvider provider) {
    // may have levels
    TocOptions result = options;
    BasedSequence[] levelsOptionValue = optionText.split(',');
    final ParserParams parserParams = new ParserParams();
    if (provider == null)
        provider = MessageProvider.DEFAULT;
    int newLevels = 0;
    int i = 0;
    final MessageProvider finalProvider = provider;
    Computable<Integer, BasedSequence> convertWithMessage = new Computable<Integer, BasedSequence>() {

        @Override
        public Integer compute(BasedSequence option) {
            try {
                return option.isEmpty() ? null : Integer.parseInt(option.toString());
            } catch (Exception ignored) {
                parserParams.add(new ParserMessage(option, ParsedOptionStatus.ERROR, finalProvider.message(KEY_OPTION_0_VALUE_1_NOT_INTEGER, OPTION_0_VALUE_1_NOT_INTEGER, myOptionName, option)));
                parserParams.skip = true;
                return null;
            }
        }
    };
    for (BasedSequence option : levelsOptionValue) {
        BasedSequence[] optionRange = option.split('-', 2, BasedSequence.SPLIT_TRIM_PARTS);
        Integer rangeStart;
        Integer rangeEnd;
        parserParams.skip = false;
        if (optionRange.length == 2) {
            rangeStart = convertWithMessage.compute(optionRange[0]);
            rangeEnd = convertWithMessage.compute(optionRange[1]);
            if (rangeStart == null)
                rangeStart = 1;
            if (rangeEnd == null)
                rangeEnd = 6;
        } else {
            rangeStart = convertWithMessage.compute(optionRange[0]);
            rangeEnd = rangeStart;
        }
        if (!parserParams.skip) {
            if (rangeStart == null) {
                parserParams.add(new ParserMessage(option, ParsedOptionStatus.IGNORED, finalProvider.message(KEY_OPTION_0_VALUE_1_TRUNCATED_TO_EMPTY_RANGE, OPTION_0_VALUE_1_TRUNCATED_TO_EMPTY_RANGE, myOptionName, option)));
            } else {
                if (rangeEnd < rangeStart) {
                    int tmp = rangeStart;
                    rangeStart = rangeEnd;
                    rangeEnd = tmp;
                }
                if (rangeEnd < 1 || rangeStart > 6) {
                    if (rangeStart == (int) rangeEnd) {
                        parserParams.add(new ParserMessage(option, ParsedOptionStatus.IGNORED, provider.message(KEY_OPTION_0_VALUE_1_NOT_IN_RANGE, OPTION_0_VALUE_1_NOT_IN_RANGE, myOptionName, option)));
                    } else {
                        parserParams.add(new ParserMessage(option, ParsedOptionStatus.WARNING, finalProvider.message(KEY_OPTION_0_VALUE_1_TRUNCATED_TO_EMPTY_RANGE, OPTION_0_VALUE_1_TRUNCATED_TO_EMPTY_RANGE, myOptionName, option)));
                    }
                } else {
                    int wasStart = rangeStart;
                    int wasEnd = rangeEnd;
                    rangeStart = Utils.minLimit(rangeStart, 1);
                    rangeEnd = Utils.maxLimit(rangeEnd, 6);
                    if (wasStart != rangeStart || wasEnd != rangeEnd) {
                        parserParams.add(new ParserMessage(option, ParsedOptionStatus.WEAK_WARNING, finalProvider.message(KEY_OPTION_0_VALUE_1_TRUNCATED_TO_RANGE_2, OPTION_0_VALUE_1_TRUNCATED_TO_RANGE_2, myOptionName, option, rangeStart + ", " + rangeEnd)));
                    }
                    for (int b = rangeStart; b <= rangeEnd; b++) newLevels = newLevels | (1 << b);
                }
            }
        }
        i++;
    }
    if (newLevels != 0)
        result = result.withLevels(newLevels);
    return new Pair<TocOptions, List<ParsedOption<TocOptions>>>(result, (List<ParsedOption<TocOptions>>) Collections.<ParsedOption<TocOptions>>singletonList(new ParsedOption(optionText, this, parserParams.status, parserParams.messages)));
}
Also used : BasedSequence(com.vladsch.flexmark.util.sequence.BasedSequence) Computable(com.vladsch.flexmark.util.Computable) Pair(com.vladsch.flexmark.util.Pair)

Aggregations

Pair (com.vladsch.flexmark.util.Pair)2 BasedSequence (com.vladsch.flexmark.util.sequence.BasedSequence)2 Computable (com.vladsch.flexmark.util.Computable)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1