OrekitParseException.java

  1. /* Copyright 2002-2016 CS Systèmes d'Information
  2.  * Licensed to CS Systèmes d'Information (CS) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * CS 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.orekit.errors;

  18. import java.text.MessageFormat;
  19. import java.text.ParseException;
  20. import java.util.Locale;

  21. import org.apache.commons.math3.exception.util.Localizable;

  22. /** Extension of {@link java.text.ParseException} with localized message.
  23.  * @since 7.1
  24.  */
  25. public class OrekitParseException extends ParseException implements LocalizedException {

  26.     /** Serializable UID. */
  27.     private static final long serialVersionUID = 20150611L;

  28.     /** Format specifier (to be translated). */
  29.     private final Localizable specifier;

  30.     /** Parts to insert in the format (no translation). */
  31.     private final Object[] parts;

  32.     /** Create an exception with localized message.
  33.      * @param specifier format specifier (to be translated)
  34.      * @param parts parts to insert in the format (no translation)
  35.      */
  36.     public OrekitParseException(final Localizable specifier, final Object ... parts) {
  37.         super("", 0);
  38.         this.specifier = specifier;
  39.         this.parts     = (parts == null) ? new Object[0] : parts.clone();
  40.     }

  41.     /** {@inheritDoc} */
  42.     @Override
  43.     public String getMessage(final Locale locale) {
  44.         return buildMessage(locale);
  45.     }

  46.     /** {@inheritDoc} */
  47.     @Override
  48.     public String getMessage() {
  49.         return buildMessage(Locale.US);
  50.     }

  51.     /** {@inheritDoc} */
  52.     @Override
  53.     public String getLocalizedMessage() {
  54.         return buildMessage(Locale.getDefault());
  55.     }

  56.     /** {@inheritDoc} */
  57.     @Override
  58.     public Localizable getSpecifier() {
  59.         return specifier;
  60.     }

  61.     /** {@inheritDoc} */
  62.     @Override
  63.     public Object[] getParts() {
  64.         return parts.clone();
  65.     }

  66.     /**
  67.      * Builds a message string by from a pattern and its arguments.
  68.      * @param locale Locale in which the message should be translated
  69.      * @return a message string
  70.      */
  71.     private String buildMessage(final Locale locale) {
  72.         return (specifier == null) ?
  73.                 "" :
  74.                 new MessageFormat(specifier.getLocalizedString(locale), locale).format(parts);
  75.     }

  76. }