Author: desruisseaux
Date: Sat Feb 24 13:45:10 2018
New Revision: 1825238

URL: http://svn.apache.org/viewvc?rev=1825238&view=rev
Log:
Replace DataStore.identifierMatches(…) by an improvement of 
Citations.identifierMatches(…).
FolderStore shoud verify if the directory is writable.
Remove unused methods.

Modified:
    
sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/internal/util/Citations.java
    
sis/branches/JDK8/core/sis-utility/src/test/java/org/apache/sis/internal/util/CitationsTest.java
    
sis/branches/JDK8/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/folder/FolderStoreProvider.java
    
sis/branches/JDK8/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/folder/Store.java
    
sis/branches/JDK8/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/folder/WritableStore.java
    
sis/branches/JDK8/storage/sis-storage/src/main/java/org/apache/sis/storage/DataStore.java
    
sis/branches/JDK8/storage/sis-storage/src/main/java/org/apache/sis/storage/DataStores.java
    
sis/branches/JDK8/storage/sis-storage/src/test/java/org/apache/sis/internal/storage/folder/StoreTest.java

Modified: 
sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/internal/util/Citations.java
URL: 
http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/internal/util/Citations.java?rev=1825238&r1=1825237&r2=1825238&view=diff
==============================================================================
--- 
sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/internal/util/Citations.java
 [UTF-8] (original)
+++ 
sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/internal/util/Citations.java
 [UTF-8] Sat Feb 24 13:45:10 2018
