org.apache.commons.text.matcher.StringMatcherFactory Java Examples

The following examples show how to use org.apache.commons.text.matcher.StringMatcherFactory. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: CompileCommandParser.java    From synopsys-detect with Apache License 2.0 5 votes vote down vote up
public List<String> parseCommandString(String commandString, Map<String, String> optionOverrides) {
    logger.trace(String.format("origCompileCommand         : %s", commandString));
    String quotesRemovedCompileCommand = encodeQuotedWhitespace(commandString);
    logger.trace(String.format("quotesRemovedCompileCommand: %s", quotesRemovedCompileCommand));
    StringTokenizer tokenizer = new StringTokenizer(quotesRemovedCompileCommand);
    tokenizer.setQuoteMatcher(StringMatcherFactory.INSTANCE.quoteMatcher());
    List<String> commandList = new ArrayList<>();
    String lastPart = "";
    int partIndex = 0;
    while (tokenizer.hasNext()) {
        String token = tokenizer.nextToken();
        String part = restoreWhitespace(token);
        if (partIndex > 0) {
            String optionValueOverride = null;
            for (Map.Entry<String, String> optionToOverride : optionOverrides.entrySet()) {
                if (optionToOverride.getKey().equals(lastPart)) {
                    optionValueOverride = optionToOverride.getValue();
                }
            }
            if (optionValueOverride != null) {
                commandList.add(optionValueOverride);
            } else {
                commandList.add(part);
            }
        } else {
            commandList.add(part);
        }
        lastPart = part;
        partIndex++;
    }
    return commandList;
}
 
Example #2
Source File: ClangCompileCommandParser.java    From hub-detect with Apache License 2.0 5 votes vote down vote up
public List<String> getCompilerArgsForGeneratingDepsMkFile(final String origCompileCommand, final String depsMkFilePath, final Map<String, String> optionOverrides) {
    logger.trace(String.format("origCompileCommand         : %s", origCompileCommand));
    String quotesRemovedCompileCommand = escapeQuotedWhitespace(origCompileCommand.trim());
    logger.trace(String.format("quotesRemovedCompileCommand: %s", quotesRemovedCompileCommand));
    StringTokenizer tokenizer = new StringTokenizer(quotesRemovedCompileCommand);
    tokenizer.setQuoteMatcher(StringMatcherFactory.INSTANCE.quoteMatcher());
    final List<String> argList = new ArrayList<>();
    String lastPart = "";
    int partIndex = 0;
    while (tokenizer.hasNext()) {
        String part = unEscapeDoubleQuotes(restoreWhitespace(tokenizer.nextToken()));
        if (partIndex > 0) {
            String optionValueOverride = null;
            for (String optionToOverride : optionOverrides.keySet()) {
                if (optionToOverride.equals(lastPart)) {
                    optionValueOverride = optionOverrides.get(optionToOverride);
                }
            }
            if (optionValueOverride != null) {
                argList.add(optionValueOverride);
            } else {
                argList.add(part);
            }
        }
        lastPart = part;
        partIndex++;
    }
    argList.add("-M");
    argList.add("-MF");
    argList.add(depsMkFilePath);
    return argList;
}
 
Example #3
Source File: JsonUtils.java    From genie with Apache License 2.0 3 votes vote down vote up
/**
 * Given a flat string of command line arguments this method will attempt to tokenize the string and split it for
 * use in DTOs. The split will occur on whitespace (tab, space, new line, carriage return) while respecting
 * single quotes to keep those elements together.
 * <p>
 * Example:
 * {@code "/bin/bash -xc 'echo "hello" world!'"} results in {@code ["/bin/bash", "-xc", "echo "hello" world!"]}
 *
 * @param commandArgs The original string representation of the command arguments
 * @return An ordered list of arguments
 */
@Nonnull
public static List<String> splitArguments(final String commandArgs) {
    final StringTokenizer tokenizer = new StringTokenizer(
        commandArgs,
        StringMatcherFactory.INSTANCE.splitMatcher(),
        StringMatcherFactory.INSTANCE.quoteMatcher()
    );

    return tokenizer.getTokenList();
}