sis-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From desruisse...@apache.org
Subject svn commit: r1684610 - in /sis/branches/JDK8/core: sis-metadata/src/main/java/org/apache/sis/io/wkt/ sis-referencing/src/main/java/org/apache/sis/internal/referencing/ sis-referencing/src/test/java/org/apache/sis/internal/referencing/provider/ sis-util...
Date Wed, 10 Jun 2015 09:15:04 GMT
Author: desruisseaux
Date: Wed Jun 10 09:15:03 2015
New Revision: 1684610

URL: http://svn.apache.org/r1684610
Log:
WKT: workaround for a JSR-275 bug when formatting DMS units.

Added:
    sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/internal/util/PatchedUnitFormat.java
  (with props)
Modified:
    sis/branches/JDK8/core/sis-metadata/src/main/java/org/apache/sis/io/wkt/Formatter.java
    sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/internal/referencing/AxisDirections.java
    sis/branches/JDK8/core/sis-referencing/src/test/java/org/apache/sis/internal/referencing/provider/CoordinateOperationMethodsHTML.java
    sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/internal/jaxb/gco/UnitAdapter.java
    sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/internal/util/Utilities.java
    sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/measure/SexagesimalConverter.java
    sis/branches/JDK8/core/sis-utility/src/test/java/org/apache/sis/measure/SexagesimalConverterTest.java

Modified: sis/branches/JDK8/core/sis-metadata/src/main/java/org/apache/sis/io/wkt/Formatter.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-metadata/src/main/java/org/apache/sis/io/wkt/Formatter.java?rev=1684610&r1=1684609&r2=1684610&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-metadata/src/main/java/org/apache/sis/io/wkt/Formatter.java
[UTF-8] (original)
+++ sis/branches/JDK8/core/sis-metadata/src/main/java/org/apache/sis/io/wkt/Formatter.java
[UTF-8] Wed Jun 10 09:15:03 2015
@@ -65,6 +65,7 @@ import org.apache.sis.util.CharSequences
 import org.apache.sis.util.ArgumentChecks;
 import org.apache.sis.util.resources.Errors;
 import org.apache.sis.internal.util.Citations;
+import org.apache.sis.internal.util.PatchedUnitFormat;
 import org.apache.sis.internal.simple.SimpleExtent;
 import org.apache.sis.internal.metadata.WKTKeywords;
 import org.apache.sis.internal.metadata.ReferencingServices;
@@ -218,7 +219,7 @@ public class Formatter implements Locali
     /**
      * The object to use for formatting unit symbols.
      */
