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.microsoft.alm.plugin.idea.git.ui.simplecheckout.SimpleCheckoutModel.java

protected SimpleCheckoutModel(final Project project, final CheckoutProvider.Listener listener,
        final String gitUrl, final String ref) {
    super();/* w  w w  . jav  a  2s  .  c o  m*/
    this.project = project;
    this.listener = listener;
    this.gitUrl = gitUrl;
    this.ref = ref;

    this.parentDirectory = PluginServiceProvider.getInstance().getPropertyService()
            .getProperty(PropertyService.PROP_REPO_ROOT);
    // use default root if no repo root is found
    if (StringUtils.isEmpty(this.parentDirectory)) {
        this.parentDirectory = DEFAULT_SOURCE_PATH;
    }

    // try and parse for the repo name to use as the directory name
    final Matcher matcher = GIT_URL_PATTERN.matcher(gitUrl);
    if (matcher.find() && matcher.groupCount() == 1) {
        this.directoryName = matcher.group(1);
    } else {
        this.directoryName = StringUtils.EMPTY;
    }
}

From source file:edu.cornell.mannlib.vitro.webapp.controller.individual.IndividualRequestAnalyzer.java

/**
 * @return null if this is not a linked data request, returns content type
 *         if it is a linked data request.
 * //from  www  .  j a v a 2s  . co m
 *         These are Vitro-specific ways of requesting rdf, unrelated to
 *         semantic web standards. They do not trigger a redirect with a
 *         303, because the request is for a set of bytes rather than an
 *         individual.
 */
protected ContentType checkUrlForLinkedDataRequest() {
    /*
     * Check for url param specifying format. Example:
     * http://vivo.cornell.edu/individual/n23?format=rdfxml
     */
    String formatParam = getRequestParameter("format", "");
    if (formatParam.contains("rdfxml")) {
        return ContentType.RDFXML;
    }
    if (formatParam.contains("n3")) {
        return ContentType.N3;
    }
    if (formatParam.contains("ttl")) {
        return ContentType.TURTLE;
    }
    if (formatParam.contains("jsonld") || formatParam.contains("json")) {
        return ContentType.JSON;
    }

    /*
     * Check for parts of URL that indicate request for RDF. Examples:
     * http://vivo.cornell.edu/individual/n23/n23.rdf
     * http://vivo.cornell.edu/individual/n23/n23.n3
     * http://vivo.cornell.edu/individual/n23/n23.ttl
     * http://vivo.cornell.edu/individual/n23/n23.jsonld
     */
    Matcher rdfMatch = RDF_REQUEST.matcher(url);
    if (rdfMatch.matches() && rdfMatch.groupCount() == 2) {
        String rdfType = rdfMatch.group(2);
        if ("rdf".equals(rdfType)) {
            return ContentType.RDFXML;
        }
        if ("n3".equals(rdfType)) {
            return ContentType.N3;
        }
        if ("ttl".equals(rdfType)) {
            return ContentType.TURTLE;
        }
        if ("jsonld".equals(rdfType)) {
            return ContentType.JSON;
        }
    }

    return null;
}

From source file:it.mb.whatshare.SendToGCMActivity.java

private boolean mustIncludeSubject(String subject, String text) {
    if (subject == null)
        return false;
    subject = subject.trim().toLowerCase(Locale.getDefault());
    text = text.trim().toLowerCase(Locale.getDefault());
    if (subject.length() == 0 || subject.equals(text)) {
        return false;
    }/*  w w  w  .  j av a  2 s  .c  om*/
    // test for flipboard
    Matcher matcher = FLIPBOARD_PATTERN.matcher(subject);
    if (matcher.matches() && matcher.groupCount() == 1) {
        String testIncluded = matcher.group(1).trim();
        Utils.debug("testing if '%s' contains '%s'", text, testIncluded);
        return !text.contains(testIncluded);
    }
    return true;
}

From source file:org.apache.wiki.auth.acl.DefaultAclManagerTest.java

