svn-gvsig-desktop / trunk / libraries / libExceptions / src / org / gvsig / exceptions / BaseException.java @ 10245
History | View | Annotate | Download (6.79 KB)
1 |
package org.gvsig.exceptions; |
---|---|
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
|
10 |
* las excepciones que se lanzan dentro del proyecto de gvSIG.
|
11 |
*
|
12 |
* A?ade la implementacion necesaria para disponer de mensajes
|
13 |
* de error internacionalizables, a traves del metodo
|
14 |
* getLocalizedMessage, asi como una serie de metodos que nos
|
15 |
* permiten obtener los mesanes de error de la cadena de excepciones
|
16 |
* enlazadas a traves de su "causa", asi como utilidades que
|
17 |
* permitan recorrer de forma comoda esta cadena de excepciones
|
18 |
* por medio de un Iterador.
|
19 |
*
|
20 |
* @author Equipo de desarrollo de gvSIG.
|
21 |
*
|
22 |
*/
|
23 |
public abstract class BaseException extends Exception implements IBaseException { |
24 |
private final static String BLANKS =" "; |
25 |
private static IExceptionTranslator translator= null; |
26 |
|
27 |
protected String messageKey; |
28 |
|
29 |
protected String formatString; |
30 |
|
31 |
/**
|
32 |
* Unique code of error.
|
33 |
*/
|
34 |
protected long code; |
35 |
|
36 |
/**
|
37 |
* Returns the format string received in the parameter
|
38 |
* with its keys replaced with the corresponding values of the map.
|
39 |
*
|
40 |
* @param formatString
|
41 |
* @param values map
|
42 |
* @return string formatted
|
43 |
*/
|
44 |
private String format(String formatString, Map values) { |
45 |
String key;
|
46 |
String ret = formatString;
|
47 |
Iterator keys = values.keySet().iterator();
|
48 |
while (keys.hasNext()){
|
49 |
key = (String) keys.next();
|
50 |
ret = ret.replaceAll("%\\("+key+"\\)s", (String)values.get(key)); |
51 |
} |
52 |
return ret;
|
53 |
} |
54 |
|
55 |
|
56 |
/* (non-Javadoc)
|
57 |
* @see java.lang.Throwable#getMessage()
|
58 |
*/
|
59 |
public String getMessage() { |
60 |
return format(this.formatString, values()); |
61 |
} |
62 |
|
63 |
/* (non-Javadoc)
|
64 |
* @see org.gvsig.exceptions.IBaseException#getMessage(int)
|
65 |
*/
|
66 |
public String getMessage(int indent) { |
67 |
return insertBlanksAtStart(format(formatString, values()),indent);
|
68 |
} |
69 |
|
70 |
/* (non-Javadoc)
|
71 |
* @see java.lang.Throwable#getLocalizedMessage()
|
72 |
*/
|
73 |
public String getLocalizedMessage() { |
74 |
return getLocalizedMessage(translator,0); |
75 |
} |
76 |
|
77 |
/* (non-Javadoc)
|
78 |
* @see org.gvsig.exceptions.IBaseException#getLocalizedMessage(org.gvsig.exceptions.IExceptionTranslator, int)
|
79 |
*/
|
80 |
public String getLocalizedMessage(IExceptionTranslator translator, int indent){ |
81 |
|
82 |
String fmt;
|
83 |
if (translator == null){ |
84 |
translator = BaseException.translator; |
85 |
} |
86 |
if (translator == null){ |
87 |
fmt = getFormatString(); |
88 |
} else {
|
89 |
fmt = getMessageKey(); |
90 |
if (fmt == null){ |
91 |
fmt = getFormatString(); |
92 |
} else {
|
93 |
fmt = translator.getText(fmt); |
94 |
} |
95 |
} |
96 |
return insertBlanksAtStart(format(fmt,values()),indent);
|
97 |
} |
98 |
|
99 |
/* (non-Javadoc)
|
100 |
* @see org.gvsig.exceptions.IBaseException#getMessageStack()
|
101 |
*/
|
102 |
public String getMessageStack() { |
103 |
return getMessageStack(0); |
104 |
} |
105 |
|
106 |
/* (non-Javadoc)
|
107 |
* @see org.gvsig.exceptions.IBaseException#getMessageStack(int)
|
108 |
*/
|
109 |
public String getMessageStack(int indent) { |
110 |
Iterator iter = this.iterator(); |
111 |
String msg=""; |
112 |
String msg1;
|
113 |
Exception ex;
|
114 |
int i = 1; |
115 |
while (iter.hasNext()){
|
116 |
ex = ((Exception)iter.next());
|
117 |
if ( ex instanceof BaseException ) { |
118 |
BaseException bex = (BaseException) ex; |
119 |
msg1 = bex.getMessage(indent*i); |
120 |
} else {
|
121 |
msg1 = insertBlanksAtStart(ex.getMessage(),indent*i); |
122 |
} |
123 |
if(msg1!=null && !msg1.equals("")){ |
124 |
msg = msg + msg1 + "\n";
|
125 |
} |
126 |
i++; |
127 |
} |
128 |
return msg;
|
129 |
} |
130 |
|
131 |
|
132 |
/* (non-Javadoc)
|
133 |
* @see org.gvsig.exceptions.IBaseException#getLocalizedMessageStack()
|
134 |
*/
|
135 |
public String getLocalizedMessageStack() { |
136 |
return getLocalizedMessageStack(BaseException.translator,0); |
137 |
} |
138 |
|
139 |
/* (non-Javadoc)
|
140 |
* @see org.gvsig.exceptions.IBaseException#getLocalizedMessageStack(org.gvsig.exceptions.IExceptionTranslator, int)
|
141 |
*/
|
142 |
public String getLocalizedMessageStack(IExceptionTranslator translator, int indent) { |
143 |
Iterator iter = this.iterator(); |
144 |
String msg=""; |
145 |
Exception ex;
|
146 |
while (iter.hasNext()){
|
147 |
ex = ((Exception)iter.next());
|
148 |
if ( ex instanceof BaseException ) { |
149 |
BaseException bex = (BaseException) ex; |
150 |
msg = msg + bex.getLocalizedMessage(translator,indent) + "\n";
|
151 |
} else {
|
152 |
msg = msg + ex.getLocalizedMessage()+ "\n";
|
153 |
} |
154 |
} |
155 |
return msg;
|
156 |
} |
157 |
|
158 |
/**
|
159 |
* Inserts blanks at the start of a string.
|
160 |
*
|
161 |
* @param str A string.
|
162 |
* @param len Quantity of blanks to insert at the start of str.
|
163 |
* @return A string compund by the quantity of blanks that
|
164 |
* len indicates and str.
|
165 |
*/
|
166 |
static String insertBlanksAtStart(String str, int len){ |
167 |
try {
|
168 |
return BLANKS.substring(0,len)+str; |
169 |
} catch (IndexOutOfBoundsException e) { |
170 |
return BLANKS + str;
|
171 |
} |
172 |
} |
173 |
|
174 |
/* (non-Javadoc)
|
175 |
* @see org.gvsig.exceptions.IBaseException#getCode()
|
176 |
*/
|
177 |
public long getCode() { |
178 |
return this.code; |
179 |
} |
180 |
|
181 |
/**
|
182 |
* Sets the exception's code.
|
183 |
*/
|
184 |
public void setCode(long code) { |
185 |
this.code = code;
|
186 |
} |
187 |
|
188 |
/* (non-Javadoc)
|
189 |
* @see org.gvsig.exceptions.IBaseException#getFormatString()
|
190 |
*/
|
191 |
public String getFormatString() { |
192 |
return this.formatString; |
193 |
} |
194 |
|
195 |
/**
|
196 |
* Sets the format string.
|
197 |
*
|
198 |
* @param formatString
|
199 |
*/
|
200 |
public void setFormatString(String formatString) { |
201 |
this.formatString = formatString;
|
202 |
} |
203 |
|
204 |
/* (non-Javadoc)
|
205 |
* @see org.gvsig.exceptions.IBaseException#getMessageKey()
|
206 |
*/
|
207 |
public String getMessageKey() { |
208 |
return this.messageKey; |
209 |
} |
210 |
|
211 |
/**
|
212 |
* Sets the property messageKey.
|
213 |
*
|
214 |
* @param messageKey
|
215 |
*/
|
216 |
public void setMessageKey(String messageKey) { |
217 |
this.messageKey = messageKey;
|
218 |
} |
219 |
|
220 |
/* (non-Javadoc)
|
221 |
* @see org.gvsig.exceptions.IBaseException#iterator()
|
222 |
*/
|
223 |
public Iterator iterator() { |
224 |
return new BaseExceptionIterator(this); |
225 |
} |
226 |
|
227 |
/**
|
228 |
* @return A map that serves to replace in the format string
|
229 |
* the keys with the corresponding values.
|
230 |
*/
|
231 |
abstract protected Map values(); |
232 |
|
233 |
/**
|
234 |
* Sets the property translator.
|
235 |
* @param translator It(He,She) is used to translate
|
236 |
* the messages associated with the exceptions.
|
237 |
*/
|
238 |
public static void setTranslator(IExceptionTranslator translator){ |
239 |
BaseException.translator = translator; |
240 |
} |
241 |
|
242 |
public static void setTranslator(Object translator){ |
243 |
BaseException.translator = new TranslatorWraper(translator);
|
244 |
} |
245 |
|
246 |
public String toString(){ |
247 |
return format(this.formatString, values()); |
248 |
} |
249 |
|
250 |
} |
251 |
|
252 |
class TranslatorWraper implements IExceptionTranslator { |
253 |
|
254 |
private Object translator = null; |
255 |
private Method method = null; |
256 |
|
257 |
public TranslatorWraper(Object translator) { |
258 |
Class theClass = translator.getClass();
|
259 |
String s = ""; |
260 |
|
261 |
this.translator = translator;
|
262 |
try {
|
263 |
method = theClass.getMethod("getText",new Class[] { s.getClass() }); |
264 |
} catch (Exception e) { |
265 |
throw new RuntimeException("El objeto translator suministrado no tiene el metodo getText apropiado.", e); |
266 |
} |
267 |
|
268 |
} |
269 |
|
270 |
public String getText(String key) { |
271 |
try {
|
272 |
return (String)(method.invoke(translator,new String[] { key })); |
273 |
} catch (Exception e) { |
274 |
return key;
|
275 |
} |
276 |
} |
277 |
|
278 |
} |