Revision 23150

View differences:

branches/v2_0_0_prep/libraries/libExceptions/src/org/gvsig/exceptions/ListBaseException.java
1 1
package org.gvsig.exceptions;
2 2

  
3
import java.util.ArrayList;
4
import java.util.Collection;
5
import java.util.Iterator;
6
import java.util.List;
7
import java.util.ListIterator;
3
import java.util.*;
8 4

  
9 5
/**
10 6
 * @author Equipo de desarrollo de gvSIG
11
 *
7
 * 
8
 * @deprecated @see org.gvsig.tools.exception.ListBaseException
12 9
 */
13 10
public abstract class ListBaseException extends BaseException implements List{
14 11
	private List exceptions = new ArrayList();
branches/v2_0_0_prep/libraries/libExceptions/src/org/gvsig/exceptions/BaseException.java
18 18
 * 
19 19
 * @author Equipo de desarrollo de gvSIG.
20 20
 * 
21
 * @deprecated @see org.gvsig.tools.exception.BaseException
22
 * 
21 23
 */
22 24
public abstract class BaseException extends Exception implements IBaseException {
23 25
	private final static String BLANKS ="                                                                                                     ";
branches/v2_0_0_prep/libraries/libExceptions/src/org/gvsig/exceptions/BaseRuntimeException.java
35 35
 * Adds RuntimeException nature to the BaseException.
36 36
 * 
37 37
 * @author <a href="mailto:cordin@disid.com">C?sar Ordi?ana</a>
38
 * @deprecated @see org.gvsig.tools.exception.BaseRuntimeException
38 39
 */
39 40
public abstract class BaseRuntimeException extends RuntimeException implements
40 41
        IBaseException {
branches/v2_0_0_prep/libraries/libExceptions/src/org/gvsig/exceptions/IExceptionTranslator.java
3 3
/**
4 4
 * 
5 5
 * @author Equipo de desarrollo de gvSIG
6
 *
6
 * @deprecated @see org.gvsig.tools.exception.IExceptionTranslator
7 7
 */
8 8
public interface IExceptionTranslator {
9 9
	
branches/v2_0_0_prep/libraries/libExceptions/src/org/gvsig/exceptions/IBaseException.java
4 4

  
5 5
/**
6 6
 * 
7
 * @author Equipo de desarrollo de gvSIG
7 8
 * 
8
 * @author Equipo de desarrollo de gvSIG
9
 *
9
 * @deprecated @see org.gvsig.tools.exception.IBaseException
10 10
 */
11 11
public interface IBaseException {
12 12
	
branches/v2_0_0_prep/libraries/libExceptions/src/org/gvsig/exceptions/BaseExceptionIterator.java
2 2

  
3 3
import java.util.Iterator;
4 4

  
5
/**
6
 * @deprecated @see org.gvsig.tools.exception.BaseExceptionIterator
7
 */
5 8
class BaseExceptionIterator implements Iterator {
6 9
	
7 10
	Exception exception;
branches/v2_0_0_prep/libraries/libTools/src-test/org/gvsig/tools/exception/ListBaseExceptionTest.java
1
package org.gvsig.tools.exception;
2

  
3
import junit.framework.TestCase;
4

  
5
public class ListBaseExceptionTest extends TestCase {
6

  
7
	protected void setUp() throws Exception {
8
		super.setUp();
9
	}
10

  
11
	protected void tearDown() throws Exception {
12
		super.tearDown();
13
	}
14

  
15
	public void testSimple(){
16

  
17
	}
18

  
19
}
branches/v2_0_0_prep/libraries/libTools/src-test/org/gvsig/tools/exception/BaseExceptionTest.java
1
package org.gvsig.tools.exception;
2

  
3
import junit.framework.TestCase;
4

  
5
public class BaseExceptionTest extends TestCase {
6

  
7
	protected void setUp() throws Exception {
8
		super.setUp();
9
	}
10

  
11
	protected void tearDown() throws Exception {
12
		super.tearDown();
13
	}
14
	
15
	public void testSimple(){
16
		try {
17
			throw new NullPointerException("Excepcion de puntero nulo");
18
		} catch (NullPointerException e){
19
		    IBaseException de = createDriverException("SimpleDriver", e);
20
			assertEquals("Error in the driver SimpleDrivers", de.getMessage());
21
			assertEquals(
22
                    "Error in the driver SimpleDrivers\nExcepcion de puntero nulo",
23
                    de.getMessageStack());
24
		}
25
	}
26

  
27
	public void testSimpleLocalized(){
28
		class MyTranslator implements IExceptionTranslator {
29
			public String getText(String clave) {
30
				return clave.toUpperCase();
31
			}
32
		}
33
		BaseException.setTranslator(new MyTranslator());
34
		try {
35
			throw new NullPointerException("Excepcion de puntero nulo");
36
		} catch (NullPointerException e){
37
            IBaseException de = createDriverException("SimpleDriver", e);
38
			assertEquals("ERROR_IN_THE_DRIVER_%(DRIVERNAME)S",de.getLocalizedMessage());
39
			assertEquals(
40
                    "ERROR_IN_THE_DRIVER_%(DRIVERNAME)S\nExcepcion de puntero nulo",
41
                    de.getLocalizedMessageStack());
42
		}
43
		BaseException.setTranslator(null);
44
	}
45

  
46
	public void testSimple2(){
47
		try {
48
			throw new NullPointerException("Excepcion de puntero nulo");
49
		} catch (NullPointerException e){
50
            IBaseException de = createBadDateException("SimpleDriver", e);
51
			assertEquals("Driver SimpleDrivers: Formato de fecha incorrecto",
52
                    de.getMessage());
53
			assertEquals(
54
                    "Driver SimpleDrivers: Formato de fecha incorrecto\nExcepcion de puntero nulo",
55
                    de.getMessageStack());
56
		}
57
	}
58

  
59
	public void testSimpleLocalized2(){
60
		class MyTranslator implements IExceptionTranslator {
61
			public String getText(String clave) {
62
				return clave.toUpperCase();
63
			}
64
		}
65
		BaseException.setTranslator(new MyTranslator());
66
		try {
67
			throw new NullPointerException("Excepcion de puntero nulo");
68
		} catch (NullPointerException e){
69
            IBaseException de = createBadDateException("SimpleDriver", e);
70
			assertEquals("DRIVER_%(DRIVERNAME)S_FORMATO_DE_FECHA_INCORRECTO",de.getLocalizedMessage());
71
			assertEquals(
72
                    "DRIVER_%(DRIVERNAME)S_FORMATO_DE_FECHA_INCORRECTO\nExcepcion de puntero nulo",
73
                    de.getLocalizedMessageStack());
74
		}
75
		BaseException.setTranslator(null);
76
	}
77

  
78
	public void testTranslatorWithoutInterface() {
79
		class MyTranslator {
80
			public String getText(String clave) {
81
				return clave.toUpperCase();
82
			}
83
		}
84
		BaseException.setTranslator(new MyTranslator());
85
		try {
86
			throw new NullPointerException("Excepcion de puntero nulo");
87
		} catch (NullPointerException e){
88
            IBaseException de = createBadDateException("SimpleDriver", e);
89
			assertEquals("DRIVER_%(DRIVERNAME)S_FORMATO_DE_FECHA_INCORRECTO",de.getLocalizedMessage());
90
			assertEquals(
91
                    "DRIVER_%(DRIVERNAME)S_FORMATO_DE_FECHA_INCORRECTO\nExcepcion de puntero nulo",
92
                    de.getLocalizedMessageStack());
93
		}
94
		BaseException.setTranslator(null);
95
		
96
	}
97
	
98
    /**
99
     * Test method for
100
     * {@link org.gvsig.exceptions.ExceptionUtils#insertBlanksAtStart(java.lang.String, int)}
101
     * .
102
     */
103
    public void testInsertBlanksAtStart() {
104
        String src = "test";
105

  
106
        assertEquals("test", BaseException.insertBlanksAtStart(src, 0));
107

  
108
        assertEquals("test", BaseException.insertBlanksAtStart(src, -5));
109

  
110
        assertEquals("  test", BaseException.insertBlanksAtStart(src, 2));
111

  
112
        assertEquals("      test", BaseException.insertBlanksAtStart(src, 6));
113
    }
114
    
115
    protected IBaseException createDriverException(String driver,
116
            Throwable throwable) {
117
        return new DriverException(driver, throwable);
118
    }
119
    
120
    protected IBaseException createBadDateException(String driver,
121
            Throwable throwable) {
122
        return new BadDateException(driver, throwable);
123
    }
124

  
125
	class BadDateException extends DriverException {
126
		private static final long serialVersionUID = -8985920349210629998L;
127
	    private static final String KEY = "Driver_%(driverName)s_Formato_de_fecha_incorrecto";
128

  
129
	    private static final String MESSAGE = "Driver %(driverName)s: Formato de fecha incorrecto";
130

  
131
		public BadDateException(String driverName, Throwable cause) {
132
            super(driverName, MESSAGE, cause, KEY, serialVersionUID);
133
		}
134
	}
135
	
136
}
branches/v2_0_0_prep/libraries/libTools/src-test/org/gvsig/tools/exception/DriverException.java
1
package org.gvsig.tools.exception;
2

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

  
6
public class DriverException extends BaseException {
7
	
8
	private static final long serialVersionUID = -8985920349210629999L;
9
	
10
    private static final String KEY = "Error_in_the_driver_%(driverName)s";
11

  
12
    private static final String MESSAGE = "Error in the driver %(driverName)s";
13
	
14
	private String driverName;
15
	
16
	public DriverException(String driverName) {
17
		super(MESSAGE, KEY, serialVersionUID);
18
		this.driverName = driverName;
19
	}
20
	
21
	public DriverException(String driverName, Throwable cause) {
22
        super(MESSAGE, cause, KEY, serialVersionUID);
23
		this.driverName = driverName;
24
	}
25
    
26
    public DriverException(String driverName, String message, Throwable cause,
27
            String key, long code) {
28
        super(message, cause, key, code);
29
        this.driverName = driverName;
30
    }
31

  
32
	public String getDriverName() {
33
		return driverName;
34
	}
35
	
36
	protected Map values() {
37
		HashMap values = new HashMap();
38
		values.put("driverName",this.driverName);
39
		return values;
40
	}
41
}
branches/v2_0_0_prep/libraries/libTools/src-test/org/gvsig/tools/exception/AllTests.java
1
package org.gvsig.tools.exception;
2

  
3
import junit.framework.Test;
4
import junit.framework.TestSuite;
5

  
6
public class AllTests {
7

  
8
	public static Test suite() {
9
		TestSuite suite = new TestSuite("Test for org.gvsig.exceptions");
10
		//$JUnit-BEGIN$
11
		suite.addTestSuite(ListBaseExceptionTest.class);
12
		suite.addTestSuite(BaseExceptionTest.class);
13
		//$JUnit-END$
14
		return suite;
15
	}
16

  
17
}
branches/v2_0_0_prep/libraries/libTools/src-test/org/gvsig/tools/exception/BaseRuntimeExceptionTest.java
1
package org.gvsig.tools.exception;
2

  
3
import java.util.Map;
4

  
5
public class BaseRuntimeExceptionTest extends BaseExceptionTest {
6
    
7
    public void testRuntimeExceptionNature() {
8
        Object test = new Object() {
9

  
10
            public String toString() {
11
                throw new BaseRuntimeException("ERROR", "ERROR", 1) {
12
                    protected Map values() {
13
                        return null;
14
                    }
15
                };
16
            }
17
            
18
        };
19
        
20
        try {
21
            test.toString();
22
            fail("RuntimeException not generated");
23
        } catch (RuntimeException rex) {
24
            // success
25
        }
26
    }
27

  
28
    protected IBaseException createBadDateException(String driver,
29
            Throwable throwable) {
30
        return new BadDateException(driver, throwable);
31
    }
32

  
33
    protected IBaseException createDriverException(String driver,
34
            Throwable throwable) {
35
        return new DriverRuntimeException(driver, throwable);
36
    }
37

  
38
    class BadDateException extends DriverRuntimeException {
39
        private static final long serialVersionUID = -8985920349210629998L;
40
        private static final String KEY = "Driver_%(driverName)s_Formato_de_fecha_incorrecto";
41

  
42
        private static final String MESSAGE = "Driver %(driverName)s: Formato de fecha incorrecto";
43

  
44
        public BadDateException(String driverName, Throwable cause) {
45
            super(driverName, MESSAGE, cause, KEY, serialVersionUID);
46
        }
47
    }
48

  
49
}
branches/v2_0_0_prep/libraries/libTools/src-test/org/gvsig/tools/exception/DriverRuntimeException.java
1
package org.gvsig.tools.exception;
2

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

  
6
public class DriverRuntimeException extends BaseRuntimeException {
7
	
8
	private static final long serialVersionUID = -8985920349210629999L;
9
	
10
    private static final String KEY = "Error_in_the_driver_%(driverName)s";
11

  
12
    private static final String MESSAGE = "Error in the driver %(driverName)s";
13
	
14
	private String driverName;
15
	
16
	public DriverRuntimeException(String driverName) {
17
		super(MESSAGE, KEY, serialVersionUID);
18
		this.driverName = driverName;
19
	}
20
	
21
	public DriverRuntimeException(String driverName, Throwable cause) {
22
        super(MESSAGE, cause, KEY, serialVersionUID);
23
		this.driverName = driverName;
24
	}
25
    
26
    public DriverRuntimeException(String driverName, String message, Throwable cause,
27
            String key, long code) {
28
        super(message, cause, key, code);
29
        this.driverName = driverName;
30
    }
31

  
32
	public String getDriverName() {
33
		return driverName;
34
	}
35
	
36
	protected Map values() {
37
		HashMap values = new HashMap();
38
		values.put("driverName",this.driverName);
39
		return values;
40
	}
41
}
branches/v2_0_0_prep/libraries/libTools/src/org/gvsig/tools/exception/IBaseException.java
1
package org.gvsig.tools.exception;
2

  
3
import java.util.Iterator;
4

  
5
/**
6
 * 
7
 * 
8
 * @author Equipo de desarrollo de gvSIG
9
 *
10
 */
11
public interface IBaseException {
12
	
13
	/** 
14
	 *  Returns the message that describes the exception.
15
	 *  
16
	 *  @return The message.
17
	 */
18
	public String getMessage();
19

  
20
	/** 
21
	 *  Returns the message that describes the exception, with indentation.
22
	 *  
23
	 *  @param indent Quantity of blanks to insert
24
	 *         at the start of the message.
25
	 *  @return The message with indentation.
26
	 */
27
	public String getMessage(int indent);
28

  
29
	/** 
30
	 *  Returns the translated message that describes the exception.
31
	 *  
32
	 *  @return The translated message with indentation.
33
	 */
34
	public String getLocalizedMessage();
35
	
36
	/** 
37
	 *  Returns the translated message that
38
	 *  describes the exception with indentation.
39
	 *
40
	 *  @param translator Instance of a class that fulfills
41
	 *         the IExceptionTranslator interface.
42
	 *         His method "getText" takes charge returning
43
	 *         the expression, correspondent to the key that
44
	 *         delivers him, translated into the configured language.
45
	 *  @param indent Quantity of blanks to insert
46
	 *         at the start of the message.
47
	 *  @return The translated message with indentation.
48
	 */
49
	public String getLocalizedMessage(IExceptionTranslator translator, int indent);
50
	
51
	/** 
52
	 *  Crosses the exceptions chained through cause to conform
53
	 *  the message.
54
	 *  
55
	 *  @return The compound message with all the messages
56
	 *          of the stack of exceptions.
57
	 */
58
	public String getMessageStack();
59

  
60
	/** 
61
	 *  Crosses the exceptions chained through cause to conform
62
	 *  the compound message with indentation.
63
	 *  
64
	 *  @param indent Quantity of blanks to insert
65
	 *         at the start of the messages.
66
	 *  @return The compound message with all the messages
67
	 *          of the stack of exceptions.
68

  
69
	 */
70
	public String getMessageStack(int indent);
71

  
72
	/** 
73
	 *  Crosses the exceptions chained through cause
74
	 *  to conform the compound message in the corresponding language.
75
	 *  
76
	 *  @return The translated compound message.
77
	 *    
78
	 */
79
	public String getLocalizedMessageStack();
80

  
81
	/** 
82
	 *  Crosses the exceptions chained through cause
83
	 *  to conform the compound message in the corresponding language.
84
	 *  
85
	 *  @param translator Instance of a class that fulfills
86
	 *         the IExceptionTranslator interface.
87
	 *         His method "getText" takes charge returning
88
	 *         the expression, correspondent to the key that
89
	 *         delivers him, translated into the configured language.
90
	 *  @param indent Quantity of blanks to insert
91
	 *         at the start of the messages.
92
	 *  @return The translated message with indentation.
93
	 *  
94
	 */
95
	public String getLocalizedMessageStack(IExceptionTranslator translator, int indent);
96
	
97
	
98
	/** 
99
	 *  @return The exception's code.
100
	 */
101
	public long getCode();
102
	
103
	/** 
104
	 *  @return The format string.
105
	 */
106
	public String getFormatString();
107
	
108
	/** 
109
	 *  @return The message key associated to the exception.
110
	 */
111
	public String getMessageKey();
112
	
113
	/** 
114
	 *  @return A iterator for the chained exceptions.
115
	 */
116
	public Iterator iterator();
117
	
118
}
branches/v2_0_0_prep/libraries/libTools/src/org/gvsig/tools/exception/BaseExceptionIterator.java
1
package org.gvsig.tools.exception;
2

  
3
import java.util.Iterator;
4

  
5
class BaseExceptionIterator implements Iterator {
6
	
7
	Exception exception;
8
	
9
	BaseExceptionIterator(BaseException exception){
10
		this.exception = exception;
11
	}
12
	/** 
13
	 *  @return true if the iteration has more elements.
14
	 */
15
	public boolean hasNext() {
16
		return this.exception != null;
17
	}
18
	
19
	/** 
20
	 *  @return The next element in the iteration.
21
	 */
22
	public Object next() {
23
		Exception exception;
24
		exception = this.exception;
25
		this.exception = (Exception) exception.getCause();
26
		return exception;
27
	}
28
	
29
	/** 
30
	 *  @throws "UnsupportedOperationException" because
31
	 *  the remove operation will not be supported
32
	 *  by this Iterator.
33
	 */
34
	public void remove() {
35
		throw new UnsupportedOperationException();
36
	}
37
}
branches/v2_0_0_prep/libraries/libTools/src/org/gvsig/tools/exception/ListBaseException.java
1
package org.gvsig.tools.exception;
2

  
3
import java.util.ArrayList;
4
import java.util.Collection;
5
import java.util.Iterator;
6
import java.util.List;
7
import java.util.ListIterator;
8

  
9
/**
10
 * @author Equipo de desarrollo de gvSIG
11
 *
12
 */
13
public abstract class ListBaseException extends BaseException implements List{
14
	private List exceptions = new ArrayList();
15
	
16
	/* (non-Javadoc)
17
	 * @see java.util.Collection#size()
18
	 */
19
	public int size() {
20
		return this.exceptions.size();
21
	}
22

  
23
	/* (non-Javadoc)
24
	 * @see java.util.Collection#isEmpty()
25
	 */
26
	public boolean isEmpty() {
27
		return this.exceptions.isEmpty();
28
	}
29

  
30
	/* (non-Javadoc)
31
	 * @see java.util.Collection#contains(java.lang.Object)
32
	 */
33
	public boolean contains(Object arg0) {
34
		return this.exceptions.contains(arg0);
35
	}
36

  
37
	/* (non-Javadoc)
38
	 * @see java.util.Collection#toArray()
39
	 */
40
	public Object[] toArray() {
41
		return this.exceptions.toArray();
42
	}
43

  
44
	/**
45
	 * @param arg0
46
	 * @return
47
	 */
48
	public Object[] toArray(Object[] arg0) {
49
		return this.exceptions.toArray(arg0);
50
	}
51

  
52
	/**
53
	 * @param arg0
54
	 * @return
55
	 */
56
	public boolean add(Object arg0) {
57
		return this.exceptions.add(arg0);
58
	}
59

  
60
	/* (non-Javadoc)
61
	 * @see java.util.Collection#remove(java.lang.Object)
62
	 */
63
	public boolean remove(Object arg0) {
64
		return this.exceptions.remove(arg0);
65
	}
66

  
67
	/**
68
	 * @param arg0
69
	 * @return
70
	 */
71
	public boolean containsAll(Collection arg0) {
72
		return this.exceptions.contains(arg0);
73
	}
74

  
75
	/**
76
	 * @param arg0
77
	 * @return
78
	 */
79
	public boolean addAll(Collection arg0) {
80
		return this.exceptions.addAll(arg0);
81
	}
82

  
83
	/**
84
	 * @param arg0
85
	 * @param arg1
86
	 * @return
87
	 */
88
	public boolean addAll(int arg0, Collection arg1) {
89
		return this.exceptions.addAll(arg0, arg1);
90
	}
91

  
92
	/**
93
	 * @param arg0
94
	 * @return
95
	 */
96
	public boolean removeAll(Collection arg0) {
97
		return this.exceptions.removeAll(arg0);
98
	}
99

  
100
	/**
101
	 * @param arg0
102
	 * @return
103
	 */
104
	public boolean retainAll(Collection arg0) {
105
		return this.exceptions.retainAll(arg0);
106
	}
107

  
108
	/* (non-Javadoc)
109
	 * @see java.util.Collection#clear()
110
	 */
111
	public void clear() {
112
		this.exceptions.clear();
113
	}
114

  
115
	/* (non-Javadoc)
116
	 * @see java.util.List#get(int)
117
	 */
118
	public Object get(int arg0) {
119
		return this.exceptions.get(arg0);
120
	}
121

  
122
	/**
123
	 * @param arg0
124
	 * @param arg1
125
	 * @return
126
	 */
127
	public Object set(int arg0, Object arg1) {
128
		return this.exceptions.set(arg0, arg1);
129
	}
130

  
131
	/**
132
	 * @param arg0
133
	 * @param arg1
134
	 */
135
	public void add(int arg0, Object arg1) {
136
		this.exceptions.add(arg0, arg1);
137
	}
138

  
139
	/* (non-Javadoc)
140
	 * @see java.util.List#remove(int)
141
	 */
142
	public Object remove(int arg0) {
143
		return this.exceptions.remove(arg0);
144
	}
145

  
146
	/* (non-Javadoc)
147
	 * @see java.util.List#indexOf(java.lang.Object)
148
	 */
149
	public int indexOf(Object arg0) {
150
		return this.exceptions.indexOf(arg0);
151
	}
152

  
153
	/* (non-Javadoc)
154
	 * @see java.util.List#lastIndexOf(java.lang.Object)
155
	 */
156
	public int lastIndexOf(Object arg0) {
157
		return this.exceptions.lastIndexOf(arg0);
158
	}
159

  
160
	/* (non-Javadoc)
161
	 * @see java.util.List#listIterator()
162
	 */
163
	public ListIterator listIterator() {
164
		return this.exceptions.listIterator();
165
	}
166

  
167
	/* (non-Javadoc)
168
	 * @see java.util.List#listIterator(int)
169
	 */
170
	public ListIterator listIterator(int arg0) {
171
		return this.exceptions.listIterator(arg0);
172
	}
173

  
174
	/* (non-Javadoc)
175
	 * @see java.util.List#subList(int, int)
176
	 */
177
	public List subList(int arg0, int arg1) {
178
		return this.exceptions.subList(arg0, arg1);
179
	}
180
	
181
	/* (non-Javadoc)
182
	 * @see java.lang.Throwable#getMessage()
183
	 */
184
	public String getMessage() {
185
		String msg = super.getMessage();
186
		Exception bex;
187
		Iterator iter=this.exceptions.iterator();
188
		while (iter.hasNext()) {
189
			bex = (Exception) iter.next();
190
			msg = msg + "\n  " + bex.getMessage();
191
		}
192
		return msg;
193
	}
194
	
195
	/* (non-Javadoc)
196
	 * @see org.gvsig.exceptions.IBaseException#getLocalizedMessage(org.gvsig.exceptions.IExceptionTranslator, int)
197
	 */
198
	public String getLocalizedMessage(IExceptionTranslator exceptionTranslator, int indent) {
199
		String msg = super.getLocalizedMessage(exceptionTranslator, indent);
200
		Exception bex;
201
		Iterator iter=this.exceptions.iterator();
202
		while (iter.hasNext()) {
203
			bex = (Exception) iter.next();
204
			if( bex instanceof BaseException ) {
205
				msg = msg + "\n  " + ((BaseException)bex).getLocalizedMessage(exceptionTranslator, indent);				
206
			} else {
207
				msg = msg + "\n  " + bex.getLocalizedMessage();
208
			}
209
		}
210
		return BaseException.insertBlanksAtStart(msg,indent);
211
	}
212

  
213
}
branches/v2_0_0_prep/libraries/libTools/src/org/gvsig/tools/exception/BaseException.java
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
}
branches/v2_0_0_prep/libraries/libTools/src/org/gvsig/tools/exception/BaseRuntimeException.java
1
/* gvSIG. Geographic Information System of the Valencian Government
2
*
3
* Copyright (C) 2007-2008 Infrastructures and Transports Department
4
* of the Valencian Gobernment (CIT)
5
* 
6
* This program is free software; you can redistribute it and/or
7
* modify it under the terms of the GNU General Public License
8
* as published by the Free Software Foundation; either version 2
9
* of the License, or (at your option) any later version.
10
* 
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
* GNU General Public License for more details.
15
* 
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 
19
* MA  02110-1301, USA.
20
* 
21
*/
22

  
23
/*
24
* AUTHORS (In addition to CIT):
25
* 2008 DiSiD Technologies  Create a BaseException as a RuntimeException.
26
*/
27
package org.gvsig.tools.exception;
28

  
29
import java.io.PrintStream;
30
import java.io.PrintWriter;
31
import java.util.Iterator;
32
import java.util.Map;
33

  
34
/**
35
 * Adds RuntimeException nature to the BaseException.
36
 * 
37
 * @author <a href="mailto:cordin@disid.com">C?sar Ordi?ana</a>
38
 */
39
public abstract class BaseRuntimeException extends RuntimeException implements
40
        IBaseException {
41
    
42
    // Inner delegate exception
43
    private BaseException exception;
44

  
45
    /**
46
     * Constructs a RuntimeBaseException with a default message format, a key to
47
     * find a localized message format, and a unique code to identify the
48
     * 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 BaseRuntimeException(String message, String key, long code) {
58
        exception = new DelegateBaseException(message, key, code, this);
59
    }
60

  
61
    /**
62
     * Constructs a BaseException with a default message format, a key to find a
63
     * localized message format, and a unique code to identify the exception.
64
     * 
65
     * @param message
66
     *            the default messageFormat to describe the exception
67
     * @param cause
68
     *            the original cause of the exception
69
     * @param key
70
     *            the key to use to search a localized messageFormnata
71
     * @param code
72
     *            the unique code to identify the exception
73
     */
74
    public BaseRuntimeException(String message, Throwable cause, String key,
75
            long code) {
76
        exception = new DelegateBaseException(message, cause, key, code, this);
77
    }
78

  
79
    public boolean equals(Object obj) {
80
        return exception.equals(obj);
81
    }
82

  
83
    public Throwable getCause() {
84
        return exception.getCause();
85
    }
86

  
87
    public long getCode() {
88
        return exception.getCode();
89
    }
90

  
91
    public String getFormatString() {
92
        return exception.getFormatString();
93
    }
94

  
95
    public String getLocalizedMessage() {
96
        return exception.getLocalizedMessage();
97
    }
98

  
99
    public String getLocalizedMessage(IExceptionTranslator translator,
100
            int indent) {
101
        return exception.getLocalizedMessage(translator, indent);
102
    }
103

  
104
    public String getLocalizedMessageStack() {
105
        return exception.getLocalizedMessageStack();
106
    }
107

  
108
    public String getLocalizedMessageStack(IExceptionTranslator translator,
109
            int indent) {
110
        return exception.getLocalizedMessageStack(translator, indent);
111
    }
112

  
113
    public String getMessage() {
114
        return exception.getMessage();
115
    }
116

  
117
    public String getMessage(int indent) {
118
        return exception.getMessage(indent);
119
    }
120

  
121
    public String getMessageKey() {
122
        return exception.getMessageKey();
123
    }
124

  
125
    public String getMessageStack() {
126
        return exception.getMessageStack();
127
    }
128

  
129
    public String getMessageStack(int indent) {
130
        return exception.getMessageStack(indent);
131
    }
132

  
133
    public StackTraceElement[] getStackTrace() {
134
        return exception.getStackTrace();
135
    }
136

  
137
    public int hashCode() {
138
        return exception.hashCode();
139
    }
140

  
141
    public Throwable initCause(Throwable cause) {
142
        return exception.initCause(cause);
143
    }
144

  
145
    public Iterator iterator() {
146
        return exception.iterator();
147
    }
148

  
149
    public void printStackTrace() {
150
        exception.printStackTrace();
151
    }
152

  
153
    public void printStackTrace(PrintStream s) {
154
        exception.printStackTrace(s);
155
    }
156

  
157
    public void printStackTrace(PrintWriter s) {
158
        exception.printStackTrace(s);
159
    }
160

  
161
    public void setCode(long code) {
162
        exception.setCode(code);
163
    }
164

  
165
    public void setFormatString(String formatString) {
166
        exception.setFormatString(formatString);
167
    }
168

  
169
    public void setMessageKey(String messageKey) {
170
        exception.setMessageKey(messageKey);
171
    }
172

  
173
    public void setStackTrace(StackTraceElement[] stackTrace) {
174
        exception.setStackTrace(stackTrace);
175
    }
176

  
177
    public String toString() {
178
        return exception.toString();
179
    }
180

  
181
    /**
182
     * Used to return a map that serves to replace in the format string the keys
183
     * with the corresponding values.
184
     * 
185
     * @return the message values
186
     */
187
    abstract protected Map values();
188

  
189
    /**
190
     * Inner BaseException implementation to use as the inner exception.
191
     * 
192
     * @author <a href="mailto:cordin@disid.com">C?sar Ordi?ana</a>
193
     */
194
    public class DelegateBaseException extends BaseException {
195
        
196
        private BaseRuntimeException baseException;
197
        
198
        public DelegateBaseException(String message, String key, long code,
199
                BaseRuntimeException baseException) {
200
            super(message, key, code);
201
            this.baseException = baseException;
202
        }
203

  
204
        public DelegateBaseException(String message, Throwable cause,
205
                String key, long code, BaseRuntimeException baseException) {
206
            super(message, cause, key, code);
207
            this.baseException = baseException;
208
        }
209

  
210
        protected Map values() {
211
            return baseException.values();
212
        }
213
    }
214
}
branches/v2_0_0_prep/libraries/libTools/src/org/gvsig/tools/exception/IExceptionTranslator.java
1
package org.gvsig.tools.exception;
2

  
3
/**
4
 * 
5
 * @author Equipo de desarrollo de gvSIG
6
 *
7
 */
8
public interface IExceptionTranslator {
9
	
10
	/** 
11
	 *  @param key The key of the message error.
12
	 *  @return The translated error message
13
	 *  corresponding to the key that it
14
	 *  obtains as parameter.
15
	 */
16
	public String getText(String key);
17
}

Also available in: Unified diff