/* * Geotoolkit - An Open Source Java GIS Toolkit * http://www.geotoolkit.org * * (C) 2011, Geomatys * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; * version 2.1 of the License. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. */ package org.geotoolkit.data.csv; import org.geotoolkit.data.FileFeatureStoreFactory; import org.geotoolkit.data.AbstractFolderFeatureStoreFactory; import org.apache.sis.metadata.iso.identification.DefaultServiceIdentification; import org.geotoolkit.nio.IOUtilities; import org.geotoolkit.nio.PosixDirectoryFilter; import org.opengis.metadata.identification.Identification; import org.opengis.parameter.ParameterDescriptor; import org.opengis.parameter.ParameterDescriptorGroup; import org.opengis.parameter.ParameterValueGroup; import java.io.IOException; import java.net.URI; import java.nio.file.DirectoryStream; import java.nio.file.Files; import java.nio.file.Path; import java.util.logging.Level; import org.geotoolkit.storage.DataStores; import org.geotoolkit.storage.DataType; import org.geotoolkit.storage.DefaultFactoryMetadata; import org.geotoolkit.storage.FactoryMetadata; /** * FeatureStore for a folder of CSV files. * * @author Johann Sorel (Geomatys) * @module */ public class CSVFolderFeatureStoreFactory extends AbstractFolderFeatureStoreFactory{ /** factory identification **/ public static final DefaultServiceIdentification IDENTIFICATION = derivateIdentification(CSVFeatureStoreFactory.IDENTIFICATION); public static final String NAME = IDENTIFICATION.getCitation().getTitle().toString(); public static final ParameterDescriptor<String> IDENTIFIER = createFixedIdentifier(NAME); public static final ParameterDescriptorGroup PARAMETERS_DESCRIPTOR = derivateDescriptor(IDENTIFIER,CSVFeatureStoreFactory.PARAMETERS_DESCRIPTOR); public CSVFolderFeatureStoreFactory(){ super(PARAMETERS_DESCRIPTOR); } /** * {@inheritDoc} */ @Override public Identification getIdentification() { return IDENTIFICATION; } /** * {@inheritDoc} */ @Override public FileFeatureStoreFactory getSingleFileFactory() { return DataStores.getAllFactories(CSVFeatureStoreFactory.class).iterator().next(); } /** * {@inheritDoc} */ @Override public CharSequence getDescription() { return Bundle.formatInternational(Bundle.Keys.datastoreFolderDescription); } /** * {@inheritDoc} */ @Override public CharSequence getDisplayName() { return Bundle.formatInternational(Bundle.Keys.datastoreFolderTitle); } /** * {@inheritDoc} */ @Override public boolean canProcess(final ParameterValueGroup params) { final boolean valid = super.canProcess(params); if (!valid) { return false; } final Object obj = params.parameter(FOLDER_PATH.getName().toString()).getValue(); if(!(obj instanceof URI)){ return false; } final URI path = (URI)obj; try { Path pathFile = IOUtilities.toPath(path); if (Files.isDirectory(pathFile)) { try (DirectoryStream<Path> stream = Files.newDirectoryStream(pathFile, new PosixDirectoryFilter("*.csv", true))) { //at least one return stream.iterator().hasNext(); } } } catch (IOException e) { LOGGER.log(Level.FINE, e.getLocalizedMessage()); } return false; } @Override public FactoryMetadata getMetadata() { return new DefaultFactoryMetadata(DataType.VECTOR, true, true, true, false, GEOMS_ALL); } }