Author: desruisseaux
Date: Fri Jan 24 20:58:17 2014
New Revision: 1561169

URL: http://svn.apache.org/r1561169
Log:
Refactored and simplified the Convention enum in light of the upcomming WKT 2 
standard.
Partia port of WKTFormat.

Modified:
    
sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/io/wkt/Colors.java
    
sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/io/wkt/Convention.java
    
sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/io/wkt/FormattableObject.java
    
sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/io/wkt/Formatter.java
    
sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/io/wkt/Symbols.java
    
sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/io/wkt/WKTFormat.java
    
sis/branches/JDK7/core/sis-referencing/src/test/java/org/apache/sis/io/wkt/ConventionTest.java

Modified: 
sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/io/wkt/Colors.java
URL: 
http://svn.apache.org/viewvc/sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/io/wkt/Colors.java?rev=1561169&r1=1561168&r2=1561169&view=diff
==============================================================================
--- 
sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/io/wkt/Colors.java
 [UTF-8] (original)
+++ 
sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/io/wkt/Colors.java
 [UTF-8] Fri Jan 24 20:58:17 2014
@@ -50,13 +50,15 @@ public class Colors implements Serializa
     private static final long serialVersionUID = 256160285861027191L;
 
     /**
-     * The immutable default set of colors.
+     * A map of colors for outputs to the {@link java.io.Console}.
+     * Those colors give better results on a black background.
+     * This map is immutable.
      *
-     * @see Symbols#DEFAULT
+     * @see FormattableObject#print()
      */
-    public static final Colors DEFAULT = new Immutable();
+    public static final Colors CONSOLE = new Immutable();
     static {
-        final EnumMap<ElementKind,X364> map = DEFAULT.map;
+        final EnumMap<ElementKind,X364> map = CONSOLE.map;
         map.put(ElementKind.NUMBER,     X364.FOREGROUND_YELLOW);
         map.put(ElementKind.INTEGER,    X364.FOREGROUND_YELLOW);
         map.put(ElementKind.UNIT,       X364.FOREGROUND_YELLOW);
@@ -134,7 +136,7 @@ public class Colors implements Serializa
     }
 
     /**
-     * An immutable subclass of {@link Colors} for the {@link Colors#DEFAULT} 
constant
+     * An immutable subclass of {@link Colors} for the {@link Colors#CONSOLE} 
constant
      * or for the object to be used by {@link WKTFormat}.
      */
     private static final class Immutable extends Colors {
@@ -174,10 +176,10 @@ public class Colors implements Serializa
         }
 
         /**
-         * Replaces the deserialized instance by {@link #DEFAULT} one if 
possible.
+         * Replaces the deserialized instance by {@link #CONSOLE} one if 
possible.
          */
         Object readResolve() throws ObjectStreamException {
-            return super.map.equals(DEFAULT.map) ? DEFAULT : this;
+            return super.map.equals(CONSOLE.map) ? CONSOLE : this;
         }
     }
 }

Modified: 
sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/io/wkt/Convention.java
URL: 
http://svn.apache.org/viewvc/sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/io/wkt/Convention.java?rev=1561169&r1=1561168&r2=1561169&view=diff
==============================================================================
--- 
sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/io/wkt/Convention.java
 [UTF-8] (original)
+++ 
sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/io/wkt/Convention.java
 [UTF-8] Fri Jan 24 20:58:17 2014
@@ -23,8 +23,6 @@ import org.opengis.metadata.citation.Cit
 import org.opengis.referencing.cs.CartesianCS;
 import org.opengis.referencing.cs.CoordinateSystem;
 import org.opengis.referencing.crs.GeocentricCRS;
-import org.opengis.referencing.operation.OperationMethod;
-import org.opengis.referencing.operation.CoordinateOperation;
 import org.apache.sis.util.Debug;
 import org.apache.sis.metadata.iso.citation.Citations;
 