@@ -247,25 +247,49 @@ public final class Citations extends Sta
      */
     public static boolean identifierMatches(final Citation citation, final 
Identifier identifier, final CharSequence code) {
         if (citation != null && code != null) {
-            final Iterator<? extends Identifier> identifiers = 
iterator(citation.getIdentifiers());
-            if (identifiers == null) {
+            final Collection<? extends Identifier> citIds = 
citation.getIdentifiers();
+            Iterator<? extends Identifier> it = iterator(citIds);
+            if (it == null) {
                 return titleMatches(citation, code);
             }
-            while (identifiers.hasNext()) {
-                final Identifier id = identifiers.next();
-                if (id != null && equalsFiltered(code, id.getCode())) {
+            while (it.hasNext()) {
+                final Identifier citId = it.next();
+                if (citId != null && equalsFiltered(code, citId.getCode())) {
+                    /*
+                     * Found a possible match. We will take the code space in 
account only if it is defined
+                     * by both identifiers. If a code space is undefined, we 
consider that we have a match.
+                     */
                     if (identifier != null) {
                         final String codeSpace = identifier.getCodeSpace();
                         if (codeSpace != null) {
-                            final String cs = id.getCodeSpace();
-                            if (cs != null) {
-                                return equalsFiltered(codeSpace, cs);
+                            final String cs = citId.getCodeSpace();
+                            if (cs != null && !equalsFiltered(codeSpace, cs)) {
+                                continue;       // Check other identifiers.
                             }
                         }
                     }
                     return true;
                 }
             }
+            /*
+             * Before to give up, maybe the given code argument is actually 
written using a "codeSpace:code" syntax.
+             * Try to parse that syntax only if no Identifier argument were 
specified (otherwise we require the code
+             * and code space to be splitted as defined in the identifier).
+             */
+            if (identifier == null) {
+                int s = 0;
+                final int length = code.length();
+                while ((s = CharSequences.indexOf(code, DEFAULT_SEPARATOR, s, 
length)) >= 0) {
+                    final CharSequence codeSpace = code.subSequence(0, s);
+                    final CharSequence localPart = code.subSequence(++s, 
length);
+                    for (it = citIds.iterator(); it.hasNext();) {
+                        final Identifier id = it.next();
+                        if (equalsFiltered(codeSpace, id.getCodeSpace()) && 
equalsFiltered(localPart, id.getCode())) {
+                            return true;
+                        }
+                    }
+                }
+            }
         }
         return false;
     }

Modified: 
sis/branches/JDK8/core/sis-utility/src/test/java/org/apache/sis/internal/util/CitationsTest.java
URL: 
http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-utility/src/test/java/org/apache/sis/internal/util/CitationsTest.java?rev=1825238&r1=1825237&r2=1825238&view=diff
==============================================================================
--- 
sis/branches/JDK8/core/sis-utility/src/test/java/org/apache/sis/internal/util/CitationsTest.java
 [UTF-8] (original)
+++ 
sis/branches/JDK8/core/sis-utility/src/test/java/org/apache/sis/internal/util/CitationsTest.java
 [UTF-8] Sat Feb 24 13:45:10 2018
@@ -33,7 +33,7 @@ import static org.junit.Assert.*;
  * Tests the internal {@link Citations} class.
  *
  * @author  Martin Desruisseaux (Geomatys)
- * @version 0.7
+ * @version 1.0
  * @since   0.6
  * @module
  */
@@ -112,4 +112,22 @@ public final strictfp class CitationsTes
         assertEquals("OGC:06-042", Citations.getIdentifier(citation, false));
         assertEquals("ISO_19128",  Citations.getIdentifier(citation, true));
     }
+
+    /**
+     * Tests {@link Citations#identifierMatches(Citation, Identifier, 
CharSequence)}.
+     */
+    @Test
+    public void testIdentifierMatches() {
+        final Identifier ogc = identifier("OGC", "06-042");
+        final Identifier iso = identifier("ISO", "19128");
+        final Citation citation = citation("Web Map Server", ogc, iso, 
identifier("Foo", "06-042"));
+        assertTrue ("With full identifier",  
Citations.identifierMatches(citation, ogc, ogc.getCode()));
+        assertTrue ("With full identifier",  
Citations.identifierMatches(citation, iso, iso.getCode()));
+        assertFalse("With wrong code",       
Citations.identifierMatches(citation, identifier("ISO", "19115"), "19115"));
+        assertFalse("With wrong code space", 
Citations.identifierMatches(citation, identifier("Foo", "19128"), "19128"));
+        assertFalse("With wrong code",       
Citations.identifierMatches(citation, null, "Foo"));
+        assertTrue ("Without identifier",    
Citations.identifierMatches(citation, null, "19128"));
+        assertTrue ("With parsing",          
Citations.identifierMatches(citation, null, "ISO:19128"));
+        assertFalse("With wrong code space", 
Citations.identifierMatches(citation, null, "Foo:19128"));
+    }
 }

Modified: 
sis/branches/JDK8/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/folder/FolderStoreProvider.java
URL: 
http://svn.apache.org/viewvc/sis/branches/JDK8/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/folder/FolderStoreProvider.java?rev=1825238&r1=1825237&r2=1825238&view=diff
==============================================================================
--- 
sis/branches/JDK8/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/folder/FolderStoreProvider.java
 [UTF-8] (original)
+++ 
sis/branches/JDK8/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/folder/FolderStoreProvider.java
 [UTF-8] Sat Feb 24 13:45:10 2018
@@ -110,8 +110,6 @@ public final class FolderStoreProvider e
 
     /**
      * The unique instance of this provider.
-     *
-     * @see #open(Path)
      */
     public static final FolderStoreProvider INSTANCE = new 
FolderStoreProvider();
 
@@ -205,7 +203,7 @@ public final class FolderStoreProvider e
             componentProvider = null;
             options.clear();                // Can not write if we don't know 
the components format.
         }
-        Path path = null;
+        final Path path = connector.getStorageAs(Path.class);
         final Store store;
         try {
             /*
@@ -214,23 +212,23 @@ public final class FolderStoreProvider e
              * In the particular case of CREATE_NEW, we unconditionally 
attempt to create the
              * directory in order to rely on the atomic check performed by 
Files.createDirectory(…).
              */
+            boolean isNew = false;
             if (options.contains(StandardOpenOption.CREATE)) {
-                path = connector.getStorageAs(Path.class);
                 if (options.contains(StandardOpenOption.CREATE_NEW) || 
Files.notExists(path)) {
                     Files.createDirectory(path);                        // 
IOException if the directory already exists.
+                    isNew = true;
                 }
             }
-            if (options.contains(StandardOpenOption.WRITE)) {
-                store = new WritableStore(this, connector, componentProvider); 
   // May throw NoSuchFileException.
+            if (isNew || (options.contains(StandardOpenOption.WRITE) && 
Files.isWritable(path))) {
+                store = new WritableStore(this, connector, path, 
componentProvider);  // May throw NoSuchFileException.
             } else {
-                store = new Store(this, connector, componentProvider);         
   // May throw NoSuchFileException.
+                store = new Store(this, connector, path, componentProvider);   
       // May throw NoSuchFileException.
             }
             /*
              * If there is a destructive operation to perform 
(TRUNCATE_EXISTING), do it last only
              * after we have successfully created the data store. The check 
for directory existence
              * is also done after creation to be sure to check the path used 
by the store.
              */
-            path = store.location;
             if (!Files.isDirectory(path)) {
                 throw new 
DataStoreException(Resources.format(Resources.Keys.FileIsNotAResourceDirectory_1,
 path));
             }
@@ -280,15 +278,4 @@ public final class FolderStoreProvider e
         }
         return open(connector, pg.getValue(FORMAT), options);
     }
-
-    /**
-     * Returns a folder data store for the given path.
-     *
-     * @param  path  the directory for which to create a data store.
-     * @return a data store for the given directory.
-     * @throws DataStoreException if an error occurred while creating the data 
store instance.
-     */
-    public static DataStore open(final Path path) throws DataStoreException {
-        return INSTANCE.open(new StorageConnector(path));
-    }
 }

