X-Git-Url: https://code.wpia.club/?a=blobdiff_plain;f=src%2Forg%2Fcacert%2Fgigi%2FGigiApiException.java;h=0d600b973f3284fa17fe8861e51398148b9aa448;hb=08f417851b48202af9f3a9b6254ac4d1c18262cb;hp=e766d9ea9e6e56b50fa87ec69c61924da05f4c7a;hpb=7fd647e95b1505fe2e0b111a121818213f60e2b0;p=gigi.git diff --git a/src/org/cacert/gigi/GigiApiException.java b/src/org/cacert/gigi/GigiApiException.java index e766d9ea..0d600b97 100644 --- a/src/org/cacert/gigi/GigiApiException.java +++ b/src/org/cacert/gigi/GigiApiException.java @@ -1,16 +1,25 @@ package org.cacert.gigi; import java.io.PrintWriter; +import java.io.StringWriter; import java.sql.SQLException; +import java.util.HashMap; import java.util.LinkedList; +import java.util.Locale; import org.cacert.gigi.localisation.Language; +import org.cacert.gigi.output.template.Outputable; +import org.cacert.gigi.output.template.TranslateCommand; public class GigiApiException extends Exception { + private static final Language PLAIN_LANGUAGE = Language.getInstance(Locale.ENGLISH); + + private static final long serialVersionUID = -164928670180852588L; + private SQLException e; - private LinkedList messages = new LinkedList<>(); + private LinkedList messages = new LinkedList<>(); public GigiApiException(SQLException e) { super(e); @@ -19,13 +28,17 @@ public class GigiApiException extends Exception { public GigiApiException(String message) { super(message); - messages.add(message); + messages.add(new TranslateCommand(message)); } public GigiApiException() { } + public GigiApiException(Outputable out) { + messages.add(out); + } + public void mergeInto(GigiApiException e2) { messages.addAll(e2.messages); if (e == null) { @@ -42,18 +55,39 @@ public class GigiApiException extends Exception { if (isInternalError()) { e.printStackTrace(); out.print("
"); - out.println(language.getTranslation("An internal error ouccured.")); + out.println(language.getTranslation("An internal error occurred.")); out.println("
"); } - for (String message : messages) { + HashMap map = new HashMap<>(); + for (Outputable message : messages) { + map.clear(); + out.print("
"); - out.print(language.getTranslation(message)); + message.output(out, language, map); out.println("
"); } out.println(""); } + public void formatPlain(PrintWriter out) { + if (isInternalError()) { + out.println(PLAIN_LANGUAGE.getTranslation("An internal error occurred.")); + } + HashMap map = new HashMap<>(); + for (Outputable message : messages) { + if (message instanceof TranslateCommand) { + String m = ((TranslateCommand) message).getRaw(); + // Skip HTML Entities + out.println(PLAIN_LANGUAGE.getTranslation(m)); + } else { + map.clear(); + message.output(out, PLAIN_LANGUAGE, map); + out.println(); + } + } + } + public boolean isEmpty() { return e == null && messages.size() == 0; } @@ -61,11 +95,17 @@ public class GigiApiException extends Exception { @Override public String getMessage() { if (messages.size() != 0) { - StringBuffer res = new StringBuffer(); - for (String string : messages) { - res.append(string + "\n"); + StringWriter sw = new StringWriter(); + PrintWriter pw = new PrintWriter(sw); + + HashMap map = new HashMap<>(); + for (Outputable message : messages) { + map.clear(); + message.output(pw, PLAIN_LANGUAGE, map); } - return res.toString(); + pw.flush(); + + return sw.toString(); } return ""; }