View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.nabla.core;
18  
19  import java.text.MessageFormat;
20  import java.util.ResourceBundle;
21  import java.util.MissingResourceException;
22  
23  /** This class is the base class for all specific exceptions thrown by
24   * the nabla classes.
25  
26   * <p>When the nabla classes throw exceptions that are specific to
27   * the package, these exceptions are always subclasses of
28   * NablaException. When exceptions that are already covered by the
29   * standard java API should be thrown, like
30   * ArrayIndexOutOfBoundsException or IllegalArgumentException, these
31   * standard exceptions are thrown rather than the nabla specific
32   * ones.</p>
33  
34   * @version $Id: nablaException.java 1686 2005-12-16 12:59:51Z luc $
35  
36   */
37  
38  public class NablaException extends Exception {
39  
40      /** Version UID. */
41      private static final long serialVersionUID = 9178429434459078429L;
42  
43      /** Localized messages resources. */
44      private static ResourceBundle resources =
45          ResourceBundle.getBundle("META-INF/localization/ExceptionsMessages");
46  
47      /** Simple constructor.
48       * Build an exception by translating and formating a message
49       * @param specifier format specifier (to be translated)
50       * @param parts to insert in the format (no translation)
51       */
52      public NablaException(final String specifier, final Object... parts) {
53          super(translate(specifier, parts));
54      }
55  
56      /** Translate a string.
57       * @param s string to translate
58       * @return translated string
59       */
60      public static String translate(final String s) {
61          try {
62              return resources.getString(s);
63          } catch (MissingResourceException mre) {
64              return s;
65          }
66      }
67  
68      /** Translate a message.
69       * @param specifier format specifier (to be translated)
70       * @param parts to insert in the format (no translation)
71       * @return translated message
72       */
73      public static String translate(final String specifier,
74                                     final Object... parts) {
75          return new MessageFormat(translate(specifier)).format(parts);
76      }
77  
78  }