Statistics
| Revision:

root / trunk / libraries / libExceptions / src / org / gvsig / exceptions / BaseException.java @ 10854

History | View | Annotate | Download (6.78 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
                if (values != null){
48
                        Iterator keys = values.keySet().iterator();
49
                        while (keys.hasNext()){
50
                                key = (String) keys.next();
51
                                ret = ret.replaceAll("%\\("+key+"\\)", (String)values.get(key));
52
                        }
53
                }
54
                return ret;
55
        }
56

    
57

    
58
        /* (non-Javadoc)
59
         * @see java.lang.Throwable#getMessage()
60
         */
61
        public String getMessage() {
62
                return format(this.formatString, values());
63
        }
64

    
65
        /* (non-Javadoc)
66
         * @see org.gvsig.exceptions.IBaseException#getMessage(int)
67
         */
68
        public String getMessage(int indent) {
69
                return insertBlanksAtStart(format(formatString, values()),indent);
70
        }
71

    
72
        /* (non-Javadoc)
73
         * @see java.lang.Throwable#getLocalizedMessage()
74
         */
75
        public String getLocalizedMessage() {
76
                return getLocalizedMessage(translator,0);
77
        }
78

    
79
        /* (non-Javadoc)
80
         * @see org.gvsig.exceptions.IBaseException#getLocalizedMessage(org.gvsig.exceptions.IExceptionTranslator, int)
81
         */
82
        public String getLocalizedMessage(IExceptionTranslator translator, int indent){
83

    
84
                String fmt;
85
                if (translator == null){
86
                        translator = BaseException.translator;
87
                }
88
                if (translator == null){
89
                        fmt = getFormatString();
90
                } else {
91
                        fmt = getMessageKey();
92
                        if (fmt == null){
93
                                fmt = getFormatString();
94
                        } else {
95
                                fmt = translator.getText(fmt);
96
                        }
97
                }
98
                return insertBlanksAtStart(format(fmt,values()),indent);
99
        }
100

    
101
        /* (non-Javadoc)
102
         * @see org.gvsig.exceptions.IBaseException#getMessageStack()
103
         */
104
        public String getMessageStack() {
105
                return getMessageStack(0);
106
        }
107

    
108
        /* (non-Javadoc)
109
         * @see org.gvsig.exceptions.IBaseException#getMessageStack(int)
110
         */
111
        public String getMessageStack(int indent) {
112
                Iterator iter = this.iterator();
113
                String msg="";
114
                String msg1;
115
                Exception ex;
116
                int i = 1;
117
                while (iter.hasNext()){
118
                        ex = ((Exception)iter.next());
119
                        if ( ex instanceof BaseException ) {
120
                                BaseException bex = (BaseException) ex;
121
                                msg1 = bex.getMessage(indent*i);
122
                        } else {
123
                                msg1 = insertBlanksAtStart(ex.getMessage(),indent*i);
124
                        }
125
                        if(msg1!=null && !msg1.equals("")){
126
                                msg = msg + msg1 + "\n";
127
                        }
128
                        i++;
129
                }
130
                return msg;
131
        }
132

    
133

    
134
        /* (non-Javadoc)
135
         * @see org.gvsig.exceptions.IBaseException#getLocalizedMessageStack()
136
         */
137
        public String getLocalizedMessageStack() {
138
                return getLocalizedMessageStack(BaseException.translator,0);
139
        }
140

    
141
        /* (non-Javadoc)
142
         * @see org.gvsig.exceptions.IBaseException#getLocalizedMessageStack(org.gvsig.exceptions.IExceptionTranslator, int)
143
         */
144
        public String getLocalizedMessageStack(IExceptionTranslator translator, int indent) {
145
                Iterator iter = this.iterator();
146
                String msg="";
147
                Exception ex;
148
                while (iter.hasNext()){
149
                        ex = ((Exception)iter.next());
150
                        if ( ex instanceof BaseException ) {
151
                                BaseException bex = (BaseException) ex;
152
                                msg = msg + bex.getLocalizedMessage(translator,indent) + "\n";
153
                        } else {
154
                                msg = msg + ex.getLocalizedMessage()+ "\n";
155
                        }
156
                }
157
                return msg;
158
        }
159

    
160
        /**
161
         * Inserts blanks at the start of a string.
162
         *
163
         * @param str A string.
164
         * @param len Quantity of blanks to insert at the start of str.
165
         * @return A string compund by the quantity of blanks that
166
         *         len indicates and str.
167
         */
168
        static String insertBlanksAtStart(String str, int len){
169
                try {
170
                        return BLANKS.substring(0,len)+str;
171
                } catch (IndexOutOfBoundsException e) {
172
                        return BLANKS + str;
173
                }
174
        }
175

    
176
        /* (non-Javadoc)
177
         * @see org.gvsig.exceptions.IBaseException#getCode()
178
         */
179
        public long getCode() {
180
                return this.code;
181
        }
182

    
183
        /**
184
         * Sets the exception's code.
185
         */
186
        public void setCode(long code) {
187
                this.code = code;
188
        }
189

    
190
        /* (non-Javadoc)
191
         * @see org.gvsig.exceptions.IBaseException#getFormatString()
192
         */
193
        public String getFormatString() {
194
                return this.formatString;
195
        }
196

    
197
        /**
198
         * Sets the format string.
199
         *
200
         * @param formatString
201
         */
202
        public void setFormatString(String formatString) {
203
                this.formatString = formatString;
204
        }
205

    
206
        /* (non-Javadoc)
207
         * @see org.gvsig.exceptions.IBaseException#getMessageKey()
208
         */
209
        public String getMessageKey() {
210
                return this.messageKey;
211
        }
212

    
213
        /**
214
         * Sets the property messageKey.
215
         *
216
         * @param messageKey
217
         */
218
        public void setMessageKey(String messageKey) {
219
                this.messageKey = messageKey;
220
        }
221

    
222
        /* (non-Javadoc)
223
         * @see org.gvsig.exceptions.IBaseException#iterator()
224
         */
225
        public Iterator iterator() {
226
                return new BaseExceptionIterator(this);
227
        }
228

    
229
        /**
230
         * @return A map that serves to replace in the format string
231
         * the keys with the corresponding values.
232
         */
233
        abstract protected Map values();
234

    
235
        /**
236
         * Sets the property translator.
237
         * @param translator It(He,She) is used to translate
238
         *        the messages associated with the exceptions.
239
         */
240
        public static void setTranslator(IExceptionTranslator translator){
241
                BaseException.translator = translator;
242
        }
243

    
244
        public static void setTranslator(Object translator){
245
                BaseException.translator = new TranslatorWraper(translator);
246
        }
247

    
248
        public String toString(){
249
                return format(this.formatString, values());
250
        }
251

    
252
}
253

    
254
class TranslatorWraper implements IExceptionTranslator {
255

    
256
        private Object translator = null;
257
        private Method method = null;
258

    
259
        public TranslatorWraper(Object translator) {
260
                Class theClass = translator.getClass();
261
                String s = "";
262

    
263
                this.translator = translator;
264
                try {
265
                        method = theClass.getMethod("getText",new Class[] { s.getClass() });
266
                } catch (Exception e) {
267
                        throw new RuntimeException("El objeto translator suministrado no tiene el metodo getText apropiado.", e);
268
                }
269

    
270
        }
271

    
272
        public String getText(String key) {
273
                try {
274
                        return (String)(method.invoke(translator,new String[] { key }));
275
                } catch (Exception e) {
276
                        return key;
277
                }
278
        }
279

    
280
}