View Javadoc

1   package net.sf.layoutParser.exception;
2   
3   import java.text.MessageFormat;
4   import java.util.PropertyResourceBundle;
5   import java.util.ResourceBundle;
6   
7   import net.sf.layoutParser.context.LayoutCatalog;
8   
9   /**
10   * This is the base exception for all concrete exceptions implemented in the framework. <br/>
11   * It provides a mean to throw localized exceptions based on properties files on the system. These files <br/>
12   * should begin with "ExceptionsBundle" name followed by their country code name. i.e.: <br/>
13   * <p>For Deutch the file should be named: ExceptionsBundle_de.properties <br/>
14   * For Portugues the file should be named: ExceptionsBundle_pt.properties <p/>
15   * This is defined by the constant {@link ExceptionKey#BUNDLE_NAME}.
16   * 
17   * Each key on the message file should be parameterized in the enumeration {@link ExceptionKey}, <br/>
18   * otherwise it wont throw the message correctly. 
19   * 
20   * @author Mário Valentim Junior
21   * @since 1.0
22   * @date 12/06/2008
23   */
24  public abstract class BaseLocalizedException extends Exception{
25  	private static final long serialVersionUID = -3919243068930356929L;
26  	
27  	private static ResourceBundle bundle = LayoutCatalog.getInstance().getLocale() == null ? 
28  		PropertyResourceBundle.getBundle(ExceptionKey.BUNDLE_NAME.toString()) :
29  		PropertyResourceBundle.getBundle(ExceptionKey.BUNDLE_NAME.toString(), LayoutCatalog.getInstance().getLocale())	;
30  	
31  	/**
32  	 * @see Exception#Exception()
33  	 */
34  	public BaseLocalizedException(){
35  		super();
36  	}
37  	
38  	/**
39  	 * Create's a new exception with the message that matches the key in the bundle exception file.
40  	 * This should be thrown when the message doesn't have parameters in it.
41  	 * 
42  	 * @param key
43  	 * 		The key of the message on the properties file
44  	 * @see Exception#Exception(String)
45  	 */
46  	public BaseLocalizedException(ExceptionKey key) {
47  		super(bundle.getString(key.toString()));
48  	}
49  	
50  	/**
51  	 * Create's a new exception with the message that matches the key in the bundle exception file.
52  	 * This should be thrown when the message have parameters in it.
53  	 * 
54  	 * @param key
55  	 * 		The key of the message on the properties file
56  	 * @param parameters
57  	 * 		The parameters of the message
58  	 * @see Exception#Exception(String)
59  	 */
60  	public BaseLocalizedException(ExceptionKey key, Object[] parameters) {
61  		super(MessageFormat.format(bundle.getString(key.toString()), parameters));
62  	}
63  	
64  	/**
65  	 * Create's a new exception with the message that matches the key in the bundle exception file. <br/>
66  	 * This should be thrown when the message have parameters in it and there's a root exception that caused this message. <br/>
67  	 * 
68  	 * @param key
69  	 * 		The key of the message on the properties file
70  	 * @param parameters
71  	 * 		The parameters of the message
72  	 * @param cause
73  	 * 		The root cause of the exception
74  	 * @see Exception#Exception(String, Throwable)
75  	 */
76  	public BaseLocalizedException(ExceptionKey key, Object[] parameters, Throwable cause) {
77  		super(MessageFormat.format(bundle.getString(key.toString()), parameters), cause);
78  	}
79  
80  	/**
81  	 * Create's a new exception with the message that matches the key in the bundle exception file. <br/>
82  	 * This should be thrown when the message doesn't have parameters in it and there's a root exception that caused this message. <br/>
83  	 * 
84  	 * @param key
85  	 * @param cause
86  	 * @see Exception#Exception(String, Throwable)
87  	 */
88  	public BaseLocalizedException(ExceptionKey key, Throwable cause) {
89  		super(bundle.getString(key.toString()), cause);
90  	}
91  
92  	/**
93  	 * Create a new base exception that encapsulates a root exception.
94  	 * 
95  	 * @param cause
96  	 * 		The root cause of the exception
97  	 * @see Exception#Exception(Throwable)
98  	 */
99  	public BaseLocalizedException(Throwable cause) {
100 		super(cause);
101 	}
102 
103 }