Example usage for java.util.regex Matcher groupCount

List of usage examples for java.util.regex Matcher groupCount

Introduction

In this page you can find the example usage for java.util.regex Matcher groupCount.

Prototype

public int groupCount() 

Source Link

Document

Returns the number of capturing groups in this matcher's pattern.

Usage

From source file:com.adobe.acs.tools.tag_maker.tagdataconverters.impl.LocalizedTitleAndNodeNameConverterImpl.java

@Override
public final TagData convert(String data) {
    data = StringUtils.stripToEmpty(data);

    Map<String, String> localizedTitles = new TreeMap<String, String>();

    String name;//  w w  w.ja va 2s.  com
    String title = "";

    final Matcher matcher = NODE_NAME_PATTERN.matcher(data);

    if (matcher.find() && matcher.groupCount() == 1) {
        name = StringUtils.stripToEmpty(matcher.group(1));
    } else {
        return TagData.EMPTY;
    }

    final String rawLocalizedTitles = StringUtils.stripToEmpty(NODE_NAME_PATTERN.matcher(data).replaceAll(""));
    final Matcher localeMatch = LOCALIZED_TITLES_PATTERN.matcher(rawLocalizedTitles);

    String firstLocale = null;
    while (localeMatch.find()) {
        final String locale = StringUtils.stripToEmpty(localeMatch.group(1));
        final String localeTitle = StringUtils.stripToEmpty(localeMatch.group(2));

        if (firstLocale == null) {
            firstLocale = locale;
        }

        if (DEFAULT_LOCALE_KEY.equals(locale)) {
            title = localeTitle;
        } else {
            localizedTitles.put(locale, localeTitle);
        }
    }

    // It no default title was found, fall back to the first locale listed

    if (StringUtils.isEmpty(title) && firstLocale != null) {
        title = localizedTitles.get(firstLocale);
    }

    if (StringUtils.isEmpty(title)) {
        return TagData.EMPTY;
    }

    final TagData tagData = new TagData(name);
    tagData.setTitle(title);
    tagData.setLocalizedTitles(localizedTitles);
    return tagData;
}

From source file:net.solarnetwork.node.util.TemplatedMessageSource.java

@Override
public String getMessage(String code, Object[] args, String defaultMessage, Locale locale) {
    if (pat != null) {
        Matcher m = pat.matcher(code);
        final int count = m.groupCount();
        if (m.matches() && count > 0) {
            // remap using regex capture groups
            StringBuilder buf = new StringBuilder();
            for (int i = 1; i <= count; i++) {
                buf.append(m.group(i));/* w ww .  j  av  a2  s .c o m*/
            }
            code = buf.toString();
        }
    }
    return delegate.getMessage(code, args, defaultMessage, locale);
}

From source file:net.solarnetwork.node.util.TemplatedMessageSource.java

@Override
public String getMessage(String code, Object[] args, Locale locale) throws NoSuchMessageException {
    if (pat != null) {
        Matcher m = pat.matcher(code);
        final int count = m.groupCount();
        if (m.matches() && count > 0) {
            // remap using regex capture groups
            StringBuilder buf = new StringBuilder();
            for (int i = 1; i <= count; i++) {
                buf.append(m.group(i));//from w w w. j  a  va 2s.com
            }
            code = buf.toString();
        }
    }
    return delegate.getMessage(code, args, locale);
}

From source file:com.bluexml.side.Framework.alfresco.share.formExtension.MyUrlConfigSource.java

/**
 * @param sourceString/*w w  w  . j  av  a  2  s  .co  m*/
 */
private void processWildcardClassPath(String sourceString) {
    logger.debug("process :" + sourceString);
    // get a classPathStore instance

    // get store part from sourceString
    if (sourceString.matches(regexp)) {
        // ok
        Pattern p = Pattern.compile(regexp);
        Matcher m = p.matcher(sourceString);
        m.find();
        if (m.groupCount() == 2) {
            // ok
            String storeString = m.group(1);
            String filepathtoMatch = m.group(2);

            logger.debug("store :" + storeString);
            logger.debug("fileName :" + filepathtoMatch);

            ClassPathStore store = getClassPathStore(storeString);

            if (store.hasDocument(filepathtoMatch)) {
                logger.debug("file found in store");

                computeSourceString(filepathtoMatch, storeString);

            } else {
                logger.debug("fileName not found");

                if (filepathtoMatch.indexOf("*") != -1) {
                    logger.debug("use white card");
                    // convert pattern to regexp
                    filepathtoMatch = filepathtoMatch.replaceAll("\\.", "\\\\.");
                    filepathtoMatch = filepathtoMatch.replaceAll("\\*", "\\.\\*");
                }
                String[] allDocumentPaths = store.getAllDocumentPaths();
                logger.debug("documents in store :" + allDocumentPaths.length);
                for (String fileNamePath : allDocumentPaths) {
                    if (fileNamePath.matches(filepathtoMatch)) {
                        logger.debug("matches found :" + fileNamePath);
                        computeSourceString(fileNamePath, storeString);
                    }
                }
            }
        }
    }
}

