Revision 36509

View differences:

tags/v_1_12_0_Build_1402/libExceptions/.project
1
<?xml version="1.0" encoding="UTF-8"?>
2
<projectDescription>
3
	<name>libExceptions</name>
4
	<comment></comment>
5
	<projects>
6
	</projects>
7
	<buildSpec>
8
		<buildCommand>
9
			<name>org.eclipse.jdt.core.javabuilder</name>
10
			<arguments>
11
			</arguments>
12
		</buildCommand>
13
		<buildCommand>
14
			<name>de.loskutov.FileSync.FSBuilder</name>
15
			<arguments>
16
			</arguments>
17
		</buildCommand>
18
	</buildSpec>
19
	<natures>
20
		<nature>org.eclipse.jdt.core.javanature</nature>
21
	</natures>
22
</projectDescription>
0 23

  
tags/v_1_12_0_Build_1402/libExceptions/src-test/org/gvsig/exceptions/AllTests.java
1
package org.gvsig.exceptions;
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
}
0 18

  
tags/v_1_12_0_Build_1402/libExceptions/src-test/org/gvsig/exceptions/ListBaseExceptionTest.java
1
package org.gvsig.exceptions;
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
}
0 20

  
tags/v_1_12_0_Build_1402/libExceptions/src-test/org/gvsig/exceptions/BaseExceptionTest.java
1
package org.gvsig.exceptions;
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
			DriverException de = new DriverException("SimpleDriver", e);
20
			assertEquals("Error in the driver SimpleDriver",de.getMessage());
21
			assertEquals("Error in the driver SimpleDriver\nExcepcion de puntero nulo\n",de.getMessageStack());
22
		}
23
	}
24

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

  
42
	public void testSimple2(){
43
		try {
44
			throw new NullPointerException("Excepcion de puntero nulo");
45
		} catch (NullPointerException e){
46
			BadDateException de = new BadDateException("SimpleDriver", e);
47
			assertEquals("Driver SimpleDriver: Formato de fecha incorrecto",de.getMessage());
48
			assertEquals("Driver SimpleDriver: Formato de fecha incorrecto\nExcepcion de puntero nulo\n",de.getMessageStack());
49
		}
50
	}
51

  
52
	public void testSimpleLocalized2(){
53
		class MyTranslator implements IExceptionTranslator {
54
			public String getText(String clave) {
55
				return clave.toUpperCase();
56
			}
57
		}
58
		BaseException.setTranslator(new MyTranslator());
59
		try {
60
			throw new NullPointerException("Excepcion de puntero nulo");
61
		} catch (NullPointerException e){
62
			BadDateException de = new BadDateException("SimpleDriver", e);
63
			assertEquals("DRIVER_%(DRIVERNAME)S_FORMATO_DE_FECHA_INCORRECTO",de.getLocalizedMessage());
64
			assertEquals("DRIVER_%(DRIVERNAME)S_FORMATO_DE_FECHA_INCORRECTO\nExcepcion de puntero nulo\n",de.getLocalizedMessageStack());
65
		}
66
		BaseException.setTranslator(null);
67
	}
68

  
69
	public void testTranslatorWithoutInterface() {
70
		class MyTranslator {
71
			public String getTest(String clave) {
72
				return clave.toUpperCase();
73
			}
74
		}
75
		BaseException.setTranslator(new MyTranslator());
76
		try {
77
			throw new NullPointerException("Excepcion de puntero nulo");
78
		} catch (NullPointerException e){
79
			BadDateException de = new BadDateException("SimpleDriver", e);
80
			assertEquals("DRIVER_%(DRIVERNAME)S_FORMATO_DE_FECHA_INCORRECTO",de.getLocalizedMessage());
81
			assertEquals("DRIVER_%(DRIVERNAME)S_FORMATO_DE_FECHA_INCORRECTO\nExcepcion de puntero nulo\n",de.getLocalizedMessageStack());
82
		}
83
		BaseException.setTranslator(null);
84
		
85
	}
86

  
87
	class BadDateException extends DriverException {
88
		private static final long serialVersionUID = -8985920349210629998L;
89
		
90
		public BadDateException(String driverName, Throwable cause){
91
			super(driverName, cause);
92
			messageKey="Driver_%(driverName)s_Formato_de_fecha_incorrecto";
93
			formatString="Driver %(driverName)s: Formato de fecha incorrecto";
94
			code = serialVersionUID;
95
		}
96
	}
