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:hu.bme.mit.sette.common.model.snippet.Snippet.java

/**
 * Parses a method which should be considered in coverage.
 *
 * @param includedClass//from www  . jav  a 2s  .  com
 *            the Java class of the method
 * @param includedMethodString
 *            the string representing the included method
 * @param v
 *            a {@link MethodValidator}
 * @param classLoader
 *            the class loader for loading snippet project classes
 */
private void parseIncludedMethod(final Class<?> includedClass, final String includedMethodString,
        final MethodValidator v, final ClassLoader classLoader) {
    Matcher matcher = Snippet.METHOD_STRING_PATTERN.matcher(includedMethodString);

    if (!matcher.matches() || matcher.groupCount() != 2) {
        // invalid method string
        String message = String.format("The included method string must match " + "the required format.\n"
                + "(includedMethodString: [%s])", includedMethodString);
        v.addException(message);
    } else {
        // valid method string
        String includedMethodName = matcher.group(1).trim();
        String[] paramTypeStrings = StringUtils.split(matcher.group(2), ',');
        Class<?>[] paramTypes = new Class<?>[paramTypeStrings.length];
        boolean isConstructor = includedMethodName.equals(includedClass.getSimpleName());
        boolean shouldAdd = true; // only add if there was no problem with
        // the parameters

        // check parameter types
        for (int i = 0; i < paramTypes.length; i++) {
            String parameterTypeString = paramTypeStrings[i].trim();

            if (StringUtils.isBlank(parameterTypeString)) {
                // blank parameter type string
                String message = String.format("The included method string has " + "a blank parameter type.\n"
                        + "(includedMethodString: [%s])\n" + "(index: [%d])", includedMethodString, i);
                v.addException(message);
                shouldAdd = false;
            } else {
                try {
                    paramTypes[i] = ClassUtils.getClass(classLoader, parameterTypeString);
                } catch (ClassNotFoundException e) {
                    // parameter type was not found
                    String format = "The parameter type in " + "the included method string "
                            + "could not have been loaded.\n" + "(includedMethodString: [%s])\n"
                            + "(index: [%d])";
                    String message = String.format(format, includedMethodString, i);
                    v.addException(message);
                    shouldAdd = false;
                }
            }
        }

        if (shouldAdd) {
            // get included method object
            if (isConstructor) {
                try {
                    // only search declared constructors
                    Constructor<?> found = includedClass.getDeclaredConstructor(paramTypes);
                    addIncludedConstructor(found, v);
                } catch (NoSuchMethodException e) {
                    String format = "Included constructor cannot be found "
                            + "(it must be declared in the class)\n" + "(includedClass: [%s])\n"
                            + "(includedMethodString: [%s])";
                    String message = String.format(format, includedClass, includedMethodString);
                    v.addException(message);
                }
            } else {
                try {
                    // only search declared methods
                    Method found = includedClass.getDeclaredMethod(includedMethodName, paramTypes);
                    addIncludedMethod(found, v);
                } catch (NoSuchMethodException e) {
                    String format = "Included method cannot be found " + "(it must be declared in the class)\n"
                            + "(includedClass: [%s])\n" + "(includedMethodString: [%s])";
                    String message = String.format(format, includedClass, includedMethodString);
                    v.addException(message, e);
                }
            }
        }
    }
}

From source file:edu.mit.mobile.android.locast.data.interfaces.LocatableUtils.java

public Cursor queryByLocation(SQLiteQueryBuilder qb, SQLiteDatabase db, String locString,
        String locatableItemTable, String[] projection, String selection, String[] selectionArgs,
        String sortOrder) {/*from  w w w .  jav  a2  s  .c  o m*/

    qb.setTables(locatableItemTable);
    final Matcher m = LOC_STRING_REGEX.matcher(locString);
    if (!m.matches()) {
        throw new IllegalArgumentException("bad location string '" + locString + "'");
    }
    final String lon = m.group(1);
    final String lat = m.group(2);
    String dist = "1500";
    if (m.groupCount() == 3) {
        dist = m.group(3);
    }

    final String cell = GeocellUtils.compute(new Point(Double.valueOf(lat), Double.valueOf(lon)), 8);

    final List<String> adjacent = GeocellUtils.allAdjacents(cell);

    adjacent.add(cell);

    final StringBuilder selSb = new StringBuilder();

    boolean join = false;
    for (int i = 0; i < adjacent.size(); i++) {
        if (join) {
            selSb.append(" OR ");
        } else {
            join = true;
        }

        selSb.append(Locatable.COL_GEOCELL);
        selSb.append(" LIKE ? || '%'");

    }

    final String selectionExtra = selSb.toString();

    return qb.query(db, projection, ProviderUtils.addExtraWhere(selection, selectionExtra),
            ProviderUtils.addExtraWhereArgs(selectionArgs, adjacent.toArray(new String[] {})), null, null,
            sortOrder);

    // String extraWhere =
    // "(lat - 2) > ? AND (lon - 2) > ? AND (lat + 2) < ? AND (lat + 2) < ?";

    // final String[] extraArgs = {lat, lon};
    // return qb.query(db, projection, ProviderUtils.addExtraWhere(selection,
    // LocatableUtils.SELECTION_LAT_LON), ProviderUtils.addExtraWhereArgs(selectionArgs,
    // extraArgs),
    // null, null, sortOrder);
}

