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