X-Git-Url: https://code.wpia.club/?a=blobdiff_plain;f=src%2Forg%2Fcacert%2Fgigi%2FGigiApiException.java;h=c2a60e69fc6a47137738177973a20cf16a7a86f5;hb=63a050b637aef27c32d84a2a62b91cd4b8276398;hp=a79877396061d9134fdbc8e16eb0145a6c923a5b;hpb=d895448cb685adc4c2bfac8d92759252d2ce8c36;p=gigi.git diff --git a/src/org/cacert/gigi/GigiApiException.java b/src/org/cacert/gigi/GigiApiException.java index a7987739..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 { + 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) { @@ -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 ""; + } + }