Statistics
| Revision:

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

History | View | Annotate | Download (5.39 KB)

1
package org.gvsig.exceptions;
2

    
3
import java.util.Map;
4
import java.util.Iterator;
5

    
6
/**
7
 * @author Equipo de desarrollo de gvSIG.
8
 *
9
 */
10
public abstract class BaseException extends Exception implements IBaseException {
11
        private final static String BLANKS ="                                                                                                     ";
12
        static IExceptionTranslator translator= null;
13

    
14
        protected String messageKey;
15
        
16
        protected String formatString;
17
        
18
        /** 
19
         * Unique code of error.
20
         */
21
        protected long code;
22
        
23
        /** 
24
         * Returns the format string received in the parameter
25
         * with its keys replaced with the corresponding values of the map.
26
         * 
27
         * @param formatString
28
         * @param values map 
29
         * @return string formatted
30
         */
31
        private String format(String formatString, Map values) {
32
                String key;
33
                String ret = formatString;
34
                Iterator keys = values.keySet().iterator();
35
                while (keys.hasNext()){
36
                        key = (String) keys.next();
37
                        ret = ret.replaceAll("%\\("+key+"\\)s", (String)values.get(key));
38
                }
39
                return ret;
40
        }
41
        
42
        
43
        /* (non-Javadoc)
44
         * @see java.lang.Throwable#getMessage()
45
         */
46
        public String getMessage() {
47
                return format(this.formatString, values());
48
        }
49

    
50
        /* (non-Javadoc)
51
         * @see org.gvsig.exceptions.IBaseException#getMessage(int)
52
         */
53
        public String getMessage(int indent) {
54
                return insertBlanksAtStart(format(formatString, values()),indent);
55
        }
56

    
57
        /* (non-Javadoc)
58
         * @see java.lang.Throwable#getLocalizedMessage()
59
         */
60
        public String getLocalizedMessage() {
61
                return getLocalizedMessage(translator,0);
62
        }
63

    
64
        /* (non-Javadoc)
65
         * @see org.gvsig.exceptions.IBaseException#getLocalizedMessage(org.gvsig.exceptions.IExceptionTranslator, int)
66
         */
67
        public String getLocalizedMessage(IExceptionTranslator translator, int indent){
68

    
69
                String fmt;
70
                if (translator == null){
71
                        translator = BaseException.translator;
72
                }
73
                if (translator == null){
74
                        fmt = getFormatString();
75
                } else {
76
                        fmt = getMessageKey();
77
                        if (fmt == null){
78
                                fmt = getFormatString();
79
                        } else {
80
                                fmt = translator.getText(fmt);
81
                        }
82
                }
83
                return insertBlanksAtStart(format(fmt,values()),indent);
84
        }
85

    
86
        /* (non-Javadoc)
87
         * @see org.gvsig.exceptions.IBaseException#getMessageStack()
88
         */
89
        public String getMessageStack() {
90
                return getMessageStack(0);
91
        }
92

    
93
        /* (non-Javadoc)
94
         * @see org.gvsig.exceptions.IBaseException#getMessageStack(int)
95
         */
96
        public String getMessageStack(int indent) {
97
                Iterator iter = this.iterator();
98
                String msg="";
99
                String msg1;
100
                Exception ex;
101
                int i = 1;
102
                while (iter.hasNext()){
103
                        ex = ((Exception)iter.next());
104
                        if ( ex instanceof BaseException ) {
105
                                BaseException bex = (BaseException) ex; 
106
                                msg1 = bex.getMessage(indent*i);
107
                        } else {
108
                                msg1 = insertBlanksAtStart(ex.getMessage(),indent*i);
109
                        }
110
                        if(msg1!=null && !msg1.equals("")){
111
                                msg = msg + msg1 + "\n";
112
                        }
113
                        i++;
114
                }
115
                return msg;
116
        }
117

    
118

    
119
        /* (non-Javadoc)
120
         * @see org.gvsig.exceptions.IBaseException#getLocalizedMessageStack()
121
         */
122
        public String getLocalizedMessageStack() {
123
                return getLocalizedMessageStack(BaseException.translator,0);
124
        }
125
        
126
        /* (non-Javadoc)
127
         * @see org.gvsig.exceptions.IBaseException#getLocalizedMessageStack(org.gvsig.exceptions.IExceptionTranslator, int)
128
         */
129
        public String getLocalizedMessageStack(IExceptionTranslator translator, int indent) {
130
                Iterator iter = this.iterator();
131
                String msg="";
132
                Exception ex;
133
                while (iter.hasNext()){
134
                        ex = ((Exception)iter.next());
135
                        if ( ex instanceof BaseException ) {
136
                                BaseException bex = (BaseException) ex; 
137
                                msg = msg + bex.getLocalizedMessage(translator,indent) + "\n";
138
                        } else {
139
                                msg = msg + ex.getLocalizedMessage()+ "\n";
140
                        }
141
                }
142
                return msg;
143
        }
144

    
145
        /** 
146
         * Inserts blanks at the start of a string.
147
         * 
148
         * @param str A string.
149
         * @param len Quantity of blanks to insert at the start of str.
150
         * @return A string compund by the quantity of blanks that
151
         *         len indicates and str.
152
         */
153
        static String insertBlanksAtStart(String str, int len){
154
                try {
155
                        return BLANKS.substring(0,len)+str;
156
                } catch (IndexOutOfBoundsException e) {
157
                        return BLANKS + str;
158
                }
159
        }
160
        
161
        /* (non-Javadoc)
162
         * @see org.gvsig.exceptions.IBaseException#getCode()
163
         */
164
        public long getCode() {
165
                return this.code;
166
        }
167
        
168
        /** 
169
         * Sets the exception's code.
170
         */
171
        public void setCode(long code) {
172
                this.code = code;
173
        }
174
        
175
        /* (non-Javadoc)
176
         * @see org.gvsig.exceptions.IBaseException#getFormatString()
177
         */
178
        public String getFormatString() {
179
                return this.formatString;
180
        }
181
        
182
        /** 
183
         * Sets the format string.
184
         *  
185
         * @param formatString
186
         */
187
        public void setFormatString(String formatString) {
188
                this.formatString = formatString;
189
        }
190
        
191
        /* (non-Javadoc)
192
         * @see org.gvsig.exceptions.IBaseException#getMessageKey()
193
         */
194
        public String getMessageKey() {
195
                return this.messageKey;
196
        }
197
        
198
        /** 
199
         * Sets the property messageKey.
200
         *  
201
         * @param messageKey
202
         */
203
        public void setMessageKey(String messageKey) {
204
                this.messageKey = messageKey;
205
        }
206
        
207
        /* (non-Javadoc)
208
         * @see org.gvsig.exceptions.IBaseException#iterator()
209
         */
210
        public Iterator iterator() {
211
                return new BaseExceptionIterator(this);
212
        }
213
        
214
        /** 
215
         * @return A map that serves to replace in the format string
216
         * the keys with the corresponding values.
217
         */
218
        abstract protected Map values();
219
        
220
        /**
221
         * Sets the property translator.  
222
         * @param translator It(He,She) is used to translate
223
         *        the messages associated with the exceptions.
224
         */
225
        public static void setTranslator(IExceptionTranslator translator){
226
                BaseException.translator = translator;
227
        }
228
        
229
}