public void testAclRegex() {
    String acl;/*  w  ww  . ja v a 2s.com*/
    Matcher m;

    acl = "[{ALLOW view Bob, Alice, Betty}] Test text.";
    m = DefaultAclManager.ACL_PATTERN.matcher(acl);
    assertTrue(m.find());
    assertEquals(2, m.groupCount());
    assertEquals("[{ALLOW view Bob, Alice, Betty}]", m.group(0));
    assertEquals("view", m.group(1));
    assertEquals("Bob, Alice, Betty", m.group(2));
    assertFalse(m.find());

    acl = "[{ALLOW view Alice}] Test text.";
    m = DefaultAclManager.ACL_PATTERN.matcher(acl);
    assertTrue(m.find());
    System.out.println(m.group());
    assertEquals(2, m.groupCount());
    assertEquals("[{ALLOW view Alice}]", m.group(0));
    assertEquals("view", m.group(1));
    assertEquals("Alice", m.group(2));
    assertFalse(m.find());

    acl = "Test text   [{   ALLOW   view   Alice  }]  Test text.";
    m = DefaultAclManager.ACL_PATTERN.matcher(acl);
    assertTrue(m.find());
    System.out.println(m.group());
    assertEquals(2, m.groupCount());
    assertEquals("[{   ALLOW   view   Alice  }]", m.group(0));
    assertEquals("view", m.group(1));
    assertEquals("Alice", m.group(2));
    assertFalse(m.find());

    acl = "Test text   [{   ALLOW   view  Alice  ,  Bob  }]  Test text.";
    m = DefaultAclManager.ACL_PATTERN.matcher(acl);
    assertTrue(m.find());
    System.out.println(m.group());
    assertEquals(2, m.groupCount());
    assertEquals("[{   ALLOW   view  Alice  ,  Bob  }]", m.group(0));
    assertEquals("view", m.group(1));
    assertEquals("Alice  ,  Bob", m.group(2));
    assertFalse(m.find());

    acl = "Test text   [{   ALLOW   view  Alice  ,  Bob  }]  Test text  [{ALLOW edit Betty}].";
    m = DefaultAclManager.ACL_PATTERN.matcher(acl);
    assertTrue(m.find());
    System.out.println(m.group());
    assertEquals(2, m.groupCount());
    assertEquals("[{   ALLOW   view  Alice  ,  Bob  }]", m.group(0));
    assertEquals("view", m.group(1));
    assertEquals("Alice  ,  Bob", m.group(2));
    assertTrue(m.find());
    assertEquals(2, m.groupCount());
    assertEquals("[{ALLOW edit Betty}]", m.group(0));
    assertEquals("edit", m.group(1));
    assertEquals("Betty", m.group(2));
    assertFalse(m.find());
}

From source file:edu.cornell.mannlib.vitro.webapp.controller.individual.IndividualRequestAnalyzer.java

/**
 * Gets the entity id from the request. Works for the following styles of
 * URLs://  w w  w  . ja v  a 2 s .co  m
 * 
 * <pre>
 *     /individual?uri=urlencodedURI
 *     /individual?netId=bdc34
 *     /individual?netid=bdc34
 *     /individual/localname         
 *     /display/localname
 *     /individual/localname/localname.rdf
 *     /individual/localname/localname.n3
 *     /individual/localname/localname.ttl
 *     /individual/localname/localname.jsonld
 * </pre>
 * 
 * @return null on failure.
 */
public Individual getIndividualFromRequest() {
    try {
        // Check for "uri" parameter.
        String uri = getRequestParameter("uri", "");
        if (!uri.isEmpty()) {
            return getIndividualByUri(uri);
        }

        // Check for "netId" or "netid" parameter
        String netId = getRequestParameter("netId", getRequestParameter("netid", ""));
        if (!netId.isEmpty()) {
            return getIndividualByNetId(netId);
        }

        // Check for just a local name
        Matcher linkedDataMatch = LINKED_DATA_URL.matcher(url);
        if (linkedDataMatch.matches() && linkedDataMatch.groupCount() == 1) {
            return getIndividualByLocalname(linkedDataMatch.group(1));
        }

        // Check for the canonical HTML request.
        Matcher htmlMatch = HTML_REQUEST.matcher(url);
        if (htmlMatch.matches() && htmlMatch.groupCount() == 1) {
            return getIndividualByLocalname(htmlMatch.group(1));
        }

        // Check for a request for RDF.
        Matcher rdfMatch = RDF_REQUEST.matcher(url);
        if (rdfMatch.matches() && rdfMatch.groupCount() == 2) {
            return getIndividualByLocalname(rdfMatch.group(1));
        }

        // Couldn't match it to anything.
        return null;
    } catch (Throwable e) {
        log.error("Problems trying to find Individual", e);
        return null;
    }
}

