001: package org.gui4j.exception;
002:
003: import java.util.ArrayList;
004: import java.util.HashSet;
005: import java.util.Iterator;
006: import java.util.List;
007: import java.util.Locale;
008: import java.util.Set;
009:
010: import org.apache.commons.logging.Log;
011: import org.apache.commons.logging.LogFactory;
012:
013: import org.gui4j.util.DefaultTranslator;
014: import org.gui4j.util.Translator;
015:
016: public abstract class Gui4jUncheckedException extends RuntimeException
017: implements ErrorTags {
018: private final String mErrorTag;
019: private final Object[] mErrorArgs;
020:
021: protected static final Log mLogger = LogFactory
022: .getLog(Gui4jUncheckedException.class);
023: protected static final Translator defaultTranslator = new DefaultTranslator(
024: Locale.US, Gui4jUncheckedException.class, "errorMessages");
025:
026: public String getMessage() {
027: return getMessage(defaultTranslator);
028: }
029:
030: public String getMessage(Locale locale) {
031: Translator translator = new DefaultTranslator(locale,
032: getClass(), "errorMessages");
033: return getMessage(translator);
034: }
035:
036: public String getMessage(Translator translator) {
037: return getContext(translator)
038: + translator.translate(mErrorTag, mErrorArgs);
039: }
040:
041: public String getContext(Translator translator) {
042: return "";
043: }
044:
045: protected Gui4jUncheckedException(String errorTag,
046: Object[] errorArgs, Throwable e) {
047: super (errorTag, e);
048: mErrorTag = errorTag;
049: mErrorArgs = errorArgs;
050: }
051:
052: public static class InternalError extends Gui4jUncheckedException {
053: protected InternalError(String errorTag, Object[] errorArgs,
054: Throwable e) {
055: super (errorTag, errorArgs, e);
056: }
057:
058: protected void printLogMessage() {
059: mLogger.warn("Exception created: " + getMessage());
060: }
061: }
062:
063: public static class ErrorList extends InternalError {
064: private final List errorList;
065:
066: public ErrorList(List errorList) {
067: super ("error_list", new String[] { getMessage(errorList) },
068: null);
069: this .errorList = new ArrayList(errorList);
070: }
071:
072: public List getErrorList() {
073: return errorList;
074: }
075:
076: private static String getMessage(List errorList) {
077: StringBuffer str = new StringBuffer();
078: Set alreadyAdded = new HashSet();
079: add(str, errorList, alreadyAdded);
080: return str.toString();
081: }
082:
083: private static void add(StringBuffer str, List errorList,
084: Set alreadyAdded) {
085: for (Iterator it = errorList.iterator(); it.hasNext();) {
086: Throwable t = (Throwable) it.next();
087: if (t instanceof ErrorList) {
088: ErrorList errorListExc = (ErrorList) t;
089: add(str, errorListExc.getErrorList(), alreadyAdded);
090: } else {
091: String message = t.getMessage();
092: if (message == null) {
093: message = t.getClass().getName();
094: }
095: if (!alreadyAdded.contains(message)) {
096: str.append("\n --> ");
097: str.append(message);
098: alreadyAdded.add(message);
099: }
100: }
101: }
102: }
103: }
104:
105: public static class ProgrammingError extends InternalError {
106: public ProgrammingError(String errorTag) {
107: super (errorTag, null, null);
108: printLogMessage();
109: }
110:
111: public ProgrammingError(String errorTag, Throwable e) {
112: super (errorTag, null, e);
113: printLogMessage();
114: }
115:
116: public ProgrammingError(String errorTag, Object[] args) {
117: super (errorTag, args, null);
118: printLogMessage();
119: }
120:
121: public String getContext(Translator translator) {
122: return translator.translate(PROGRAMMING_ERROR, null);
123: }
124: }
125:
126: public static class ResourceError extends InternalError {
127: private final String mRessourceName;
128: private final int mLineNumber;
129:
130: public ResourceError(String ressourceName, int lineNumber,
131: String errorTag) {
132: super (errorTag, null, null);
133: mRessourceName = ressourceName;
134: mLineNumber = lineNumber;
135: printLogMessage();
136: }
137:
138: public ResourceError(String ressourceName, int lineNumber,
139: String errorTag, Object[] args) {
140: super (errorTag, args, null);
141: mRessourceName = ressourceName;
142: mLineNumber = lineNumber;
143: printLogMessage();
144: }
145:
146: public ResourceError(String ressourceName, int lineNumber,
147: String errorTag, Object[] args, Throwable e) {
148: super (errorTag, args, e);
149: mRessourceName = ressourceName;
150: mLineNumber = lineNumber;
151: printLogMessage();
152: }
153:
154: public ResourceError(String ressourceName, int lineNumber,
155: String errorTag, Throwable e) {
156: super (errorTag, null, e);
157: mRessourceName = ressourceName;
158: mLineNumber = lineNumber;
159: printLogMessage();
160: }
161:
162: public String getContext(Translator translator) {
163: if (mLineNumber == -1) {
164: Object[] args = { mRessourceName };
165: return translator.translate(RESOURCE_ERROR, args);
166: } else {
167: Object[] args = { mRessourceName + ":" + mLineNumber };
168: return translator.translate(RESOURCE_ERROR, args);
169: }
170: }
171: }
172: }
|