X-Git-Url: https://code.wpia.club/?p=gigi.git;a=blobdiff_plain;f=src%2Forg%2Fcacert%2Fgigi%2FGigiApiException.java;h=c2a60e69fc6a47137738177973a20cf16a7a86f5;hp=8851ec4412efaf4f848a4ed142de11344bbc1291;hb=f718e460ae93ab692b817359e7b79696387e8dd1;hpb=480cb29387c76ccc19f8fa8fb0abe8ae1b069730 diff --git a/src/org/cacert/gigi/GigiApiException.java b/src/org/cacert/gigi/GigiApiException.java index 8851ec44..c2a60e69 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 { - SQLException e; + private static final Language PLAIN_LANGUAGE = Language.getInstance(Locale.ENGLISH); - LinkedList messages = new LinkedList<>(); + private static final long serialVersionUID = -164928670180852588L; + + private SQLException e; + + 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) { @@ -45,17 +58,56 @@ public class GigiApiException extends Exception { out.println(language.getTranslation("An internal error ouccured.")); 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 ouccured.")); + } + 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; } + @Override + public String getMessage() { + if (messages.size() != 0) { + 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); + } + pw.flush(); + + return sw.toString(); + } + return ""; + } + }