001package jmri.jmrit.symbolicprog; 002 003import java.io.BufferedReader; 004import java.io.File; 005import java.io.FileReader; 006import java.io.IOException; 007 008/** 009 * Import CV values from a generic CSV format CV list file such as those written 010 * by the CsvExportAction class. 011 * 012 * 013 * <hr> 014 * This file is part of JMRI. 015 * <p> 016 * JMRI is free software; you can redistribute it and/or modify it under the 017 * terms of version 2 of the GNU General Public License as published by the Free 018 * Software Foundation. See the "COPYING" file for a copy of this license. 019 * <p> 020 * JMRI is distributed in the hope that it will be useful, but WITHOUT ANY 021 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR 022 * A PARTICULAR PURPOSE. See the GNU General Public License for more details. 023 * 024 * @author Alex Shepherd Copyright (C) 2003 025 * @author Dave Heap Copyright (C) 2014 026 */ 027public class CsvImporter { 028 029 public CsvImporter(File file, CvTableModel cvModel) 030 throws IOException, NumberFormatException { 031 032 try (FileReader fileReader = new FileReader(file); 033 BufferedReader bufferedReader = new BufferedReader(fileReader)) { 034 035 String line; 036 037 while ((line = bufferedReader.readLine()) != null) { 038 String[] lineStrings = line.split(" *, *"); 039 if (lineStrings.length < 2) { 040 bufferedReader.close(); 041 throw new IOException(); 042 } else if (lineStrings[0].contains("CV")) { 043 log.debug("Header OK"); 044 } else { 045 String name = lineStrings[0].trim(); 046 int value = Integer.parseInt(lineStrings[1].trim()); 047 CvValue cvObject = cvModel.allCvMap().get(name); 048 if (cvObject == null) { 049 log.warn("CV {} was in import file, but not defined by the decoder definition", name); 050 cvModel.addCV(name, false, false, false); 051 cvObject = cvModel.allCvMap().get(name); 052 } 053 cvObject.setValue(value); 054 } 055 } 056 } 057 } 058 059 private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(CsvImporter.class); 060}