Author: desruisseaux
Date: Fri Sep  4 14:48:14 2015
New Revision: 1701277

URL: http://svn.apache.org/r1701277
Log:
More accurate description about where the SIS-166 fix can not be applied, and 
consolidate the workaround.

Modified:
    
sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/internal/referencing/ReferencingUtilities.java
    
sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/referencing/crs/AbstractCRS.java
    
sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/referencing/crs/DefaultEngineeringCRS.java
    
sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/referencing/crs/DefaultGeodeticCRS.java
    
sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/referencing/crs/DefaultImageCRS.java
    
sis/branches/JDK8/core/sis-referencing/src/test/java/org/apache/sis/internal/referencing/ReferencingUtilitiesTest.java
    
sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/util/CharSequences.java

Modified: 
sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/internal/referencing/ReferencingUtilities.java
URL: 
http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/internal/referencing/ReferencingUtilities.java?rev=1701277&r1=1701276&r2=1701277&view=diff
==============================================================================
--- 
sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/internal/referencing/ReferencingUtilities.java
 [UTF-8] (original)
+++ 
sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/internal/referencing/ReferencingUtilities.java
 [UTF-8] Fri Sep  4 14:48:14 2015
@@ -27,6 +27,7 @@ import org.opengis.referencing.datum.Ell
 import org.opengis.referencing.datum.PrimeMeridian;
 import org.apache.sis.util.Static;
 import org.apache.sis.util.Utilities;
+import org.apache.sis.util.CharSequences;
 import org.apache.sis.util.resources.Errors;
 import org.apache.sis.internal.jaxb.Context;
 import org.apache.sis.referencing.CommonCRS;
