View Javadoc

1   package org.hyphenType.lexerparser.exceptions;
2   
3   import java.text.MessageFormat;
4   import java.util.MissingResourceException;
5   import java.util.ResourceBundle;
6   
7   import org.hyphenType.datastructure.Options;
8   import org.hyphenType.util.I18NResourceBundle;
9   
10  public abstract class OptionValuesException extends Exception {
11  
12      /**
13       * 
14       */
15      private static final long serialVersionUID = -4134292017377886083L;
16      
17      private String localizedMessage;
18      
19      public OptionValuesException(Class<? extends Options<?>> optionsInterface, String pattern, Object... arguments) {
20          super(MessageFormat.format(pattern, arguments));
21          ResourceBundle rb = new I18NResourceBundle(optionsInterface);
22          String key = this.getClass().getName();
23          try {
24              if(rb.containsKey(key)) {
25                  this.localizedMessage = MessageFormat.format(rb.getString(key), arguments);
26              } else {
27                  this.localizedMessage = this.getMessage();
28              }
29          } catch (MissingResourceException e) {
30              System.err.println("Key " + key + " not found.");
31              this.localizedMessage = this.getMessage();
32          }
33      }
34      
35      public OptionValuesException(Throwable cause, Class<? extends Options<?>> optionsInterface, String pattern, Object... arguments) {
36          super(MessageFormat.format(pattern, arguments), cause);
37          ResourceBundle rb = new I18NResourceBundle(optionsInterface);
38          String key = this.getClass().getName();
39          try {
40              if(rb.containsKey(key)) {
41                  this.localizedMessage = MessageFormat.format(rb.getString(key), arguments);
42              } else {
43                  this.localizedMessage = this.getMessage();
44              }
45          } catch (MissingResourceException e) {
46              System.err.println("Key " + key + " not found.");
47              this.localizedMessage = this.getMessage();
48          }
49      }
50      
51      @Override
52      public String getLocalizedMessage() {
53          return localizedMessage;
54      }
55  }