sis-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From desruisse...@apache.org
Subject svn commit: r1732855 - in /sis/branches/JDK8: application/sis-console/src/main/java/org/apache/sis/console/ application/sis-console/src/test/java/org/apache/sis/console/ core/sis-metadata/src/main/java/org/apache/sis/io/wkt/
Date Mon, 29 Feb 2016 11:15:01 GMT
Author: desruisseaux
Date: Mon Feb 29 11:15:01 2016
New Revision: 1732855

URL: http://svn.apache.org/viewvc?rev=1732855&view=rev
Log:
Support CRS authority codes from the command-line (e.g. "sis crs epsg:4326").

Modified:
    sis/branches/JDK8/application/sis-console/src/main/java/org/apache/sis/console/MetadataCommand.java
    sis/branches/JDK8/application/sis-console/src/test/java/org/apache/sis/console/MetadataCommandTest.java
    sis/branches/JDK8/core/sis-metadata/src/main/java/org/apache/sis/io/wkt/Colors.java
    sis/branches/JDK8/core/sis-metadata/src/main/java/org/apache/sis/io/wkt/Formatter.java

Modified: sis/branches/JDK8/application/sis-console/src/main/java/org/apache/sis/console/MetadataCommand.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/application/sis-console/src/main/java/org/apache/sis/console/MetadataCommand.java?rev=1732855&r1=1732854&r2=1732855&view=diff
==============================================================================
--- sis/branches/JDK8/application/sis-console/src/main/java/org/apache/sis/console/MetadataCommand.java
[UTF-8] (original)
+++ sis/branches/JDK8/application/sis-console/src/main/java/org/apache/sis/console/MetadataCommand.java
[UTF-8] Mon Feb 29 11:15:01 2016
@@ -18,7 +18,11 @@ package org.apache.sis.console;
 
 import java.util.List;
 import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
 import java.util.EnumSet;
+import java.util.HashSet;
+import java.util.Set;
 import java.io.Console;
 import java.io.IOException;
 import javax.xml.bind.Marshaller;
@@ -33,7 +37,9 @@ import org.apache.sis.io.wkt.Convention;
 import org.apache.sis.io.wkt.Colors;
 import org.apache.sis.metadata.MetadataStandard;
 import org.apache.sis.metadata.ValueExistencePolicy;
+import org.apache.sis.metadata.iso.DefaultMetadata;
 import org.apache.sis.referencing.IdentifiedObjects;
+import org.apache.sis.referencing.CRS;
 import org.apache.sis.storage.DataStore;
 import org.apache.sis.storage.DataStores;
 import org.apache.sis.storage.DataStoreException;