@@ -224,6 +225,41 @@ public final class ReferencingUtilities
     }
 
     /**
+     * Returns the XML property name of the given interface.
+     *
+     * For {@link CoordinateSystem} base type, the returned value shall be one 
of
+     * {@code affineCS}, {@code cartesianCS}, {@code cylindricalCS}, {@code 
ellipsoidalCS}, {@code linearCS},
+     * {@code parametricCS}, {@code polarCS}, {@code sphericalCS}, {@code 
timeCS} or {@code verticalCS}.
+     *
+     * @param  base The abstract base interface.
+     * @param  type The interface or classes for which to get the XML property 
name.
+     * @return The XML property name for the given class or interface, or 
{@code null} if none.
+     *
+     * @since 0.6
+     */
+    public static StringBuilder toPropertyName(final Class<?> base, final 
Class<?> type) {
+        final UML uml = type.getAnnotation(UML.class);
+        if (uml != null && uml.specification() == Specification.ISO_19111) {
+            final String name = uml.identifier();
+            final int length = name.length();
+            final StringBuilder buffer = new 
StringBuilder(length).append(name, name.indexOf('_') + 1, length);
+            if (buffer.length() != 0) {
+                buffer.setCharAt(0, Character.toLowerCase(buffer.charAt(0)));
+                return buffer;
+            }
+        }
+        for (final Class<?> c : type.getInterfaces()) {
+            if (base.isAssignableFrom(c)) {
+                final StringBuilder name = toPropertyName(base, c);
+                if (name != null) {
+                    return name;
+                }
+            }
+        }
+        return null;
+    }
+
+    /**
      * Returns the WKT type of the given interface.
      *
      * For {@link CoordinateSystem} base type, the returned value shall be one 
of
@@ -236,28 +272,18 @@ public final class ReferencingUtilities
      */
     public static String toWKTType(final Class<?> base, final Class<?> type) {
         if (type != base) {
-            final UML uml = type.getAnnotation(UML.class);
-            if (uml != null && uml.specification() == Specification.ISO_19111) 
{
-                String name = uml.identifier();
-                final int length = name.length() - 5; // Length without "CS_" 
and "CS".
-                if (length >= 1 && name.startsWith("CS_") && 
name.endsWith("CS")) {
-                    final StringBuilder buffer = new 
StringBuilder(length).append(name, 3, 3 + length);
-                    if (!name.regionMatches(3, "Cartesian", 0, 9)) {
-                        buffer.setCharAt(0, 
Character.toLowerCase(buffer.charAt(0)));
-                    }
-                    name = buffer.toString();
-                    if (name.equals("time")) {
-                        name = "temporal";
+            final StringBuilder name = toPropertyName(base, type);
+            if (name != null) {
+                int end = name.length() - 2;
+                if (CharSequences.regionMatches(name, end, "CS")) {
+                    name.setLength(end);
+                    if ("time".contentEquals(name)) {
+                        return "temporal";
                     }
-                    return name;
-                }
-            }
-            for (final Class<?> c : type.getInterfaces()) {
-                if (base.isAssignableFrom(c)) {
-                    final String name = toWKTType(base, c);
-                    if (name != null) {
-                        return name;
+                    if (CharSequences.regionMatches(name, 0, "cartesian")) {
+                        name.setCharAt(0, 'C');     // "Cartesian"
                     }
+                    return name.toString();
                 }
             }
         }

Modified: 
sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/referencing/crs/AbstractCRS.java
URL: 
http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/referencing/crs/AbstractCRS.java?rev=1701277&r1=1701276&r2=1701277&view=diff
==============================================================================
--- 
sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/referencing/crs/AbstractCRS.java
 [UTF-8] (original)
+++ 
sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/referencing/crs/AbstractCRS.java
 [UTF-8] Fri Sep  4 14:48:14 2015
@@ -512,42 +512,19 @@ public class AbstractCRS extends Abstrac
     }
 
     /**
-     * Sets the coordinate system to the given value. This method is invoked 
only by JAXB at
-     * unmarshalling time and can be invoked only if the coordinate system has 
never been set.
+     * Sets the coordinate system to the given value.
+     * This method is indirectly invoked by JAXB at unmarshalling time.
      *
-     * <div class="note"><b>Implementation note:</b>
-     * It was easy to put JAXB annotations directly on datum fields in 
subclasses because each CRS type
-     * can be associated to only one datum type. But we do not have this 
convenience for coordinate systems,
-     * where the same CRS may accept different kinds of CS. In GML, the 
different kinds of CS are marshalled
-     * as different XML elements. The usual way to handle such {@code 
<xs:choice>} with JAXB is to annotate
-     * a single method like below:
-     *
-     * {@preformat java
-     *   &#64;Override
-     *   &#64;XmlElements({
-     *     &#64;XmlElement(name = "cartesianCS",   type = 
DefaultCartesianCS.class),
-     *     &#64;XmlElement(name = "affineCS",      type = 
DefaultAffineCS.class),
-     *     &#64;XmlElement(name = "cylindricalCS", type = 
DefaultCylindricalCS.class),
-     *     &#64;XmlElement(name = "linearCS",      type = 
DefaultLinearCS.class),
-     *     &#64;XmlElement(name = "polarCS",       type = 
DefaultPolarCS.class),
-     *     &#64;XmlElement(name = "sphericalCS",   type = 
DefaultSphericalCS.class),
-     *     &#64;XmlElement(name = "userDefinedCS", type = 
DefaultUserDefinedCS.class)
-     *   })
-     *   public CoordinateSystem getCoordinateSystem() {
-     *       return super.getCoordinateSystem();
-     *   }
-     * }
-     *
-     * However our attempts to apply this approach have not been conclusive.
-     * For an unknown reason, the unmarshalled CS object was empty.</div>
-     *
-     * @param  name The property name, used only in case of error message to 
format.
+     * @param  name The property name, used only in case of error message to 
format. Can be null for auto-detect.
      * @throws IllegalStateException If the coordinate system has already been 
set.
      */
-    final void setCoordinateSystem(final String name, final CoordinateSystem 
cs) {
+    final void setCoordinateSystem(String name, final CoordinateSystem cs) {
         if (coordinateSystem == null) {
             coordinateSystem = cs;
         } else {
+            if (name == null) {
+                name = 
String.valueOf(ReferencingUtilities.toPropertyName(CoordinateSystem.class, 
cs.getClass()));
+            }
             ReferencingUtilities.propertyAlreadySet(AbstractCRS.class, 
"setCoordinateSystem", name);
         }
     }

Modified: 
sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/referencing/crs/DefaultEngineeringCRS.java
URL: 
http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/referencing/crs/DefaultEngineeringCRS.java?rev=1701277&r1=1701276&r2=1701277&view=diff
==============================================================================
--- 
sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/referencing/crs/DefaultEngineeringCRS.java
 [UTF-8] (original)
+++ 
sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/referencing/crs/DefaultEngineeringCRS.java
 [UTF-8] Fri Sep  4 14:48:14 2015
@@ -273,6 +273,6 @@ public class DefaultEngineeringCRS exten
      * @see #getCoordinateSystem()
      */
     private void setCoordinateSystem(final CoordinateSystem cs) {
-        super.setCoordinateSystem("coordinateSystem", cs);
+        setCoordinateSystem(null, cs);  // 'null' here means to infer the XML 
property name from the cs type.
     }
 }

Modified: 
sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/referencing/crs/DefaultGeodeticCRS.java
URL: 
http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/referencing/crs/DefaultGeodeticCRS.java?rev=1701277&r1=1701276&r2=1701277&view=diff
==============================================================================
--- 
sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/referencing/crs/DefaultGeodeticCRS.java
 [UTF-8] (original)
+++ 
sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/referencing/crs/DefaultGeodeticCRS.java
 [UTF-8] Fri Sep  4 14:48:14 2015
@@ -255,15 +255,39 @@ class DefaultGeodeticCRS extends Abstrac
 
     /**
      * Invoked by JAXB at marshalling time.
+     *
+     * <div class="note"><b>Implementation note:</b>
+     * The usual way to handle {@code <xs:choice>} with JAXB is to annotate a 
single method like below:
+     *
+     * {@preformat java
+     *     &#64;Override
+     *     &#64;XmlElements({
+     *       &#64;XmlElement(name = "ellipsoidalCS", type = 
DefaultEllipsoidalCS.class),
+     *       &#64;XmlElement(name = "cartesianCS",   type = 
DefaultCartesianCS.class),
+     *       &#64;XmlElement(name = "sphericalCS",   type = 
DefaultSphericalCS.class)
+     *     })
+     *     public CoordinateSystem getCoordinateSystem() {
+     *         return super.getCoordinateSystem();
+     *     }
+     * }
+     *
+     * However our attempts to apply this approach worked for {@link 
DefaultEngineeringCRS} but not for this class:
+     * for an unknown reason, the unmarshalled CS object is empty. Maybe this 
is because the covariant return type
+     * in the {@link DefaultGeographicCRS} ({@code EllipsoidCS} instead than 
{@code CoordinateSystem} in above code)
+     * is causing confusion.</div>
+     *
+     * @see <a href="http://issues.apache.org/jira/browse/SIS-166";>SIS-166</a>
      */
+    @XmlElement(name="ellipsoidalCS") private EllipsoidalCS getEllipsoidalCS() 
{return getCoordinateSystem(EllipsoidalCS.class);}
     @XmlElement(name="cartesianCS")   private CartesianCS   getCartesianCS()   
{return getCoordinateSystem(CartesianCS  .class);}
     @XmlElement(name="sphericalCS")   private SphericalCS   getSphericalCS()   
{return getCoordinateSystem(SphericalCS  .class);}
-    @XmlElement(name="ellipsoidalCS") private EllipsoidalCS getEllipsoidalCS() 
{return getCoordinateSystem(EllipsoidalCS.class);}
 
     /**
      * Invoked by JAXB at unmarshalling time.
+     *
+     * @see #getEllipsoidalCS()
      */
+    private void setEllipsoidalCS(final EllipsoidalCS cs) 
{super.setCoordinateSystem("ellipsoidalCS", cs);}
     private void setCartesianCS  (final CartesianCS   cs) 
{super.setCoordinateSystem("cartesianCS",   cs);}
     private void setSphericalCS  (final SphericalCS   cs) 
{super.setCoordinateSystem("sphericalCS",   cs);}
-    private void setEllipsoidalCS(final EllipsoidalCS cs) 
{super.setCoordinateSystem("ellipsoidalCS", cs);}
 }

Modified: 
sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/referencing/crs/DefaultImageCRS.java
URL: 
http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/referencing/crs/DefaultImageCRS.java?rev=1701277&r1=1701276&r2=1701277&view=diff
==============================================================================
--- 
sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/referencing/crs/DefaultImageCRS.java
 [UTF-8] (original)
+++ 
sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/referencing/crs/DefaultImageCRS.java
 [UTF-8] Fri Sep  4 14:48:14 2015
@@ -258,14 +258,36 @@ public class DefaultImageCRS extends Abs
 
     /**
      * Used by JAXB only (invoked by reflection).
-     * Only one of {@code getAffineCS()} and {@link #getCartesianCS()} can 
return a non-null value.
+     * Only one of {@code getCartesianCS()} and {@link #getAffineCS()} can 
return a non-null value.
+     *
+     * <div class="note"><b>Implementation note:</b>
+     * The usual way to handle {@code <xs:choice>} with JAXB is to annotate a 
single method like below:
+     *
+     * {@preformat java
+     *     &#64;Override
+     *     &#64;XmlElements({
+     *       &#64;XmlElement(name = "cartesianCS", type = 
DefaultCartesianCS.class),
+     *       &#64;XmlElement(name = "affineCS",    type = 
DefaultAffineCS.class)
+     *     })
+     *     public AffineCS getCoordinateSystem() {
+     *         return super.getCoordinateSystem();
+     *     }
+     * }
+     *
+     * However our attempts to apply this approach worked for {@link 
DefaultEngineeringCRS} but not for this class:
+     * for an unknown reason, the unmarshalled CS object is empty. Maybe this 
is because the covariant return type
+     * ({@code AffineCS} instead than {@code CoordinateSystem} in above code) 
is causing confusion.</div>
+     *
+     * @see <a href="http://issues.apache.org/jira/browse/SIS-166";>SIS-166</a>
      */
-    @XmlElement(name = "affineCS")    private AffineCS    getAffineCS()    
{return getCoordinateSystem(AffineCS.class);}
     @XmlElement(name = "cartesianCS") private CartesianCS getCartesianCS() 
{return getCoordinateSystem(CartesianCS.class);}
+    @XmlElement(name = "affineCS")    private AffineCS    getAffineCS()    
{return getCoordinateSystem(AffineCS.class);}
 
     /**
      * Used by JAXB only (invoked by reflection).
+     *
+     * @see #getCartesianCS()
      */
-    private void setAffineCS   (final AffineCS    cs) 
{setCoordinateSystem("affineCS",    cs);}
     private void setCartesianCS(final CartesianCS cs) 
{setCoordinateSystem("cartesianCS", cs);}
+    private void setAffineCS   (final AffineCS    cs) 
{setCoordinateSystem("affineCS",    cs);}
 }

Modified: 
sis/branches/JDK8/core/sis-referencing/src/test/java/org/apache/sis/internal/referencing/ReferencingUtilitiesTest.java
URL: 
http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-referencing/src/test/java/org/apache/sis/internal/referencing/ReferencingUtilitiesTest.java?rev=1701277&r1=1701276&r2=1701277&view=diff
==============================================================================
--- 
sis/branches/JDK8/core/sis-referencing/src/test/java/org/apache/sis/internal/referencing/ReferencingUtilitiesTest.java
 [UTF-8] (original)
+++ 
sis/branches/JDK8/core/sis-referencing/src/test/java/org/apache/sis/internal/referencing/ReferencingUtilitiesTest.java
 [UTF-8] Fri Sep  4 14:48:14 2015
@@ -38,7 +38,7 @@ import static org.apache.sis.internal.re
  *
  * @author  Martin Desruisseaux (Geomatys)
  * @since   0.5 (derived from 0.4)
- * @version 0.5
+ * @version 0.6
  * @module
  */
 public final strictfp class ReferencingUtilitiesTest extends TestCase {
@@ -84,6 +84,26 @@ public final strictfp class ReferencingU
     }
 
     /**
+     * Tests {@link ReferencingUtilities#toPropertyName(Class, Class)}.
+     *
+     * @since 0.6
+     */
+    @Test
+    public void testToPropertyName() {
+        assertEquals("coordinateSystem", 
toPropertyName(CoordinateSystem.class, CoordinateSystem.class).toString());
+        assertEquals("affineCS",         
toPropertyName(CoordinateSystem.class, AffineCS        .class).toString());
+        assertEquals("cartesianCS",      
toPropertyName(CoordinateSystem.class, CartesianCS     .class).toString());
+        assertEquals("cylindricalCS",    
toPropertyName(CoordinateSystem.class, CylindricalCS   .class).toString());
+        assertEquals("ellipsoidalCS",    
toPropertyName(CoordinateSystem.class, EllipsoidalCS   .class).toString());
+        assertEquals("linearCS",         
toPropertyName(CoordinateSystem.class, LinearCS        .class).toString());
+//      assertEquals("parametricCS",     
toPropertyName(CoordinateSystem.class, ParametricCS    .class).toString());
+        assertEquals("polarCS",          
toPropertyName(CoordinateSystem.class, PolarCS         .class).toString());
+        assertEquals("sphericalCS",      
toPropertyName(CoordinateSystem.class, SphericalCS     .class).toString());
+        assertEquals("timeCS",           
toPropertyName(CoordinateSystem.class, TimeCS          .class).toString());
+        assertEquals("verticalCS",       
toPropertyName(CoordinateSystem.class, VerticalCS      .class).toString());
+    }
+
+    /**
      * Tests {@link ReferencingUtilities#toWKTType(Class, Class)}.
      */
     @Test

Modified: 
sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/util/CharSequences.java
URL: 
http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/util/CharSequences.java?rev=1701277&r1=1701276&r2=1701277&view=diff
==============================================================================
--- 
sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/util/CharSequences.java
 [UTF-8] (original)
+++ 
sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/util/CharSequences.java
 [UTF-8] Fri Sep  4 14:48:14 2015
@@ -651,6 +651,7 @@ search:     for (; fromIndex <= toIndex;
      *
      * @see String#split(String)
      */
+    @SuppressWarnings("ReturnOfCollectionOrArrayField")
     public static CharSequence[] split(final CharSequence text, final char 
separator) {
         if (text == null) {
             return EMPTY_ARRAY;
@@ -717,6 +718,7 @@ search:     for (; fromIndex <= toIndex;
      *
      * @see #indexOfLineStart(CharSequence, int, int)
      */
+    @SuppressWarnings("ReturnOfCollectionOrArrayField")
     public static CharSequence[] splitOnEOL(final CharSequence text) {
         if (text == null) {
             return EMPTY_ARRAY;


Reply via email to