Modified: 
sis/branches/JDK8/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/folder/Store.java
URL: 
http://svn.apache.org/viewvc/sis/branches/JDK8/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/folder/Store.java?rev=1825238&r1=1825237&r2=1825238&view=diff
==============================================================================
--- 
sis/branches/JDK8/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/folder/Store.java
 [UTF-8] (original)
+++ 
sis/branches/JDK8/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/folder/Store.java
 [UTF-8] Sat Feb 24 13:45:10 2018
@@ -134,22 +134,23 @@ class Store extends DataStore implements
      *
      * @param  provider   the factory that created this {@code DataStore} 
instance, or {@code null} if unspecified.
      * @param  connector  information about the storage (URL, stream, 
<i>etc</i>).
+     * @param  path       the value of {@code 
connector.getStorageAs(Path.class)}.
      * @param  format     format to use for reading or writing the directory 
content, or {@code null}.
      * @throws UnsupportedStorageException if the given format name is unknown.
      * @throws DataStoreException if an error occurred while fetching the 
directory {@link Path}.
      * @throws IOException if an error occurred while using the directory 
{@code Path}.
      */
     @SuppressWarnings("ThisEscapedInObjectConstruction")    // Okay because 
'children' does not escape.
-    Store(final DataStoreProvider provider, final StorageConnector connector, 
final DataStoreProvider format)
+    Store(final DataStoreProvider provider, final StorageConnector connector, 
final Path path, final DataStoreProvider format)
             throws DataStoreException, IOException
     {
         super(provider, connector);
-        location = connector.getStorageAs(Path.class);
+        location = path;
         locale   = connector.getOption(OptionKey.LOCALE);
         timezone = connector.getOption(OptionKey.TIMEZONE);
         encoding = connector.getOption(OptionKey.ENCODING);
         children = new ConcurrentHashMap<>();
-        children.put(location.toRealPath(), this);
+        children.put(path.toRealPath(), this);
         componentProvider = format;
     }
 

Modified: 
sis/branches/JDK8/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/folder/WritableStore.java
URL: 
http://svn.apache.org/viewvc/sis/branches/JDK8/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/folder/WritableStore.java?rev=1825238&r1=1825237&r2=1825238&view=diff
==============================================================================
--- 
sis/branches/JDK8/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/folder/WritableStore.java
 [UTF-8] (original)
+++ 
sis/branches/JDK8/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/folder/WritableStore.java
 [UTF-8] Sat Feb 24 13:45:10 2018
