RuggedMessages.java

  1. /* Copyright 2013-2020 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. import java.io.IOException;
  19. import java.io.InputStream;
  20. import java.io.InputStreamReader;
  21. import java.net.URL;
  22. import java.net.URLConnection;
  23. import java.nio.charset.StandardCharsets;
  24. import java.util.Locale;
  25. import java.util.MissingResourceException;
  26. import java.util.PropertyResourceBundle;
  27. import java.util.ResourceBundle;

  28. import org.hipparchus.exception.Localizable;


  29. /**
  30.  * Enumeration for localized messages formats.
  31.  * <p>
  32.  * The constants in this enumeration represent the available
  33.  * formats as localized strings. These formats are intended to be
  34.  * localized using simple properties files, using the constant
  35.  * name as the key and the property value as the message format.
  36.  * The source English format is provided in the constants themselves
  37.  * to serve both as a reminder for developers to understand the parameters
  38.  * needed by each format, as a basis for translators to create
  39.  * localized properties files, and as a default format if some
  40.  * translation is missing.
  41.  * </p>
  42.  * <p>
  43.  * This class is heavily based on {@code OrekitMessages},
  44.  * which is distributed under the terms of the Apache License V2.
  45.  * </p>
  46.  */
  47. public enum RuggedMessages implements Localizable {

  48.     // CHECKSTYLE: stop JavadocVariable check

  49.     INTERNAL_ERROR("internal error, please notify development team by creating an issue at {0}"),
  50.     OUT_OF_TILE_INDICES("no data at indices [{0}, {1}], tile only covers from [0, 0] to [{2}, {3}] (inclusive)"),
  51.     OUT_OF_TILE_ANGLES("no data at latitude {0} and longitude {1}, tile covers only latitudes {2} to {3} and longitudes {4} to {5}"),
  52.     NO_DEM_DATA("no Digital Elevation Model data at latitude {0} and longitude {1}"),
  53.     TILE_WITHOUT_REQUIRED_NEIGHBORS_SELECTED("the tile selected for latitude {0} and longitude {1} does not contain required point neighborhood"),
  54.     OUT_OF_TIME_RANGE("date {0} is out of time span [{1}, {2}] (UTC)"),
  55.     UNINITIALIZED_CONTEXT("general context has not been initialized (missing call to {0})"),
  56.     EMPTY_TILE("tile is empty: {0} ⨉ {1}"),
  57.     UNKNOWN_SENSOR("unknown sensor {0}"),
  58.     LINE_OF_SIGHT_DOES_NOT_REACH_GROUND("line-of-sight does not reach ground"),
  59.     LINE_OF_SIGHT_NEVER_CROSSES_LATITUDE("line-of-sight never crosses latitude {0}"),
  60.     LINE_OF_SIGHT_NEVER_CROSSES_LONGITUDE("line-of-sight never crosses longitude {0}"),
  61.     LINE_OF_SIGHT_NEVER_CROSSES_ALTITUDE("line-of-sight never crosses altitude {0}"),
  62.     DEM_ENTRY_POINT_IS_BEHIND_SPACECRAFT("line-of-sight enters the Digital Elevation Model behind spacecraft!"),
  63.     FRAMES_MISMATCH_WITH_INTERPOLATOR_DUMP("frame {0} does not match frame {1} from interpolator dump"),
  64.     NOT_INTERPOLATOR_DUMP_DATA("data is not an interpolator dump"),
  65.     DEBUG_DUMP_ALREADY_ACTIVE("debug dump is already active for this thread"),
  66.     DEBUG_DUMP_ACTIVATION_ERROR("unable to active debug dump with file {0}: {1}"),
  67.     DEBUG_DUMP_NOT_ACTIVE("debug dump is not active for this thread"),
  68.     CANNOT_PARSE_LINE("cannot parse line {0}, file {1}: {2}"),
  69.     LIGHT_TIME_CORRECTION_REDEFINED("light time correction redefined, line {0}, file {1}: {2}"),
  70.     ABERRATION_OF_LIGHT_CORRECTION_REDEFINED("aberration of light correction redefined, line {0}, file {1}: {2}"),
  71.     ATMOSPHERIC_REFRACTION_REDEFINED("atmospheric refraction correction redefined, line {0}, file {1}: {2}"),
  72.     TILE_ALREADY_DEFINED("tile {0} already defined, line {1}, file {2}: {3}"),
  73.     UNKNOWN_TILE("unknown tile {0}, line {1}, file {2}: {3}"),
  74.     NO_PARAMETERS_SELECTED("no parameters have been selected for estimation"),
  75.     NO_REFERENCE_MAPPINGS("no reference mappings for parameters estimation"),
  76.     DUPLICATED_PARAMETER_NAME("a different parameter with name {0} already exists"),
  77.     INVALID_RUGGED_NAME("invalid rugged name"),
  78.     UNSUPPORTED_REFINING_CONTEXT("refining using {0} rugged instance is not handled"),
  79.     NO_LAYER_DATA("no atmospheric layer data at altitude {0} (lowest altitude: {1})"),
  80.     INVALID_STEP("step {0} is not valid : {1}"),
  81.     INVALID_RANGE_FOR_LINES("range between min line {0} and max line {1} is invalid {2}");

  82.     // CHECKSTYLE: resume JavadocVariable check

  83.     /** Base name of the resource bundle in classpath. */
  84.     private static final String RESOURCE_BASE_NAME = "assets/org/orekit/rugged/RuggedMessages";

  85.     /** Source English format. */
  86.     private final String sourceFormat;

  87.     /** Simple constructor.
  88.      * @param sourceFormat source English format to use when no
  89.      * localized version is available
  90.      */
  91.     RuggedMessages(final String sourceFormat) {
  92.         this.sourceFormat = sourceFormat;
  93.     }

  94.     /** {@inheritDoc} */
  95.     @Override
  96.     public String getSourceString() {
  97.         return sourceFormat;
  98.     }

  99.     /** {@inheritDoc} */
  100.     @Override
  101.     public String getLocalizedString(final Locale locale) {
  102.         try {
  103.             final ResourceBundle bundle =
  104.                     ResourceBundle.getBundle(RESOURCE_BASE_NAME, locale, new UTF8Control());
  105.             if (bundle.getLocale().getLanguage().equals(locale.getLanguage())) {
  106.                 final String translated = bundle.getString(name());
  107.                 if ((translated != null) &&
  108.                     (translated.length() > 0) &&
  109.                     (!translated.toLowerCase(locale).contains("missing translation"))) {
  110.                     // the value of the resource is the translated format
  111.                     return translated;
  112.                 }
  113.             }

  114.         } catch (MissingResourceException mre) {
  115.             // do nothing here
  116.         }

  117.         // either the locale is not supported or the resource is not translated or
  118.         // it is unknown: don't translate and fall back to using the source format
  119.         return sourceFormat;

  120.     }

  121.     /** Control class loading properties in UTF-8 encoding.
  122.      * <p>
  123.      * This class has been very slightly adapted from BalusC answer to question: <a
  124.      * href="http://stackoverflow.com/questions/4659929/how-to-use-utf-8-in-resource-properties-with-resourcebundle">
  125.      * How to use UTF-8 in resource properties with ResourceBundle</a>.
  126.      * </p>
  127.      */
  128.     public static class UTF8Control extends ResourceBundle.Control {

  129.         /** {@inheritDoc} */
  130.         @Override
  131.         public ResourceBundle newBundle(final String baseName, final Locale locale, final String format,
  132.                                         final ClassLoader loader, final boolean reload) throws IOException {
  133.             // The below is a copy of the default implementation.
  134.             final String bundleName = toBundleName(baseName, locale);
  135.             final String resourceName = toResourceName(bundleName, "utf8");
  136.             ResourceBundle bundle = null;
  137.             InputStream stream = null;
  138.             if (reload) {
  139.                 final URL url = loader.getResource(resourceName);
  140.                 if (url != null) {
  141.                     final URLConnection connection = url.openConnection();
  142.                     if (connection != null) {
  143.                         connection.setUseCaches(false);
  144.                         stream = connection.getInputStream();
  145.                     }
  146.                 }
  147.             } else {
  148.                 stream = loader.getResourceAsStream(resourceName);
  149.             }
  150.             if (stream != null) {
  151.                 try (InputStreamReader inputStreamReader = new InputStreamReader(stream, StandardCharsets.UTF_8)) {
  152.                     // Only this line is changed to make it to read properties files as UTF-8.
  153.                     bundle = new PropertyResourceBundle(inputStreamReader);
  154.                 }
  155.             }
  156.             return bundle;
  157.         }

  158.     }

  159. }