1   /* Copyright 2002-2026 CS GROUP
2    * Licensed to CS GROUP (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  
19  import java.io.Serial;
20  import java.text.MessageFormat;
21  import java.text.ParseException;
22  import java.util.Locale;
23  
24  import org.hipparchus.exception.Localizable;
25  
26  /** Extension of {@link java.text.ParseException} with localized message.
27   * @since 7.1
28   */
29  public class OrekitParseException extends ParseException implements LocalizedException {
30  
31      /** Serializable UID. */
32      @Serial
33      private static final long serialVersionUID = 20150611L;
34  
35      /** Format specifier (to be translated). */
36      private final Localizable specifier;
37  
38      /** Parts to insert in the format (no translation). */
39      private final Object[] parts;
40  
41      /** Create an exception with localized message.
42       * @param specifier format specifier (to be translated)
43       * @param parts parts to insert in the format (no translation)
44       */
45      public OrekitParseException(final Localizable specifier, final Object... parts) {
46          super("", 0);
47          this.specifier = specifier;
48          this.parts     = (parts == null) ? new Object[0] : parts.clone();
49      }
50  
51      /** {@inheritDoc} */
52      @Override
53      public String getMessage(final Locale locale) {
54          return buildMessage(locale);
55      }
56  
57      /** {@inheritDoc} */
58      @Override
59      public String getMessage() {
60          return buildMessage(Locale.US);
61      }
62  
63      /** {@inheritDoc} */
64      @Override
65      public String getLocalizedMessage() {
66          return buildMessage(Locale.getDefault());
67      }
68  
69      /** {@inheritDoc} */
70      @Override
71      public Localizable getSpecifier() {
72          return specifier;
73      }
74  
75      /** {@inheritDoc} */
76      @Override
77      public Object[] getParts() {
78          return parts.clone();
79      }
80  
81      /**
82       * Builds a message string by from a pattern and its arguments.
83       * @param locale Locale in which the message should be translated
84       * @return a message string
85       */
86      private String buildMessage(final Locale locale) {
87          return (specifier == null) ?
88                  "" :
89                  new MessageFormat(specifier.getLocalizedString(locale), locale).format(parts);
90      }
91  
92  }