use of java.util.Scanner in project tomcat by apache.
the class ErrorReportValve method report.
// ------------------------------------------------------ Protected Methods
/**
* Prints out an error report.
*
* @param request The request being processed
* @param response The response being generated
* @param throwable The exception that occurred (which possibly wraps
* a root cause exception
*/
protected void report(Request request, Response response, Throwable throwable) {
int statusCode = response.getStatus();
// and that error has not been reported.
if (statusCode < 400 || response.getContentWritten() > 0 || !response.setErrorReported()) {
return;
}
String message = RequestUtil.filter(response.getMessage());
if (message == null) {
if (throwable != null) {
String exceptionMessage = throwable.getMessage();
if (exceptionMessage != null && exceptionMessage.length() > 0) {
message = RequestUtil.filter((new Scanner(exceptionMessage)).nextLine());
}
}
if (message == null) {
message = "";
}
}
// Do nothing if there is no reason phrase for the specified status code and
// no error message provided
String reason = null;
String description = null;
StringManager smClient = StringManager.getManager(Constants.Package, request.getLocales());
response.setLocale(smClient.getLocale());
try {
reason = smClient.getString("http." + statusCode + ".reason");
description = smClient.getString("http." + statusCode + ".desc");
} catch (Throwable t) {
ExceptionUtils.handleThrowable(t);
}
if (reason == null || description == null) {
if (message.isEmpty()) {
return;
} else {
reason = smClient.getString("errorReportValve.unknownReason");
description = smClient.getString("errorReportValve.noDescription");
}
}
StringBuilder sb = new StringBuilder();
sb.append("<!doctype html><html lang=\"");
sb.append(smClient.getLocale().getLanguage()).append("\">");
sb.append("<head>");
sb.append("<title>");
sb.append(smClient.getString("errorReportValve.statusHeader", String.valueOf(statusCode), reason));
sb.append("</title>");
sb.append("<style type=\"text/css\">");
sb.append(TomcatCSS.TOMCAT_CSS);
sb.append("</style>");
sb.append("</head><body>");
sb.append("<h1>");
sb.append(smClient.getString("errorReportValve.statusHeader", String.valueOf(statusCode), reason)).append("</h1>");
if (isShowReport()) {
sb.append("<hr class=\"line\" />");
sb.append("<p><b>");
sb.append(smClient.getString("errorReportValve.type"));
sb.append("</b> ");
if (throwable != null) {
sb.append(smClient.getString("errorReportValve.exceptionReport"));
} else {
sb.append(smClient.getString("errorReportValve.statusReport"));
}
sb.append("</p>");
if (!message.isEmpty()) {
sb.append("<p><b>");
sb.append(smClient.getString("errorReportValve.message"));
sb.append("</b> ");
sb.append(message).append("</p>");
}
sb.append("<p><b>");
sb.append(smClient.getString("errorReportValve.description"));
sb.append("</b> ");
sb.append(description);
sb.append("</p>");
if (throwable != null) {
String stackTrace = getPartialServletStackTrace(throwable);
sb.append("<p><b>");
sb.append(smClient.getString("errorReportValve.exception"));
sb.append("</b></p><pre>");
sb.append(RequestUtil.filter(stackTrace));
sb.append("</pre>");
int loops = 0;
Throwable rootCause = throwable.getCause();
while (rootCause != null && (loops < 10)) {
stackTrace = getPartialServletStackTrace(rootCause);
sb.append("<p><b>");
sb.append(smClient.getString("errorReportValve.rootCause"));
sb.append("</b></p><pre>");
sb.append(RequestUtil.filter(stackTrace));
sb.append("</pre>");
// In case root cause is somehow heavily nested
rootCause = rootCause.getCause();
loops++;
}
sb.append("<p><b>");
sb.append(smClient.getString("errorReportValve.note"));
sb.append("</b> ");
sb.append(smClient.getString("errorReportValve.rootCauseInLogs"));
sb.append("</p>");
}
sb.append("<hr class=\"line\" />");
}
if (isShowServerInfo()) {
sb.append("<h3>").append(ServerInfo.getServerInfo()).append("</h3>");
}
sb.append("</body></html>");
try {
try {
response.setContentType("text/html");
response.setCharacterEncoding("utf-8");
} catch (Throwable t) {
ExceptionUtils.handleThrowable(t);
if (container.getLogger().isDebugEnabled()) {
container.getLogger().debug("status.setContentType", t);
}
}
Writer writer = response.getReporter();
if (writer != null) {
// If writer is null, it's an indication that the response has
// been hard committed already, which should never happen
writer.write(sb.toString());
response.finishResponse();
}
} catch (IOException e) {
// Ignore
} catch (IllegalStateException e) {
// Ignore
}
}
use of java.util.Scanner in project zookeeper by apache.
the class ReconfigBackupTest method getFileContent.
// upgrade this once we have Google-Guava or Java 7+
public static String getFileContent(File file) throws FileNotFoundException {
Scanner sc = new Scanner(file);
StringBuilder sb = new StringBuilder();
while (sc.hasNextLine()) {
sb.append(sc.nextLine() + "\n");
}
return sb.toString();
}
use of java.util.Scanner in project checkstyle by checkstyle.
the class InputRightCurlyDoWhile method foo4.
public void foo4() {
int prog, user;
prog = (int) (Math.random() * 10) + 1;
Scanner input = new Scanner(System.in, "utf-8");
if (input.hasNextInt()) {
do {
user = input.nextInt();
if (user == prog) {
String.CASE_INSENSITIVE_ORDER.equals("Good!");
} else {
if (user > 0 && user <= 10) {
String.CASE_INSENSITIVE_ORDER.equals("Bad! ");
if (prog < user) {
String.CASE_INSENSITIVE_ORDER.equals("My number is less than yours.");
} else {
String.CASE_INSENSITIVE_ORDER.equals("My number is greater than yours.");
}
} else {
String.CASE_INSENSITIVE_ORDER.equals("Error!");
}
}
} while (user != prog);
} else {
String.CASE_INSENSITIVE_ORDER.equals("Error!");
}
String.CASE_INSENSITIVE_ORDER.equals("Goodbye!");
}
use of java.util.Scanner in project hibernate-orm by hibernate.
the class TestConsole method start.
private void start() {
Scanner scanner = new Scanner(System.in);
PrintStream out = System.out;
while (true) {
out.println("-----------------------------------------------");
out.println("1 - list persons 5 - list addresses");
out.println("2 - list person history 6 - list addresses history");
out.println("3 - new person 7 - new address");
out.println("4 - modify person 8 - modify address");
out.println("9 - get person at revision 10 - get address at revision");
out.println(" 0 - end");
try {
int choice = scanner.nextInt();
scanner.nextLine();
entityManager.getTransaction().begin();
StringBuilder sb;
int personId;
int addressId;
int revision;
switch(choice) {
case 1:
sb = new StringBuilder();
printPersons(sb);
out.println(sb.toString());
break;
case 2:
out.print("Person id: ");
personId = scanner.nextInt();
scanner.nextLine();
sb = new StringBuilder();
printPersonHistory(sb, personId);
out.println(sb.toString());
break;
case 3:
Person p = readNewPerson(out, scanner);
entityManager.persist(p);
break;
case 4:
out.print("Person id: ");
personId = scanner.nextInt();
scanner.nextLine();
readModifyPerson(out, scanner, personId);
break;
case 5:
sb = new StringBuilder();
printAddresses(sb);
out.println(sb.toString());
break;
case 6:
out.print("Address id: ");
addressId = scanner.nextInt();
scanner.nextLine();
sb = new StringBuilder();
printAddressHistory(sb, addressId);
out.println(sb.toString());
break;
case 7:
Address a = readNewAddress(out, scanner);
entityManager.persist(a);
break;
case 8:
out.print("Address id: ");
addressId = scanner.nextInt();
scanner.nextLine();
readModifyAddress(out, scanner, addressId);
break;
case 9:
out.print("Person id: ");
personId = scanner.nextInt();
scanner.nextLine();
out.print("Revision number: ");
revision = scanner.nextInt();
scanner.nextLine();
if (revision <= 0) {
System.out.println("Revision must be greater then 0!");
continue;
}
sb = new StringBuilder();
printPersonAtRevision(sb, personId, revision);
out.println(sb.toString());
break;
case 10:
out.print("Address id: ");
addressId = scanner.nextInt();
scanner.nextLine();
out.print("Revision number: ");
revision = scanner.nextInt();
scanner.nextLine();
if (revision <= 0) {
System.out.println("Revision must be greater then 0!");
continue;
}
sb = new StringBuilder();
printAddressAtRevision(sb, addressId, revision);
out.println(sb.toString());
break;
case 0:
return;
}
} catch (InputMismatchException e) {
// continuing
} finally {
entityManager.getTransaction().commit();
}
}
}
use of java.util.Scanner in project jmonkeyengine by jMonkeyEngine.
the class MTLLoader method load.
@SuppressWarnings("empty-statement")
public Object load(AssetInfo info) throws IOException {
reset();
this.key = info.getKey();
this.assetManager = info.getManager();
folderName = info.getKey().getFolder();
matList = new MaterialList();
InputStream in = null;
try {
in = info.openStream();
scan = new Scanner(in);
scan.useLocale(Locale.US);
while (readLine()) ;
} finally {
if (in != null) {
in.close();
}
}
if (matName != null) {
// still have a material in the vars
createMaterial();
resetMaterial();
}
MaterialList list = matList;
return list;
}
Aggregations