97
}
0 98

  
tags/v_1_12_0_Build_1402/libExceptions/src-test/org/gvsig/exceptions/DriverException.java
1
package org.gvsig.exceptions;
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
	private String driverName;
10
	
11
	public DriverException() {
12
		init();
13
	}
14
	
15
	public DriverException(String driverName) {
16
		init();
17
		this.driverName = driverName;
18
	}
19
	
20
	public DriverException(String driverName, Throwable cause) {
21
		init();
22
		this.driverName = driverName;
23
		initCause(cause);
24
	}
25

  
26
	public void init() {
27
		messageKey="Error_in_the_driver_%(driverName)s";
28
		formatString="Error in the driver %(driverName)s";
29
		code = serialVersionUID;
30
	}
31

  
32
	
33
	public String getDriverName() {
34
		return driverName;
35
	}
36
	
37
	public void setDriverName(String driverName) {
38
		this.driverName = driverName;
39
	}
40
	
41
	protected Map values() {
42
		HashMap values = new HashMap();
43
		values.put("driverName",this.driverName);
44
		return values;
45
	}
46
}
0 47

  
tags/v_1_12_0_Build_1402/libExceptions/src/org/gvsig/exceptions/IExceptionTranslator.java
1
package org.gvsig.exceptions;
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
}
0 18

  
tags/v_1_12_0_Build_1402/libExceptions/src/org/gvsig/exceptions/IBaseException.java
1
package org.gvsig.exceptions;
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
}
0 119

  
tags/v_1_12_0_Build_1402/libExceptions/src/org/gvsig/exceptions/BaseExceptionIterator.java
1
package org.gvsig.exceptions;
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
}
0 38

  
tags/v_1_12_0_Build_1402/libExceptions/src/org/gvsig/exceptions/ListBaseException.java
1
package org.gvsig.exceptions;
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
}
0 214

  
tags/v_1_12_0_Build_1402/libExceptions/src/org/gvsig/exceptions/BaseException.java
1
package org.gvsig.exceptions;
2

  
3
import java.lang.reflect.Method;
4
import java.util.Iterator;
5
import java.util.Map;
6

  
7
import org.apache.log4j.Logger;
8

  
9
/**
10
 *
11
 * Esta clase esta pensada para actuar como clase base para
12
 * las excepciones que se lanzan dentro del proyecto de gvSIG.
13
 *
14
 * A?ade la implementacion necesaria para disponer de mensajes
15
 * de error internacionalizables, a traves del metodo
16
 * getLocalizedMessage, asi como una serie de metodos que nos
17
 * permiten obtener los mesanes de error de la cadena de excepciones
18
 * enlazadas a traves de su "causa", asi como utilidades que
19
 * permitan recorrer de forma comoda esta cadena de excepciones
20
 * por medio de un Iterador.
21
 *
22
 * @author Equipo de desarrollo de gvSIG.
23
 *
24
 */
