Example usage for org.apache.commons.lang.text StrMatcher quoteMatcher

List of usage examples for org.apache.commons.lang.text StrMatcher quoteMatcher

Introduction

In this page you can find the example usage for org.apache.commons.lang.text StrMatcher quoteMatcher.

Prototype

public static StrMatcher quoteMatcher() 

Source Link

Document

Returns a matcher which matches the single or double quote character.

Usage

From source file:it.jnrpe.utils.StringUtils.java

/**
 * Splits the given string using as separator the <code>separator</code>
 * character./*from   w w  w  .ja v  a 2  s .com*/
 * 
 * @param string
 *            The string to be splitted
 * @param separator
 *            The separator character
 * @param ignoreQuotes
 *            <code>true</code> if the quotes must be ignored.
        
 * @return The splitted string */
public static String[] split(final String string, final char separator, final boolean ignoreQuotes) {
    StrTokenizer strtok = new StrTokenizer(string, StrMatcher.charMatcher(separator),
            StrMatcher.quoteMatcher());
    return strtok.getTokenArray();
}

From source file:au.org.ala.delta.intkey.directives.DefineNamesDirective.java

@Override
protected BasicIntkeyDirectiveInvocation doProcess(IntkeyContext context, String data) throws Exception {
    String keyword = null;/*from w w w. ja  v a  2 s. c  o  m*/
    List<String> names = new ArrayList<String>();

    // Need to prompt if data starts with a wildcard - don't bother
    // tokenizing
    if (!data.toUpperCase().startsWith(IntkeyDirectiveArgument.DEFAULT_DIALOG_WILDCARD)) {
        // Taxon names are separated by newlines or by commas
        List<String> tokens = new StrTokenizer(data, StrMatcher.charSetMatcher(new char[] { '\n', '\r', ',' }))
                .getTokenList();

        if (!tokens.isEmpty()) {
            String firstToken = tokens.get(0);

            // The keyword (which may quoted) and first taxon name may be
            // separated by a space
            List<String> splitFirstToken = new StrTokenizer(firstToken,
                    StrMatcher.charSetMatcher(new char[] { ' ' }), StrMatcher.quoteMatcher()).getTokenList();

            keyword = splitFirstToken.get(0);

            if (splitFirstToken.size() > 1) {
                names.add(StringUtils.join(splitFirstToken.subList(1, splitFirstToken.size()), " "));
            }

            for (int i = 1; i < tokens.size(); i++) {
                names.add(tokens.get(i).trim());
            }

            //If first name begins with a wildcard, we need to prompt for names. Clear out the names list if this is the case.
            if (!names.isEmpty()) {
                if (names.get(0).toUpperCase().startsWith(IntkeyDirectiveArgument.DEFAULT_DIALOG_WILDCARD)) {
                    names.clear();
                }
            }
        }
    }

    List<Item> taxa = new ArrayList<Item>();
    for (String taxonName : names) {
        Item taxon = context.getDataset().getTaxonByName(taxonName);
        if (taxon == null) {
            throw new IntkeyDirectiveParseException(
                    UIUtils.getResourceString("InvalidTaxonName.error", taxonName));
        } else {
            taxa.add(taxon);
        }
    }

    String directiveName = StringUtils.join(getControlWords(), " ").toUpperCase();

    if (StringUtils.isEmpty(keyword)) {
        keyword = context.getDirectivePopulator()
                .promptForString(UIUtils.getResourceString("EnterKeyword.caption"), null, directiveName);
        if (keyword == null) {
            // cancelled
            return null;
        }
    }

    if (taxa.isEmpty()) {
        List<String> selectedKeywords = new ArrayList<String>(); // Not
                                                                 // used,
                                                                 // but
                                                                 // required
                                                                 // as an
                                                                 // argument
        taxa = context.getDirectivePopulator().promptForTaxaByList(directiveName, false, false, false, false,
                null, selectedKeywords);
        if (taxa == null || taxa.isEmpty()) {
            // cancelled
            return null;
        }

        // extract taxon names for use in building string representation of
        // command
        for (Item taxon : taxa) {
            names.add(_taxonFormatter.formatItemDescription(taxon));
        }
    }

    DefineNamesDirectiveInvocation invoc = new DefineNamesDirectiveInvocation(keyword, taxa);
    invoc.setStringRepresentation(
            String.format("%s \"%s\" %s", getControlWordsAsString(), keyword, StringUtils.join(names, ", ")));

    return invoc;
}

From source file:it.jnrpe.server.console.CommandExecutor.java

