001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.commons.nabla.core;
018    
019    import java.text.MessageFormat;
020    import java.util.ResourceBundle;
021    import java.util.MissingResourceException;
022    
023    /** This class is the base class for all specific exceptions thrown by
024     * the nabla classes.
025    
026     * <p>When the nabla classes throw exceptions that are specific to
027     * the package, these exceptions are always subclasses of
028     * NablaException. When exceptions that are already covered by the
029     * standard java API should be thrown, like
030     * ArrayIndexOutOfBoundsException or IllegalArgumentException, these
031     * standard exceptions are thrown rather than the nabla specific
032     * ones.</p>
033    
034     * @version $Id: nablaException.java 1686 2005-12-16 12:59:51Z luc $
035    
036     */
037    
038    public class NablaException extends Exception {
039    
040        /** Version UID. */
041        private static final long serialVersionUID = 9178429434459078429L;
042    
043        /** Localized messages resources. */
044        private static ResourceBundle resources =
045            ResourceBundle.getBundle("META-INF/localization/ExceptionsMessages");
046    
047        /** Simple constructor.
048         * Build an exception by translating and formating a message
049         * @param specifier format specifier (to be translated)
050         * @param parts to insert in the format (no translation)
051         */
052        public NablaException(final String specifier, final Object... parts) {
053            super(translate(specifier, parts));
054        }
055    
056        /** Translate a string.
057         * @param s string to translate
058         * @return translated string
059         */
060        public static String translate(final String s) {
061            try {
062                return resources.getString(s);
063            } catch (MissingResourceException mre) {
064                return s;
065            }
066        }
067    
068        /** Translate a message.
069         * @param specifier format specifier (to be translated)
070         * @param parts to insert in the format (no translation)
071         * @return translated message
072         */
073        public static String translate(final String specifier,
074                                       final Object... parts) {
075            return new MessageFormat(translate(specifier)).format(parts);
076        }
077    
078    }