sis-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From desruisse...@apache.org
Subject svn commit: r1576686 - in /sis/branches/JDK7/core/sis-referencing/src: main/java/org/apache/sis/internal/referencing/ main/java/org/apache/sis/parameter/ test/java/org/apache/sis/parameter/
Date Wed, 12 Mar 2014 10:42:35 GMT
Author: desruisseaux
Date: Wed Mar 12 10:42:34 2014
New Revision: 1576686

URL: http://svn.apache.org/r1576686
Log:
Complete the support of ParameterFormat.setPreferredCodespaces(String...).

Modified:
    sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/internal/referencing/NameToIdentifier.java
    sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/parameter/ParameterFormat.java
    sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/parameter/ParameterTableRow.java
    sis/branches/JDK7/core/sis-referencing/src/test/java/org/apache/sis/parameter/ParameterFormatTest.java

Modified: sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/internal/referencing/NameToIdentifier.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/internal/referencing/NameToIdentifier.java?rev=1576686&r1=1576685&r2=1576686&view=diff
==============================================================================
--- sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/internal/referencing/NameToIdentifier.java
[UTF-8] (original)
+++ sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/internal/referencing/NameToIdentifier.java
[UTF-8] Wed Mar 12 10:42:34 2014
@@ -16,11 +16,14 @@
  */
 package org.apache.sis.internal.referencing;
 
+import java.util.Locale;
 import org.opengis.util.NameSpace;
 import org.opengis.util.ScopedName;
 import org.opengis.util.GenericName;
+import org.opengis.util.InternationalString;
 import org.opengis.metadata.citation.Citation;
 import org.opengis.referencing.ReferenceIdentifier;
+import org.apache.sis.referencing.IdentifiedObjects;
 import org.apache.sis.metadata.iso.citation.Citations;
 
 import static org.apache.sis.util.ArgumentChecks.ensureNonNull;
@@ -51,44 +54,53 @@ public final class NameToIdentifier impl
     }
 
     /**
-     * Infers the authority from the scope of the given name.
+     * Infers the authority from the scope.
      *
-     * @param  name The name from which to infer the authority, or {@code null}.
      * @return The authority, or {@code null} if none.
      */