public boolean executeCommand(String commandLine) throws Exception {
    StrTokenizer strtok = new StrTokenizer(commandLine, StrMatcher.charMatcher(' '), StrMatcher.quoteMatcher());
    String[] tokensAry = strtok.getTokenArray();
    String commandName = tokensAry[0];
    String[] params;/*from  ww  w  .ja va 2s.c  om*/
    if (tokensAry.length == 1) {
        params = new String[0];
    } else {
        params = new String[tokensAry.length - 1];
        System.arraycopy(tokensAry, 1, params, 0, params.length);
    }

    IConsoleCommand command = getCommand(commandName);
    if (command != null) {
        return command.execute(params);
    } else {
        throw new UnknownCommandException("Unknown command :'" + commandName + "'");
    }
}

From source file:org.eclipse.skalli.services.search.SearchQuery.java

StrTokenizer getTokenizer() {
    StrTokenizer tokenizer = new StrTokenizer("", StrMatcher.commaMatcher(), StrMatcher.quoteMatcher());
    tokenizer.setTrimmerMatcher(StrMatcher.trimMatcher());
    return tokenizer;
}

From source file:org.soaplab.clients.BatchTestClient.java

/*************************************************************************
 * Call service as define in 'locator' (which also has an input
 * data for the service) and report results into a new entry in
 * 'reports'.// w  w  w .  j  a  va 2s .  co  m
 *
 * It also returns its own report.
 *************************************************************************/
private static Properties callService(MyLocator locator, List<Properties> reports) {

    Properties report = new Properties();
    reports.add(report);
    String serviceName = locator.getServiceName();
    String inputLine = locator.getInputLine();
    report.setProperty(REPORT_SERVICE_NAME, serviceName);
    report.setProperty(REPORT_INPUT_LINE, inputLine);

    try {
        SoaplabBaseClient client = new SoaplabBaseClient(locator);

        // collect inputs from the input line
        BaseCmdLine cmd = null;
        if (StringUtils.isBlank(inputLine)) {
            cmd = new BaseCmdLine(new String[] {}, true);
        } else {
            cmd = new BaseCmdLine(
                    new StrTokenizer(inputLine, StrMatcher.charSetMatcher(" \t\f"), StrMatcher.quoteMatcher())
                            .getTokenArray(),
                    true);
        }
        SoaplabMap inputs = SoaplabMap
                .fromMap(InputUtils.collectInputs(cmd, SoaplabMap.toMaps(client.getInputSpec())));

        // any unrecognized inputs on the command-line?
        if (cmd.params.length > 0) {
            StringBuilder buf = new StringBuilder();
            buf.append("Unrecognized inputs: ");
            for (String arg : cmd.params)
                buf.append(arg + " ");
            report.setProperty(REPORT_ERROR_MESSAGE, buf.toString());
            report.setProperty(REPORT_JOB_STATUS, "NOT STARTED");
            return report;
        }

        // start service and wait for its completion
        String jobId = client.createAndRun(inputs);
        report.setProperty(REPORT_JOB_ID, jobId);
        client.waitFor(jobId);

        // save all info about just finished job
        String status = client.getStatus(jobId);
        report.setProperty(REPORT_JOB_STATUS, status);

        SoaplabMap times = client.getCharacteristics(jobId);
        Object elapsed = times.get(SoaplabConstants.TIME_ELAPSED);
        if (elapsed != null) {
            try {
                report.setProperty(REPORT_ELAPSED_TIME,
                        DurationFormatUtils.formatDurationHMS(Long.decode(elapsed.toString())));
            } catch (NumberFormatException e) {
            }
        }

        String lastEvent = client.getLastEvent(jobId);
        if (lastEvent != null)
            report.setProperty(REPORT_LAST_EVENT, lastEvent);

        if (!client.getLocator().getProtocol().equals(ClientConfig.PROTOCOL_AXIS1)) {
            // get result infos (about all available results)
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            ResultUtils.formatResultInfo(SoaplabMap.toStringMaps(client.getResultsInfo(jobId)),
                    new PrintStream(bos));
            report.setProperty(REPORT_RESULTS_INFO, bos.toString());
        }
        // get results (but ignore them - except the special ones)
        client.getResults(jobId);
        Map<String, Object> results = SoaplabMap.toMap(client.getSomeResults(jobId,
                new String[] { SoaplabConstants.RESULT_REPORT, SoaplabConstants.RESULT_DETAILED_STATUS }));
        for (Iterator<String> it = results.keySet().iterator(); it.hasNext();) {
            String resultName = it.next();
            report.setProperty(resultName, results.get(resultName).toString());
        }

        // clean the job
        if (!locator.isEnabledKeepResults()) {
            client.destroy(jobId);
            report.remove(REPORT_JOB_ID);
        }

    } catch (Throwable e) {
        reportError(report, e);
    }
    return report;
}

From source file:org.soaplab.clients.InputUtils.java

static String[] splitMultiValue(String value) {
    StrTokenizer tokenizer = new StrTokenizer(value, StrMatcher.charMatcher('|'), StrMatcher.quoteMatcher());
    return tokenizer.getTokenArray();
}