From source file:org.apache.tajo.storage.regex.RegexLineDeserializer.java

@Override
public void deserialize(final ByteBuf lineBuf, Tuple output) throws IOException, TextLineParsingError {

    if (lineBuf == null || targetColumnIndexes.length == 0) {
        return;/*from   ww  w  . j  a v  a 2s  .  co m*/
    }

    String line = decoder.decode(lineBuf.nioBuffer(lineBuf.readerIndex(), lineBuf.readableBytes())).toString();
    int[] projection = targetColumnIndexes;

    // Projection
    int currentTarget = 0;
    int currentIndex = 0;
    Matcher m = inputPattern.matcher(line);

    if (!m.matches()) {
        unmatchedRows++;
        if (unmatchedRows >= nextUnmatchedRows) {
            nextUnmatchedRows *= 100;
            // Report the row
            LOG.warn("" + unmatchedRows + " unmatched rows are found: " + line);
        }
    } else {

        int groupCount = m.groupCount();
        int currentGroup = 1;
        while (currentGroup <= groupCount) {

            if (projection.length > currentTarget && currentIndex == projection[currentTarget]) {

                try {
                    Datum datum = fieldSerDer.deserialize(currentIndex,
                            lineBuf.setIndex(m.start(currentGroup), m.end(currentGroup)), nullChars);

                    output.put(currentTarget, datum);
                } catch (Exception e) {
                    partialMatchedRows++;
                    if (partialMatchedRows >= nextPartialMatchedRows) {
                        nextPartialMatchedRows *= 100;
                        // Report the row
                        LOG.warn("" + partialMatchedRows + " partially unmatched rows are found, "
                                + " cannot find group " + currentIndex + ": " + line);
                    }
                    output.put(currentTarget, NullDatum.get());
                }
                currentTarget++;
            }

            if (projection.length == currentTarget) {
                break;
            }

            currentIndex++;
            currentGroup++;
        }
    }

    /* If a text row is less than table schema size, tuple should set to NullDatum */
    if (projection.length > currentTarget) {
        for (; currentTarget < projection.length; currentTarget++) {
            output.put(currentTarget, NullDatum.get());
        }
    }
}

From source file:org.apache.zeppelin.submarine.hadoop.HdfsClient.java

public String parseText(String text) {
    String script = "", intpText = "";

    // parse text to get interpreter component
    if (text != null) {
        Matcher matcher = REPL_PATTERN.matcher(text);
        if (matcher.matches()) {
            String headingSpace = matcher.group(1);
            intpText = matcher.group(2);

            if (matcher.groupCount() == 3 && matcher.group(3) != null) {
                String localPropertiesText = matcher.group(3);
                String[] splits = localPropertiesText.substring(1, localPropertiesText.length() - 1).split(",");
                for (String split : splits) {
                    String[] kv = split.split("=");
                    if (StringUtils.isBlank(split) || kv.length == 0) {
                        continue;
                    }//from  w  w w.java 2s . c  o  m
                    if (kv.length > 2) {
                        throw new RuntimeException("Invalid paragraph properties format: " + split);
                    }
                }
                script = text
                        .substring(headingSpace.length() + intpText.length() + localPropertiesText.length() + 1)
                        .trim();
            } else {
                script = text.substring(headingSpace.length() + intpText.length() + 1).trim();
            }
        } else {
            script = text.trim();
        }
    }

    return script;
}

From source file:de.unirostock.sems.cbarchive.web.HttpImporter.java