-    public static Citation getAuthority(final GenericName name) {
-        if (name != null) {
-            final NameSpace scope = name.scope();
-            if (scope != null && !scope.isGlobal()) {
-                return Citations.fromName(scope.name().tip().toString());
-            }
+    @Override
+    public Citation getAuthority() {
+        final NameSpace scope = name.scope();
+        if (scope != null && !scope.isGlobal()) {
+            return Citations.fromName(scope.name().tip().toString());
         }
         return null;
     }
 
     /**
-     * Infers the authority from the scope.
-     */
-    @Override
-    public Citation getAuthority() {
-        return getAuthority(name);
+     * Returns the code space of the given name, or its authority if there is no code space.
+     *
+     * @param  name The name from which to get the code space or authority, or {@code null}.
+     * @param  locale The locale, or {@code null} for a call to {@code name.toString()}.
+     * @return The code space or authority, or {@code null} if none.
+     */
+    public static String getCodespaceOrAuthority(final GenericName name, final Locale locale)
{
+        String codespace = getCodeSpace(name, locale);
+        if (codespace == null) {
+            final NameSpace scope = name.scope();
+            if (scope != null && !scope.isGlobal()) {
+                codespace = toString(scope.name().tip(), locale);
+            }
+        }
+        return codespace;
     }
 
     /**
      * Takes everything except the tip as the code space.
      *
-     * @param name The name from which to get the code space, or {@code null}.
+     * @param  name The name from which to get the code space, or {@code null}.
+     * @param  locale The locale, or {@code null} for a call to {@code name.toString()}.
      * @return The code space, or {@code null} if none.
      */
-    public static String getCodeSpace(final GenericName name) {
+    public static String getCodeSpace(final GenericName name, final Locale locale) {
         if (name != null) {
             if (name instanceof ScopedName) {
-                return ((ScopedName) name).path().toString();
+                return toString(((ScopedName) name).path(), locale);
             }
             if (name.depth() == 2) {
                 // May happen on GenericName implementation that do not implement the ScopedName
interface.
                 // The most importance case is org.apache.sis.referencing.NamedIdentifier.
-                return name.head().toString();
+                return toString(name.head(), locale);
             }
         }
         return null;
@@ -99,7 +111,7 @@ public final class NameToIdentifier impl
      */
     @Override
     public String getCodeSpace() {
-        return getCodeSpace(name);
+        return getCodeSpace(name, null);
     }
 
     /**
@@ -117,4 +129,39 @@ public final class NameToIdentifier impl
     public String getVersion() {
         return null;
     }
+
+    /**
+     * Returns the string representation of this identifier.
+     *
+     * @return The string representation of this identifier.
+     */
+    @Override
+    public String toString() {
+        return IdentifiedObjects.toString(this);
+    }
+
+    /**
+     * Returns a string representation of the given name in the given locale, with paranoiac
checks against null value.
+     * Such null values should never happen since the properties used here are mandatory,
but we try to make this class
+     * robust to broken implementations.
+     *
+     * @param  name   The name from which to get the localized string.
+     * @param  locale The locale, or {@code null} for a call to {@code name.toString()}.
+     * @return The localized string representation.
+     */
+    public static String toString(final GenericName name, final Locale locale) {
+        if (name != null) {
+            if (locale != null) {
+                final InternationalString i18n = name.toInternationalString();
+                if (i18n != null) {
+                    final String s = i18n.toString(locale);
+                    if (s != null) {
+                        return s;
+                    }
+                }
+            }
+            return name.toString();
+        }
+        return null;
+    }
 }

Modified: sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/parameter/ParameterFormat.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/parameter/ParameterFormat.java?rev=1576686&r1=1576685&r2=1576686&view=diff
==============================================================================
--- sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/parameter/ParameterFormat.java
[UTF-8] (original)
+++ sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/parameter/ParameterFormat.java
[UTF-8] Wed Mar 12 10:42:34 2014
@@ -49,7 +49,6 @@ import org.apache.sis.util.resources.Err
 import org.apache.sis.util.resources.Vocabulary;
 import org.apache.sis.referencing.IdentifiedObjects;
 import org.apache.sis.internal.referencing.NameToIdentifier;
-import org.apache.sis.metadata.iso.citation.Citations;
 import org.apache.sis.internal.util.CollectionsExt;
 import org.apache.sis.internal.util.X364;
 
@@ -74,7 +73,7 @@ import static org.apache.sis.util.collec
  * {@preformat text
  *   EPSG: Mercator (variant A)
  *   ┌────────────────────────────────┬────────┬───────────────┬───────────────┐
- *   │ Name                           │ Type   │ Value domain  │ Default value │
+ *   │ Name (EPSG)                    │ Type   │ Value domain  │ Default value │
  *   ├────────────────────────────────┼────────┼───────────────┼───────────────┤
  *   │ Latitude of natural origin     │ Double │  [-80 … 84]°  │         0.0°
 │
  *   │ Longitude of natural origin    │ Double │ [-180 … 180]° │         0.0°
 │
@@ -88,10 +87,10 @@ import static org.apache.sis.util.collec
  * The kind of objects accepted by this formatter are:
  * <table class="sis">
  *   <tr><th>Class</th> <th>Remarks</th></tr>
- *   <tr><td>{@link ParameterValueGroup}</td> <td>Column of <cite>default
values</cite> is replaced by a column of the actual values.</td></tr>
- *   <tr><td>{@link ParameterDescriptorGroup}</td> <td></td></tr>
- *   <tr><td>{@link OperationMethod}</td> <td></td></tr>
- *   <tr><td><code>{@linkplain IdentifiedObject}[]</code></td>
<td>Accepted only for {@link ContentLevel#NAME_SUMMARY}.</td></tr>
+ *   <tr><td>{@link ParameterValueGroup}</td><td><cite>Default
values</cite> column is replaced by a column of the actual values.</td></tr>
+ *   <tr><td>{@link ParameterDescriptorGroup}</td><td>Table title
is the parameter group name.</td></tr>
+ *   <tr><td>{@link OperationMethod}</td><td>Table title is the method
name (not necessarily the same than parameter group name).</td></tr>
+ *   <tr><td><code>{@linkplain IdentifiedObject}[]</code></td><td>Accepted
only for {@link ContentLevel#NAME_SUMMARY}.</td></tr>
  * </table>
  *
  * <div class="warning"><b>Limitation:</b>
@@ -177,7 +176,7 @@ public class ParameterFormat extends Tab
          * {@preformat text
          *   EPSG: Mercator (variant A)
          *   ┌────────────────────────────────┬────────┬───────────────┬───────────────┐
-         *   │ Name                           │ Type   │ Value domain  │ Default
value │
+         *   │ Name (EPSG)                    │ Type   │ Value domain  │ Default
value │
          *   ├────────────────────────────────┼────────┼───────────────┼───────────────┤
          *   │ Latitude of natural origin     │ Double │  [-80 … 84]°  │     
   0.0°  │
          *   │ Longitude of natural origin    │ Double │ [-180 … 180]° │     
   0.0°  │
@@ -316,9 +315,11 @@ public class ParameterFormat extends Tab
     }
 
     /**
-     * Returns the code spaces of names and identifiers to show, or {@code null} if there
is no restriction.
-     * This method returns the sequence specified by the last call to {@link #setPreferredCodespaces(String[])}.
-     * The default value is {@code null}.
+     * Returns the code spaces of names, aliases and identifiers to show, or {@code null}
if there is no restriction.
+     * This method returns the sequence specified by the last call to {@link #setPreferredCodespaces(String[])},
+     * without duplicated values.
+     *
+     * <p>The default value is {@code null}.</p>
      *
      * @return The code spaces of names and identifiers to show, or {@code null} if no restriction.
      */
@@ -327,18 +328,11 @@ public class ParameterFormat extends Tab
     }
 
     /**
-     * Filters names and identifiers by their code spaces. If the given array is non-null,
then the only names,
+     * Filters names, aliases and identifiers by their code spaces. If the given array is
non-null, then the only names,
      * aliases and identifiers to be formatted are those having a {@link ReferenceIdentifier#getCodeSpace()},
-     *  {@link ScopedName#head()} or {@link GenericName#scope()} value in the given list,
unless no name or alias
+     * {@link ScopedName#head()} or {@link GenericName#scope()} value in the given list,
unless no name or alias
      * matches this criterion.
      *
-     * <p>Additional effects:</p>
-     * <ul>
-     *   <li>With {@link ContentLevel#BRIEF}, the given list determines the preference
order for choosing the name
-     *       or identifier to format.</li>
-     *   <li>With {@link ContentLevel#NAME_SUMMARY}, the given list sets the column
order.</li>
-     * </ul>
-     *
      * @param codespaces The preferred code spaces of names, aliases and identifiers to format,
or {@code null}
      *        for accepting all of them. Some typical values are {@code "EPSG"}, {@code "OGC"}
or {@code "GeoTIFF"}.
      */
@@ -436,9 +430,11 @@ public class ParameterFormat extends Tab
     private void format(final String name, final ParameterDescriptorGroup group,
             final ParameterValueGroup values, final Appendable out) throws IOException
     {
-        final boolean isBrief       = (contentLevel == ContentLevel.BRIEF);
-        final boolean hasColors     = (colors != null);
-        final String  lineSeparator = this.lineSeparator;
+        final boolean    isBrief       = (contentLevel == ContentLevel.BRIEF);
+        final boolean    hasColors     = (colors != null);
+        final String     lineSeparator = this.lineSeparator;
+        final ParameterTableRow header = new ParameterTableRow(group, displayLocale, preferredCodespaces,
isBrief);
+        final String    groupCodespace = header.getCodeSpace();
         /*
          * Prepares the informations to be printed later as table rows. We scan all rows
before to print them
          * in order to compute the width of codespaces. During this process, we split the
objects to be printed
@@ -485,7 +481,7 @@ public class ParameterFormat extends Tab
             }
             ParameterTableRow row = descriptorValues.get(descriptor);
             if (row == null) {
-                row = new ParameterTableRow(descriptor, displayLocale, isBrief);
+                row = new ParameterTableRow(descriptor, displayLocale, preferredCodespaces,
isBrief);
                 descriptorValues.put(descriptor, row);
                 if (row.codespaceWidth > codespaceWidth) {
                     codespaceWidth = row.codespaceWidth;
@@ -501,14 +497,25 @@ public class ParameterFormat extends Tab
          *   - Value domains are formatted.
          *   - Position of the character on which to do the alignment are remembered.
          */
-        int unitWidth = 0;
-        int valueDomainAlignment = 0;
-        final StringBuffer buffer = new StringBuffer();
-        final FieldPosition fp = new FieldPosition(-1);
+        int     unitWidth            = 0;
+        int     valueDomainAlignment = 0;
+        boolean writeCodespaces      = (groupCodespace == null);
+        final   StringBuffer  buffer = new StringBuffer();
+        final   FieldPosition fp     = new FieldPosition(-1);
         for (final Map.Entry<GeneralParameterDescriptor,ParameterTableRow> entry :
descriptorValues.entrySet()) {
             final GeneralParameterDescriptor descriptor = entry.getKey();
             if (descriptor instanceof ParameterDescriptor<?>) {
                 final ParameterTableRow row = entry.getValue();
+                /*
+                 * Verify if all rows use the same codespace than the header, in which case
we can omit
+                 * row codespace formatting.
+                 */
+                if (!writeCodespaces && !groupCodespace.equals(entry.getValue().getCodeSpace()))
{
+                    writeCodespaces = true;
+                }
+                /*
+                 * Format the value domain, so we can compute the character position on which
to perform alignment.
+                 */
                 final Range<?> valueDomain = Parameters.getValueDomain((ParameterDescriptor<?>)
descriptor);
                 if (valueDomain != null) {
                     final int p = row.setValueDomain(valueDomain, getFormat(Range.class),
buffer);
@@ -547,7 +554,7 @@ public class ParameterFormat extends Tab
          * First, formats the table header (i.e. the column names).
          */
         final Vocabulary resources = Vocabulary.getResources(displayLocale);
-        new ParameterTableRow(group, displayLocale, isBrief).writeIdentifiers(out, hasColors,
false, lineSeparator);
+        header.writeIdentifiers(out, true, hasColors, false, lineSeparator);
         out.append(lineSeparator);
         final char horizontalBorder = isBrief ? '─' : '═';
         final TableAppender table = (isBrief || !columnSeparator.equals(SEPARATOR)) ?
@@ -567,6 +574,9 @@ public class ParameterFormat extends Tab
             if (hasColors) table.append(X364.BOLD.sequence());
             table.append(resources.getString(key));
             if (hasColors) table.append(X364.NORMAL.sequence());
+            if (!writeCodespaces && i == 0) {
+                table.append(" (").append(groupCodespace).append(')');
+            }
             if (end) break;
             table.append(beforeFill);
             table.nextColumn(fillCharacter);
@@ -586,7 +596,7 @@ public class ParameterFormat extends Tab
             horizontalLine = isBrief ? 0 : '─';
             final ParameterTableRow row = entry.getValue();
             row.codespaceWidth = codespaceWidth;
-            row.writeIdentifiers(table, false, hasColors, lineSeparator);
+            row.writeIdentifiers(table, writeCodespaces, false, hasColors, lineSeparator);
             table.append(beforeFill);
             table.nextColumn(fillCharacter);
             final GeneralParameterDescriptor generalDescriptor = entry.getKey();
@@ -736,10 +746,7 @@ public class ParameterFormat extends Tab
             if (aliases != null) { // Paranoiac check.
                 for (final GenericName alias : aliases) {
                     if (alias != null) { // Paranoiac check.
-                        String codespace = NameToIdentifier.getCodeSpace(alias);
-                        if (codespace == null) {
-                            codespace = Citations.getIdentifier(NameToIdentifier.getAuthority(alias));
-                        }
+                        final String codespace = NameToIdentifier.getCodespaceOrAuthority(alias,
displayLocale);
                         if (isPreferredCodespace(codespace)) {
                             row = putIfAbsent(resources, row, columnIndices, codespace,
                                     alias.tip().toInternationalString().toString(displayLocale));

Modified: sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/parameter/ParameterTableRow.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/parameter/ParameterTableRow.java?rev=1576686&r1=1576685&r2=1576686&view=diff
==============================================================================
--- sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/parameter/ParameterTableRow.java
[UTF-8] (original)
+++ sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/parameter/ParameterTableRow.java
[UTF-8] Wed Mar 12 10:42:34 2014
@@ -33,7 +33,6 @@ import javax.measure.unit.Unit;
 import org.opengis.util.GenericName;
 import org.opengis.referencing.IdentifiedObject;
 import org.opengis.referencing.ReferenceIdentifier;
-import org.opengis.util.InternationalString;
 import org.opengis.util.NameSpace;
 import org.apache.sis.measure.Range;
 import org.apache.sis.measure.RangeFormat;
@@ -113,36 +112,65 @@ final class ParameterTableRow {
      * @param object The object for which to get the (<var>codespace(s)</var>,
<var>name(s)</var>).
      * @param locale The locale for formatting the names.
      */
-    ParameterTableRow(final IdentifiedObject object, final Locale locale, final boolean brief)
{
+    ParameterTableRow(final IdentifiedObject object, final Locale locale,
+            final Set<String> preferredCodespaces, final boolean isBrief)
+    {
         values = new ArrayList<>(2); // In the vast majority of cases, we will have
only one value.
         units  = new ArrayList<>(2);
+        identifiers = new LinkedHashMap<>();
+        ReferenceIdentifier name = object.getName();
+        if (name != null) { // Paranoiac check.
+            final String codespace = name.getCodeSpace();
+            if (preferredCodespaces == null || preferredCodespaces.contains(codespace)) {
+                addIdentifier(codespace, name.getCode()); // Value needs to be a String here.
+                name = null;
+            }
+        }
         /*
-         * Creates a collection which will contain the identifier and all aliases
-         * found for the given IdentifiedObject. We begin with the primary name.
+         * For detailed content, add aliases.
+         * For brief content, add the first alias if we have not been able to add the name.
          */
-        identifiers = new LinkedHashMap<>();
-        final ReferenceIdentifier identifier = object.getName();
-        addIdentifier(identifier.getCodeSpace(), identifier.getCode()); // Value needs to
be a String here.
-        if (!brief) {
+        if (!isBrief || identifiers.isEmpty()) {
             final Collection<GenericName> aliases = object.getAlias();
             if (aliases != null) { // Paranoiac check.
                 for (GenericName alias : aliases) {
-                    String codespace = NameToIdentifier.getCodeSpace(alias);
+                    String codespace = NameToIdentifier.getCodeSpace(alias, locale);
                     if (codespace != null) {
                         alias = alias.tip();
                     } else {
                         final NameSpace scope = alias.scope();
                         if (scope != null && !scope.isGlobal()) {
-                            codespace = toString(scope.name().tip(), locale);
+                            codespace = NameToIdentifier.toString(scope.name().tip(), locale);
+                        }
+                    }
+                    if (preferredCodespaces == null || preferredCodespaces.contains(codespace))
{
+                        addIdentifier(codespace, NameToIdentifier.toString(alias, locale));
+                        name = null;
+                        if (isBrief) {
+                            break;
                         }
                     }
-                    addIdentifier(codespace, toString(alias, locale));
                 }
             }
+        }
+        /*
+         * If we found no name and no alias in the codespaces requested by the user,
+         * unconditionally add the name regardless its namespace.
+         */
+        if (name != null) {
+            addIdentifier(name.getCodeSpace(), name.getCode()); // Value needs to be a String
here.
+        }
+        /*
+         * Add identifiers (detailed mode only).
+         */
+        if (!isBrief) {
             final Collection<? extends ReferenceIdentifier> ids = object.getIdentifiers();
             if (ids != null) { // Paranoiac check.
                 for (final ReferenceIdentifier id : ids) {
-                    addIdentifier(id.getCodeSpace(), id); // No .getCode() here.
+                    final String codespace = id.getCodeSpace();
+                    if (preferredCodespaces == null || preferredCodespaces.contains(codespace))
{
+                        addIdentifier(codespace, id); // No .getCode() here.
+                    }
                 }
             }
         }
@@ -168,6 +196,21 @@ final class ParameterTableRow {
     }
 
     /**
+     * If this row has exactly one codespace, returns that codespace.
+     * Otherwise returns {@code null}.
+     */
+    final String getCodeSpace() {
+        final Iterator<Map.Entry<String,Set<Object>>> it = identifiers.entrySet().iterator();
+        if (it.hasNext()) {
+            final Map.Entry<String,Set<Object>> entry = it.next();
+            if (!it.hasNext()) {
+                return entry.getKey();
+            }
+        }
+        return null;
+    }
+
+    /**
      * Sets the value domain to the string representation of the given range.
      *
      * @param  range  The range to format.
@@ -239,13 +282,14 @@ final class ParameterTableRow {
      * at its implementation destroys the internal list of identifiers.</p>
      *
      * @param  out             Where to write.
+     * @param  writeCodespaces {@code true} for writing codespaces, or {@code false} for
omitting them.
      * @param  colorsForTitle  {@code true} if syntax coloring should be applied for table
title.
      * @param  colorsForRows   {@code true} if syntax coloring should be applied for table
rows.
      * @param  lineSeparator   The system-dependent line separator.
      * @throws IOException     If an exception occurred while writing.
      */
-    final void writeIdentifiers(final Appendable out, final boolean colorsForTitle,
-            final boolean colorsForRows, final String lineSeparator) throws IOException
+    final void writeIdentifiers(final Appendable out, final boolean writeCodespaces,
+            final boolean colorsForTitle, final boolean colorsForRows, final String lineSeparator)
throws IOException
     {
         boolean isNewLine = false;
         for (final Map.Entry<String,Set<Object>> entry : identifiers.entrySet())
{
@@ -262,18 +306,20 @@ final class ParameterTableRow {
                  * in which case the code space will be repeated on a new line each time.
                  */
                 writeColor(out, FOREGROUND_GREEN, colorsForTitle);
-                int pad = codespaceWidth + 1;
-                if (codespace != null) {
-                    writeColor(out, FAINT, colorsForRows);
-                    out.append(codespace).append(':');
-                    writeColor(out, NORMAL, colorsForRows);
-                    pad -= codespace.length();
+                if (writeCodespaces) {
+                    int pad = codespaceWidth + 1;
+                    if (codespace != null) {
+                        writeColor(out, FAINT, colorsForRows);
+                        out.append(codespace).append(':');
+                        writeColor(out, NORMAL, colorsForRows);
+                        pad -= codespace.length();
+                    }
+                    out.append(spaces(pad));
                 }
                 /*
                  * Write the name or alias after the codespace. We remove what we wrote,
                  * because we may iterate over the 'identifiers' set more than once.
                  */
-                out.append(spaces(pad));
                 writeColor(out, BOLD, colorsForTitle);
                 out.append(toString(it.next()));
                 writeColor(out, RESET, colorsForTitle);
@@ -308,19 +354,6 @@ final class ParameterTableRow {
     }
 
     /**
-     * Returns a string representation of the given name in the given locale, with paranoiac
checks against null value.
-     * Such null values should never happen since the properties used here are mandatory,
but we try to make this class
-     * robust to broken implementations.
-     */
-    private static String toString(final GenericName name, final Locale locale) {
-        if (name != null) {
-            final InternationalString i18n = name.toInternationalString();
-            return (i18n != null) ? i18n.toString(locale) : name.toString();
-        }
-        return null;
-    }
-
-    /**
      * Returns the string representation of the given parameter name.
      */
     private static String toString(Object parameter) {

Modified: sis/branches/JDK7/core/sis-referencing/src/test/java/org/apache/sis/parameter/ParameterFormatTest.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/core/sis-referencing/src/test/java/org/apache/sis/parameter/ParameterFormatTest.java?rev=1576686&r1=1576685&r2=1576686&view=diff
==============================================================================
--- sis/branches/JDK7/core/sis-referencing/src/test/java/org/apache/sis/parameter/ParameterFormatTest.java
[UTF-8] (original)
+++ sis/branches/JDK7/core/sis-referencing/src/test/java/org/apache/sis/parameter/ParameterFormatTest.java
[UTF-8] Wed Mar 12 10:42:34 2014
@@ -17,6 +17,7 @@
 package org.apache.sis.parameter;
 
 import java.util.List;
+import java.util.Locale;
 import javax.measure.unit.SI;
 import javax.measure.unit.NonSI;
 import javax.measure.unit.Unit;
@@ -24,6 +25,8 @@ import org.opengis.referencing.Identifie
 import org.opengis.parameter.ParameterDescriptor;
 import org.opengis.parameter.ParameterDescriptorGroup;
 import org.opengis.parameter.GeneralParameterDescriptor;
+import org.opengis.parameter.ParameterValueGroup;
+import org.apache.sis.test.DependsOnMethod;
 import org.apache.sis.test.DependsOn;
 import org.apache.sis.test.TestCase;
 import org.junit.BeforeClass;
@@ -36,7 +39,7 @@ import static org.apache.sis.metadata.is
 
 
 /**
- * Tests the {@link ParameterBuilder} class.
+ * Tests the {@link ParameterFormat} class.
  *
  * @author  Martin Desruisseaux (Geomatys)
  * @since   0.4
@@ -56,7 +59,7 @@ public final strictfp class ParameterFor
      * with arbitrary non-zero default values.
      */
     @BeforeClass
-    public static void createParameters() {
+    public static void createParameterDescriptor() {
         ParameterBuilder builder = new ParameterBuilder();
         builder.setCodeSpace(OGP, "EPSG").setRequired(true);
         ParameterDescriptor<?>[] parameters = {
@@ -77,11 +80,23 @@ public final strictfp class ParameterFor
      * Forgets the parameter descriptors after all tests are done.
      */
     @AfterClass
-    public static void clearParameters() {
+    public static void clearParameterDescriptor() {
         descriptor = null;
     }
 
     /**
+     * Creates parameter values with some arbitrary values different than the default values.
+     */
+    private static ParameterValueGroup createParameterValues() {
+        final ParameterValueGroup group = descriptor.createValue();
+        group.parameter("latitude_of_origin").setValue(20);
+        group.parameter("central_meridian").setValue(-70);
+        group.parameter("scale_factor").setValue(0.997);
+        group.parameter("FalseNorthing").setValue(20, SI.KILOMETRE);
+        return group;
+    }
+
+    /**
      * Tests {@link ParameterFormat#format(Object, Appendable)} with {@code ContentLevel.BRIEF}.
      */
     @Test
@@ -91,15 +106,50 @@ public final strictfp class ParameterFor
         String text = format.format(descriptor);
         assertMultilinesEquals(
                 "EPSG: Mercator (variant A)\n" +
-                "┌──────────────────────────────────────┬────────┬───────────────┬───────────────┐\n"
+
-                "│ Name                                 │ Type   │ Value domain  │
Default value │\n" +
-                "├──────────────────────────────────────┼────────┼───────────────┼───────────────┤\n"
+
-                "│ EPSG: Latitude of natural origin     │ Double │  [-80 … 84]°
 │        40.0°  │\n" +
-                "│ EPSG: Longitude of natural origin    │ Double │ [-180 … 180]°
│       -60.0°  │\n" +
-                "│ EPSG: Scale factor at natural origin │ Double │    (0 … ∞) 
  │         1.0   │\n" +
-                "│ EPSG: False easting                  │ Double │   (-∞ … ∞)
m  │      5000.0 m │\n" +
-                "│ EPSG: False northing                 │ Double │   (-∞ … ∞)
m  │     10000.0 m │\n" +
-                "└──────────────────────────────────────┴────────┴───────────────┴───────────────┘\n",
text);
+                "┌────────────────────────────────┬────────┬───────────────┬───────────────┐\n"
+
+                "│ Name (EPSG)                    │ Type   │ Value domain  │ Default
value │\n" +
+                "├────────────────────────────────┼────────┼───────────────┼───────────────┤\n"
+
+                "│ Latitude of natural origin     │ Double │  [-80 … 84]°  │ 
      40.0°  │\n" +
+                "│ Longitude of natural origin    │ Double │ [-180 … 180]° │ 
     -60.0°  │\n" +
+                "│ Scale factor at natural origin │ Double │    (0 … ∞)    │
        1.0   │\n" +
+                "│ False easting                  │ Double │   (-∞ … ∞) m  │
     5000.0 m │\n" +
+                "│ False northing                 │ Double │   (-∞ … ∞) m  │
    10000.0 m │\n" +
+                "└────────────────────────────────┴────────┴───────────────┴───────────────┘\n",
text);
+
+        text = format.format(createParameterValues());
+        assertMultilinesEquals(
+                "EPSG: Mercator (variant A)\n" +
+                "┌────────────────────────────────┬────────┬───────────────┬───────────┐\n"
+
+                "│ Name (EPSG)                    │ Type   │ Value domain  │ Value
    │\n" +
+                "├────────────────────────────────┼────────┼───────────────┼───────────┤\n"
+
+                "│ Latitude of natural origin     │ Double │  [-80 … 84]°  │ 
 20.0°   │\n" +
+                "│ Longitude of natural origin    │ Double │ [-180 … 180]° │ 
-70.0°   │\n" +
+                "│ Scale factor at natural origin │ Double │    (0 … ∞)    │
 0.997    │\n" +
+                "│ False easting                  │ Double │   (-∞ … ∞) m  │
5000.0 m  │\n" +
+                "│ False northing                 │ Double │   (-∞ … ∞) m  │
  20.0 km │\n" +
+                "└────────────────────────────────┴────────┴───────────────┴───────────┘\n",
text);
+    }
+
+    /**
+     * Tests formatting in a non-English locale.
+     */
+    @Test
+    @DependsOnMethod("testFormatBrief")
+    public void testFormatLocalized() {
+        final ParameterFormat format = new ParameterFormat(Locale.FRANCE, null);
+        format.setContentLevel(ParameterFormat.ContentLevel.BRIEF);
+        final String text = format.format(createParameterValues());
+        assertMultilinesEquals(
+                "EPSG: Mercator (variant A)\n" +
+                "┌────────────────────────────────┬────────┬─────────────────────┬──────────┐\n"
+
+                "│ Nom (EPSG)                     │ Type   │ Domaine des valeurs │
Valeur   │\n" +
+                "├────────────────────────────────┼────────┼─────────────────────┼──────────┤\n"
+
+                "│ Latitude of natural origin     │ Double │  [-80 … 84]°      
 │    20°   │\n" +
+                "│ Longitude of natural origin    │ Double │ [-180 … 180]°     
 │   -70°   │\n" +
+                "│ Scale factor at natural origin │ Double │    (0 … ∞)       
  │ 0,997    │\n" +
+                "│ False easting                  │ Double │   (-∞ … ∞) m   
    │ 5 000 m  │\n" +
+                "│ False northing                 │ Double │   (-∞ … ∞) m   
    │    20 km │\n" +
+                "└────────────────────────────────┴────────┴─────────────────────┴──────────┘\n",
text);
     }
 
     /**
@@ -132,6 +182,30 @@ public final strictfp class ParameterFor
                 "║ EPSG: False northing                 │ Double │   (-∞ … ∞)
m  │     10000.0 m ║\n" +
                 "║ OGC:  FalseNorthing                  │        │               │
              ║\n" +
                 "╚══════════════════════════════════════╧════════╧═══════════════╧═══════════════╝\n",
text);
+
+        text = format.format(createParameterValues());
+        assertMultilinesEquals(
+                "EPSG: Mercator (variant A) (9804)\n" +
+                "EPSG: Mercator (1SP)\n" +
+                "OGC:  Mercator_1SP\n" +
+                "╔══════════════════════════════════════╤════════╤═══════════════╤═══════════╗\n"
+
+                "║ Name                                 │ Type   │ Value domain  │
Value     ║\n" +
+                "╟──────────────────────────────────────┼────────┼───────────────┼───────────╢\n"
+
+                "║ EPSG: Latitude of natural origin     │ Double │  [-80 … 84]°
 │   20.0°   ║\n" +
+                "║ OGC:  latitude_of_origin             │        │               │
          ║\n" +
+                "╟──────────────────────────────────────┼────────┼───────────────┼───────────╢\n"
+
+                "║ EPSG: Longitude of natural origin    │ Double │ [-180 … 180]°
│  -70.0°   ║\n" +
+                "║ OGC:  central_meridian               │        │               │
          ║\n" +
+                "╟──────────────────────────────────────┼────────┼───────────────┼───────────╢\n"
+
+                "║ EPSG: Scale factor at natural origin │ Double │    (0 … ∞) 
  │  0.997    ║\n" +
+                "║ OGC:  scale_factor                   │        │               │
          ║\n" +
+                "╟──────────────────────────────────────┼────────┼───────────────┼───────────╢\n"
+
+                "║ EPSG: False easting                  │ Double │   (-∞ … ∞)
m  │ 5000.0 m  ║\n" +
+                "║ OGC:  FalseEasting                   │        │               │
          ║\n" +
+                "╟──────────────────────────────────────┼────────┼───────────────┼───────────╢\n"
+
+                "║ EPSG: False northing                 │ Double │   (-∞ … ∞)
m  │   20.0 km ║\n" +
+                "║ OGC:  FalseNorthing                  │        │               │
          ║\n" +
+                "╚══════════════════════════════════════╧════════╧═══════════════╧═══════════╝\n",
text);
     }
 
     /**
@@ -162,4 +236,48 @@ public final strictfp class ParameterFor
                 "│ False northing                 │ FalseNorthing      │\n" +
                 "└────────────────────────────────┴────────────────────┘\n",
text);
     }
+
+    /**
+     * Tests the effect of {@link ParameterFormat#setPreferredCodespaces(String[])}.
+     */
+    @Test
+    @DependsOnMethod({"testFormatNameSummary", "testFormatBrief"})
+    public void testPreferredCodespaces() {
+        final ParameterFormat format = new ParameterFormat(null, null);
+        format.setContentLevel(ParameterFormat.ContentLevel.NAME_SUMMARY);
+        assertNull(format.getPreferredCodespaces());
+        format.setPreferredCodespaces("OGC", "EPSG");
+        assertArrayEquals(new String[] {"OGC", "EPSG"}, format.getPreferredCodespaces());
+        String text = format.format(new IdentifiedObject[] {descriptor});
+        assertMultilinesEquals(
+                "┌────────────┬──────────────┬──────────────────────┐\n"
+
+                "│ Identifier │ OGC          │ EPSG                 │\n" +
+                "├────────────┼──────────────┼──────────────────────┤\n"
+
+                "│ EPSG:9804  │ Mercator_1SP │ Mercator (variant A) │\n" +
+                "└────────────┴──────────────┴──────────────────────┘\n",
text);
+
+        format.setPreferredCodespaces("OGC");
+        assertArrayEquals(new String[] {"OGC"}, format.getPreferredCodespaces());
+        text = format.format(new IdentifiedObject[] {descriptor});
+        assertMultilinesEquals(
+                "┌────────────┬──────────────┐\n"
+
+                "│ Identifier │ OGC          │\n" +
+                "├────────────┼──────────────┤\n"
+
+                "│ EPSG:9804  │ Mercator_1SP │\n" +
+                "└────────────┴──────────────┘\n",
text);
+
+        format.setContentLevel(ParameterFormat.ContentLevel.BRIEF);
+        text = format.format(createParameterValues());
+        assertMultilinesEquals(
+                "OGC: Mercator_1SP\n" +
+                "┌────────────────────┬────────┬───────────────┬───────────┐\n"
+
+                "│ Name (OGC)         │ Type   │ Value domain  │ Value     │\n"
+
+                "├────────────────────┼────────┼───────────────┼───────────┤\n"
+
+                "│ latitude_of_origin │ Double │  [-80 … 84]°  │   20.0°   │\n"
+
+                "│ central_meridian   │ Double │ [-180 … 180]° │  -70.0°   │\n"
+
+                "│ scale_factor       │ Double │    (0 … ∞)    │  0.997    │\n"
+
+                "│ FalseEasting       │ Double │   (-∞ … ∞) m  │ 5000.0 m 
│\n" +
+                "│ FalseNorthing      │ Double │   (-∞ … ∞) m  │   20.0 km
│\n" +
+                "└────────────────────┴────────┴───────────────┴───────────┘\n",
text);
+    }
 }



Mime
View raw message