25
public abstract class BaseException extends Exception implements IBaseException {
26
	private final static String BLANKS ="                                                                                                     ";
27
	private static IExceptionTranslator translator= null;
28
	private static Logger logger = null;
29

  
30

  
31
	protected String messageKey;
32

  
33
	protected String formatString;
34

  
35
	/**
36
	 * Unique code of error.
37
	 */
38
	protected long code;
39

  
40
	/**
41
	 * Returns the format string received in the parameter
42
	 * with its keys replaced with the corresponding values of the map.
43
	 *
44
	 * @param formatString
45
	 * @param values map
46
	 * @return string formatted
47
	 */
48
	private String format(String formatString, Map values) {
49
		String key;
50
		String ret = formatString;
51
		if(formatString == null){
52
			Logger lLogger=getLogger();
53
			lLogger.error(this.getClass().getName()+": formatString is null.");
54
			if (values != null){
55
				Iterator keys = values.keySet().iterator();
56
				ret = "values = { ";
57
				while (keys.hasNext()){
58
					key = (String) keys.next();
59
					ret = ret.concat(key+": "+ (String)values.get(key)+"; ");
60
				}
61
				ret = ret.concat(" }");
62
			}
63
			
64
			return ret;
65
		}
66
		if (values != null){
67
			Iterator keys = values.keySet().iterator();
68
			while (keys.hasNext()){
69
				key = (String) keys.next();
70
				ret = ret.replaceAll("%\\("+key+"\\)", (String)values.get(key));
71
			}
72
		}
73
		return ret;
74
	}
75
	protected Logger getLogger() {
76
		if(logger==null){
77
			logger = Logger.getLogger(BaseException.class.getName());
78
		}
79
		return logger;
80
	}
81

  
82
	/* (non-Javadoc)
83
	 * @see java.lang.Throwable#getMessage()
84
	 */
85
	public String getMessage() {
86
		return format(this.formatString, values());
87
	}
88

  
89
	/* (non-Javadoc)
90
	 * @see org.gvsig.exceptions.IBaseException#getMessage(int)
91
	 */
92
	public String getMessage(int indent) {
93
		return insertBlanksAtStart(format(formatString, values()),indent);
94
	}
95

  
96
	/* (non-Javadoc)
97
	 * @see java.lang.Throwable#getLocalizedMessage()
98
	 */
99
	public String getLocalizedMessage() {
100
		return getLocalizedMessage(translator,0);
101
	}
102

  
103
	/* (non-Javadoc)
104
	 * @see org.gvsig.exceptions.IBaseException#getLocalizedMessage(org.gvsig.exceptions.IExceptionTranslator, int)
105
	 */
106
	public String getLocalizedMessage(IExceptionTranslator translator, int indent){
107

  
108
		String fmt;
109
		if (translator == null){
110
			translator = BaseException.translator;
111
		}
112
		if (translator == null){
113
			fmt = getFormatString();
114
		} else {
115
			fmt = getMessageKey();
116
			if (fmt == null){
117
				fmt = getFormatString();
118
			} else {
119
				fmt = translator.getText(fmt);
120
			}
121
		}
122
		return insertBlanksAtStart(format(fmt,values()),indent);
123
	}
124

  
125
	/* (non-Javadoc)
126
	 * @see org.gvsig.exceptions.IBaseException#getMessageStack()
127
	 */
128
	public String getMessageStack() {
129
		return getMessageStack(0);
130
	}
131

  
132
	/* (non-Javadoc)
133
	 * @see org.gvsig.exceptions.IBaseException#getMessageStack(int)
134
	 */
135
	public String getMessageStack(int indent) {
136
		Iterator iter = this.iterator();
137
		String msg="";
138
		String msg1;
139
		Exception ex;
140
		int i = 1;
141
		while (iter.hasNext()){
142
			ex = ((Exception)iter.next());
143
			if ( ex instanceof BaseException ) {
144
				BaseException bex = (BaseException) ex;
145
				msg1 = bex.getMessage(indent*i);
146
			} else {
147
				msg1 = insertBlanksAtStart(ex.getMessage(),indent*i);
148
			}
149
			if(msg1!=null && !msg1.equals("")){
150
				if( msg.equals("")) {
151
					msg = msg1 ;					
152
				} else {
153
					msg = msg + "\n" + msg1 ;
154
				}
155
			}
156
			i++;
157
		}
158
		return msg;
159
	}
160

  
161

  
162
	/* (non-Javadoc)
163
	 * @see org.gvsig.exceptions.IBaseException#getLocalizedMessageStack()
164
	 */
165
	public String getLocalizedMessageStack() {
166
		return getLocalizedMessageStack(BaseException.translator,0);
167
	}
168

  
169
	/* (non-Javadoc)
170
	 * @see org.gvsig.exceptions.IBaseException#getLocalizedMessageStack(org.gvsig.exceptions.IExceptionTranslator, int)
171
	 */
172
	public String getLocalizedMessageStack(IExceptionTranslator translator, int indent) {
173
		Iterator iter = this.iterator();
174
		String msg="";
175
		Exception ex;
176
		while (iter.hasNext()){
177
			ex = ((Exception)iter.next());
178
			if ( ex instanceof BaseException ) {
179
				BaseException bex = (BaseException) ex;
180
				if( msg.equals("") ) {
181
					msg = bex.getLocalizedMessage(translator,indent);
182
				} else {
183
					msg = msg + "\n" + bex.getLocalizedMessage(translator,indent).trim();
184
				}
185
			} else {
186
				if( msg.equals("") ) {
187
					msg = ex.getLocalizedMessage();			
188
				} else {
189
					msg = msg + "\n" + ex.getLocalizedMessage();
190
				}
191
			}
192
		}
193
		return msg;
194
	}
195

  
196
	/**
197
	 * Inserts blanks at the start of a string.
198
	 *
199
	 * @param str A string.
200
	 * @param len Quantity of blanks to insert at the start of str.
201
	 * @return A string compund by the quantity of blanks that
202
	 *         len indicates and str.
203
	 */
204
	static String insertBlanksAtStart(String str, int len){
205
		try {
206
			return BLANKS.substring(0,len)+str;
207
		} catch (IndexOutOfBoundsException e) {
208
			return BLANKS + str;
209
		}
210
	}
211

  
212
	/* (non-Javadoc)
213
	 * @see org.gvsig.exceptions.IBaseException#getCode()
214
	 */
215
	public long getCode() {
216
		return this.code;
217
	}
218

  
219
	/**
220
	 * Sets the exception's code.
221
	 */
222
	public void setCode(long code) {
223
		this.code = code;
224
	}
225

  
226
	/* (non-Javadoc)
227
	 * @see org.gvsig.exceptions.IBaseException#getFormatString()
228
	 */
229
	public String getFormatString() {
230
		return this.formatString;
231
	}
232

  
233
	/**
234
	 * Sets the format string.
235
	 *
236
	 * @param formatString
237
	 */
238
	public void setFormatString(String formatString) {
239
		this.formatString = formatString;
240
	}
241

  
242
	/* (non-Javadoc)
243
	 * @see org.gvsig.exceptions.IBaseException#getMessageKey()
244
	 */
245
	public String getMessageKey() {
246
		return this.messageKey;
247
	}
248

  
249
	/**
250
	 * Sets the property messageKey.
251
	 *
252
	 * @param messageKey
253
	 */
254
	public void setMessageKey(String messageKey) {
255
		this.messageKey = messageKey;
256
	}
257

  
258
	/* (non-Javadoc)
259
	 * @see org.gvsig.exceptions.IBaseException#iterator()
260
	 */
261
	public Iterator iterator() {
262
		return new BaseExceptionIterator(this);
263
	}
264

  
265
	/**
266
	 * @return A map that serves to replace in the format string
267
	 * the keys with the corresponding values.
268
	 */
269
	abstract protected Map values();
270

  
271
	/**
272
	 * Sets the property translator.
273
	 * @param translator It(He,She) is used to translate
274
	 *        the messages associated with the exceptions.
275
	 */
276
	public static void setTranslator(IExceptionTranslator translator){
277
		BaseException.translator = translator;
278
	}
279

  
280
	public static void setTranslator(Object translator){
281
		BaseException.translator = new TranslatorWraper(translator);
282
	}
283

  
284
	public String toString(){
285
		return format(this.formatString, values());
286
	}
287

  
288
}
289

  
290
class TranslatorWraper implements IExceptionTranslator {
291

  
292
	private Object translator = null;
293
	private Method method = null;
294

  
295
	public TranslatorWraper(Object translator) {
296
		Class theClass = translator.getClass();
297
		String s = "";
298

  
299
		this.translator = translator;
300
		try {
301
			method = theClass.getMethod("getText",new Class[] { s.getClass() });
302
		} catch (Exception e) {
303
			throw new RuntimeException("El objeto translator suministrado no tiene el metodo getText apropiado.", e);
304
		}
305

  
306
	}
307

  
308
	public String getText(String key) {
309
		try {
310
			return (String)(method.invoke(translator,new String[] { key }));
311
		} catch (Exception e) {
312
			return key;
313
		}
314
	}
315

  
316
}
0 317

  
tags/v_1_12_0_Build_1402/libExceptions/pom.xml
1
<project xmlns="http://maven.apache.org/POM/4.0.0"
2
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
4
	<modelVersion>4.0.0</modelVersion>