From source file:com.android.example.github.api.ApiResponse.java

public ApiResponse(Response<T> response) {
    code = response.code();// w w w  .j  ava2s  .  com
    if (response.isSuccessful()) {
        body = response.body();
        errorMessage = null;
    } else {
        String message = null;
        if (response.errorBody() != null) {
            try {
                message = response.errorBody().string();
            } catch (IOException ignored) {
                Timber.e(ignored, "error while parsing response");
            }
        }
        if (message == null || message.trim().length() == 0) {
            message = response.message();
        }
        errorMessage = message;
        body = null;
    }
    String linkHeader = response.headers().get("link");
    if (linkHeader == null) {
        links = Collections.emptyMap();
    } else {
        links = new ArrayMap<>();
        Matcher matcher = LINK_PATTERN.matcher(linkHeader);

        while (matcher.find()) {
            int count = matcher.groupCount();
            if (count == 2) {
                links.put(matcher.group(2), matcher.group(1));
            }
        }
    }
}

From source file:cf.service.ServiceBroker.java

public String deleteService(String authToken, String uri) throws ServiceBrokerException {
    LOGGER.debug("Deleting service");
    validateAuthToken(authToken);//ww w . j a v  a2s . co  m
    final Matcher matcher = INSTANCE_PATTERN.matcher(uri);
    if (!matcher.matches() && matcher.groupCount() != 2) {
        throw new ResourceNotFoundException();
    }
    final String serviceInstanceId = matcher.group(2);
    provisioner.delete(serviceInstanceId);
    return "{}";
}

From source file:cf.service.ServiceBroker.java

public String unbindService(String authToken, String uri) throws ServiceBrokerException {
    LOGGER.debug("Unbinding service");
    validateAuthToken(authToken);/*www . j a v  a 2 s. c  o  m*/
    final Matcher matcher = BINDING_PATTERN.matcher(uri);
    if (!matcher.matches() && matcher.groupCount() != 3) {
        throw new ResourceNotFoundException();
    }
    final String instanceId = matcher.group(1);
    final String bindingId = matcher.group(2);
    provisioner.unbind(instanceId, bindingId);
    return "{}";
}

From source file:gemlite.core.internal.support.jpa.files.service.JarFileService.java

public boolean splitVersion(ReleasedJarFile jarFile, String fname) {
    String reg = "([-a-zA-Z]+)\\-([0-9].+)\\.jar";
    Pattern pattern = Pattern.compile(reg);
    Matcher matcher = pattern.matcher(fname);
    if (matcher.find() && matcher.groupCount() == 2) {
        jarFile.setFileName(matcher.group(1));
        jarFile.setVersion(matcher.group(2));
    } else {// w w w .j a v a 2  s  . c o  m
        jarFile.setFileName(fname);
    }
    return true;
}

From source file:com.comcast.video.dawg.DawgClient.java

/**
 * Get the socket timeout for the current <code>REST</code> operation.
 * @param url <code>REST</code> request url.
 * @return timeout value in milliseconds. If hold key action, it will return timeout based on the number of
 * keys to be sent. Otherwise it will return the default timeout(10 sec).
 *//* w ww  .jav  a  2s .c om*/
private int getRestSocketTimeOut(URL url) {
    String buildUrl = null;
    // Default socket timeout
    int timeout = DefaultRestClient.DEFAULT_TIMEOUT;

    try {
        // Build the request url. It involves appending path and query strings properly to the base url.
        buildUrl = url.build();
    } catch (HttpException e) {
        logger.warn("Exception while building URL.", e);
    }

    if (null != buildUrl) {
        Matcher matcher = HOLD_KEY_PATTERN.matcher(buildUrl);
        if (matcher.find() && matcher.groupCount() == 1) {
            timeout = Integer.valueOf(matcher.group(1)) * ONE_SEC_IN_MILLIS;
            logger.info("REST communication socket timeout overriden to " + timeout + " milliseconds");
        }
    }
    return timeout;
}