use of java.util.Scanner in project sonarqube by SonarSource.
the class ScannerReportViewerApp method updateSource.
private void updateSource(Component component) {
File sourceFile = reader.getFileStructure().fileFor(Domain.SOURCE, component.getRef());
sourceEditor.setText("");
if (sourceFile.exists()) {
try (Scanner s = new Scanner(sourceFile, StandardCharsets.UTF_8.name()).useDelimiter("\\Z")) {
if (s.hasNext()) {
sourceEditor.setText(s.next());
}
} catch (IOException ex) {
StringWriter errors = new StringWriter();
ex.printStackTrace(new PrintWriter(errors));
sourceEditor.setText(errors.toString());
}
}
}
use of java.util.Scanner in project visualee by Thomas-S-B.
the class Examiner method getClassBody.
protected static String getClassBody(String sourceCode) {
StringBuilder classBody = new StringBuilder();
boolean isInBodyNow = false;
try (Scanner scanner = new Scanner(sourceCode)) {
scanner.useDelimiter("[\n]+");
while (scanner.hasNext()) {
String token = scanner.next();
if (!isInBodyNow) {
if (token.contains("class ")) {
isInBodyNow = true;
}
} else {
classBody.append(token).append("\n");
}
}
}
return classBody.toString();
}
use of java.util.Scanner in project visualee by Thomas-S-B.
the class ExaminerTest method testScanAfterClosedParenthesisInsufficientTokens.
@Test(expected = IllegalArgumentException.class)
public void testScanAfterClosedParenthesisInsufficientTokens() {
JavaSource javaSource;
String sourceCode;
Scanner scanner;
String currentToken;
javaSource = JavaSourceFactory.getInstance().newJavaSource("TestClass");
sourceCode = "@NotNull(groups";
javaSource.setSourceCode(sourceCode);
scanner = Examiner.getSourceCodeScanner(javaSource.getSourceCode());
// now @NotNull((groups
currentToken = scanner.next();
ExaminerImpl.scanAfterClosedParenthesis(currentToken, scanner);
}
use of java.util.Scanner in project visualee by Thomas-S-B.
the class ExaminerTest method testJumpOverTokenInsufficientTokens.
@Test(expected = IllegalArgumentException.class)
public void testJumpOverTokenInsufficientTokens() {
JavaSource javaSource;
String sourceCode;
Scanner scanner;
String currentToken;
javaSource = JavaSourceFactory.getInstance().newJavaSource("TestClass");
sourceCode = "public";
javaSource.setSourceCode(sourceCode);
scanner = Examiner.getSourceCodeScanner(javaSource.getSourceCode());
// now public
currentToken = scanner.next();
ExaminerImpl.jumpOverJavaToken(currentToken, scanner);
}
use of java.util.Scanner in project visualee by Thomas-S-B.
the class ExaminerTest method testScanAfterQuote.
@Test
public void testScanAfterQuote() {
JavaSource javaSource;
String sourceCode;
String actual;
String expected;
Scanner scanner;
String currentToken;
javaSource = JavaSourceFactory.getInstance().newJavaSource("TestClass");
sourceCode = "out.println(\"<h1>UserTransaction obtained using @Inject</h1>\");\n" + "mytoken";
javaSource.setSourceCode(sourceCode);
scanner = Examiner.getSourceCodeScanner(javaSource.getSourceCode());
// now out.println(\"<h1>UserTransaction
currentToken = scanner.next();
ExaminerImpl.scanAfterQuote(currentToken, scanner);
expected = "mytoken";
actual = scanner.next();
assertEquals(expected, actual);
javaSource = JavaSourceFactory.getInstance().newJavaSource("TestClass");
sourceCode = "out.println(\"<title>UserTransaction obtained using @Inject</title>\");\n" + "out.println(\"<h1>UserTransaction obtained using @Inject</h1>);\n" + "mytoken";
javaSource.setSourceCode(sourceCode);
scanner = Examiner.getSourceCodeScanner(javaSource.getSourceCode());
// now out.println(\"<h1>UserTransaction\"
currentToken = scanner.next();
ExaminerImpl.scanAfterQuote(currentToken, scanner);
expected = "out.println(\"<h1>UserTransaction";
actual = scanner.next();
assertEquals(expected, actual);
javaSource = JavaSourceFactory.getInstance().newJavaSource("TestClass");
sourceCode = "@NotNull((groups = PersistenceConstraint.class) saddas)\n" + "private Album2 album;\n";
javaSource.setSourceCode(sourceCode);
scanner = Examiner.getSourceCodeScanner(javaSource.getSourceCode());
// now @NotNull((groups
currentToken = scanner.next();
ExaminerImpl.scanAfterQuote(currentToken, scanner);
expected = "=";
actual = scanner.next();
assertEquals(expected, actual);
javaSource = JavaSourceFactory.getInstance().newJavaSource("TestClass");
sourceCode = "LOG.log(Level.FINE, \"Added {0} to watch channel {1}\", new Object[]{browserWindow.hashCode(), browserWindow.getChannel()});";
javaSource.setSourceCode(sourceCode);
scanner = Examiner.getSourceCodeScanner(javaSource.getSourceCode());
//LOG.log(Level.FINE,
scanner.next();
//"Added...
currentToken = scanner.next();
ExaminerImpl.scanAfterQuote(currentToken, scanner);
expected = "new";
actual = scanner.next();
assertEquals(expected, actual);
}
Aggregations