RuggedMessages.java

  1. /* Copyright 2013-2019 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.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.util.Locale;
  24. import java.util.MissingResourceException;
  25. import java.util.PropertyResourceBundle;
  26. import java.util.ResourceBundle;

  27. import org.hipparchus.exception.Localizable;


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

  47.     // CHECKSTYLE: stop JavadocVariable check

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

  81.     // CHECKSTYLE: resume JavadocVariable check

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

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

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

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

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

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

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

  119.     }

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

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

  159.     }

  160. }