From source file:net.mindengine.blogix.Blogix.java

private Map<String, String> createParametersMap(RouteURL url, String uri) {
    if (!uri.endsWith("/")) {
        uri = uri + "/";
    }/*from   www  . java2  s.co  m*/

    if (url.getParameters() != null && !url.getParameters().isEmpty()) {
        Matcher matcher = url.asRegexPattern().matcher(uri);

        if (matcher.find()) {
            if (matcher.groupCount() >= url.getParameters().size()) {
                Map<String, String> parametersMap = new HashMap<String, String>();
                int i = 0;
                for (String parameter : url.getParameters()) {
                    i++;
                    parametersMap.put(parameter, matcher.group(i));
                }
                return parametersMap;
            }
        }

        throw new IllegalArgumentException("Can't extract controller arguments from uri: " + uri);
    }
    return Collections.emptyMap();
}

From source file:uk.ac.ebi.intact.editor.controller.misc.MyNotesController.java

private String processMacro(String line, Pattern pattern) {
    String macro = line.trim();/*w  w  w.j a v a 2 s .co  m*/

    Matcher matcher = pattern.matcher(macro);

    String outcome = "";

    while (matcher.find()) {
        if (matcher.groupCount() < 3) {
            outcome = "[invalid macro: " + macro + "]";
        } else {
            String macroType = matcher.group(1);
            String macroName = matcher.group(2);
            String macroStatement = matcher.group(3);

            if ("query".equals(macroType)) {
                if (!macroStatement.toLowerCase().startsWith("select")) {
                    outcome = "[only select queries allowed]";
                } else {
                    try {
                        DataModel results = getMyNotesService().createDataModel(macroStatement);

                        QueryMacro queryMacro = new QueryMacro(macroName, macroStatement, results);
                        queryMacros.add(queryMacro);

                        outcome = "<a href=\"" + absoluteContextPath + "/notes/query/" + macroName
                                + "\">[query: " + macroName + "]</a><br/>";
                    } catch (Exception e) {
                        addErrorMessage("Cannot run query: " + macroName, e.getMessage());
                        outcome = "[query cannot be run: " + macroName + "]";
                    }
                }

            } else {
                outcome = "[macro with unexpected type: " + macroName + "]";
                addErrorMessage("Invalid macro", "Macro with unexpected type: " + macroName);
            }
        }
    }

    return outcome;
}

From source file:com.ebay.nest.io.sede.RegexSerDe.java