@@ -65,10 +65,10 @@ final class WritableStore extends Store
      * Contrarily to the {@link Store} parent class, the {@code format} is 
mandatory for writable stores.
      * This is not verified by this constructor; it should be verified by 
{@link FolderStoreProvider} instead.
      */
-    WritableStore(DataStoreProvider provider, StorageConnector connector, 
DataStoreProvider format)
+    WritableStore(DataStoreProvider provider, StorageConnector connector, Path 
path, DataStoreProvider format)
             throws DataStoreException, IOException
     {
-        super(provider, connector, format);
+        super(provider, connector, path, format);
     }
 
     /**

Modified: 
sis/branches/JDK8/storage/sis-storage/src/main/java/org/apache/sis/storage/DataStore.java
URL: 
http://svn.apache.org/viewvc/sis/branches/JDK8/storage/sis-storage/src/main/java/org/apache/sis/storage/DataStore.java?rev=1825238&r1=1825237&r2=1825238&view=diff
==============================================================================
--- 
sis/branches/JDK8/storage/sis-storage/src/main/java/org/apache/sis/storage/DataStore.java
 [UTF-8] (original)
+++ 
sis/branches/JDK8/storage/sis-storage/src/main/java/org/apache/sis/storage/DataStore.java
 [UTF-8] Sat Feb 24 13:45:10 2018
@@ -16,14 +16,11 @@
  */
 package org.apache.sis.storage;
 
-import java.util.Collection;
 import java.util.Locale;
 import java.util.Map;
 import java.util.IdentityHashMap;
 import java.util.NoSuchElementException;
 import org.opengis.metadata.Metadata;
-import org.opengis.metadata.Identifier;
-import org.opengis.metadata.citation.Citation;
 import org.opengis.metadata.identification.Identification;
 import org.opengis.parameter.ParameterValueGroup;
 import org.apache.sis.util.Localized;
@@ -31,7 +28,6 @@ import org.apache.sis.util.ArgumentCheck
 import org.apache.sis.util.logging.WarningListener;
 import org.apache.sis.util.logging.WarningListeners;
 import org.apache.sis.internal.storage.Resources;
-import org.apache.sis.internal.metadata.NameToIdentifier;
 import org.apache.sis.internal.util.Citations;
 
 