-    private final UnitFormat unitFormat;
+    private final PatchedUnitFormat unitFormat;
 
     /**
      * Dummy field position.
@@ -337,7 +338,7 @@ public class Formatter implements Locali
         this.indentation   = (byte) indentation;
         this.numberFormat  = symbols.createNumberFormat();
         this.dateFormat    = new SimpleDateFormat(WKTFormat.DATE_PATTERN, symbols.getLocale());
-        this.unitFormat    = UnitFormat.getInstance(symbols.getLocale());
+        this.unitFormat    = new PatchedUnitFormat(UnitFormat.getInstance(symbols.getLocale()));
         this.buffer        = new StringBuffer();
         dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
     }
@@ -357,7 +358,7 @@ public class Formatter implements Locali
         this.indentation   = WKTFormat.DEFAULT_INDENTATION;
         this.numberFormat  = numberFormat; // No clone needed.
         this.dateFormat    = dateFormat;   // No clone needed.
-        this.unitFormat    = unitFormat;   // No clone needed.
+        this.unitFormat    = new PatchedUnitFormat(unitFormat);
         // Do not set the buffer. It will be set by WKTFormat.format(…).
     }
 

Modified: sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/internal/referencing/AxisDirections.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/internal/referencing/AxisDirections.java?rev=1684610&r1=1684609&r2=1684610&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/internal/referencing/AxisDirections.java
[UTF-8] (original)
+++ sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/internal/referencing/AxisDirections.java
[UTF-8] Wed Jun 10 09:15:03 2015
@@ -20,7 +20,7 @@ import javax.measure.unit.Unit;
 import org.opengis.referencing.cs.AxisDirection;
 import org.opengis.referencing.cs.CoordinateSystem;
 import org.opengis.referencing.cs.CoordinateSystemAxis;
-import org.apache.sis.internal.util.Utilities;
+import org.apache.sis.internal.util.PatchedUnitFormat;
 import org.apache.sis.util.Characters;
 import org.apache.sis.util.Static;
 import org.apache.sis.util.iso.Types;
@@ -611,7 +611,7 @@ public final class AxisDirections extend
         for (final CoordinateSystemAxis axis : axes) {
             buffer.append(separator).append(Types.getCodeLabel(axis.getDirection()));
             separator = ", ";
-            final String symbol = Utilities.toString(axis.getUnit());
+            final String symbol = PatchedUnitFormat.toString(axis.getUnit());
             if (symbol != null && !symbol.isEmpty()) {
                 buffer.append(" (").append(symbol).append(')');
             }

Modified: sis/branches/JDK8/core/sis-referencing/src/test/java/org/apache/sis/internal/referencing/provider/CoordinateOperationMethodsHTML.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-referencing/src/test/java/org/apache/sis/internal/referencing/provider/CoordinateOperationMethodsHTML.java?rev=1684610&r1=1684609&r2=1684610&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-referencing/src/test/java/org/apache/sis/internal/referencing/provider/CoordinateOperationMethodsHTML.java
[UTF-8] (original)
+++ sis/branches/JDK8/core/sis-referencing/src/test/java/org/apache/sis/internal/referencing/provider/CoordinateOperationMethodsHTML.java
[UTF-8] Wed Jun 10 09:15:03 2015
@@ -34,7 +34,7 @@ import org.opengis.referencing.crs.Coord
 import org.opengis.referencing.crs.GeneralDerivedCRS;
 import org.apache.sis.internal.system.DefaultFactories;
 import org.apache.sis.internal.util.Constants;
-import org.apache.sis.internal.util.Utilities;
+import org.apache.sis.internal.util.PatchedUnitFormat;
 import org.apache.sis.measure.Range;
 import org.apache.sis.measure.Latitude;
 import org.apache.sis.measure.Longitude;
@@ -472,7 +472,7 @@ public final class CoordinateOperationMe
      * or an empty string (never {@code null}) if none.
      */
     private static String getUnit(final ParameterDescriptor<?> param) {
-        final String unit = Utilities.toString(param.getUnit());
+        final String unit = PatchedUnitFormat.toString(param.getUnit());
         if (unit != null && !unit.isEmpty()) {
             return " " + unit;
         }

Modified: sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/internal/jaxb/gco/UnitAdapter.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/internal/jaxb/gco/UnitAdapter.java?rev=1684610&r1=1684609&r2=1684610&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/internal/jaxb/gco/UnitAdapter.java
[UTF-8] (original)
+++ sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/internal/jaxb/gco/UnitAdapter.java
[UTF-8] Wed Jun 10 09:15:03 2015
@@ -19,7 +19,7 @@ package org.apache.sis.internal.jaxb.gco
 import javax.measure.unit.Unit;
 import javax.xml.bind.annotation.adapters.XmlAdapter;
 import org.apache.sis.internal.jaxb.Context;
-import org.apache.sis.internal.util.Utilities;
+import org.apache.sis.internal.util.PatchedUnitFormat;
 
 
 /**
@@ -55,7 +55,7 @@ public class UnitAdapter extends XmlAdap
      */
     @Override
     public String marshal(final Unit<?> value) {
-        return Utilities.toString(value);
+        return PatchedUnitFormat.toString(value);
     }
 
     /**

Added: sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/internal/util/PatchedUnitFormat.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/internal/util/PatchedUnitFormat.java?rev=1684610&view=auto
==============================================================================
--- sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/internal/util/PatchedUnitFormat.java
(added)
+++ sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/internal/util/PatchedUnitFormat.java
[UTF-8] Wed Jun 10 09:15:03 2015
@@ -0,0 +1,150 @@
+/*
+ * 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.Map;
+import java.util.HashMap;
+import java.text.Format;
+import java.text.FieldPosition;
+import java.text.ParsePosition;
+import javax.measure.unit.Unit;
+import javax.measure.unit.UnitFormat;
+import org.apache.sis.util.Workaround;
+
+
+/**
+ * Workaround for JSR-275 issues.
+ *
+ * @author  Martin Desruisseaux (Geomatys)
+ * @since   0.6
+ * @version 0.6
+ * @module
+ */
+@Workaround(library="JSR-275", version="0.9.3")
+public final class PatchedUnitFormat extends Format {
+    /**
+     * For cross-version compatibility (even if this class is hopefully temporary).
+     */
+    private static final long serialVersionUID = -3064428584419360693L;
+
+    /**
+     * The symbols for some units defined by Apache SIS. We store here the symbols that we
were not able
+     * to set in the units created by {@link org.apache.sis.measure.SexagesimalConverter}
because of
+     * <a href="http://kenai.com/jira/browse/JSR_275-41">JSR-275 bug</a>.
+     *
+     * <p>We do not bother making this map unmodifiable. This is okay if this map is
never modified
+     * after this field has been assigned a value.</p>
+     */
+    private static volatile Map<Unit<?>,String> SYMBOLS;
+
+    /**
+     * Invoked by {@code SexagesimalConverter} static class initializer for declaring the
SIS units
+     * that JSR-275 0.9.3 can not format by itself. This method should not be invoked in
any other
+     * circumstance, otherwise an {@link IllegalStateException} will be thrown.
+     *
+     * @param entries The (unit, symbol) pairs.
+     */
+    public static void init(final Object... entries) {
+        final Map<Unit<?>,String> symbols = new HashMap<>(8);
+        for (int i=0; i<entries.length; i++) {
+            final String uom;
+            if (symbols.put((Unit<?>) entries[i], uom = (String) entries[++i]) != null)
{
+                throw new IllegalArgumentException(uom);   // Duplicated unit.
+            }
+        }
+        if (SYMBOLS != null) {  // Check on a best-effort basis only (ignoring race conditions).
+            throw new IllegalStateException();
+        }
+        SYMBOLS = symbols;
+    }
+
+    /**
+     * The {@link UnitFormat} to patch.
+     */
+    private final UnitFormat format;
+
+    /**
+     * Creates a new {@code PatchedUnitFormat} instance wrapping the given format.
+     *
+     * @param format the format to wrap.
+     */
+    public PatchedUnitFormat(final UnitFormat format) {
+        this.format = format;
+    }
+
+    /**
+     * Returns the string representation of the given unit, or {@code null} if none.
+     * This method is used as a workaround for a bug in JSR-275, which sometime throws
+     * an exception in the {@link Unit#toString()} method.
+     *
+     * @param  unit The unit for which to get a string representation, or {@code null}.
+     * @return The string representation of the given string (may be an empty string), or
{@code null}.
+     */
+    @Workaround(library="JSR-275", version="0.9.3")
+    public static String toString(final Unit<?> unit) {
+        if (unit != null) {
+            final Map<Unit<?>,String> symbols = SYMBOLS;
+            if (symbols != null) {
+                final String symbol = symbols.get(unit);
+                if (symbol != null) {
+                    return symbol;
+                }
+            }
+            try {
+                String text = unit.toString();
+                if (text.equals("deg")) {
+                    text = "°";
+                }
+                return text;
+            } catch (IllegalArgumentException e) {
+                // Workaround for JSR-275 implementation bug.
+                // Do nothing, we will return null below.
+            }
+        }
+        return null;
+    }
+
+    /**
+     * Formats the given unit.
+     *
+     * @param  unit The unit to format.
+     * @param  toAppendTo where to append to unit.
+     * @param  pos Ignored.
+     * @return The given {@code toAppendTo} argument.
+     */
+    @Override
+    public StringBuffer format(final Object unit, final StringBuffer toAppendTo, final FieldPosition
pos) {
+        final Map<Unit<?>,String> symbols = SYMBOLS;
+        if (symbols != null) {
+            final String symbol = symbols.get(unit);
+            if (symbol != null) {
+                return toAppendTo.append(symbol);
+            }
+        }
+        return format.format(unit, toAppendTo, pos);
+    }
+
+    /**
+     * Delegates to the wrapped {@link UnitFormat}.
+     *
+     * @return The parsed unit, or {@code null}.
+     */
+    @Override
+    public Object parseObject(final String source, final ParsePosition pos) {
+        return format.parseObject(source, pos);
+    }
+}

Propchange: sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/internal/util/PatchedUnitFormat.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/internal/util/PatchedUnitFormat.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain;charset=UTF-8

Modified: sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/internal/util/Utilities.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/internal/util/Utilities.java?rev=1684610&r1=1684609&r2=1684610&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/internal/util/Utilities.java
[UTF-8] (original)
+++ sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/internal/util/Utilities.java
[UTF-8] Wed Jun 10 09:15:03 2015
@@ -18,11 +18,9 @@ package org.apache.sis.internal.util;
 
 import java.util.Formatter;
 import java.util.FormattableFlags;
-import javax.measure.unit.Unit;
 import org.apache.sis.util.Static;
 import org.apache.sis.util.Classes;
 import org.apache.sis.util.CharSequences;
-import org.apache.sis.util.Workaround;
 
 
 /**
@@ -41,31 +39,6 @@ public final class Utilities extends Sta
     }
 
     /**
-     * Returns the string representation of the given unit, or {@code null} if none.
-     * This method is used as a workaround for a bug in JSR-275, which sometime throws
-     * an exception in the {@link Unit#toString()} method.
-     *
-     * @param  unit The unit for which to get a string representation, or {@code null}.
-     * @return The string representation of the given string (may be an empty string), or
{@code null}.
-     *
-     * @since 0.6
-     */
-    @Workaround(library="JSR-275", version="0.9.3")
-    public static String toString(final Unit<?> unit) {
-        if (unit != null) try {
-            String text = unit.toString();
-            if (text.equals("deg")) {
-                text = "°";
-            }
-            return text;
-        } catch (IllegalArgumentException e) {
-            // Workaround for JSR-275 implementation bug.
-            // Do nothing, we will return null below.
-        }
-        return null;
-    }
-
-    /**
      * Appends to the given buffer only the characters that are valid for a Unicode identifier.
      * The given separator character is append before the given {@code text} only if the
buffer
      * is not empty and at least one {@code text} character is valid.

Modified: sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/measure/SexagesimalConverter.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/measure/SexagesimalConverter.java?rev=1684610&r1=1684609&r2=1684610&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/measure/SexagesimalConverter.java
[UTF-8] (original)
+++ sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/measure/SexagesimalConverter.java
[UTF-8] Wed Jun 10 09:15:03 2015
@@ -24,6 +24,7 @@ import javax.measure.quantity.Angle;
 import javax.measure.converter.UnitConverter;
 import org.apache.sis.util.resources.Errors;
 import org.apache.sis.util.resources.Vocabulary;
+import org.apache.sis.internal.util.PatchedUnitFormat;
 
 import static org.apache.sis.math.MathFunctions.truncate;
 
@@ -43,7 +44,7 @@ import static org.apache.sis.math.MathFu
  *
  * @author  Martin Desruisseaux (IRD, Geomatys)
  * @since   0.3
- * @version 0.3
+ * @version 0.6
  * @module
  */
 class SexagesimalConverter extends UnitConverter { // Intentionally not final.
@@ -115,6 +116,13 @@ class SexagesimalConverter extends UnitC
     static final Unit<Angle> DMS_SCALED = NonSI.DEGREE_ANGLE.transform(
             new SexagesimalConverter(true, 1).inverse()).asType(Angle.class);//.alternate("DMS");
 
+    /*
+     * Declares the units that we were not able to declare in calls to Unit.alternate(String).
+     */
+    static {
+        PatchedUnitFormat.init(DM, "D.M", DMS, "D.MS", DMS_SCALED, "DMS");
+    }
+
     /**
      * {@code true} if the seconds field is present.
      */

Modified: sis/branches/JDK8/core/sis-utility/src/test/java/org/apache/sis/measure/SexagesimalConverterTest.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-utility/src/test/java/org/apache/sis/measure/SexagesimalConverterTest.java?rev=1684610&r1=1684609&r2=1684610&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-utility/src/test/java/org/apache/sis/measure/SexagesimalConverterTest.java
[UTF-8] (original)
+++ sis/branches/JDK8/core/sis-utility/src/test/java/org/apache/sis/measure/SexagesimalConverterTest.java
[UTF-8] Wed Jun 10 09:15:03 2015
@@ -19,6 +19,7 @@ package org.apache.sis.measure;
 import javax.measure.unit.Unit;
 import javax.measure.quantity.Quantity;
 import javax.measure.converter.UnitConverter;
+import org.apache.sis.internal.util.PatchedUnitFormat;
 import org.apache.sis.test.TestCase;
 import org.junit.Test;
 
@@ -32,7 +33,7 @@ import static org.apache.sis.test.Assert
  *
  * @author  Martin Desruisseaux (Geomatys)
  * @since   0.3
- * @version 0.4
+ * @version 0.6
  * @module
  */
 public final strictfp class SexagesimalConverterTest extends TestCase {
@@ -92,4 +93,15 @@ public final strictfp class SexagesimalC
         checkConversion(10.99,              DEGREE_ANGLE, 105924,     DMS_SCALED);
         checkConversion(44.505590277777777, DEGREE_ANGLE, 443020.125, DMS_SCALED);
     }
+
+    /**
+     * Tests {@link PatchedUnitFormat#toString(Unit)}.
+     * Tested here because it can be tested only after {@link SexagesimalConverter} initialization.
+     */
+    @Test
+    public void testPatchedUnitFormat() {
+        assertEquals("D.M",  PatchedUnitFormat.toString(DM));
+        assertEquals("D.MS", PatchedUnitFormat.toString(DMS));
+        assertEquals("DMS",  PatchedUnitFormat.toString(DMS_SCALED));
+    }
 }



Mime
View raw message