Search in sources :

Example 31 with MatchResult

use of java.util.regex.MatchResult in project gradle by gradle.

the class FindBrokenInternalLinks method hasDeadLink.

private void hasDeadLink(File baseDir, File sourceFile, Map<File, List<Error>> errors) {
    int lineNumber = 0;
    List<Error> errorsForFile = new ArrayList<>();
    try (BufferedReader br = new BufferedReader(new FileReader(sourceFile))) {
        String line = br.readLine();
        while (line != null) {
            lineNumber++;
            Matcher matcher = linkPattern.matcher(line);
            while (matcher.find()) {
                MatchResult xrefMatcher = matcher.toMatchResult();
                String link = xrefMatcher.group(1);
                if (link.contains("#")) {
                    Matcher linkMatcher = linkWithHashPattern.matcher(link);
                    if (linkMatcher.matches()) {
                        MatchResult result = linkMatcher.toMatchResult();
                        String fileName = getFileName(result.group(1), sourceFile);
                        File referencedFile = new File(baseDir, fileName);
                        if (!referencedFile.exists() || referencedFile.isDirectory()) {
                            errorsForFile.add(new Error(lineNumber, line, "Looking for file named " + fileName));
                        } else {
                            String idName = result.group(2);
                            if (idName.isEmpty()) {
                                errorsForFile.add(new Error(lineNumber, line, "Missing section reference for link to " + fileName));
                            } else {
                                if (!fileContainsText(referencedFile, "[[" + idName + "]]")) {
                                    errorsForFile.add(new Error(lineNumber, line, "Looking for section named " + idName + " in " + fileName));
                                }
                            }
                        }
                    }
                } else {
                    if (!fileContainsText(sourceFile, "[[" + link + "]]")) {
                        errorsForFile.add(new Error(lineNumber, line, "Looking for section named " + link + " in " + sourceFile.getName()));
                    }
                }
            }
            line = br.readLine();
        }
    } catch (IOException e) {
        throw new UncheckedIOException(e);
    }
    if (!errorsForFile.isEmpty()) {
        errors.put(sourceFile, errorsForFile);
    }
}
Also used : Matcher(java.util.regex.Matcher) ArrayList(java.util.ArrayList) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader) UncheckedIOException(org.gradle.api.UncheckedIOException) UncheckedIOException(org.gradle.api.UncheckedIOException) IOException(java.io.IOException) MatchResult(java.util.regex.MatchResult) File(java.io.File) OutputFile(org.gradle.api.tasks.OutputFile)

Example 32 with MatchResult

use of java.util.regex.MatchResult in project AutoRefactor by JnRouvignac.

the class CFGBuilder method getLineAndColumn.

private LineAndColumn getLineAndColumn(final int position) {
    // TODO Use CompilationUnit.getLineNumber() and
    // CompilationUnit.getColumnNumber()
    // Return SourceLocation class with also startNodePosition to be used for graph
    // node names
    // line number and column number are then used as comments for the node
    // file starts with line 1
    int lineNo = 1;
    int lastMatchPosition = 0;
    Matcher matcher = NEWLINE.matcher(source);
    while (matcher.find()) {
        MatchResult matchResult = matcher.toMatchResult();
        if (matchResult.end() >= position) {
            String startOfLine = this.source.substring(lastMatchPosition, position);
            int nbChars = countCharacters(startOfLine, tabSize);
            // + 1 because line starts with column 1
            return new LineAndColumn(position, lineNo, nbChars + 1);
        }
        lastMatchPosition = matchResult.end();
        ++lineNo;
    }
    // $NON-NLS-1$
    throw new IllegalStateException(null, "A line and column number should have been found");
}
Also used : IllegalStateException(org.autorefactor.util.IllegalStateException) Matcher(java.util.regex.Matcher) MatchResult(java.util.regex.MatchResult)

Example 33 with MatchResult

use of java.util.regex.MatchResult in project riposte by Nike-Inc.

the class ParserTest method test_regex_parser_parse_fails.

@Test
public void test_regex_parser_parse_fails() throws ParserFailure {
    Parser<MatchResult> parser = regex("^.*(test).*$");
    Throwable ex = catchThrowable(() -> parser.parse("this should not work"));
    assertThat(ex).isInstanceOf(ParserFailure.class);
}
Also used : Assertions.catchThrowable(org.assertj.core.api.Assertions.catchThrowable) MatchResult(java.util.regex.MatchResult) Test(org.junit.Test)

Example 34 with MatchResult

use of java.util.regex.MatchResult in project riposte by Nike-Inc.

the class ParserTest method test_regex_parser_parse_works.

@Test
public void test_regex_parser_parse_works() throws ParserFailure {
    Parser<MatchResult> parser = regex("^.*(test).*$");
    MatchResult successResult = parser.parse("this test should work");
    assertThat(successResult.groupCount()).isEqualTo(1);
    assertThat(successResult.group(1)).isEqualTo("test");
}
Also used : MatchResult(java.util.regex.MatchResult) Test(org.junit.Test)

Example 35 with MatchResult

use of java.util.regex.MatchResult in project omegat by omegat-org.

the class ScriptItem method scanFileForDescription.

private void scanFileForDescription(File file) {
    try (Scanner scan = new Scanner(file, StandardCharsets.UTF_8.name())) {
        scan.findInLine(":name\\s*=\\s*(.*)\\s+:description\\s*=\\s*(.*)");
        MatchResult results = scan.match();
        mScriptName = results.group(1).trim();
        mDescription = results.group(2).trim();
    } catch (IllegalStateException e) {
    /* bad luck */
    } catch (FileNotFoundException e) {
    /* ignore - it should not happen here */
    }
}
Also used : Scanner(java.util.Scanner) FileNotFoundException(java.io.FileNotFoundException) MatchResult(java.util.regex.MatchResult)

Aggregations

MatchResult (java.util.regex.MatchResult)62 Matcher (java.util.regex.Matcher)26 Pattern (java.util.regex.Pattern)16 Scanner (java.util.Scanner)11 Point (java.awt.Point)5 Test (org.junit.Test)5 ArrayList (java.util.ArrayList)4 IOException (java.io.IOException)3 NoSuchElementException (java.util.NoSuchElementException)3 XMLStreamException (javax.xml.stream.XMLStreamException)3 MatcherState (com.github.anba.es6draft.regexp.MatcherState)2 ArrayObject (com.github.anba.es6draft.runtime.types.builtins.ArrayObject)2 InputStream (java.io.InputStream)2 Fault (org.apache.cxf.interceptor.Fault)2 CachedOutputStream (org.apache.cxf.io.CachedOutputStream)2 IterableMatchResult (com.github.anba.es6draft.regexp.IterableMatchResult)1 MatcherResult (com.github.anba.es6draft.regexp.MatcherResult)1 ScriptObject (com.github.anba.es6draft.runtime.types.ScriptObject)1 OrdinaryObject (com.github.anba.es6draft.runtime.types.builtins.OrdinaryObject)1 ImmutableMap (com.google.common.collect.ImmutableMap)1