@@ -312,7 +308,7 @@ public abstract class DataStore implemen
             if (metadata != null) {
                 for (final Identification identification : 
metadata.getIdentificationInfo()) {
                     if (identification != null) {                              
                     // Paranoiac check.
-                        if (identifierMatches(identification.getCitation(), 
identifier)) {
+                        if 
(Citations.identifierMatches(identification.getCitation(), null, identifier)) {
                             return candidate;
                         }
                     }
@@ -338,26 +334,6 @@ public abstract class DataStore implemen
     }
 
     /**
-     * Search a citation title and identifiers for a possible matches against
-     * an identifier.
-     *
-     * @param citation   identification citation to compare.
-     * @param identifier identifier of the resource to compare.
-     * @return true if identifiers matches
-     */
-    private static boolean identifierMatches(final Citation citation, final 
String identifier) {
-        if (citation != null && identifier != null) {
-            final Collection<? extends Identifier> identifiers = 
citation.getIdentifiers();
-            if (identifiers == null || identifiers.isEmpty()) {
-                return Citations.titleMatches(citation, identifier);
-            } else {
-                return 
NameToIdentifier.isHeuristicMatchForIdentifier(identifiers, identifier);
-            }
-        }
-        return false;
-    }
-
-    /**
      * Adds a listener to be notified when a warning occurred while reading 
from or writing to the storage.
      * When a warning occurs, there is a choice:
      *

Modified: 
sis/branches/JDK8/storage/sis-storage/src/main/java/org/apache/sis/storage/DataStores.java
URL: 
http://svn.apache.org/viewvc/sis/branches/JDK8/storage/sis-storage/src/main/java/org/apache/sis/storage/DataStores.java?rev=1825238&r1=1825237&r2=1825238&view=diff
==============================================================================
--- 
sis/branches/JDK8/storage/sis-storage/src/main/java/org/apache/sis/storage/DataStores.java
 [UTF-8] (original)
+++ 
sis/branches/JDK8/storage/sis-storage/src/main/java/org/apache/sis/storage/DataStores.java
 [UTF-8] Sat Feb 24 13:45:10 2018
@@ -16,9 +16,7 @@
  */
 package org.apache.sis.storage;
 
-import java.util.ArrayList;
 import java.util.Collection;
-import java.util.List;
 import org.apache.sis.util.Static;
 import org.apache.sis.internal.system.Modules;
 import org.apache.sis.internal.system.SystemListener;
@@ -94,45 +92,6 @@ public final class DataStores extends St
     }
 
     /**
-     * Returns the list of data store providers capable of opening the given 
input.
-     * Any provider causing an exception while probing the input is considered
-     * as unsupported.
-     *
-     * @param input data input, usually a StorageConnector, Path or URI to 
test.
-     * @return descriptions of compatible data stores.
-     *
-     * @since 1.0
-     *
-     * @deprecated this method force initialization and probing of all data 
stores, while maybe only
-     *             the first instance is needed. We could lazily probe the 
content during the first
-     *             iteration, but this would require an {@link AutoCloseable} 
iterator for closing
-     *             the {@link StorageConnector}. We could return a {@link 
java.util.stream.Stream}
-     *             for resolving that issue, but that would be at odd with the 
{@link #providers()} API.
-     */
-    @Deprecated
-    public static Collection<DataStoreProvider> providers(final Object input) {
-        final StorageConnector connector = (input instanceof StorageConnector) 
?
-                (StorageConnector) input : new StorageConnector(input);
-
-        final List<DataStoreProvider> providers = new ArrayList<>();
-        for (DataStoreProvider provider : registry().providers()) {
-            try {
-                final ProbeResult result = provider.probeContent(connector);
-                if (result.isSupported()) {
-                    providers.add(provider);
-                }
-            } catch (DataStoreException ex) {
-                // could be caused for multiple reasons, we assume it does not
-                // support the given input.
-                // TODO: should we? DataStoreException in probeContent are 
usually caused by IOException,
-                // in which state the stream is probably in an invalid state 
and likely to cause failure
-                // in next providers too.
-            }
-        }
-        return providers;
-    }
-
-    /**
      * Returns the MIME type of the storage file format, or {@code null} if 
unknown or not applicable.
      *
      * @param  storage  the input/output object as a URL, file, image input 
stream, <i>etc.</i>.

Modified: 
sis/branches/JDK8/storage/sis-storage/src/test/java/org/apache/sis/internal/storage/folder/StoreTest.java
URL: 
http://svn.apache.org/viewvc/sis/branches/JDK8/storage/sis-storage/src/test/java/org/apache/sis/internal/storage/folder/StoreTest.java?rev=1825238&r1=1825237&r2=1825238&view=diff
==============================================================================
--- 
sis/branches/JDK8/storage/sis-storage/src/test/java/org/apache/sis/internal/storage/folder/StoreTest.java
 [UTF-8] (original)
+++ 
sis/branches/JDK8/storage/sis-storage/src/test/java/org/apache/sis/internal/storage/folder/StoreTest.java
 [UTF-8] Sat Feb 24 13:45:10 2018
@@ -70,7 +70,8 @@ public final strictfp class StoreTest ex
     @Test
     public void testComponents() throws URISyntaxException, 
DataStoreException, IOException {
         final Set<String> identifiers = new 
HashSet<>(Arrays.asList("EPSG:4326", "Sample 1", "Sample 2", "Sample 3", 
"data4"));
-        try (Store store = new Store(null, new 
StorageConnector(testDirectory()), null)) {
+        final Path path = testDirectory();
+        try (Store store = new Store(null, new StorageConnector(path), path, 
null)) {
             assertEquals("Wrong number of data stores.", 4, 
store.components().size());
             verifyContent(store, identifiers);
         }


Reply via email to