1   /* Copyright 2013-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.rugged.errors;
18  
19  import java.io.IOException;
20  import java.io.InputStream;
21  import java.io.InputStreamReader;
22  import java.net.URL;
23  import java.net.URLConnection;
24  import java.nio.charset.StandardCharsets;
25  import java.util.Locale;
26  import java.util.MissingResourceException;
27  import java.util.PropertyResourceBundle;
28  import java.util.ResourceBundle;
29  
30  import org.hipparchus.exception.Localizable;
31  
32  
33  /**
34   * Enumeration for localized messages formats.
35   * <p>
36   * The constants in this enumeration represent the available
37   * formats as localized strings. These formats are intended to be
38   * localized using simple properties files, using the constant
39   * name as the key and the property value as the message format.
40   * The source English format is provided in the constants themselves
41   * to serve both as a reminder for developers to understand the parameters
42   * needed by each format, as a basis for translators to create
43   * localized properties files, and as a default format if some
44   * translation is missing.
45   * </p>
46   * <p>
47   * This class is heavily based on {@code OrekitMessages},
48   * which is distributed under the terms of the Apache License V2.
49   * </p>
50   */
51  public enum RuggedMessages implements Localizable {
52  
53  
54      /** INTERNAL_ERROR. */
55      INTERNAL_ERROR("internal error, please notify development team by creating an issue at {0}"),
56      /** OUT_OF_TILE_INDICES. */
57      OUT_OF_TILE_INDICES("no data at indices [{0}, {1}], tile only covers from [0, 0] to [{2}, {3}] (inclusive)"),
58      /** OUT_OF_TILE_ANGLES. */
59      OUT_OF_TILE_ANGLES("no data at latitude {0} and longitude {1}, tile covers only latitudes {2} to {3} and longitudes {4} to {5}"),
60      /** NO_DEM_DATA. */
61      NO_DEM_DATA("no Digital Elevation Model data at latitude {0} and longitude {1}"),
62      /** TILE_WITHOUT_REQUIRED_NEIGHBORS_SELECTED. */
63      TILE_WITHOUT_REQUIRED_NEIGHBORS_SELECTED("the tile selected for latitude {0} and longitude {1} does not contain required point neighborhood"),
64      /** OUT_OF_TIME_RANGE. */
65      OUT_OF_TIME_RANGE("date {0} is out of time span [{1}, {2}] (UTC)"),
66      /** UNINITIALIZED_CONTEXT. */
67      UNINITIALIZED_CONTEXT("general context has not been initialized (missing call to {0})"),
68      /** EMPTY_TILE. */
69      EMPTY_TILE("tile is empty: {0} ⨉ {1}"),
70      /** UNKNOWN_SENSOR. */
71      UNKNOWN_SENSOR("unknown sensor {0}"),
72      /** LINE_OF_SIGHT_DOES_NOT_REACH_GROUND. */
73      LINE_OF_SIGHT_DOES_NOT_REACH_GROUND("line-of-sight does not reach ground"),
74      /** LINE_OF_SIGHT_NEVER_CROSSES_LATITUDE. */
75      LINE_OF_SIGHT_NEVER_CROSSES_LATITUDE("line-of-sight never crosses latitude {0}"),
76      /** LINE_OF_SIGHT_NEVER_CROSSES_LONGITUDE. */
77      LINE_OF_SIGHT_NEVER_CROSSES_LONGITUDE("line-of-sight never crosses longitude {0}"),
78      /** LINE_OF_SIGHT_NEVER_CROSSES_ALTITUDE. */
79      LINE_OF_SIGHT_NEVER_CROSSES_ALTITUDE("line-of-sight never crosses altitude {0}"),
80      /** DEM_ENTRY_POINT_IS_BEHIND_SPACECRAFT. */
81      DEM_ENTRY_POINT_IS_BEHIND_SPACECRAFT("line-of-sight enters the Digital Elevation Model behind spacecraft!"),
82      /** FRAMES_MISMATCH_WITH_INTERPOLATOR_DUMP. */
83      FRAMES_MISMATCH_WITH_INTERPOLATOR_DUMP("frame {0} does not match frame {1} from interpolator dump"),
84      /** NOT_INTERPOLATOR_DUMP_DATA. */
85      NOT_INTERPOLATOR_DUMP_DATA("data is not an interpolator dump"),
86      /** DEBUG_DUMP_ALREADY_ACTIVE. */
87      DEBUG_DUMP_ALREADY_ACTIVE("debug dump is already active for this thread"),
88      /** DEBUG_DUMP_ACTIVATION_ERROR. */
89      DEBUG_DUMP_ACTIVATION_ERROR("unable to active debug dump with file {0}: {1}"),
90      /** DEBUG_DUMP_NOT_ACTIVE. */
91      DEBUG_DUMP_NOT_ACTIVE("debug dump is not active for this thread"),
92      /** CANNOT_PARSE_LINE. */
93      CANNOT_PARSE_LINE("cannot parse line {0}, file {1}: {2}"),
94      /** LIGHT_TIME_CORRECTION_REDEFINED. */
95      LIGHT_TIME_CORRECTION_REDEFINED("light time correction redefined, line {0}, file {1}: {2}"),
96      /** ABERRATION_OF_LIGHT_CORRECTION_REDEFINED. */
97      ABERRATION_OF_LIGHT_CORRECTION_REDEFINED("aberration of light correction redefined, line {0}, file {1}: {2}"),
98      /** ATMOSPHERIC_REFRACTION_REDEFINED. */
99      ATMOSPHERIC_REFRACTION_REDEFINED("atmospheric refraction correction redefined, line {0}, file {1}: {2}"),
100     /** TILE_ALREADY_DEFINED. */
101     TILE_ALREADY_DEFINED("tile {0} already defined, line {1}, file {2}: {3}"),
102     /** UNKNOWN_TILE. */
103     UNKNOWN_TILE("unknown tile {0}, line {1}, file {2}: {3}"),
104     /** NO_PARAMETERS_SELECTED. */
105     NO_PARAMETERS_SELECTED("no parameters have been selected for estimation"),
106     /** NO_REFERENCE_MAPPINGS. */
107     NO_REFERENCE_MAPPINGS("no reference mappings for parameters estimation"),
108     /** DUPLICATED_PARAMETER_NAME. */
109     DUPLICATED_PARAMETER_NAME("a different parameter with name {0} already exists"),
110     /** INVALID_RUGGED_NAME. */
111     INVALID_RUGGED_NAME("invalid rugged name"),
112     /** UNSUPPORTED_REFINING_CONTEXT. */
113     UNSUPPORTED_REFINING_CONTEXT("refining using {0} rugged instance is not handled"),
114     /** NO_LAYER_DATA. */
115     NO_LAYER_DATA("no atmospheric layer data at altitude {0} (lowest altitude: {1})"),
116     /** INVALID_STEP. */
117     INVALID_STEP("step {0} is not valid : {1}"),
118     /** INVALID_RANGE_FOR_LINES. */
119     INVALID_RANGE_FOR_LINES("range between min line {0} and max line {1} is invalid {2}"),
120     /** SENSOR_PIXEL_NOT_FOUND_IN_RANGE_LINES. */
121     SENSOR_PIXEL_NOT_FOUND_IN_RANGE_LINES("impossible to find sensor pixel in given range lines (with atmospheric refraction) between lines {0} and {1}"),
122     /** SENSOR_PIXEL_NOT_FOUND_IN_PIXELS_LINE. */
123     SENSOR_PIXEL_NOT_FOUND_IN_PIXELS_LINE("impossible to find sensor pixel: pixel {0} outside interval [ {1} , {2} [ (with atmospheric refraction margin = {3})");
124 
125     /** Base name of the resource bundle in classpath. */
126     private static final String RESOURCE_BASE_NAME = "assets/org/orekit/rugged/RuggedMessages";
127 
128     /** Source English format. */
129     private final String sourceFormat;
130 
131     /** Simple constructor.
132      * @param sourceFormat source English format to use when no
133      * localized version is available
134      */
135     RuggedMessages(final String sourceFormat) {
136         this.sourceFormat = sourceFormat;
137     }
138 
139     /** {@inheritDoc} */
140     @Override
141     public String getSourceString() {
142         return sourceFormat;
143     }
144 
145     /** {@inheritDoc} */
146     @Override
147     public String getLocalizedString(final Locale locale) {
148         try {
149             final ResourceBundle bundle =
150                     ResourceBundle.getBundle(RESOURCE_BASE_NAME, locale, new UTF8Control());
151             if (bundle.getLocale().getLanguage().equals(locale.getLanguage())) {
152                 final String translated = bundle.getString(name());
153                 if (translated.length() > 0 &&
154                     !translated.toLowerCase(locale).contains("missing translation")) {
155                     // the value of the resource is the translated format
156                     return translated;
157                 }
158             }
159 
160         } catch (MissingResourceException mre) {
161             // do nothing here
162         }
163 
164         // either the locale is not supported or the resource is not translated or
165         // it is unknown: don't translate and fall back to using the source format
166         return sourceFormat;
167 
168     }
169 
170     /** Control class loading properties in UTF-8 encoding.
171      * <p>
172      * This class has been very slightly adapted from BalusC answer to question: <a
173      * href="https://stackoverflow.com/questions/4659929/how-to-use-utf-8-in-resource-properties-with-resourcebundle">
174      * How to use UTF-8 in resource properties with ResourceBundle</a>.
175      * </p>
176      */
177     public static class UTF8Control extends ResourceBundle.Control {
178 
179         /** {@inheritDoc} */
180         @Override
181         public ResourceBundle newBundle(final String baseName, final Locale locale, final String format,
182                                         final ClassLoader loader, final boolean reload) throws IOException {
183             // The below is a copy of the default implementation.
184             final String bundleName = toBundleName(baseName, locale);
185             final String resourceName = toResourceName(bundleName, "utf8");
186             ResourceBundle bundle = null;
187             InputStream stream = null;
188             if (reload) {
189                 final URL url = loader.getResource(resourceName);
190                 if (url != null) {
191                     final URLConnection connection = url.openConnection();
192                     if (connection != null) {
193                         connection.setUseCaches(false);
194                         stream = connection.getInputStream();
195                     }
196                 }
197             } else {
198                 stream = loader.getResourceAsStream(resourceName);
199             }
200             if (stream != null) {
201                 try (InputStreamReader inputStreamReader = new InputStreamReader(stream, StandardCharsets.UTF_8)) {
202                     // Only this line is changed to make it to read properties files as UTF-8.
203                     bundle = new PropertyResourceBundle(inputStreamReader);
204                 }
205             }
206             return bundle;
207         }
208 
209     }
210 
211 }