sis-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From desruisse...@apache.org
Subject svn commit: r1797185 - in /sis/branches/JDK8: core/sis-utility/src/main/java/org/apache/sis/util/ storage/sis-netcdf/src/main/java/org/apache/sis/storage/netcdf/ storage/sis-netcdf/src/test/java/org/apache/sis/storage/netcdf/ storage/sis-storage/src/ma...
Date Thu, 01 Jun 2017 10:52:58 GMT
Author: desruisseaux
Date: Thu Jun  1 10:52:57 2017
New Revision: 1797185

URL: http://svn.apache.org/viewvc?rev=1797185&view=rev
Log:
Comma-separated list in NetCDF attribute needs to take in account quoted text (for escaping
comma).

Modified:
    sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/util/CharSequences.java
    sis/branches/JDK8/storage/sis-netcdf/src/main/java/org/apache/sis/storage/netcdf/MetadataReader.java
    sis/branches/JDK8/storage/sis-netcdf/src/test/java/org/apache/sis/storage/netcdf/MetadataReaderTest.java
    sis/branches/JDK8/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/csv/Store.java

Modified: sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/util/CharSequences.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/util/CharSequences.java?rev=1797185&r1=1797184&r2=1797185&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/util/CharSequences.java
[UTF-8] (original)
+++ sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/util/CharSequences.java
[UTF-8] Thu Jun  1 10:52:57 2017
@@ -651,7 +651,7 @@ search:     for (; fromIndex <= toIndex;
         }
         if (separator == '\n' || separator == '\r') {
             final CharSequence[] splitted = splitOnEOL(text);
-            for (int i=0; i<splitted.length; i++) {
+            for (int i=0; i < splitted.length; i++) {
                 // For consistency with the rest of this method.
                 splitted[i] = trimWhitespaces(splitted[i]);
             }

Modified: sis/branches/JDK8/storage/sis-netcdf/src/main/java/org/apache/sis/storage/netcdf/MetadataReader.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/storage/sis-netcdf/src/main/java/org/apache/sis/storage/netcdf/MetadataReader.java?rev=1797185&r1=1797184&r2=1797185&view=diff
==============================================================================
--- sis/branches/JDK8/storage/sis-netcdf/src/main/java/org/apache/sis/storage/netcdf/MetadataReader.java
[UTF-8] (original)
+++ sis/branches/JDK8/storage/sis-netcdf/src/main/java/org/apache/sis/storage/netcdf/MetadataReader.java
[UTF-8] Thu Jun  1 10:52:57 2017
@@ -25,6 +25,7 @@ import java.util.LinkedHashSet;
 import java.util.Map;
 import java.util.HashMap;
 import java.util.Arrays;
+import java.util.ArrayList;
 import java.util.Collection;
 import java.io.IOException;
 import javax.measure.Unit;
@@ -124,11 +125,19 @@ final class MetadataReader {
     private static final String[] SERVICES = {"wms_service", "wcs_service"};
 
     /**
-     * The string to use as a keyword separator. This separator is used for parsing the
-     * {@value org.apache.sis.storage.netcdf.AttributeNames#KEYWORDS} attribute value.
-     * This is a regular expression.
+     * The character to use as a separator in comma-separated list. This separator is used
for parsing the
+     * {@value org.apache.sis.storage.netcdf.AttributeNames#KEYWORDS} attribute value for
instance.
      */
-    private static final String KEYWORD_SEPARATOR = ",";
+    private static final char SEPARATOR = ',';
+
+    /**
+     * The character to use for quoting strings in a comma-separated list. Quoted strings
may contain comma.
+     *
+     * <div class="note"><b>Example:</b>
+     * John Doe, Jane Lee, "L J Smith, Jr."
+     * </div>
+     */
+    private static final char QUOTE = '"';
 
     /**
      * The vertical coordinate reference system to be given to the object created by {@link
#createExtent()}.
@@ -206,10 +215,39 @@ final class MetadataReader {
     }
 
     /**
-     * Splits comma-separated values. Leading and trailing spaces are removed.
-     */
-    private static String[] split(final String value) {
-        return (String[]) CharSequences.split(value, ',');
+     * Splits comma-separated values. Leading and trailing spaces are removed for each item
+     * unless the item is between double quotes. Empty strings are ignored unless between
double quotes.
+     * If a value begin with double quotes, all content will be copied verbatim until the
closing double quote.
+     * A double quote is considered as a closing double quote if just before a comma separator
(ignoring spaces).
+     */
+    static List<String> split(final String value) {
+        final List<String> items = new ArrayList<>();
+        int start = 0;      // Index of the first character of the next item to add in the
list.
+        int end;            // Index after the last character of the next item to add in
the list.
+        int next;           // Index of the next separator (comma) after 'end'.
+        final int length = CharSequences.skipTrailingWhitespaces(value, 0, value.length());
+split:  while ((start = CharSequences.skipLeadingWhitespaces(value, start, length)) <
length) {
+            if (value.charAt(start) == QUOTE) {
+                next = ++start;                                 // Skip the quote character.
+                do {
+                    end = value.indexOf(QUOTE, next);           // End of quoted text, may
have comma separator before.
+                    if (end < 0) break split;
+                    next = CharSequences.skipLeadingWhitespaces(value, end+1, length);
+                } while (next < length && value.charAt(next) != SEPARATOR);
+            } else {
+                next = value.indexOf(SEPARATOR, start);         // Unquoted text - comma
is the item separator.
+                if (next < 0) break;
+                end = CharSequences.skipTrailingWhitespaces(value, start, next);
+            }
+            if (start != end) {
+                items.add(value.substring(start, end));
+            }
+            start = next+1;
+        }
+        if (start < length) {
+            items.add(value.substring(start, length));
+        }
+        return items;
     }
 
     /**

Modified: sis/branches/JDK8/storage/sis-netcdf/src/test/java/org/apache/sis/storage/netcdf/MetadataReaderTest.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/storage/sis-netcdf/src/test/java/org/apache/sis/storage/netcdf/MetadataReaderTest.java?rev=1797185&r1=1797184&r2=1797185&view=diff
==============================================================================
--- sis/branches/JDK8/storage/sis-netcdf/src/test/java/org/apache/sis/storage/netcdf/MetadataReaderTest.java
[UTF-8] (original)
+++ sis/branches/JDK8/storage/sis-netcdf/src/test/java/org/apache/sis/storage/netcdf/MetadataReaderTest.java
[UTF-8] Thu Jun  1 10:52:57 2017
@@ -38,7 +38,7 @@ import static org.apache.sis.test.TestUt
  * for reading NetCDF attributes.
  *
  * @author  Martin Desruisseaux (Geomatys)
- * @version 0.7
+ * @version 0.8
  * @since   0.3
  * @module
  */
@@ -48,6 +48,15 @@ import static org.apache.sis.test.TestUt
 })
 public final strictfp class MetadataReaderTest extends IOTestCase {
     /**
+     * Tests {@link MetadataReader#split(String)}.
+     */
+    @Test
+    public void testSplit() {
+        assertArrayEquals(new String[] {"John Doe", "Foo \" Bar", "Jane Lee", "L J Smith,
Jr."},
+                MetadataReader.split("John Doe, \"Foo \" Bar\" ,Jane Lee,\"L J Smith, Jr.\"").toArray());
+    }
+
+    /**
      * Reads the metadata using the NetCDF decoder embedded with SIS,
      * and compares its string representation with the expected one.
      *

Modified: sis/branches/JDK8/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/csv/Store.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/csv/Store.java?rev=1797185&r1=1797184&r2=1797185&view=diff
==============================================================================
--- sis/branches/JDK8/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/csv/Store.java
[UTF-8] (original)
+++ sis/branches/JDK8/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/csv/Store.java
[UTF-8] Thu Jun  1 10:52:57 2017
@@ -725,7 +725,7 @@ public final class Store extends Feature
      * @param line      the line to parse.
      * @param elements  an initially empty list where to add elements.
      */
-    static void split(final String line, final List<? super String> elements) {
+    private static void split(final String line, final List<? super String> elements)
{
         int startAt = 0;
         boolean isQuoting = false;        // If a quote has been opened and not yet closed.
         boolean hasQuotes = false;        // If the value contains at least one quote (not
used for quoting the value).



Mime
View raw message