5
	<groupId>org.gvsig</groupId>
6
	<artifactId>libExceptions</artifactId>
7
	<packaging>jar</packaging>
8
	<version>1.9-SNAPSHOT</version>
9
	<name>libExceptions</name>
10
	<url>http://maven.apache.org</url>
11
	<parent>
12
		<groupId>org.gvsig</groupId>
13
		<artifactId>gvsig-library-base-pom</artifactId>
14
		<version>1.9-SNAPSHOT</version>
15
	</parent>
16
	<dependencies>
17
	</dependencies>
18
	<properties>
19
        <build-dir>${basedir}/../build</build-dir>
20
    </properties>
21
	<build>
22
		<sourceDirectory>src</sourceDirectory>
23
		<testSourceDirectory>src-test</testSourceDirectory>
24
	</build>
25
</project>
tags/v_1_12_0_Build_1402/libExceptions/.cvsignore
1
bin
2
bin-test
3
dist/*
0 4

  
tags/v_1_12_0_Build_1402/libExceptions/build.xml
1
<project name="libExceptions" default="create-jar" basedir=".">
2
  	<!-- set global properties for this build -->
3
	<property name="src"  location="src"/>
4
	<property name="build"  location="bin"/>
5
	<property name="src-test"  location="src-test"/>
6
	<property name="build-test"  location="bin-test"/>
7
	<property name="dist"  location="dist"/>
8
	<property name="targetDir" location="dist"/>
9
	<property name="fmapLibs" location="../libFMap/lib" />
10
	<property name="andamiLibs" location="../_fwAndami/lib" />
11
	<property name="remoteServicesLibs" location="../libRemoteServices/lib" />
12
	<property name="jarName" value="org.gvsig.exceptions"/>
13
	<!--<property name="compile-classpath" value="" />-->
14
	<import file="../binaries/ant/utilities.xml"/>
15

  
16
  <target name="init">
17
    <!-- Create the time stamp -->
18
    <tstamp/>
19
	<echo>
20
		Compiling ${ant.project.name}...</echo>
21
  </target>
22

  
23
	<target name="batch-build"
24
				description="compile the sources, create the jar file"
25
				depends="init,compile,create-jar">
26
	</target>
27

  
28
	<target name="compile" description="compile the source">
29
		<!-- Compile the Java code from ${src} to ${build} -->
30
		<mkdir dir="${build}" />
31
		<loadEclipseClasspath project="${basedir}"/>
32
		<gvSIG-javac
33
			classpath="${eclipseClasspath}"
34
		/>
35

  
36
		<!--<javac	srcdir="${src}"
37
				destdir="${build}"
38
				source="1.4"
39
				target="1.4"
40
				debug="${debug}"
41
				debuglevel="${debuglevel}"
42
				classpath="${compile-classpath}"/>-->
43
		<!-- copy any images or resources present in the src dir -->
44
		<copy todir="${build}">
45
			<fileset dir="${src}" excludes="**/*.java" casesensitive="false"/>
