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.math;
018    
019    import java.io.PrintStream;
020    import java.io.PrintWriter;
021    import java.text.MessageFormat;
022    import java.util.Locale;
023    import java.util.MissingResourceException;
024    import java.util.ResourceBundle;
025    
026    
027    /**
028    * Base class for commons-math checked exceptions.
029    * <p>
030    * Supports nesting, emulating JDK 1.4 behavior if necessary.</p>
031    * <p>
032    * Adapted from <a href="http://commons.apache.org/collections/api-release/org/apache/commons/collections/FunctorException.html"/>.</p>
033    *
034    * @version $Revision: 822850 $ $Date: 2009-10-07 14:56:42 -0400 (Wed, 07 Oct 2009) $
035    */
036    public class MathException extends Exception {
037    
038        /** Serializable version identifier. */
039        private static final long serialVersionUID = -9004610152740737812L;
040    
041        /**
042         * Pattern used to build the message.
043         */
044        private final String pattern;
045    
046        /**
047         * Arguments used to build the message.
048         */
049        private final Object[] arguments;
050    
051        /**
052         * Constructs a new <code>MathException</code> with no
053         * detail message.
054         */
055        public MathException() {
056            this.pattern   = null;
057            this.arguments = new Object[0];
058        }
059    
060        /**
061         * Constructs a new <code>MathException</code> with specified
062         * formatted detail message.
063         * Message formatting is delegated to {@link java.text.MessageFormat}.
064         * @param pattern format specifier
065         * @param arguments format arguments
066         */
067        public MathException(String pattern, Object ... arguments) {
068          this.pattern   = pattern;
069          this.arguments = (arguments == null) ? new Object[0] : arguments.clone();
070        }
071    
072        /**
073         * Constructs a new <code>MathException</code> with specified
074         * nested <code>Throwable</code> root cause.
075         *
076         * @param rootCause  the exception or error that caused this exception
077         *                   to be thrown.
078         */
079        public MathException(Throwable rootCause) {
080            super(rootCause);
081            this.pattern   = getMessage();
082            this.arguments = new Object[0];
083        }
084    
085        /**
086         * Constructs a new <code>MathException</code> with specified
087         * formatted detail message and nested <code>Throwable</code> root cause.
088         * Message formatting is delegated to {@link java.text.MessageFormat}.
089         * @param rootCause the exception or error that caused this exception
090         * to be thrown.
091         * @param pattern format specifier
092         * @param arguments format arguments
093         * @since 1.2
094         */
095        public MathException(Throwable rootCause, String pattern, Object ... arguments) {
096          super(rootCause);
097          this.pattern   = pattern;
098          this.arguments = (arguments == null) ? new Object[0] : arguments.clone();
099        }
100    
101        /**
102         * Translate a string to a given locale.
103         * @param s string to translate
104         * @param locale locale into which to translate the string
105         * @return translated string or original string
106         * for unsupported locales or unknown strings
107         */
108        private static String translate(String s, Locale locale) {
109            try {
110                ResourceBundle bundle =
111                        ResourceBundle.getBundle("org.apache.commons.math.MessagesResources", locale);
112                if (bundle.getLocale().getLanguage().equals(locale.getLanguage())) {
113                    // the value of the resource is the translated string
114                    return bundle.getString(s);
115                }
116    
117            } catch (MissingResourceException mre) {
118                // do nothing here
119            }
120    
121            // the locale is not supported or the resource is unknown
122            // don't translate and fall back to using the string as is
123            return s;
124    
125        }
126    
127        /** Gets the pattern used to build the message of this throwable.
128         *
129         * @return the pattern used to build the message of this throwable
130         * @since 1.2
131         */
132        public String getPattern() {
133            return pattern;
134        }
135    
136        /** Gets the arguments used to build the message of this throwable.
137         *
138         * @return the arguments used to build the message of this throwable
139         * @since 1.2
140         */
141        public Object[] getArguments() {
142            return arguments.clone();
143        }
144    
145        /** Gets the message in a specified locale.
146         *
147         * @param locale Locale in which the message should be translated
148         *
149         * @return localized message
150         * @since 1.2
151         */
152        public String getMessage(final Locale locale) {
153            return (pattern == null) ? "" : new MessageFormat(translate(pattern, locale), locale).format(arguments);
154        }
155    
156        /** {@inheritDoc} */
157        @Override
158        public String getMessage() {
159            return getMessage(Locale.US);
160        }
161    
162        /** {@inheritDoc} */
163        @Override
164        public String getLocalizedMessage() {
165            return getMessage(Locale.getDefault());
166        }
167    
168        /**
169         * Prints the stack trace of this exception to the standard error stream.
170         */
171        @Override
172        public void printStackTrace() {
173            printStackTrace(System.err);
174        }
175    
176        /**
177         * Prints the stack trace of this exception to the specified stream.
178         *
179         * @param out  the <code>PrintStream</code> to use for output
180         */
181        @Override
182        public void printStackTrace(PrintStream out) {
183            synchronized (out) {
184                PrintWriter pw = new PrintWriter(out, false);
185                printStackTrace(pw);
186                // Flush the PrintWriter before it's GC'ed.
187                pw.flush();
188            }
189        }
190    
191    }