root / branches / v2_0_0_prep / libraries / libTools / src / org / gvsig / tools / exception / BaseException.java @ 23150
History | View | Annotate | Download (9.36 KB)
1 |
package org.gvsig.tools.exception; |
---|---|
2 |
|
3 |
import java.lang.reflect.Method; |
4 |
import java.util.Iterator; |
5 |
import java.util.Map; |
6 |
|
7 |
/**
|
8 |
*
|
9 |
* Esta clase esta pensada para actuar como clase base para las excepciones que
|
10 |
* se lanzan dentro del proyecto de gvSIG.
|
11 |
*
|
12 |
* A?ade la implementacion necesaria para disponer de mensajes de error
|
13 |
* internacionalizables, a traves del metodo getLocalizedMessage, asi como una
|
14 |
* serie de metodos que nos permiten obtener los mesanes de error de la cadena
|
15 |
* de excepciones enlazadas a traves de su "causa", asi como utilidades que
|
16 |
* permitan recorrer de forma comoda esta cadena de excepciones por medio de un
|
17 |
* Iterador.
|
18 |
*
|
19 |
* @author Equipo de desarrollo de gvSIG.
|
20 |
*
|
21 |
*/
|
22 |
public abstract class BaseException extends Exception implements IBaseException { |
23 |
private final static String BLANKS =" "; |
24 |
private static IExceptionTranslator translator= null; |
25 |
|
26 |
protected String messageKey; |
27 |
|
28 |
/**
|
29 |
* TODO: remove the variable, use the Exception get/setMessage() instead.
|
30 |
*/
|
31 |
protected String formatString; |
32 |
|
33 |
/**
|
34 |
* Unique code of error.
|
35 |
*/
|
36 |
protected long code; |
37 |
|
38 |
/**
|
39 |
* Empty constructor, don't use it anymore.
|
40 |
*
|
41 |
* @deprecated
|
42 |
*/
|
43 |
public BaseException() {
|
44 |
} |
45 |
|
46 |
/**
|
47 |
* Constructs a BaseException with a default message format, a key to find a
|
48 |
* localized message format, and a unique code to identify the exception.
|
49 |
*
|
50 |
* @param message
|
51 |
* the default messageFormat to describe the exception
|
52 |
* @param key
|
53 |
* the key to use to search a localized messageFormnata
|
54 |
* @param code
|
55 |
* the unique code to identify the exception
|
56 |
*/
|
57 |
public BaseException(String message, String key, long code) { |
58 |
super(message);
|
59 |
this.formatString = message;
|
60 |
this.messageKey = key;
|
61 |
this.code = code;
|
62 |
} |
63 |
|
64 |
/**
|
65 |
* Constructs a BaseException with a default message format, a key to find a
|
66 |
* localized message format, and a unique code to identify the exception.
|
67 |
*
|
68 |
* @param message
|
69 |
* the default messageFormat to describe the exception
|
70 |
* @param cause
|
71 |
* the original cause of the exception
|
72 |
* @param key
|
73 |
* the key to use to search a localized messageFormnata
|
74 |
* @param code
|
75 |
* the unique code to identify the exception
|
76 |
*/
|
77 |
public BaseException(String message, Throwable cause, String key, long code) { |
78 |
super(message, cause);
|
79 |
this.formatString = message;
|
80 |
this.messageKey = key;
|
81 |
this.code = code;
|
82 |
} |
83 |
|
84 |
/**
|
85 |
* Returns the format string received in the parameter with its keys
|
86 |
* replaced with the corresponding values of the map.
|
87 |
*
|
88 |
* @param formatString
|
89 |
* @param values
|
90 |
* map
|
91 |
* @return string formatted
|
92 |
*/
|
93 |
private String format(String formatString, Map values) { |
94 |
if (values != null) { |
95 |
|
96 |
// If there is no message format, create a text with the values.
|
97 |
if (formatString == null) { |
98 |
return "values = ".concat(values.toString()); |
99 |
} else {
|
100 |
// Replace the keys as variables with the values in the Map
|
101 |
Iterator keys = values.keySet().iterator();
|
102 |
String message = formatString;
|
103 |
while (keys.hasNext()) {
|
104 |
String key = (String) keys.next(); |
105 |
String varName = "%\\(".concat(key).concat("\\)"); |
106 |
message = message.replaceAll(varName, (String) values
|
107 |
.get(key)); |
108 |
} |
109 |
return message;
|
110 |
} |
111 |
} |
112 |
// Return the original format message in any other case
|
113 |
return formatString;
|
114 |
} |
115 |
|
116 |
/* (non-Javadoc)
|
117 |
* @see java.lang.Throwable#getMessage()
|
118 |
*/
|
119 |
public String getMessage() { |
120 |
return format(this.formatString, values()); |
121 |
} |
122 |
|
123 |
/* (non-Javadoc)
|
124 |
* @see org.gvsig.exceptions.IBaseException#getMessage(int)
|
125 |
*/
|
126 |
public String getMessage(int indent) { |
127 |
return insertBlanksAtStart(format(formatString, values()),indent);
|
128 |
} |
129 |
|
130 |
/* (non-Javadoc)
|
131 |
* @see java.lang.Throwable#getLocalizedMessage()
|
132 |
*/
|
133 |
public String getLocalizedMessage() { |
134 |
return getLocalizedMessage(translator,0); |
135 |
} |
136 |
|
137 |
/* (non-Javadoc)
|
138 |
* @see org.gvsig.exceptions.IBaseException#getLocalizedMessage(org.gvsig.exceptions.IExceptionTranslator, int)
|
139 |
*/
|
140 |
public String getLocalizedMessage(IExceptionTranslator translator, int indent){ |
141 |
|
142 |
String fmt;
|
143 |
if (translator == null){ |
144 |
translator = BaseException.translator; |
145 |
} |
146 |
if (translator == null){ |
147 |
fmt = getFormatString(); |
148 |
} else {
|
149 |
fmt = getMessageKey(); |
150 |
if (fmt == null){ |
151 |
fmt = getFormatString(); |
152 |
} else {
|
153 |
fmt = translator.getText(fmt); |
154 |
} |
155 |
} |
156 |
return insertBlanksAtStart(format(fmt,values()),indent);
|
157 |
} |
158 |
|
159 |
/* (non-Javadoc)
|
160 |
* @see org.gvsig.exceptions.IBaseException#getMessageStack()
|
161 |
*/
|
162 |
public String getMessageStack() { |
163 |
return getMessageStack(0); |
164 |
} |
165 |
|
166 |
/* (non-Javadoc)
|
167 |
* @see org.gvsig.exceptions.IBaseException#getMessageStack(int)
|
168 |
*/
|
169 |
public String getMessageStack(int indent) { |
170 |
Iterator iter = this.iterator(); |
171 |
StringBuffer msgBuffer = new StringBuffer(); |
172 |
int i = 1; |
173 |
while (iter.hasNext()){
|
174 |
Exception ex = ((Exception) iter.next()); |
175 |
|
176 |
if (msgBuffer.length() > 0) { |
177 |
msgBuffer.append("\n");
|
178 |
} |
179 |
|
180 |
if ( ex instanceof BaseException ) { |
181 |
BaseException bex = (BaseException) ex; |
182 |
msgBuffer.append(bex.getMessage(indent * i)); |
183 |
} else {
|
184 |
msgBuffer.append(insertBlanksAtStart(ex.getMessage(), indent |
185 |
* i)); |
186 |
} |
187 |
|
188 |
i++; |
189 |
} |
190 |
return msgBuffer.toString();
|
191 |
} |
192 |
|
193 |
|
194 |
/* (non-Javadoc)
|
195 |
* @see org.gvsig.exceptions.IBaseException#getLocalizedMessageStack()
|
196 |
*/
|
197 |
public String getLocalizedMessageStack() { |
198 |
return getLocalizedMessageStack(BaseException.translator,0); |
199 |
} |
200 |
|
201 |
/* (non-Javadoc)
|
202 |
* @see org.gvsig.exceptions.IBaseException#getLocalizedMessageStack(org.gvsig.exceptions.IExceptionTranslator, int)
|
203 |
*/
|
204 |
public String getLocalizedMessageStack(IExceptionTranslator translator, int indent) { |
205 |
Iterator iter = this.iterator(); |
206 |
StringBuffer msgBuffer = new StringBuffer(); |
207 |
Exception ex;
|
208 |
while (iter.hasNext()) {
|
209 |
ex = ((Exception) iter.next());
|
210 |
if (msgBuffer.length() > 0) { |
211 |
msgBuffer.append("\n");
|
212 |
} |
213 |
|
214 |
if (ex instanceof BaseException) { |
215 |
BaseException bex = (BaseException) ex; |
216 |
msgBuffer.append(bex.getLocalizedMessage(translator, indent)); |
217 |
} else {
|
218 |
msgBuffer.append(ex.getLocalizedMessage()); |
219 |
} |
220 |
} |
221 |
return msgBuffer.toString();
|
222 |
} |
223 |
|
224 |
/**
|
225 |
* Inserts blanks at the start of a string.
|
226 |
*
|
227 |
* @param str A string.
|
228 |
* @param len Quantity of blanks to insert at the start of str.
|
229 |
* @return A string compund by the quantity of blanks that
|
230 |
* len indicates and str.
|
231 |
*/
|
232 |
static String insertBlanksAtStart(String str, int len){ |
233 |
int blanksLen = len > BLANKS.length() ? BLANKS.length() : (len < 0 ? 0 |
234 |
: len); |
235 |
|
236 |
return BLANKS.substring(0, blanksLen) + str; |
237 |
} |
238 |
|
239 |
/* (non-Javadoc)
|
240 |
* @see org.gvsig.exceptions.IBaseException#getCode()
|
241 |
*/
|
242 |
public long getCode() { |
243 |
return this.code; |
244 |
} |
245 |
|
246 |
/**
|
247 |
* Sets the exception's code.
|
248 |
*/
|
249 |
public void setCode(long code) { |
250 |
this.code = code;
|
251 |
} |
252 |
|
253 |
/* (non-Javadoc)
|
254 |
* @see org.gvsig.exceptions.IBaseException#getFormatString()
|
255 |
*/
|
256 |
public String getFormatString() { |
257 |
return this.formatString; |
258 |
} |
259 |
|
260 |
/**
|
261 |
* Sets the format string.
|
262 |
*
|
263 |
* @param formatString
|
264 |
*/
|
265 |
public void setFormatString(String formatString) { |
266 |
this.formatString = formatString;
|
267 |
} |
268 |
|
269 |
/* (non-Javadoc)
|
270 |
* @see org.gvsig.exceptions.IBaseException#getMessageKey()
|
271 |
*/
|
272 |
public String getMessageKey() { |
273 |
return this.messageKey; |
274 |
} |
275 |
|
276 |
/**
|
277 |
* Sets the property messageKey.
|
278 |
*
|
279 |
* @param messageKey
|
280 |
*/
|
281 |
public void setMessageKey(String messageKey) { |
282 |
this.messageKey = messageKey;
|
283 |
} |
284 |
|
285 |
/* (non-Javadoc)
|
286 |
* @see org.gvsig.exceptions.IBaseException#iterator()
|
287 |
*/
|
288 |
public Iterator iterator() { |
289 |
return new BaseExceptionIterator(this); |
290 |
} |
291 |
|
292 |
/**
|
293 |
* @return A map that serves to replace in the format string
|
294 |
* the keys with the corresponding values.
|
295 |
*/
|
296 |
abstract protected Map values(); |
297 |
|
298 |
/**
|
299 |
* Sets the property translator.
|
300 |
* @param translator It(He,She) is used to translate
|
301 |
* the messages associated with the exceptions.
|
302 |
*/
|
303 |
public static void setTranslator(IExceptionTranslator translator){ |
304 |
BaseException.translator = translator; |
305 |
} |
306 |
|
307 |
public static void setTranslator(Object translator){ |
308 |
BaseException.translator = new TranslatorWraper(translator);
|
309 |
} |
310 |
|
311 |
public String toString(){ |
312 |
return format(this.formatString, values()); |
313 |
} |
314 |
|
315 |
} |
316 |
|
317 |
class TranslatorWraper implements IExceptionTranslator { |
318 |
|
319 |
private Object translator = null; |
320 |
private Method method = null; |
321 |
|
322 |
public TranslatorWraper(Object translator) { |
323 |
Class theClass = translator.getClass();
|
324 |
String s = ""; |
325 |
|
326 |
this.translator = translator;
|
327 |
try {
|
328 |
method = theClass.getMethod("getText",new Class[] { s.getClass() }); |
329 |
} catch (Exception e) { |
330 |
throw new RuntimeException("El objeto translator suministrado no tiene el metodo getText apropiado.", e); |
331 |
} |
332 |
|
333 |
} |
334 |
|
335 |
public String getText(String key) { |
336 |
try {
|
337 |
return (String)(method.invoke(translator,new String[] { key })); |
338 |
} catch (Exception e) { |
339 |
return key;
|
340 |
} |
341 |
} |
342 |
|
343 |
} |