@@ -63,136 +61,62 @@ import static javax.measure.unit.NonSI.D
  */
 public enum Convention {
     /**
-     * The <a href="http://www.opengeospatial.org";>Open Geospatial 
consortium</a> convention.
+     * The ISO 19162 format, also known as “WKT 2”.
      * This is the default convention for all WKT formatting in the Apache SIS 
library.
-     * Some worthy aspects to note:
+     * Unless otherwise specified by {@link WKTFormat#setAuthority(Citation)}, 
when using
+     * this convention SIS will favor <a href="http://www.epsg.org";>EPSG</a> 
definitions
+     * of projection and parameter names.
      *
-     * <ul>
-     *   <li>For {@link GeocentricCRS}, this convention uses the legacy set of 
Cartesian axes.
-     *     Those axes were defined in OGC 01-009 as <var>Other</var>, 
<var>Easting</var> and <var>Northing</var>
-     *     in metres, where the "<var>Other</var>" axis is toward prime 
meridian.</li>
-     * </ul>
-     *
-     * @see Citations#OGC
-     * @see #toConformCS(CoordinateSystem)
+     * @see Citations#ISO
+     * @see Citations#EPSG
      */
-    OGC(Citations.OGC, null, false),
+    WKT2(Citations.EPSG, true, false),
 
     /**
-     * The <a href="http://www.epsg.org";>European Petroleum Survey Group</a> 
convention.
-     * This convention uses the most descriptive parameter and projection 
names.
-     * Some worthy aspects to note:
+     * The OGC 01-009 format, also known as “WKT 1”.
+     * A definition for this format is shown in Extended Backus Naur Form 
(EBNF)
+     * <a 
href="http://www.geoapi.org/3.0/javadoc/org/opengis/referencing/doc-files/WKT.html";>on
 GeoAPI</a>.
+     * Unless otherwise specified by {@link WKTFormat#setAuthority(Citation)}, 
when using
+     * this convention SIS will favor OGC definitions of projection and 
parameter names.
      *
+     * <p>Some worthy aspects to note:</p>
      * <ul>
-     *   <li>For {@link GeocentricCRS}, this convention uses the new set of 
Cartesian axes.
-     *     Those axes are defined in ISO 19111 as <var>Geocentric X</var>, 
<var>Geocentric Y</var>
-     *     and <var>Geocentric Z</var> in metres.</li>
+     *   <li>For {@link GeocentricCRS}, this convention uses the legacy set of 
Cartesian axes.
+     *     Those axes were defined in OGC 01-009 as <var>Other</var>, 
<var>Easting</var> and <var>Northing</var>
+     *     in metres, where the "<var>Other</var>" axis is toward prime 
meridian.</li>
      * </ul>
      *
-     * @see Citations#EPSG
-     * @see #toConformCS(CoordinateSystem)
+     * @see Citations#OGC
      */
-    EPSG(Citations.EPSG, null, false) {
-        @Override
-        public CoordinateSystem toConformCS(CoordinateSystem cs) {
-            if (cs instanceof CartesianCS) {
-                cs = Legacy.replace((CartesianCS) cs, false);
-            }
-            return cs;
-        }
-    },
+    WKT1(Citations.OGC, false, false),
 
     /**
-     * The <a href="http://www.esri.com";>ESRI</a> convention.
-     * This convention is similar to the {@link #OGC} convention except in the 
following aspects:
+     * The <cite>Simple Feature</cite> format, also known as “WKT 1”.
+     * <cite>Simple Feature</cite> is anterior to OGC 01-009 and defines the 
same format,
+     * but was unclear about the unit of measurement for prime meridians and 
projection parameters.
+     * Consequently many implementations interpreted those angular units as 
fixed to degrees instead
+     * than being context-dependent.
      *
+     * <p>This convention is identical to {@link #WKT1} except for the 
following aspects:</p>
      * <ul>
      *   <li>The angular units of {@code PRIMEM} and {@code PARAMETER} 
elements are always degrees,
      *       no matter the units of the enclosing {@code GEOGCS} element.</li>
-     *   <li>The {@code AXIS} elements are ignored at parsing time.</li>
      *   <li>Unit names use American spelling instead than the international 
ones
      *       (e.g. "<cite>meter</cite>" instead than 
"<cite>metre</cite>").</li>
-     *   <li>At parsing time, the {@code AXIS} elements are ignored.</li>
-     * </ul>
-     *
-     * @see Citations#ESRI
-     */
-    ESRI(Citations.ESRI, DEGREE_ANGLE, true),
-
-    /**
-     * The <a href="http://www.oracle.com";>Oracle</a> convention.
-     * This convention is similar to the {@link #OGC} convention except in the 
following aspects:
-     *
-     * <ul>
-     *   <li>The Bursa-Wolf parameters are inserted straight into the {@code 
DATUM} element,
-     *       without enclosing them in a {@code TOWGS84} element.</li>
-     *   <li>The {@code PROJECTION} names are {@linkplain CoordinateOperation 
Coordinate Operation}
-     *       names instead than {@linkplain OperationMethod Operation Method} 
names.</li>
-     *   <li>Unit names use American spelling instead than the international 
ones
-     *       (e.g. "<cite>meter</cite>" instead than 
"<cite>metre</cite>").</li>
-     * </ul>
-     *
-     * @see Citations#ORACLE
-     */
-    ORACLE(Citations.ORACLE, null, true),
-
-    /**
-     * The <a 
href="http://www.unidata.ucar.edu/software/netcdf-java";>NetCDF</a> convention.
-     * This convention is similar to the {@link #OGC} convention except in the 
following aspects:
-     *
-     * <ul>
-     *   <li>Parameter and projection names.</li>
-     * </ul>
-     *
-     * @see Citations#NETCDF
-     */
-    NETCDF(Citations.NETCDF, null, false),
-
-    /**
-     * The <a 
href="http://www.remotesensing.org/geotiff/geotiff.html";>GeoTIFF</a> convention.
-     * This convention is similar to the {@link #OGC} convention except in the 
following aspects:
-     *
-     * <ul>
-     *   <li>Parameter and projection names.</li>
      * </ul>
-     *
-     * @see Citations#GEOTIFF
      */
-    GEOTIFF(Citations.GEOTIFF, null, false),
-
-    /**
-     * The <a href="http://trac.osgeo.org/proj/";>Proj.4</a> convention.
-     * This convention is similar to the {@link #OGC} convention except in the 
following aspects:
-     *
-     * <ul>
-     *   <li>Very short parameter and projection names.</li>
-     *   <li>The angular units of {@code PRIMEM} and {@code PARAMETER} 
elements are always degrees,
-     *       no matter the units of the enclosing {@code GEOGCS} element.</li>
-     * </ul>
-     *
-     * @see Citations#PROJ4
-     */
-    PROJ4(Citations.PROJ4, DEGREE_ANGLE, false),
+    WKT1_COMMON_UNITS(Citations.OGC, false, true),
 
     /**
      * A special convention for formatting objects as stored internally by 
Apache SIS.
-     * In the majority of cases, the result will be identical to the one we 
would get using the {@link #OGC} convention.
+     * In the majority of cases, the result will be identical to the one we 
would get using the {@link #WKT1} convention.
      * However in the particular case of map projections, the result may be 
quite different because of the way
      * SIS separates the linear from the non-linear parameters.
      *
      * <p>This convention is used only for debugging purpose.</p>
      */
     @Debug
-    INTERNAL(Citations.OGC, null, false) {
-        /**
-         * Declares publicly that this convention is defined by Apache SIS, 
despite the
-         * package-private {@link #authority} field being set to OGC for 
{@link Formatter} needs.
-         */
-        @Override
-        public Citation getAuthority() {
-            return Citations.SIS;
-        }
-
+    INTERNAL(Citations.OGC, true, false) {
         @Override
         public CoordinateSystem toConformCS(final CoordinateSystem cs) {
             return cs; // Prevent any modification on the internal CS.
@@ -200,78 +124,61 @@ public enum Convention {
     };
 
     /**
-     * If non-null, forces {@code PRIMEM} and {@code PARAMETER} angular units 
to this field
-     * value instead than inferring it from the context. The standard value is 
{@code null},
-     * which means that the angular units are inferred from the context as 
required by the
-     * <a 
href="http://www.geoapi.org/3.0/javadoc/org/opengis/referencing/doc-files/WKT.html#PRIMEM";>WKT
 specification</a>.
+     * The default conventions.
      *
-     * @see #getForcedUnit(Class)
+     * @todo Make final after we completed the migration from Geotk.
      */
-    final Unit<Angle> forcedAngularUnit;
+    static Convention DEFAULT = WKT2;
 
     /**
-     * {@code true} if the convention uses US unit names instead of the 
international names.
-     * For example Americans said [@code "meter"} instead of {@code "metre"}.
+     * {@code true} for using WKT 2 syntax, or {@code false} for using WKT 1 
syntax.
      */
-    final boolean unitUS;
+    final boolean isISO;
 
     /**
-     * The organization, standard or project to use for fetching Map 
Projection parameter names.
-     * Shall be one of the authorities known to {@link 
org.apache.sis.referencing.operation.provider}.
+     * {@code true} for a frequently-used convention about units instead than 
the standard one.
+     * <ul>
+     *   <li>If {@code true}, forces {@code PRIMEM} and {@code PARAMETER} 
angular units to degrees
+     *       instead than inferring the unit from the context. The standard 
value is {@code false},
+     *       which means that the angular units are inferred from the context 
as required by the
+     *       WKT 1 specification.</li>
+     *   <li>If {@code true}, uses US unit names instead of the international 
names.
+     *       For example Americans said {@code "meter"} instead of {@code 
"metre"}.</li>
+     * </ul>
+     *
+     * @see #getForcedUnit(Class)
      */
-    final Citation authority;
+    final boolean commonUnits;
 
     /**
-     * Creates a new enumeration value.
+     * The organization, standard or project to look for when fetching Map 
Projection parameter names.
+     * Should be one of the authorities known to {@link 
org.apache.sis.referencing.operation.provider}.
      */
-    private Convention(final Citation authority, final Unit<Angle> 
angularUnit, final boolean unitUS) {
-        this.authority         = authority;
-        this.forcedAngularUnit = angularUnit;
-        this.unitUS            = unitUS;
-    }
+    private final Citation authority;
 
     /**
-     * Returns the convention for the organization, standard or project 
specified by the given citation.
-     *
-     * @param  authority The organization, standard or project for which to 
get the convention, or {@code null}.
-     * @param  defaultConvention The default convention to return if none 
where found for the given citation.
-     * @return The convention, or {@code null} if no matching convention were 
found and the
-     *         {@code defaultConvention} argument is {@code null}.
-     */
-    public static Convention forCitation(final Citation authority, final 
Convention defaultConvention) {
-        if (authority != null) {
-            for (final Convention candidate : values()) {
-                if (Citations.identifierMatches(candidate.getAuthority(), 
authority)) {
-                    return candidate;
-                }
-            }
-        }
-        return defaultConvention;
+     * Creates a new enumeration value.
+     */
+    private Convention(final Citation authority, final boolean isISO, final 
boolean commonUnits) {
+        this.authority   = authority;
+        this.isISO       = isISO;
+        this.commonUnits = commonUnits;
     }
 
     /**
-     * Returns the convention for the organization, standard or project 
specified by the given identifier.
+     * Returns the default authority to look for when fetching Map Projection 
parameter names.
+     * The value returned by this method can be overwritten by {@link 
WKTFormat#setAuthority(Citation)}.
      *
-     * @param  authority The organization, standard or project for which to 
get the convention, or {@code null}.
-     * @param  defaultConvention The default convention to return if none 
where found for the given identifier.
-     * @return The convention, or {@code null} if no matching convention were 
found and the
-     *         {@code defaultConvention} argument is {@code null}.
-     */
-    public static Convention forIdentifier(final String authority, final 
Convention defaultConvention) {
-        if (authority != null) {
-            for (final Convention candidate : values()) {
-                if (Citations.identifierMatches(candidate.getAuthority(), 
authority)) {
-                    return candidate;
-                }
-            }
-        }
-        return defaultConvention;
-    }
-
-    /**
-     * Returns the citation for the organization, standard of project that 
defines this convention.
+     * {@example The following table shows the names given by various 
organizations or projects for the same projection:
      *
-     * @return The organization, standard or project that defines this 
convention.
+     * <table class="sis">
+     *   <tr><th>Authority</th>                 <th>Projection name</th></tr>
+     *   <tr><td>{@link Citations#EPSG}</td>    <td>Mercator (variant 
A)</td></tr>
+     *   <tr><td>{@link Citations#OGC}</td>     <td>Mercator_1SP</td></tr>
+     *   <tr><td>{@link Citations#GEOTIFF}</td> <td>CT_Mercator</td></tr>
+     * </table>}
+     *
+     * @return The organization, standard or project to look for when fetching 
Map Projection parameter names.
      *
      * @see WKTFormat#getAuthority()
      */
@@ -292,8 +199,10 @@ public enum Convention {
      */
     @SuppressWarnings("unchecked")
     public <T extends Quantity> Unit<T> getForcedUnit(final Class<T> quantity) 
{
-        if (quantity == Angle.class) {
-            return (Unit) forcedAngularUnit;
+        if (commonUnits) {
+            if (quantity == Angle.class) {
+                return (Unit<T>) DEGREE_ANGLE;
+            }
         }
         return null;
     }
@@ -313,9 +222,6 @@ public enum Convention {
      * @param  cs The coordinate system.
      * @return A coordinate system equivalent to the given one but with 
conform axis names,
      *         or the given {@code cs} if no change apply to the given 
coordinate system.
-     *
-     * @see #OGC
-     * @see #EPSG
      */
     public CoordinateSystem toConformCS(CoordinateSystem cs) {
         if (cs instanceof CartesianCS) {

Modified: 
sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/io/wkt/FormattableObject.java
URL: 
http://svn.apache.org/viewvc/sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/io/wkt/FormattableObject.java?rev=1561169&r1=1561168&r2=1561169&view=diff
==============================================================================
--- 
sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/io/wkt/FormattableObject.java
 [UTF-8] (original)
+++ 
sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/io/wkt/FormattableObject.java
 [UTF-8] Fri Jan 24 20:58:17 2014
@@ -89,7 +89,7 @@ public class FormattableObject {
      * @see org.opengis.referencing.IdentifiedObject#toWKT()
      */
     public String toWKT() throws UnformattableObjectException {
-        return formatWKT(Convention.OGC, WKTFormat.DEFAULT_INDENTATION, false, 
true);
+        return formatWKT(Convention.DEFAULT, WKTFormat.DEFAULT_INDENTATION, 
false, true);
     }
 
     /**
@@ -101,13 +101,11 @@ public class FormattableObject {
      */
     @Override
     public String toString() {
-        return formatWKT(Convention.OGC, WKTFormat.DEFAULT_INDENTATION, false, 
false);
+        return formatWKT(Convention.DEFAULT, WKTFormat.DEFAULT_INDENTATION, 
false, false);
     }
 
     /**
      * Returns a <cite>Well Known Text</cite> (WKT) for this object using the 
specified convention.
-     * The convention is usually {@link Convention#OGC OGC}, but other common 
conventions are
-     * {@link Convention#GEOTIFF GEOTIFF} and {@link Convention#EPSG EPSG}.
      * The {@link Convention#INTERNAL INTERNAL} convention is a special value 
for debugging map projections.
      *
      * @param  convention The WKT convention to use.
@@ -125,12 +123,14 @@ public class FormattableObject {
      * the console supports the ANSI escape codes (a.k.a. X3.64), then a 
syntax coloring will be applied.
      *
      * <p>This is a convenience method for debugging purpose and for console 
applications.</p>
+     *
+     * @see Colors#CONSOLE
      */
     @Debug
     public void print() {
         final Console console = System.console();
         final PrintWriter out = (console != null) ? console.writer() : null;
-        final String wkt = formatWKT(Convention.OGC, 
WKTFormat.DEFAULT_INDENTATION,
+        final String wkt = formatWKT(Convention.DEFAULT, 
WKTFormat.DEFAULT_INDENTATION,
                 (out != null) && X364.isAnsiSupported(), false);
         if (out != null) {
             out.println(wkt);
@@ -159,7 +159,7 @@ public class FormattableObject {
             formatter = new Formatter();
         }
         formatter.indentation = indentation;
-        formatter.colors = colorize ? Colors.DEFAULT : null;
+        formatter.colors = colorize ? Colors.CONSOLE : null;
         formatter.setConvention(convention, null);
         final String wkt;
         try {

Modified: 
sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/io/wkt/Formatter.java
URL: 
http://svn.apache.org/viewvc/sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/io/wkt/Formatter.java?rev=1561169&r1=1561168&r2=1561169&view=diff
==============================================================================
--- 
sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/io/wkt/Formatter.java
 [UTF-8] (original)
+++ 
sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/io/wkt/Formatter.java
 [UTF-8] Fri Jan 24 20:58:17 2014
@@ -215,7 +215,7 @@ public class Formatter {
      * Creates a new formatter instance with the default symbols, no syntax 
coloring and the default indentation.
      */
     public Formatter() {
-        this(Convention.OGC, Symbols.DEFAULT, null, 
WKTFormat.DEFAULT_INDENTATION);
+        this(Convention.DEFAULT, Symbols.DEFAULT, null, 
WKTFormat.DEFAULT_INDENTATION);
     }
 
     /**
@@ -247,7 +247,7 @@ public class Formatter {
      * This constructor helps to share some objects with {@link Parser}.
      */
     Formatter(final Symbols symbols, final NumberFormat numberFormat) {
-        this.convention   = Convention.OGC;
+        this.convention   = Convention.DEFAULT;
         this.symbols      = symbols;
         this.indentation  = WKTFormat.DEFAULT_INDENTATION;
         this.numberFormat = numberFormat; // No clone needed.
@@ -256,8 +256,7 @@ public class Formatter {
     }
 
     /**
-     * Returns the convention to use for formatting the WKT. The default 
convention is {@link Convention#OGC OGC}.
-     * A different convention will usually result in different parameter 
names, but may also change the WKT syntax.
+     * Returns the convention to use for formatting the WKT. The default is 
{@link Convention#WKT2}.
      *
      * @return The convention (never {@code null}).
      *
@@ -275,25 +274,21 @@ public class Formatter {
      * @param authority  The authority, or {@code null} for inferring it from 
the convention.
      */
     final void setConvention(Convention convention, final Citation authority) {
-        if (convention == null) {
-            convention = Convention.forCitation(authority, Convention.OGC);
-        }
         this.convention = convention;
-        this.authority  = (authority != null) ? authority : 
convention.authority; // NOT convention.getAuthority()
+        this.authority  = (authority != null) ? authority : 
convention.getAuthority();
     }
 
     /**
      * Returns the preferred name for the specified object.
-     * If the specified object contains a name from the preferred authority
-     * (usually {@linkplain org.apache.sis.metadata.iso.citation.Citations#OGC 
Open Geospatial}),
-     * then this name is returned. Otherwise, the first name found is returned.
+     * If the specified object contains a name from the preferred authority, 
then this name is returned.
+     * Otherwise, the first name found is returned.
      *
      * <p>The preferred authority can be set by the {@link 
WKTFormat#setAuthority(Citation)} method.
      * This is not necessarily the authority of the given {@linkplain 
IdentifiedObject#getName() object name}.</p>
      *
      * {@example The EPSG name of the <code>EPSG:6326</code> datum is 
"<cite>World Geodetic System 1984</cite>".
-     *           However if the preferred authority is OGC (which is the case 
by default), then this method usually
-     *           returns "<cite>WGS84</cite>" (the exact string to be returned 
depends on the object aliases).}
+     *           However if the preferred authority is OGC, then this method 
usually returns "<cite>WGS84</cite>"
+     *           (the exact string to be returned depends on the object 
aliases).}
      *
      * @param  object The object to look for a preferred name.
      * @return The preferred name, or {@code null} if the given object has no 
name.
@@ -311,9 +306,8 @@ public class Formatter {
 
     /**
      * Returns the preferred identifier for the specified object.
-     * If the specified object contains an identifier from the preferred 
authority
-     * (usually {@linkplain org.apache.sis.metadata.iso.citation.Citations#OGC 
Open Geospatial}),
-     * then this identifier is returned. Otherwise, the first identifier is 
returned.
+     * If the specified object contains an identifier from the preferred 
authority, then this identifier is returned.
+     * Otherwise, the first identifier is returned.
      * If the specified object contains no identifier, then this method 
returns {@code null}.
      *
      * @param  info The object to look for a preferred identifier, or {@code 
null} if none.
@@ -598,7 +592,7 @@ public class Formatter {
                 if (contextUnit!=null && unit.isCompatible(contextUnit)) {
                     unit = contextUnit;
                 } else {
-                    contextUnit = convention.forcedAngularUnit;
+                    contextUnit = convention.getForcedUnit(Angle.class);
                     if (contextUnit == null) {
                         contextUnit = angularUnit;
                     }
@@ -776,7 +770,7 @@ public class Formatter {
             if (NonSI.DEGREE_ANGLE.equals(unit)) {
                 buffer.append("degree");
             } else if (SI.METRE.equals(unit)) {
-                buffer.append(convention.unitUS ? "meter" : "metre");
+                buffer.append(convention.commonUnits ? "meter" : "metre");
             } else {
                 unitFormat.format(unit, buffer, dummy);
             }

Modified: 
sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/io/wkt/Symbols.java
URL: 
http://svn.apache.org/viewvc/sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/io/wkt/Symbols.java?rev=1561169&r1=1561168&r2=1561169&view=diff
==============================================================================
--- 
sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/io/wkt/Symbols.java
 [UTF-8] (original)
+++ 
sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/io/wkt/Symbols.java
 [UTF-8] Fri Jan 24 20:58:17 2014
@@ -73,8 +73,6 @@ public class Symbols implements Localize
     /**
      * The default set of symbols, as documented in the class javadoc.
      * This is currently set to {@link #SQUARE_BRACKETS}.
-     *
-     * @see Colors#DEFAULT
      */
     public static final Symbols DEFAULT = SQUARE_BRACKETS;
 

Modified: 
sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/io/wkt/WKTFormat.java
URL: 
http://svn.apache.org/viewvc/sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/io/wkt/WKTFormat.java?rev=1561169&r1=1561168&r2=1561169&view=diff
==============================================================================
--- 
sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/io/wkt/WKTFormat.java
 [UTF-8] (original)
+++ 
sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/io/wkt/WKTFormat.java
 [UTF-8] Fri Jan 24 20:58:17 2014
@@ -16,8 +16,71 @@
  */
 package org.apache.sis.io.wkt;
 
+import java.text.Format;
+import java.text.FieldPosition;
+import java.text.ParsePosition;
+import org.opengis.metadata.citation.Citation;
+import org.opengis.parameter.GeneralParameterValue;
+import org.opengis.referencing.IdentifiedObject;
+import org.opengis.referencing.operation.Matrix;
+import org.opengis.referencing.operation.MathTransform;
+import org.apache.sis.util.ArgumentChecks;
+import org.apache.sis.util.resources.Errors;
+
+
+/**
+ * Parser and formatter for <cite>Well Known Text</cite> (WKT) objects.
+ * This format handles a pair of {@link Parser} and {@link Formatter},
+ * to be used by {@code parse} and {@code format} methods respectively.
+ * {@code WKTFormat} objects allow the following configuration:
+ *
+ * <ul>
+ *   <li>The {@linkplain Symbols symbols} to use (curly braces or brackets, 
<i>etc.</i>)</li>
+ *   <li>The preferred authority of {@linkplain IdentifiedObject#getName() 
object name} to
+ *       format (see {@link Formatter#getName(IdentifiedObject)} for more 
information)</li>
+ *   <li>Whatever ANSI X3.64 colors are allowed or not (default is not)</li>
+ *   <li>The indentation</li>
+ * </ul>
+ *
+ * {@section String expansion}
+ * Because the strings to be parsed by this class are long and tend to contain 
repetitive substrings,
+ * {@code WKTFormat} provides a mechanism for performing string substitutions 
before the parsing take place.
+ * Long strings can be assigned short names by calls to the
+ * <code>{@linkplain 
#definitions()}.put(<var>key</var>,<var>value</var>)</code> method.
+ * After definitions have been added, any call to a parsing method will 
replace all occurrences
+ * of a short name by the associated long string.
+ *
+ * <p>The short names must comply with the rules of Java identifiers. It is 
recommended, but not
+ * required, to prefix the names by some symbol like {@code "$"} in order to 
avoid ambiguity.
+ * Note however that this class doesn't replace occurrences between quoted 
text, so string
+ * expansion still relatively safe even when used with non-prefixed 
identifiers.</p>
+ *
+ * <blockquote><font size="-1"><b>Example:</b>
+ * In the example below, the {@code $WGS84} substring which appear in the 
argument given to the
+ * {@code parseObject(…)} method will be expanded into the full {@code 
GEOGCS["WGS84", …]} string
+ * before the parsing proceed.
+ *
+ * <blockquote><code>{@linkplain #definitions()}.put("$WGS84", 
"GEOGCS[\"WGS84\", DATUM[</code> <i>…etc…</i> <code>]]);<br>
+ * Object crs = {@linkplain #parseObject(String) 
parseObject}("PROJCS[\"Mercator_1SP\", <strong>$WGS84</strong>,
+ * PROJECTION[</code> <i>…etc…</i> <code>]]");</code></blockquote>
+ * </font></blockquote>
+ *
+ * {@section Thread safety}
+ * {@code WKTFormat}s are not synchronized. It is recommended to create 
separated format instances for each thread.
+ * If multiple threads access a {@code WKTFormat} concurrently, it must be 
synchronized externally.
+ *
+ * @author  Martin Desruisseaux (Geomatys)
+ * @author  Rémi Eve (IRD)
+ * @since   0.4 (derived from geotk-3.20)
+ * @version 0.4
+ * @module
+ */
+public class WKTFormat extends Format {
+    /**
+     * For cross-version compatibility.
+     */
+    private static final long serialVersionUID = -2909110214650709560L;
 
-public class WKTFormat {
     /**
      * The indentation value to give to the {@link #setIndentation(int)}
      * method for formatting the complete object on a single line.
@@ -29,6 +92,269 @@ public class WKTFormat {
      */
     static final byte DEFAULT_INDENTATION = 2;
 
-    private WKTFormat() {
+    /**
+     * The symbols to use for this formatter.
+     * The same object is also referenced in the {@linkplain #parser} and 
{@linkplain #formatter}.
+     * It appears here for serialization purpose.
+     */
+    private Symbols symbols = Symbols.DEFAULT;
+
+    /**
+     * The colors to use for this formatter, or {@code null} for no syntax 
coloring.
+     * The same object is also referenced in the {@linkplain #formatter}.
+     * It appears here for serialization purpose.
+     */
+    private Colors colors;
+
+    /**
+     * The convention to use. The same object is also referenced in the 
{@linkplain #formatter}.
+     * It appears here for serialization purpose.
+     */
+    private Convention convention = Convention.DEFAULT;
+
+    /**
+     * The preferred authority for objects or parameter names. A {@code null} 
value
+     * means that the authority shall be inferred from the {@linkplain 
#convention}.
+     */
+    private Citation authority;
+
+    /**
+     * The amount of spaces to use in indentation, or {@value #SINGLE_LINE} if 
indentation is disabled.
+     * The same value is also stored in the {@linkplain #formatter}.
+     * It appears here for serialization purpose.
+     */
+    private byte indentation = DEFAULT_INDENTATION;
+
+    /**
+     * A formatter using the same symbols than the {@linkplain #parser}.
+     * Will be created by the {@link #format} method when first needed.
+     */
+    private transient Formatter formatter;
+
+    /**
+     * Constructs a format using the default factories.
+     */
+    public WKTFormat() {
+    }
+
+    /**
+     * Returns the symbols used for parsing and formatting WKT.
+     *
+     * @return The current set of symbols used for parsing and formatting WKT.
+     */
+    public Symbols getSymbols() {
+        return symbols;
+    }
+
+    /**
+     * Sets the symbols used for parsing and formatting WKT.
+     *
+     * @param symbols The new set of symbols to use for parsing and formatting 
WKT.
+     */
+    public void setSymbols(final Symbols symbols) {
+        ArgumentChecks.ensureNonNull("symbols", symbols);
+        if (!symbols.equals(this.symbols)) {
+            this.symbols = symbols;
+            formatter = null;
+        }
+    }
+
+    /**
+     * Returns the colors to use for syntax coloring, or {@code null} if none.
+     * By default there is no syntax coloring.
+     *
+     * @return The colors for syntax coloring, or {@code null} if none.
+     */
+    public Colors getColors() {
+        return colors;
+    }
+
+    /**
+     * Sets the colors to use for syntax coloring.
+     * This property applies only when formatting text.
+     *
+     * <p>Newly created {@code WKTFormat}s have no syntax coloring. If a 
non-null argument like
+     * {@link Colors#CONSOLE} is given to this method, then the {@link 
#format(Object) format(…)}
+     * method tries to highlight most of the elements that are relevant to
+     * {@link org.apache.sis.referencing.CRS#equalsIgnoreMetadata(Object, 
Object)}.</p>
+     *
+     * @param colors The colors for syntax coloring, or {@code null} if none.
+     */
+    public void setColors(final Colors colors) {
+        this.colors = colors;
+        if (formatter != null) {
+            formatter.colors = colors;
+        }
+    }
+
+    /**
+     * Returns the convention for parsing and formatting WKT elements.
+     * The default value is {@link Convention#WKT2}.
+     *
+     * @return The convention to use for formatting WKT elements (never {@code 
null}).
+     */
+    public Convention getConvention() {
+        return convention;
+    }
+
+    /**
+     * Sets the convention for parsing and formatting WKT elements.
+     *
+     * @param convention The new convention to use for parsing and formatting 
WKT elements.
+     */
+    public void setConvention(final Convention convention) {
+        ArgumentChecks.ensureNonNull("convention", convention);
+        this.convention = convention;
+        updateFormatter(formatter);
+    }
+
+    /**
+     * Returns the preferred authority for choosing the projection and 
parameter names.
+     * If no authority has been {@linkplain #setAuthority(Citation) explicitly 
set}, then this
+     * method returns the authority associated to the {@linkplain 
#getConvention() convention}.
+     *
+     * @return The authority for projection and parameter names.
+     *
+     * @see Convention#getAuthority()
+     * @see Formatter#getName(IdentifiedObject)
+     */
+    public Citation getAuthority() {
+        Citation result = authority;
+        if (result == null) {
+            result = convention.getAuthority();
+        }
+        return result;
+    }
+
+    /**
+     * Sets the preferred authority for choosing the projection and parameter 
names.
+     * If non-null, the given priority will have precedence over the authority 
usually
+     * associated to the {@linkplain #getConvention() convention}. A {@code 
null} value
+     * restore the default behavior.
+     *
+     * @param authority The new authority, or {@code null} for inferring it 
from the convention.
+     *
+     * @see Formatter#getName(IdentifiedObject)
+     */
+    public void setAuthority(final Citation authority) {
+        this.authority = authority;
+        updateFormatter(formatter);
+        // No need to update the parser.
+    }
+
+    /**
+     * Updates the formatter convention and authority according the current 
state of this
+     * {@code WKTFormat}. The authority may be null, in which case it will be 
inferred from
+     * the convention when first needed.
+     */
+    private void updateFormatter(final Formatter formatter) {
+        if (formatter != null) {
+            formatter.setConvention(convention, authority);
+        }
+    }
+
+    /**
+     * Returns the current indentation to be used for formatting objects.
+     * The {@value #SINGLE_LINE} value means that the whole WKT is to be 
formatted on a single line.
+     *
+     * @return The current indentation.
+     */
+    public int getIndentation() {
+        return indentation;
+    }
+
+    /**
+     * Sets a new indentation to be used for formatting objects.
+     * The {@value #SINGLE_LINE} value means that the whole WKT is to be 
formatted on a single line.
+     *
+     * @param indentation The new indentation to use.
+     */
+    public void setIndentation(final int indentation) {
+        ArgumentChecks.ensureBetween("indentation", WKTFormat.SINGLE_LINE, 
Byte.MAX_VALUE, indentation);
+        this.indentation = (byte) indentation;
+        if (formatter != null) {
+            formatter.indentation = this.indentation;
+        }
+    }
+
+    /**
+     * Not yet supported.
+     *
+     * @param  text The text to parse.
+     * @param  position The index of the first character to parse.
+     * @return The parsed object, or {@code null} in case of failure.
+     */
+    @Override
+    public Object parseObject(final String text, final ParsePosition position) 
{
+        throw new UnsupportedOperationException("Not supported yet.");
+    }
+
+    /**
+     * Returns the formatter, creating it if needed.
+     */
+    private Formatter getFormatter() {
+        Formatter formatter = this.formatter;
+        if (formatter == null) {
+            formatter = new Formatter(Convention.DEFAULT, symbols, colors, 
indentation);
+            updateFormatter(formatter);
+            this.formatter = formatter;
+        }
+        return formatter;
+    }
+
+    /**
+     * Formats the specified object as a Well Know Text. The given object 
shall be an instance of one of
+     * {@link FormattableObject}, {@link IdentifiedObject}, {@link 
MathTransform}, {@link GeneralParameterValue}
+     * or {@link Matrix}.
+     *
+     * @param  object     The object to format.
+     * @param  toAppendTo Where the text is to be appended.
+     * @param  pos        An identification of a field in the formatted text.
+     * @return The given {@code toAppendTo} buffer.
+     *
+     * @see #getWarning()
+     */
+    @Override
+    public StringBuffer format(final Object        object,
+                               final StringBuffer  toAppendTo,
+                               final FieldPosition pos)
+    {
+        final Formatter formatter = getFormatter();
+        try {
+            formatter.clear();
+            formatter.buffer = toAppendTo;
+            formatter.bufferBase = toAppendTo.length();
+            if (object instanceof FormattableObject) {
+                formatter.append((FormattableObject) object);
+            } else if (object instanceof IdentifiedObject) {
+                formatter.append((IdentifiedObject) object);
+            } else if (object instanceof MathTransform) {
+                formatter.append((MathTransform) object);
+            } else if (object instanceof GeneralParameterValue) {
+                /*
+                 * Special processing for parameter values, which is formatted 
directly in 'Formatter'.
+                 * Note that this interface doesn't share the same parent 
interface than other interfaces.
+                 */
+                formatter.append((GeneralParameterValue) object);
+            } else if (object instanceof Matrix) {
+                formatter.append((Matrix) object);
+            } else {
+                throw new ClassCastException(Errors.format(
+                        Errors.Keys.IllegalArgumentClass_2, "object", 
object.getClass()));
+            }
+            return toAppendTo;
+        } finally {
+            formatter.buffer = null;
+        }
+    }
+
+    /**
+     * If a warning occurred during the last WKT {@linkplain #format 
formatting}, returns the warning.
+     * Otherwise returns {@code null}. The warning is cleared every time a new 
object is formatted.
+     *
+     * @return The last warning, or {@code null} if none.
+     */
+    public String getWarning() {
+        return (formatter != null) ? formatter.getErrorMessage() : null;
     }
 }

Modified: 
sis/branches/JDK7/core/sis-referencing/src/test/java/org/apache/sis/io/wkt/ConventionTest.java
URL: 
http://svn.apache.org/viewvc/sis/branches/JDK7/core/sis-referencing/src/test/java/org/apache/sis/io/wkt/ConventionTest.java?rev=1561169&r1=1561168&r2=1561169&view=diff
==============================================================================
--- 
sis/branches/JDK7/core/sis-referencing/src/test/java/org/apache/sis/io/wkt/ConventionTest.java
 [UTF-8] (original)
+++ 
sis/branches/JDK7/core/sis-referencing/src/test/java/org/apache/sis/io/wkt/ConventionTest.java
 [UTF-8] Fri Jan 24 20:58:17 2014
@@ -16,8 +16,9 @@
  */
 package org.apache.sis.io.wkt;
 
-import org.opengis.metadata.citation.Citation;
-import org.apache.sis.metadata.iso.citation.Citations;
+import javax.measure.unit.NonSI;
+import javax.measure.quantity.Angle;
+import javax.measure.quantity.Duration;
 import org.apache.sis.test.TestCase;
 import org.junit.Test;
 
@@ -34,43 +35,13 @@ import static org.junit.Assert.*;
  */
 public final strictfp class ConventionTest extends TestCase {
     /**
-     * Tests all citations associated with enum values.
+     * Tests {@link Convention#getForcedUnit(Class)}.
      */
     @Test
-    public void testGetCitation() {
-        for (final Convention convention : Convention.values()) {
-            final Citation citation = convention.getAuthority();
-            if (convention != Convention.PROJ4 && convention != 
Convention.INTERNAL) {
-                assertTrue(convention.name(), 
convention.name().equalsIgnoreCase(Citations.getIdentifier(citation)));
-            }
-        }
-    }
-
-    /**
-     * Tests {@link Convention#forCitation(Citation, Convention)}.
-     */
-    @Test
-    public void testForCitation() {
-        assertSame(Convention.OGC,     Convention.forCitation(Citations.OGC,   
  null));
-        assertSame(Convention.EPSG,    Convention.forCitation(Citations.EPSG,  
  null));
-        assertSame(Convention.ESRI,    Convention.forCitation(Citations.ESRI,  
  null));
-        assertSame(Convention.ORACLE,  
Convention.forCitation(Citations.ORACLE,  null));
-        assertSame(Convention.NETCDF,  
Convention.forCitation(Citations.NETCDF,  null));
-        assertSame(Convention.GEOTIFF, 
Convention.forCitation(Citations.GEOTIFF, null));
-        assertSame(Convention.PROJ4,   Convention.forCitation(Citations.PROJ4, 
  null));
-    }
-
-    /**
-     * Tests {@link Convention#forIdentifier(String, Convention)}.
-     */
-    @Test
-    public void testForIdentifier() {
-        assertSame(Convention.OGC,     Convention.forIdentifier("OGC",     
null));
-        assertSame(Convention.EPSG,    Convention.forIdentifier("EPSG",    
null));
-        assertSame(Convention.ESRI,    Convention.forIdentifier("ESRI",    
null));
-        assertSame(Convention.ORACLE,  Convention.forIdentifier("ORACLE",  
null));
-        assertSame(Convention.NETCDF,  Convention.forIdentifier("NETCDF",  
null));
-        assertSame(Convention.GEOTIFF, Convention.forIdentifier("GEOTIFF", 
null));
-        assertSame(Convention.PROJ4,   Convention.forIdentifier("PROJ4",   
null));
+    public void testGetForcedUnit() {
+        assertNull(Convention.WKT2.getForcedUnit(Angle.class));
+        assertNull(Convention.WKT1.getForcedUnit(Angle.class));
+        assertEquals(NonSI.DEGREE_ANGLE, 
Convention.WKT1_COMMON_UNITS.getForcedUnit(Angle.class));
+        assertNull(Convention.WKT1_COMMON_UNITS.getForcedUnit(Duration.class));
     }
 }


Reply via email to