sis-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From desruisse...@apache.org
Subject [sis] branch geoapi-4.0 updated: Fix the insufficient amount of digits in some error messages.
Date Sat, 10 Nov 2018 21:18:20 GMT
This is an automated email from the ASF dual-hosted git repository.

desruisseaux pushed a commit to branch geoapi-4.0
in repository https://gitbox.apache.org/repos/asf/sis.git


The following commit(s) were added to refs/heads/geoapi-4.0 by this push:
     new 6759f5c  Fix the insufficient amount of digits in some error messages.
6759f5c is described below

commit 6759f5cb61a42860f539302833c22038ae1b478b
Author: Martin Desruisseaux <martin.desruisseaux@geomatys.com>
AuthorDate: Sat Nov 10 22:17:43 2018 +0100

    Fix the insufficient amount of digits in some error messages.
---
 .../sis/io/wkt/GeodeticObjectParserTest.java       |   4 +-
 .../sis/internal/util/AutoMessageFormat.java       | 114 +++++++++++++++++++++
 .../apache/sis/util/logging/MonolineFormatter.java |   6 +-
 .../java/org/apache/sis/util/resources/Errors.java |   4 +-
 .../apache/sis/util/resources/Errors.properties    |   4 +-
 .../apache/sis/util/resources/Errors_fr.properties |   4 +-
 .../sis/util/resources/IndexedResourceBundle.java  |  10 +-
 7 files changed, 132 insertions(+), 14 deletions(-)