@@ -56,6 +62,18 @@ import org.apache.sis.xml.XML;
  */
 final class MetadataCommand extends CommandRunner {
     /**
+     * The protocol part of the filename to be recognized as a CRS authority.
+     * In such case, this class will delegate to {@link CRS#forCode(String)}
+     * instead of opening the file.
+     */
+    static final Set<String> AUTHORITIES = new HashSet<>(Arrays.asList("URN",
"EPSG", "CRS", "AUTO", "AUTO2"));
+
+    /**
+     * Length of the longest authority name declared in {@link #AUTHORITIES}.
+     */
+    static final int MAX_AUTHORITY_LENGTH = 5;
+
+    /**
      * The desired information.
      */
     static enum Info {
@@ -144,6 +162,40 @@ final class MetadataCommand extends Comm
     }
 
     /**
+     * If the given argument begins with one of the known authorities ("URN", "EPSG", "CRS",
"AUTO", <i>etc.</i>),
+     * delegates to {@link CRS#forCode(String)} and wraps in a metadata object. Otherwise
returns {@code null}.
+     */
+    private static Metadata fromDatabase(final String code) throws FactoryException {
+        final char[] authority = new char[MAX_AUTHORITY_LENGTH];
+        final int length = code.length();
+        int p = 0, i = 0;
+        while (i < length) {
+            final int c = code.codePointAt(i);
+            if (c == ':') {
+                if (!AUTHORITIES.contains(new String(authority, 0, p))) {
+                    break;
+                }
+                final DefaultMetadata metadata = new DefaultMetadata();
+                metadata.setReferenceSystemInfo(Collections.singleton(CRS.forCode(code)));
+                return metadata;
+            }
+            if (!Character.isWhitespace(c)) {
+                if (p >= MAX_AUTHORITY_LENGTH || !Character.isLetterOrDigit(c)) {
+                    break;
+                }
+                /*
+                 * Casting to char is okay because AUTHORITIES contains only ASCII values.
+                 * If 'c' was a supplemental Unicode value, then the result of the cast
+                 * will not match any AUTHORITIES value anyway.
+                 */
+                authority[p++] = (char) Character.toUpperCase(c);
+            }
+            i += Character.charCount(c);
+        }
+        return null;
+    }
+
+    /**
      * Prints metadata or CRS information.
      *
      * @throws DataStoreException if an error occurred while reading the file.
@@ -156,10 +208,10 @@ final class MetadataCommand extends Comm
     public int run() throws InvalidOptionException, DataStoreException, JAXBException, FactoryException,
IOException {
         parseArguments();
         /*
-         * Read metadata from the data storage only aftr we verified that the arguments are
valid.
+         * Read metadata from the data storage only after we verified that the arguments
are valid.
          * The input can be a file given on the command line, or the standard input stream.
          */
-        final Metadata metadata;
+        Metadata metadata;
         if (useStandardInput()) {
             try (DataStore store = DataStores.open(System.in)) {
                 metadata = store.getMetadata();
@@ -168,8 +220,12 @@ final class MetadataCommand extends Comm
             if (hasUnexpectedFileCount(1, 1)) {
                 return Command.INVALID_ARGUMENT_EXIT_CODE;
             }
-            try (DataStore store = DataStores.open(files.get(0))) {
-                metadata = store.getMetadata();
+            final String file = files.get(0);
+            metadata = fromDatabase(file);
+            if (metadata == null) {
+                try (DataStore store = DataStores.open(file)) {
+                    metadata = store.getMetadata();
+                }
             }
         }
         if (metadata == null) {

Modified: sis/branches/JDK8/application/sis-console/src/test/java/org/apache/sis/console/MetadataCommandTest.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/application/sis-console/src/test/java/org/apache/sis/console/MetadataCommandTest.java?rev=1732855&r1=1732854&r2=1732855&view=diff
==============================================================================
--- sis/branches/JDK8/application/sis-console/src/test/java/org/apache/sis/console/MetadataCommandTest.java
[UTF-8] (original)
+++ sis/branches/JDK8/application/sis-console/src/test/java/org/apache/sis/console/MetadataCommandTest.java
[UTF-8] Mon Feb 29 11:15:01 2016
@@ -31,12 +31,25 @@ import static org.junit.Assert.*;
  *
  * @author  Martin Desruisseaux (Geomatys)
  * @since   0.3
- * @version 0.6
+ * @version 0.7
  * @module
  */
 @DependsOn(CommandRunnerTest.class)
 public final strictfp class MetadataCommandTest extends TestCase {
     /**
+     * Verifies the {@link MetadataCommand#MAX_AUTHORITY_LENGTH} value.
+     */
+    @Test
+    public void verifyMaxAuthorityLength() {
+        int length = 0;
+        for (final String authority : MetadataCommand.AUTHORITIES) {
+            final int c = authority.length();
+            if (c > length) length = c;
+        }
+        assertEquals("MAX_AUTHORITY_LENGTH", length, MetadataCommand.MAX_AUTHORITY_LENGTH);
+    }
+
+    /**
      * Tests the sub-command on a NetCDF file.
      *
      * @throws Exception Should never happen.

Modified: sis/branches/JDK8/core/sis-metadata/src/main/java/org/apache/sis/io/wkt/Colors.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-metadata/src/main/java/org/apache/sis/io/wkt/Colors.java?rev=1732855&r1=1732854&r2=1732855&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-metadata/src/main/java/org/apache/sis/io/wkt/Colors.java [UTF-8]
(original)
+++ sis/branches/JDK8/core/sis-metadata/src/main/java/org/apache/sis/io/wkt/Colors.java [UTF-8]
Mon Feb 29 11:15:01 2016
@@ -65,8 +65,8 @@ public class Colors implements Cloneable
         map.put(ElementKind.CODE_LIST,  X364.FOREGROUND_CYAN);
         map.put(ElementKind.PARAMETER,  X364.FOREGROUND_GREEN);
         map.put(ElementKind.METHOD,     X364.FOREGROUND_GREEN);
-        map.put(ElementKind.DATUM,      X364.FOREGROUND_BLUE);  // Because datum names in
SIS are like identifiers.
-        map.put(ElementKind.IDENTIFIER, X364.FOREGROUND_BLUE);
+        map.put(ElementKind.DATUM,      X364.FOREGROUND_GREEN);     // Note: datum names
in SIS are like identifiers.
+        map.put(ElementKind.IDENTIFIER, X364.FOREGROUND_RED);
         map.put(ElementKind.SCOPE,      X364.FOREGROUND_GRAY);
         map.put(ElementKind.EXTENT,     X364.FOREGROUND_GRAY);
         map.put(ElementKind.CITATION,   X364.FOREGROUND_GRAY);

Modified: sis/branches/JDK8/core/sis-metadata/src/main/java/org/apache/sis/io/wkt/Formatter.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-metadata/src/main/java/org/apache/sis/io/wkt/Formatter.java?rev=1732855&r1=1732854&r2=1732855&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-metadata/src/main/java/org/apache/sis/io/wkt/Formatter.java
[UTF-8] (original)
+++ sis/branches/JDK8/core/sis-metadata/src/main/java/org/apache/sis/io/wkt/Formatter.java
[UTF-8] Mon Feb 29 11:15:01 2016
@@ -479,9 +479,14 @@ public class Formatter implements Locali
             if (colorApplied == 0) {
                 final String color = colors.getAnsiSequence(type);
                 if (color == null) {
-                    return; // Do not increment 'colorApplied' for giving a chance to children
to apply their colors.
+                    // Do not increment 'colorApplied' for giving a chance to children to
apply their colors.
+                    return;
                 }
+                final boolean isStart = (buffer.length() == elementStart);
                 buffer.append(color);
+                if (isStart) {
+                    elementStart = buffer.length();
+                }
             }
             colorApplied++;
         }



Mime
View raw message