private File downloadFile() throws ImporterException {

    try {/* w w w .jav  a2 s  . c o m*/
        tempFile = File.createTempFile(Fields.TEMP_FILE_PREFIX, ".omex");
        HttpResponse getResponse = client.execute(new HttpGet(remoteUrl));

        // check if file exists
        if (getResponse.getStatusLine().getStatusCode() != 200) {
            LOGGER.warn(getResponse.getStatusLine().getStatusCode(), " ",
                    getResponse.getStatusLine().getReasonPhrase(), " while download ", remoteUrl);
            throw new ImporterException(String.valueOf(getResponse.getStatusLine().getStatusCode()) + " "
                    + getResponse.getStatusLine().getReasonPhrase() + " while download");
        }

        HttpEntity entity = getResponse.getEntity();
        if (entity == null) {
            LOGGER.error("No content returned while donwloading remote file ", remoteUrl);
            throw new ImporterException("No content returned while donwloading remote file " + remoteUrl);
        }

        // for name suggestions
        Header dispositionHeader = getResponse.getFirstHeader("Content-Disposition");
        if (dispositionHeader != null && dispositionHeader.getValue() != null
                && dispositionHeader.getValue().isEmpty() == false) {
            // disposition header is present -> extract name
            // inline; filename=\"{0}.{1}\"
            Matcher matcher = Pattern
                    .compile("filename=\\\"?(([a-zA-Z0-9-_\\+]+).(\\w+))\\\"?", Pattern.CASE_INSENSITIVE)
                    .matcher(dispositionHeader.getValue());
            if (matcher.find()) {
                suggestedName = matcher.group(1);
                for (int i = 0; i < matcher.groupCount(); i++)
                    LOGGER.debug(i, ": ", matcher.group(i));
            }
        } else {
            // when not -> take the last part of the url
            Matcher matcher = Pattern.compile("\\/(([a-zA-Z0-9-_\\+]+).(\\w+))$", Pattern.CASE_INSENSITIVE)
                    .matcher(remoteUrl);
            if (matcher.find()) {
                suggestedName = matcher.group(1);
                for (int i = 0; i < matcher.groupCount(); i++)
                    LOGGER.debug(i, ": ", matcher.group(i));
            }

        }

        // download it
        OutputStream output = new FileOutputStream(tempFile);
        IOUtils.copy(entity.getContent(), output);

        // check against quota
        if (length != tempFile.length()) {
            LOGGER.warn("Content-Length (", length, ") and downloaded length (", tempFile.length(),
                    ") are different.");
            length = tempFile.length();

            checkQuotas();
        }

        return tempFile;

    } catch (IOException e) {
        LOGGER.error(e, "Exception while download file from ", remoteUrl);
        throw new ImporterException("Exception while download remote file", e);
    }
}

From source file:org.andrewberman.sync.PDFSearcher.java

private GetMethod scrapeImpl(String curURL, String href, int curDepth, int maxDepth) throws Exception {
    waitOrExit();//from w  w  w  . j  a  v a  2s. c  o  m
    Thread.sleep(InheritMe.BETWEEN_WEB_SCRAPING_SLEEP_TIME);

    if (skipToNext) {
        skipToNext = false;
        status("Timed out.");
        debug("Timed out.");
        Thread.sleep(1500);
        throw new Exception();
    }

    /*
     * Capture the DOI if it exists.
     */
    if (doiString == null && curURL.contains("doi")) {
        Matcher doiM = doiExtractor.matcher(curURL);
        if (doiM.find()) {
            doiString = doiM.group(doiM.groupCount());
        }
    }

    status("Searching..." + repeat(".", curDepth * 2));
    curDepth += 1;

    // URI uri = new URI(curURL, false);
    curURL = escapeURL(curURL);
    debug(repeat(" ", curDepth * 2) + curURL);
    GetMethod get = new GetMethod(curURL);
    get.setFollowRedirects(true);
    get.setDoAuthentication(true);
    httpclient.executeMethod(get);

    /*
     * Check the response for the PDF type. If so, then we've found the PDF.
     */
    Header h = get.getResponseHeader("Content-type");
    // System.out.println(h.getValue());
    String type = h.getValue().toLowerCase();
    if (type.contains("pdf")) {
        if (!hasProblem(curURL, "")) {
            return get;
        }
    }

    /*
     * So it's not a PDF, let's get the content and search for more potential PDF links.
     */
    String content = readInputStream(get.getResponseBodyAsStream());
    get.releaseConnection();

    /*
     * Match it against the potentially useful links.
     */
    for (int i = 0; i < patterns.size(); i++) {
        Pattern p = patterns.get(i);
        Matcher m = p.matcher(content);
        boolean foundAnything = false;
        while (m.find()) {
            foundAnything = true;
            String s = m.group(m.groupCount());

            /*
             * Create a new URI from this link string.
             */
            s = relativeToAbsoluteURL(get.getURI().toString(), s);

            /*
             * GJ 2009-02-20. Remove the "+html" from annoying PNAS links (yes, it's super-specific, but it must be done!)
             */
            s = removeHTMLFromLink(s);

            /*
             * Send the current URL and the potential match to a separate method for a chance to opt-out in special cases.
             */
            if (hasProblem(curURL, s))
                continue;

            if (curDepth < maxDepth) {
                /*
                 * Recurse.
                 */
                GetMethod g = scrapeImpl(s, "", curDepth, maxDepth);
                if (g != null)
                    return g;
            }

            Thread.sleep(400);
        }
        // if (foundAnything)
        // break;
    }
    return null;
}

