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);
}
}
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");
}
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);
}
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");
}
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 */
}
}
Aggregations