@Override
public Object deserialize(Writable blob) throws SerDeException {

    Text rowText = (Text) blob;
    Matcher m = inputPattern.matcher(rowText.toString());

    if (m.groupCount() != numColumns) {
        throw new SerDeException("Number of matching groups doesn't match the number of columns");
    }/*from  www .j a  v a 2s. c o m*/

    // If do not match, ignore the line, return a row with all nulls.
    if (!m.matches()) {
        unmatchedRowsCount++;
        if (!alreadyLoggedNoMatch) {
            // Report the row if its the first time
            LOG.warn("" + unmatchedRowsCount + " unmatched rows are found: " + rowText);
            alreadyLoggedNoMatch = true;
        }
        return null;
    }

    // Otherwise, return the row.
    for (int c = 0; c < numColumns; c++) {
        try {
            String t = m.group(c + 1);
            TypeInfo typeInfo = columnTypes.get(c);
            String typeName = typeInfo.getTypeName();

            // Convert the column to the correct type when needed and set in row obj
            if (typeName.equals(serdeConstants.STRING_TYPE_NAME)) {
                row.set(c, t);
            } else if (typeName.equals(serdeConstants.TINYINT_TYPE_NAME)) {
                Byte b;
                b = Byte.valueOf(t);
                row.set(c, b);
            } else if (typeName.equals(serdeConstants.SMALLINT_TYPE_NAME)) {
                Short s;
                s = Short.valueOf(t);
                row.set(c, s);
            } else if (typeName.equals(serdeConstants.INT_TYPE_NAME)) {
                Integer i;
                i = Integer.valueOf(t);
                row.set(c, i);
            } else if (typeName.equals(serdeConstants.BIGINT_TYPE_NAME)) {
                Long l;
                l = Long.valueOf(t);
                row.set(c, l);
            } else if (typeName.equals(serdeConstants.FLOAT_TYPE_NAME)) {
                Float f;
                f = Float.valueOf(t);
                row.set(c, f);
            } else if (typeName.equals(serdeConstants.DOUBLE_TYPE_NAME)) {
                Double d;
                d = Double.valueOf(t);
                row.set(c, d);
            } else if (typeName.equals(serdeConstants.BOOLEAN_TYPE_NAME)) {
                Boolean b;
                b = Boolean.valueOf(t);
                row.set(c, b);
            } else if (typeName.equals(serdeConstants.TIMESTAMP_TYPE_NAME)) {
                Timestamp ts;
                ts = Timestamp.valueOf(t);
                row.set(c, ts);
            } else if (typeName.equals(serdeConstants.DATE_TYPE_NAME)) {
                Date d;
                d = Date.valueOf(t);
                row.set(c, d);
            } else if (typeName.equals(serdeConstants.DECIMAL_TYPE_NAME)) {
                HiveDecimal bd;
                bd = new HiveDecimal(t);
                row.set(c, bd);
            } else if (typeInfo instanceof PrimitiveTypeInfo
                    && ((PrimitiveTypeInfo) typeInfo).getPrimitiveCategory() == PrimitiveCategory.VARCHAR) {
                VarcharTypeParams varcharParams = (VarcharTypeParams) ParameterizedPrimitiveTypeUtils
                        .getTypeParamsFromTypeInfo(typeInfo);
                HiveVarchar hv = new HiveVarchar(t, varcharParams != null ? varcharParams.length : -1);
                row.set(c, hv);
            }
        } catch (RuntimeException e) {
            partialMatchedRowsCount++;
            if (!alreadyLoggedPartialMatch) {
                // Report the row if its the first row
                LOG.warn("" + partialMatchedRowsCount + " partially unmatched rows are found, "
                        + " cannot find group " + c + ": " + rowText);
                alreadyLoggedPartialMatch = true;
            }
            row.set(c, null);
        }
    }
    return row;
}

From source file:org.shredzone.commons.view.manager.ViewPattern.java

/**
 * Resolves a requested URL path. For each placeholder in the view pattern, the
 * placeholder name and its value in the URL path is returned in a map.
 *
 * @param path//from  ww w.j a v  a 2 s.com
 *            the requested URL to be resolved
 * @return Map containing the placeholder names and its values
 */
public Map<String, String> resolve(String path) {
    Matcher m = regEx.matcher(path);
    if (!m.matches()) {
        return null;
    }

    if (m.groupCount() != parameter.size()) {
        throw new IllegalStateException(
                "regex group count " + m.groupCount() + " does not match parameter count " + parameter.size());
    }

    // TODO: only use decode when #encode() was used
    return IntStream.range(0, parameter.size()).collect(HashMap::new,
            (map, ix) -> map.put(parameter.get(ix), PathUtils.decode(m.group(ix + 1))), Map::putAll);
}

From source file:com.thoughtworks.go.domain.materials.Modification.java

public String id(Matcher matcher) {
    return matcher.groupCount() > 0 ? contentsOfFirstGroupThatMatched(matcher) : matcher.group();
}