Java tutorial
/* * Copyright 2015 Robert von Burg <eitch@eitchnet.ch> * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package ch.eitchnet.csvrestendpoint.components; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.Reader; import java.nio.file.DirectoryStream; import java.nio.file.Files; import java.nio.file.Path; import java.util.ArrayList; import java.util.Collections; import java.util.Iterator; import java.util.List; import org.apache.commons.csv.CSVFormat; import org.apache.commons.csv.CSVParser; import ch.eitchnet.csvrestendpoint.marshaller.CsvDataMarshaller; import li.strolch.agent.api.ComponentContainer; import li.strolch.agent.api.StrolchComponent; import li.strolch.runtime.configuration.RuntimeConfiguration; /** * @author Robert von Burg <eitch@eitchnet.ch> */ public class CsvDataHandler extends StrolchComponent { /** * @param container * @param componentName */ public CsvDataHandler(ComponentContainer container, String componentName) { super(container, componentName); } public List<String> getCsvNames() { File csvDataDir = getCsvDataDir(); if (!csvDataDir.isDirectory()) { logger.error("CSV Data Dir is not a directory at " + csvDataDir.getAbsolutePath()); return Collections.emptyList(); } try (DirectoryStream<Path> directoryStream = Files.newDirectoryStream(csvDataDir.toPath())) { List<String> names = new ArrayList<>(); for (Iterator<Path> iter = directoryStream.iterator(); iter.hasNext();) { Path path = iter.next(); String name = path.toFile().getName(); if (name.endsWith(".csv")) names.add(name.substring(0, name.length() - 4)); } return names; } catch (Exception e) { throw new RuntimeException("Failed to read CSV names due to " + e.getMessage()); } } public <T> T getCsvData(String name, CsvDataMarshaller<T> marshaller) { File csvDataDir = getCsvDataDir(); if (!csvDataDir.isDirectory()) { logger.error("CSV Data Dir is not a directory at " + csvDataDir.getAbsolutePath()); return null; } String csvFileName = name + ".csv"; File csvFile = new File(csvDataDir, csvFileName); if (!csvFile.isFile()) { logger.error("CSV File is not a file at " + csvFile.getAbsolutePath()); return null; } try (Reader reader = new InputStreamReader(new FileInputStream(csvFile), marshaller.getCharset())) { CSVFormat csvFormat = marshaller.getCsvFormat(); CSVParser csvParser = csvFormat.parse(reader); return marshaller.marshall(csvParser); } catch (IOException e) { logger.error(e.getMessage(), e); return null; } } private File getCsvDataDir() { RuntimeConfiguration runtimeConfiguration = getConfiguration().getRuntimeConfiguration(); String context = CsvDataHandler.class.getSimpleName(); return runtimeConfiguration.getDataDir(context, "csv", false); } }