X-Git-Url: https://code.wpia.club/?p=gigi.git;a=blobdiff_plain;f=src%2Forg%2Fcacert%2Fgigi%2FGigiApiException.java;h=7b3a990938ad0df650ec2b9c5079124f6d7286ee;hp=7b91d125fb1d200a4e1a822ab7dac70bbf3a693a;hb=f0409c63fad3833d4a2d4d8c3fd60f0ab829b299;hpb=9b8c6af2b684cee31142449955b83ae66cb9ab34 diff --git a/src/org/cacert/gigi/GigiApiException.java b/src/org/cacert/gigi/GigiApiException.java index 7b91d125..7b3a9909 100644 --- a/src/org/cacert/gigi/GigiApiException.java +++ b/src/org/cacert/gigi/GigiApiException.java @@ -1,57 +1,113 @@ 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; - LinkedList messages = new LinkedList<>(); - - public GigiApiException(SQLException e) { - super(e); - this.e = e; - } - - public GigiApiException(String message) { - super(message); - messages.add(message); - } - - public GigiApiException() { - - } - - public void mergeInto(GigiApiException e2) { - messages.addAll(e2.messages); - if (e == null) { - e = e2.e; - } - } - - public boolean isInternalError() { - return e != null; - } - - public void format(PrintWriter out, Language language) { - out.println("
"); - if (isInternalError()) { - e.printStackTrace(); - out.print("
"); - out.println(language.getTranslation("An internal error ouccured.")); - out.println("
"); - } - for (String message : messages) { - out.print("
"); - out.print(language.getTranslation(message)); - out.println("
"); - } - out.println("
"); - - } - - public boolean isEmpty() { - return e == null && messages.size() == 0; - } + + private static final Language PLAIN_LANGUAGE = Language.getInstance(Locale.ENGLISH); + + private static final long serialVersionUID = -164928670180852588L; + + private SQLException e; + + private LinkedList messages = new LinkedList<>(); + + public GigiApiException(SQLException e) { + super(e); + this.e = e; + } + + public GigiApiException(String message) { + super(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) { + e = e2.e; + } + } + + public boolean isInternalError() { + return e != null; + } + + public void format(PrintWriter out, Language language) { + out.println("
"); + if (isInternalError()) { + e.printStackTrace(); + out.print("

"); + out.println(language.getTranslation("An internal error occurred.")); + out.println("

"); + } + HashMap map = new HashMap<>(); + for (Outputable message : messages) { + map.clear(); + + out.print("

"); + 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; + } + + @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 ""; + } }