From source file:geva.Mapper.DerivationTree.java

/** Extract range for value from non-terminal specification,
 * where specification is in the format <GECodonValue{x, y, z}> (set-mode)
 * or <GECodonValue(x, y)> (range-mode -- range can be open, closed, or half-open).
 * @param s symbol string/*from ww w .j a  va 2 s. co  m*/
 * @param codon codon value
 * @return string value
 * @throws MalformedGrammarException The specification s is malformed
 */
String getGECodonValue(String s, int codon) throws MalformedGrammarException {

    String c = Constants.GE_CODON_VALUE_PARSING;
    if (s.indexOf("{") != -1) {
        // in "set"-mode: uses set-notation {x, y, z}
        if (s.indexOf("}") == -1) {
            throw new MalformedGrammarException("Bad GECodonValue specification: " + s);
        }

        String GECodonValueRE = c + "\\{(.*)\\}" + ">";
        Pattern pattern = Pattern.compile(GECodonValueRE);
        Matcher matcher = pattern.matcher(s);
        if (matcher.find()) {
            if (matcher.groupCount() == 1) {
                String content = matcher.group(1);
                String[] setValues = content.split(",");

                // Success: found good values in set-mode. Return.
                return setValues[codon % setValues.length].trim();
            } else {
                throw new MalformedGrammarException(
                        "Bad GECodonValue specification (found too many matches): " + s);
            }
        } else {
            throw new MalformedGrammarException("Bad GECodonValue specification (didn't find a match): " + s);
        }
    }

    else if (s.indexOf("(") != -1 || s.indexOf("[") != -1) {
        // in "range"-mode: uses open, closed, or half-open ranges:
        // [x, y], (x, y), [x, y), or (x, y]
        if (s.indexOf(")") == -1 && s.indexOf("]") == -1) {
            throw new MalformedGrammarException(
                    "Bad GECodonValue specification (didn't find closing brackets): " + s);
        }

        // first group gets lower-boundary indicator ( or [;
        // second group gets content, eg 4.1, 4.9;
        // third group gets upper-boundary indicator ) or ].
        String GECodonValueRE = c + "([\\(\\[])(.*)([\\)\\]])" + ">";
        Pattern pattern = Pattern.compile(GECodonValueRE);
        Matcher matcher = pattern.matcher(s);
        String content;
        String[] boundaryValues;
        if (matcher.find()) {
            //       for (int i = 0; i <= matcher.groupCount(); i++) {
            //           System.out.println("debug: i = " + i + "; group = " + matcher.group(i));
            //       }

            if (matcher.groupCount() != 3) {
                throw new MalformedGrammarException(
                        "Bad GECodonValue specification (didn't find opening and closing brackets and boundaries): "
                                + s);
            }

            // Get the text
            content = matcher.group(2);
            boundaryValues = content.split(",");
            if (boundaryValues.length != 2) {
                throw new MalformedGrammarException(
                        "Bad GECodonValue specification (didn't find upper and lower boundaries): " + s);
            }
        } else {
            throw new MalformedGrammarException("Bad GECodonValue specification (didn't find a match): " + s);
        }

        int low, high;
        double lowd, highd;
        boolean intMode;
        try {
            intMode = true;
            low = Integer.parseInt(boundaryValues[0].trim());
            high = Integer.parseInt(boundaryValues[1].trim());
            // dummy values, these won't be used
            lowd = 0.0;
            highd = 0.0;
        } catch (Exception e) {
            intMode = false;
            lowd = Double.parseDouble(boundaryValues[0].trim());
            highd = Double.parseDouble(boundaryValues[1].trim());
            low = 0;
            high = genny.getMaxCodonValue();
        }

        if (matcher.group(1).equals("(")) {
            // it's an open range: exclude the low value
            // System.out.println("range semi-open at the bottom");
            low += 1;
            // FIXME what a hack
            lowd += 0.0001;
        }
        if (matcher.group(3).equals(")")) {
            // it's an open range: exclude the high value
            // System.out.println("range semi-open at the top");
            high -= 1;
            // FIXME what a hack
            highd -= 0.0001;
        }

        if (intMode == true) {
            if (low > high) {
                throw new MalformedGrammarException("Bad GECodonValue specification (bad boundaries): " + s);
            }

            // Success: found good boundaries in integer mode: return.
            return String.valueOf(low + (codon % (high - low + 1)));
        } else {
            if (lowd > highd) {
                throw new MalformedGrammarException("Bad GECodonValue specification (bad boundaries): " + s);
            }
            // Success: found good boundaries in double-mode: return.
            return String
                    .valueOf(lowd + (highd - lowd) * (codon) / Double.valueOf((genny.getMaxCodonValue() - 0)));
        }

    } else {
        // may be in legacy mode
        logger.warn("Warning: DerivationTree attempting to use GECodonValue legacy format");
        return getGECodonValueLegacyFormat(s, codon);
    }
}