46
		</copy>
47
	</target>
48

  
49
	<target name="create-jar" description="Crea el jar de la aplicacion">
50
		<mkdir dir="${dist}" />
51
	  	<jar jarfile="${targetDir}/${jarName}.jar" basedir="${build}"/>
52
		<copy todir="${andamiLibs}">
53
			<fileset dir="${targetDir}" includes="${jarName}.jar" />
54
		</copy>
55
	</target>
56

  
57
	<target name="clean" description="clean up">
58
		<!-- Delete the ${build} and ${dist} directory trees -->
59
		<delete dir="${build}" />
60
		<delete dir="${dist}" />
61
	</target>
62

  
63
	<target name="run-tests" depends="batch-build,compile-tests">
64
		<antcall target="generic-run-tests">
65
			<param name="TestSuite.Name" value="org.gvsig.gui.beans.AllTests"/>
66
		</antcall>
67
	</target>
68

  
69
</project>
0 70

  
tags/v_1_12_0_Build_1402/libExceptions/.classpath
1
<?xml version="1.0" encoding="UTF-8"?>
2
<classpath>
3
	<classpathentry kind="src" path="src"/>
4
	<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
5
	<classpathentry output="bin-test" kind="src" path="src-test"/>
6
	<classpathentry kind="var" path="JUNIT_HOME/junit.jar"/>
7
	<classpathentry kind="lib" path="lib/log4j-1.2.8.jar"/>
8
	<classpathentry kind="output" path="bin"/>
9
</classpath>
0 10

  

Also available in: Unified diff