diff --git a/core/sis-referencing/src/test/java/org/apache/sis/io/wkt/GeodeticObjectParserTest.java
b/core/sis-referencing/src/test/java/org/apache/sis/io/wkt/GeodeticObjectParserTest.java
index 12ef351..3645bf2 100644
--- a/core/sis-referencing/src/test/java/org/apache/sis/io/wkt/GeodeticObjectParserTest.java
+++ b/core/sis-referencing/src/test/java/org/apache/sis/io/wkt/GeodeticObjectParserTest.java
@@ -1149,14 +1149,14 @@ public final strictfp class GeodeticObjectParserTest extends TestCase
{
                 warnings.getUnknownElementLocations("Ext2").toArray());
 
         assertMultilinesEquals("Parsing of “WGS 84” done, but some elements were ignored.\n"
+
-                               " • Unexpected scale factor 0.017 for unit of measurement
“°”.\n" +
+                               " • Unexpected scale factor 0.01746 for unit of measurement
“°”.\n" +
                                " • The text contains unknown elements:\n" +
                                "    ‣ “Intruder” in PRIMEM, GEOGCS.\n" +
                                "    ‣ “Ext1” in SPHEROID.\n" +
                                "    ‣ “Ext2” in SPHEROID.", warnings.toString(Locale.US));
 
         assertMultilinesEquals("La lecture de « WGS 84 » a été faite, mais en ignorant
certains éléments.\n" +
-                               " • Le facteur d’échelle 0,017 est inattendu pour l’unité
de mesure « ° ».\n" +
+                               " • Le facteur d’échelle 0,01746 est inattendu pour
l’unité de mesure « ° ».\n" +
                                " • Le texte contient des éléments inconnus :\n" +
                                "    ‣ « Intruder » dans PRIMEM, GEOGCS.\n"
+
                                "    ‣ « Ext1 » dans SPHEROID.\n" +
diff --git a/core/sis-utility/src/main/java/org/apache/sis/internal/util/AutoMessageFormat.java
b/core/sis-utility/src/main/java/org/apache/sis/internal/util/AutoMessageFormat.java
new file mode 100644
index 0000000..f62e6da
--- /dev/null
+++ b/core/sis-utility/src/main/java/org/apache/sis/internal/util/AutoMessageFormat.java
@@ -0,0 +1,114 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.sis.internal.util;
+
+import java.util.Locale;
+import java.text.Format;
+import java.text.NumberFormat;
+import java.text.MessageFormat;
+
+
+/**
+ * A message format which adjust automatically the number of fraction digits needed for formatting
numbers.
+ * Callers need to invoke {@link #configure(Object[])} before to invoke any {@code format(…)}
method.
+ *
+ * @author  Martin Desruisseaux (Geomatys)
+ * @version 1.0
+ * @since   1.0
+ * @module
+ */
+@SuppressWarnings({"serial", "CloneableClassWithoutClone"})                 // Not to be
serialized.
+public final class AutoMessageFormat extends MessageFormat {
+    /**
+     * Formats that may need to be configured before to write a value, or {@code null} if
none.
+     * May contain null elements. This information is used for adjusting the amount of fraction
digits.
+     */
+    private transient NumberFormat[] formatsToConfigure;
+
+    /**
+     * Whether the {@link #formatsToConfigure} needs to be updated.
+     */
+    private transient boolean update;
+
+    /**
+     * Creates a new message for the default locale.
+     *
+     * @param pattern  the pattern for this message format.
+     */
+    public AutoMessageFormat(final String pattern) {
+        super(pattern);
+    }
+
+    /**
+     * Creates a new message for the given locale.
+     *
+     * @param pattern  the pattern for this message format.
+     * @param locale   the locale for this message format.
+     */
+    public AutoMessageFormat(final String pattern, final Locale locale) {
+        super(pattern, locale);
+    }
+
+    /**
+     * Modifies the pattern used by this message format.
+     *
+     * @param pattern the new pattern for this message format
+     */
+    @Override
+    public void applyPattern(final String pattern) {
+        super.applyPattern(pattern);
+        update = true;
+    }
+
+    /**
+     * Configures the number of fraction digits in the formats used by this {@code MessageFormat}.
+     * This method can work only for parameters declared as {@code "{#,number}"} in the message
pattern.
+     *
+     * @param  arguments  the argument to be given to {@link #format(Object)}.
+     */
+    public void configure(final Object[] arguments) {
+        if (update) {
+            formatsToConfigure = null;
+            final Format[] fc = getFormatsByArgumentIndex();
+            for (int i=fc.length; --i >= 0;) {
+                final Format c = fc[i];
+                if (c instanceof NumberFormat) {
+                    if (formatsToConfigure == null) {
+                        formatsToConfigure = new NumberFormat[i+1];
+                    }
+                    formatsToConfigure[i] = (NumberFormat) c;
+                }
+            }
+        }
+        if (formatsToConfigure != null) {
+            for (int i=Math.min(formatsToConfigure.length, arguments.length); --i >= 0;)
{
+                final NumberFormat f = formatsToConfigure[i];
+                if (f != null) {
+                    final Object value = arguments[i];
+                    int p = 3;                              // Default number of fraction
digits.
+                    if (value instanceof Number) {
+                        final double n = ((Number) value).doubleValue();
+                        if (!Double.isNaN(n)) {
+                            p = -1 - Numerics.toExp10(Math.getExponent(Math.ulp(n)));
+                        }
+                    }
+                    f.setMaximumFractionDigits(Math.min(p, 16));
+                }
+            }
+        }
+    }
+}
diff --git a/core/sis-utility/src/main/java/org/apache/sis/util/logging/MonolineFormatter.java
b/core/sis-utility/src/main/java/org/apache/sis/util/logging/MonolineFormatter.java
index 00fb831..f60a79d 100644
--- a/core/sis-utility/src/main/java/org/apache/sis/util/logging/MonolineFormatter.java
+++ b/core/sis-utility/src/main/java/org/apache/sis/util/logging/MonolineFormatter.java
@@ -34,6 +34,7 @@ import java.util.logging.*;
 import org.apache.sis.internal.system.Modules;
 import org.apache.sis.internal.system.OS;
 import org.apache.sis.internal.util.X364;
+import org.apache.sis.internal.util.AutoMessageFormat;
 import org.apache.sis.io.IO;
 import org.apache.sis.io.LineAppender;
 import org.apache.sis.util.ArgumentChecks;
@@ -245,7 +246,7 @@ public class MonolineFormatter extends Formatter {
     /**
      * The message format, or {@code null} if not yet created.
      */
-    private transient MessageFormat messageFormat;
+    private transient AutoMessageFormat messageFormat;
 
     /**
      * Value of the last call to {@link MessageFormat#applyPattern(String)}. Saved in order
to avoid
@@ -841,13 +842,14 @@ loop:   for (int i=0; ; i++) {
                 if (c >= '0' && c <= '9') {
                     synchronized (buffer) {
                         if (messageFormat == null) {
-                            messageFormat = new MessageFormat(message);
+                            messageFormat = new AutoMessageFormat(message);
                         } else if (!message.equals(messagePattern)) {
                             messageFormat.applyPattern(message);
                         }
                         messagePattern = message;
                         final int base = buffer.length();
                         try {
+                            messageFormat.configure(parameters);
                             message = messageFormat.format(parameters, buffer, new FieldPosition(0)).substring(base);
                         } finally {
                             buffer.setLength(base);
diff --git a/core/sis-utility/src/main/java/org/apache/sis/util/resources/Errors.java b/core/sis-utility/src/main/java/org/apache/sis/util/resources/Errors.java
index 67668b2..d8a9606 100644
--- a/core/sis-utility/src/main/java/org/apache/sis/util/resources/Errors.java
+++ b/core/sis-utility/src/main/java/org/apache/sis/util/resources/Errors.java
@@ -851,7 +851,7 @@ public final class Errors extends IndexedResourceBundle {
         public static final short UnexpectedProperty_2 = 141;
 
         /**
-         * Unexpected scale factor {1} for unit of measurement “{0}”.
+         * Unexpected scale factor {1,number} for unit of measurement “{0}”.
          */
         public static final short UnexpectedScaleFactorForUnit_2 = 142;
 
@@ -987,7 +987,7 @@ public final class Errors extends IndexedResourceBundle {
         public static final short ValueAlreadyDefined_1 = 164;
 
         /**
-         * Value ‘{0}’ = {1} is invalid. Expected a number greater than 0.
+         * Value ‘{0}’ = {1,number} is invalid. Expected a number greater than 0.
          */
         public static final short ValueNotGreaterThanZero_2 = 165;
 
diff --git a/core/sis-utility/src/main/java/org/apache/sis/util/resources/Errors.properties
b/core/sis-utility/src/main/java/org/apache/sis/util/resources/Errors.properties
index 926be8e..835b4d1 100644
--- a/core/sis-utility/src/main/java/org/apache/sis/util/resources/Errors.properties
+++ b/core/sis-utility/src/main/java/org/apache/sis/util/resources/Errors.properties
@@ -181,7 +181,7 @@ UnexpectedEndOfString_1           = More characters were expected at the
end of
 UnexpectedFileFormat_2            = File \u201c{1}\u201d seems to be encoded in an other
format than {0}.
 UnexpectedParameter_1             = Parameter \u201c{0}\u201d was not expected.
 UnexpectedProperty_2              = Property \u201c{1}\u201d was not expected in \u201c{0}\u201d.
-UnexpectedScaleFactorForUnit_2    = Unexpected scale factor {1} for unit of measurement \u201c{0}\u201d.
+UnexpectedScaleFactorForUnit_2    = Unexpected scale factor {1,number} for unit of measurement
\u201c{0}\u201d.
 UnexpectedTypeForReference_3      = Expected \u201c{0}\u201d to reference an instance of
\u2018{1}\u2019, but found an instance of \u2018{2}\u2019.
 UnexpectedValueInElement_2        = Unexpected value \u201c{1}\u201d in \u201c{0}\u201d element.
 UnknownCommand_1                  = Command \u201c{0}\u201d is not recognized.
@@ -208,5 +208,5 @@ UnsupportedCoordinateSystem_1     = The \u201c{0}\u201d coordinate system
is not
 UnsupportedDatum_1                = The \u201c{0}\u201d datum is not supported by this operation.
 UnsupportedType_1                 = The \u2018{0}\u2019 type is not supported in this context.
 ValueAlreadyDefined_1             = A value is already defined for \u201c{0}\u201d.
-ValueNotGreaterThanZero_2         = Value \u2018{0}\u2019 = {1} is invalid. Expected a number
greater than 0.
+ValueNotGreaterThanZero_2         = Value \u2018{0}\u2019 = {1,number} is invalid. Expected
a number greater than 0.
 ValueOutOfRange_4                 = Value \u2018{0}\u2019 = {3} is invalid. Expected a value
in the [{1} \u2026 {2}] range.
diff --git a/core/sis-utility/src/main/java/org/apache/sis/util/resources/Errors_fr.properties
b/core/sis-utility/src/main/java/org/apache/sis/util/resources/Errors_fr.properties
index 4beb847..f5f511f 100644
--- a/core/sis-utility/src/main/java/org/apache/sis/util/resources/Errors_fr.properties
+++ b/core/sis-utility/src/main/java/org/apache/sis/util/resources/Errors_fr.properties
@@ -177,7 +177,7 @@ UnexpectedEndOfString_1           = D\u2019autres caract\u00e8res \u00e9taient
a
 UnexpectedFileFormat_2            = Le fichier \u00ab\u202f{1}\u202f\u00bb semble \u00eatre
encod\u00e9 dans un autre format que {0}.
 UnexpectedParameter_1             = Le param\u00e8tre \u00ab\u202f{0}\u202f\u00bb est inattendu.
 UnexpectedProperty_2              = La propri\u00e9t\u00e9 \u00ab\u202f{1}\u202f\u00bb est
inattendue dans \u00ab\u202f{0}\u202f\u00bb.
-UnexpectedScaleFactorForUnit_2    = Le facteur d\u2019\u00e9chelle {1} est inattendu pour
l\u2019unit\u00e9 de mesure \u00ab\u202f{0}\u202f\u00bb.
+UnexpectedScaleFactorForUnit_2    = Le facteur d\u2019\u00e9chelle {1,number} est inattendu
pour l\u2019unit\u00e9 de mesure \u00ab\u202f{0}\u202f\u00bb.
 UnexpectedTypeForReference_3      = L\u2019identifiant \u201c{0}\u201d r\u00e9f\u00e9rence
une instance de \u2018{2}\u2019 alors qu\u2019on attendait une instance de \u2018{1}\u2019.
 UnexpectedValueInElement_2        = La valeur \u00ab\u202f{1}\u202f\u00bb dans  l\u2019\u00e9l\u00e9ment
\u00ab\u202f{0}\u202f\u00bb est inattendue.
 UnknownCommand_1                  = La commande \u00ab\u202f{0}\u202f\u00bb n\u2019est pas
reconnue.
@@ -204,5 +204,5 @@ UnsupportedCoordinateSystem_1     = Le syst\u00e8me de coordonn\u00e9es
\u00ab\u
 UnsupportedDatum_1                = Le r\u00e9f\u00e9rentiel \u00ab\u202f{0}\u202f\u00bb
n\u2019est pas support\u00e9 par cette op\u00e9ration.
 UnsupportedType_1                 = Le type \u2018{0}\u2019 n\u2019est pas support\u00e9
dans ce contexte.
 ValueAlreadyDefined_1             = Une valeur est d\u00e9j\u00e0 d\u00e9finie pour \u00ab\u202f{0}\u202f\u00bb.
-ValueNotGreaterThanZero_2         = La valeur \u2018{0}\u2019 = {1} n\u2019est pas valide.
On attendait un nombre positif non-nul.
+ValueNotGreaterThanZero_2         = La valeur \u2018{0}\u2019 = {1,number} n\u2019est pas
valide. On attendait un nombre positif non-nul.
 ValueOutOfRange_4                 = La valeur \u2018{0}\u2019 = {3} est invalide. Une valeur
dans la plage [{1} \u2026 {2}] \u00e9tait attendue.
diff --git a/core/sis-utility/src/main/java/org/apache/sis/util/resources/IndexedResourceBundle.java
b/core/sis-utility/src/main/java/org/apache/sis/util/resources/IndexedResourceBundle.java
index beb1879..2bc56a9 100644
--- a/core/sis-utility/src/main/java/org/apache/sis/util/resources/IndexedResourceBundle.java
+++ b/core/sis-utility/src/main/java/org/apache/sis/util/resources/IndexedResourceBundle.java
@@ -40,6 +40,7 @@ import org.apache.sis.util.Exceptions;
 import org.apache.sis.util.CharSequences;
 import org.apache.sis.util.logging.Logging;
 import org.apache.sis.internal.system.Loggers;
+import org.apache.sis.internal.util.AutoMessageFormat;
 import org.apache.sis.internal.util.MetadataServices;
 import org.apache.sis.measure.RangeFormat;
 import org.apache.sis.measure.Range;
@@ -122,7 +123,7 @@ public class IndexedResourceBundle extends ResourceBundle implements Localized
{
      * The object to use for formatting messages. This object
      * will be constructed only when first needed.
      */
-    private transient MessageFormat format;
+    private transient AutoMessageFormat format;
 
     /**
      * The key of the last resource requested. If the same resource is requested multiple
times,
@@ -531,22 +532,23 @@ public class IndexedResourceBundle extends ResourceBundle implements
Localized {
                 /*
                  * Constructs a new MessageFormat for formatting the arguments.
                  */
-                format = new MessageFormat(pattern, getLocale());
+                format  = new AutoMessageFormat(pattern, getLocale());
                 lastKey = key;
             } else if (key != lastKey) {
                 /*
-                 * Method MessageFormat.applyPattern(...) is costly! We will avoid
+                 * Method MessageFormat.applyPattern(…) is costly! We will avoid
                  * calling it again if the format already has the right pattern.
                  */
                 format.applyPattern(pattern);
                 lastKey = key;
             }
             try {
+                format.configure(arguments);
                 return format.format(arguments);
             } catch (RuntimeException e) {
                 /*
                  * Safety against badly implemented toString() method
-                 * in libraries that we don't control.
+                 * in libraries that we do not control.
                  */
                 return "[Unformattable message: " + e + ']';
             }


Mime
View raw message