From source file:com.microsoft.tfs.client.common.ui.helpers.HTMLIncludeHelper.java

/**
 * <p>/*from   w  w w .  jav a 2s.  co m*/
 * Transforms the given line, including referenced localized messages by
 * delegating to the {@link HTMLIncludeResourceProvider}.
 * </p>
 *
 * <p>
 * Lines with text matching: %%%MESSAGE(key)%%% will be transformed by
 * replacing the line with the results of the
 * {@link HTMLIncludeResourceProvider}'s response to the given key.
 *
 * <p>
 * Example:
 * </p>
 *
 * <p>
 * %%%MESSAGE(ClassName.MessageKey)%%% will include the given message for
 * the key ClassName.MessageKey.
 * </p>
 *
 * @param input
 *        An input line from a resource
 * @return The line with any message statements transformed.
 * @throws IOException
 *         If any included resources could not be read
 */
private String transformMessages(final String input) throws IOException {
    Check.notNull(input, "input"); //$NON-NLS-1$

    final Matcher messageMatcher = messagePattern.matcher(input);
    final StringBuffer transformation = new StringBuffer();

    while (messageMatcher.find()) {
        String replacement = ""; //$NON-NLS-1$

        if (messageMatcher.groupCount() == 1) {
            replacement = resourceProvider.getMessage(messageMatcher.group(1));
        } else {
            log.warn(MessageFormat.format("Could not transform message constant {0}: no messages class defined", //$NON-NLS-1$
                    messageMatcher.group(0)));
        }

        messageMatcher.appendReplacement(transformation, replacement);
    }

    messageMatcher.appendTail(transformation);

    return transformation.toString();
}

From source file:net.kautler.teamcity.sourceforge.SourceForgeIssueProvider.java

@NotNull
@Override//  w ww  .  ja v a 2s  .  c  o  m
protected String extractId(@NotNull String match) {
    Matcher matcher = myPattern.matcher(match);
    if (!matcher.matches()) {
        throw new AssertionError(
                format("Match '%s' should match the pattern '%s', but does not", match, myPattern));
    }
    // use the first group if present, or the whole match otherwise
    if (matcher.groupCount() > 0) {
        return matcher.group(1);
    } else {
        return matcher.group();
    }
}

From source file:com.jkoolcloud.tnt4j.streams.parsers.ActivityRegExParser.java

/**
 * Gets field raw data value resolved by locator.
 *
 * @param locator/*from  w  ww. j  a v  a  2  s .c o m*/
 *            activity field locator
 * @param cData
 *            RegEx data package - {@link Matcher} or {@link ArrayList} of matches
 * @param formattingNeeded
 *            flag to set if value formatting is not needed
 * @return raw value resolved by locator, or {@code null} if value is not resolved
 */
@Override
@SuppressWarnings("unchecked")
protected Object resolveLocatorValue(ActivityFieldLocator locator, ActivityContext cData,
        AtomicBoolean formattingNeeded) {
    Object val = null;
    String locStr = locator.getLocator();

    if (StringUtils.isNotEmpty(locStr)) {
        if (cData.containsKey(MATCHES_KEY)) {
            ArrayList<String> matches = (ArrayList<String>) cData.get(MATCHES_KEY);
            int loc = Integer.parseInt(locStr);

            if (loc >= 0 && loc < matches.size()) {
                val = matches.get(loc);
            }
        } else {
            Matcher matcher = (Matcher) cData.getData();
            ActivityFieldLocatorType locType = locator.getBuiltInType();

            if (locType != null && locType.getDataType() == Integer.class) {
                int loc = Integer.parseInt(locStr);
                if (loc >= 0 && loc <= matcher.groupCount()) {
                    val = matcher.group(loc);
                }
            } else {
                val = matcher.group(locStr);
            }
        }
    }

    return val;
}