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

net.sf.jcmdlineparser.strategy.SpaceSeparatedStrategy Maven / Gradle / Ivy

/**********************************************************************
Copyright (c) 2009-2010 Alexander Kerner. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
 *********************************************************************/

package net.sf.jcmdlineparser.strategy;

import java.util.Scanner;
import java.util.Set;
import java.util.regex.MatchResult;
import java.util.regex.Pattern;

import net.sf.jcmdlineparser.ParserException;
import net.sf.jcmdlineparser.options.AbstractOption;
import net.sf.jcmdlineparser.options.NoSuchOptionException;
import net.sf.jcmdlineparser.options.NoValueGivenException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 

* Implementation for {@link Strategy}. *

*

* Accepted format is * *

k v key value ...
* *

* * @author Alexander Kerner * @version 2010-08-10 * @see AbstractOption * @see Strategy * */ public class SpaceSeparatedStrategy implements Strategy { private final static Logger logger = LoggerFactory .getLogger(SpaceSeparatedStrategy.class); private final static Pattern pattern = Pattern .compile("-+(\\S+)\\s*([^( - )]*)"); public String[] parse(String[] string, Set> options) throws ParserException { final StringBuilder sb = new StringBuilder(); for (String s : string) { // + " " important !! sb.append(s + " "); } return parse(sb.toString(), options).split(" "); } public String parse(String string, Set> options) throws ParserException { logger.debug("parsing string: \"" + string + "\", options to process: \"" + options + "\""); // elements, that could not be recognized. final StringBuilder unknown = new StringBuilder(); final Scanner scanner = new Scanner(string); String current = scanner.findWithinHorizon(pattern, 0); if (current == null) { // pattern does not match return string; } // found pattern, at least key available unknown.append(handleCurrent(current, options, scanner.match()) + " "); while (scanner.hasNext()) { current = scanner.findWithinHorizon(pattern, 0); // found pattern, at least key available final String u = handleCurrent(current, options, scanner.match()); // logger.debug("string \"" + u + "\" not recognized"); unknown.append(u + " "); } logger.debug("parsing done, unknwon: \"" + unknown.toString().trim() + "\""); return unknown.toString().trim(); } private static String handleCurrent(String current, Set> options, MatchResult match) throws ParserException { logger.debug("handling string \"" + current + "\""); final String key = match.group(1).trim(); final String value = match.group(2).trim(); logger.debug("Found matching, key=" + key + ", value=" + value); if (value == null || value.length() < 1) throw new NoValueGivenException(); try { StrategyUtils.handleKeyValuePair(key, value, options); } catch (NoSuchOptionException e) { logger.debug(e.getLocalizedMessage()); return current; } return ""; } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy