001package jmri.jmrit.symbolicprog;
002
003import java.io.BufferedReader;
004import java.io.File;
005import java.io.FileReader;
006import java.io.IOException;
007import java.util.regex.Matcher;
008import java.util.regex.Pattern;
009import org.slf4j.Logger;
010import org.slf4j.LoggerFactory;
011
012/**
013 * Import CV values from a .qcv file written by the QSI
014 * Quantum CV Manager software.
015 *
016 *
017 * <hr>
018 * This file is part of JMRI.
019 * <p>
020 * JMRI is free software; you can redistribute it and/or modify it under the
021 * terms of version 2 of the GNU General Public License as published by the Free
022 * Software Foundation. See the "COPYING" file for a copy of this license.
023 * <p>
024 * JMRI is distributed in the hope that it will be useful, but WITHOUT ANY
025 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
026 * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
027 *
028 * @author Alex Shepherd Copyright (C) 2003
029 * @author Dave Heap Copyright (C) 2015
030 */
031public class QuantumCvMgrImporter {
032
033    private final static Logger log = LoggerFactory.getLogger(QuantumCvMgrImporter.class);
034    private static final String SEARCH_STRING = "^CV([0-9.]+)=([0-9.]+)\\s*(//)?\\s*(.*)$";
035
036    public QuantumCvMgrImporter(File file, CvTableModel cvModel) throws IOException {
037        try (
038                FileReader fileReader = new FileReader(file);
039                BufferedReader bufferedReader = new BufferedReader(fileReader);
040            ){
041            CvValue cvObject;
042            String line = null;
043            String name = null;
044            int value = 0;
045
046            while ((line = bufferedReader.readLine()) != null) {
047                log.debug("Line='{}'", line);
048                Pattern pattern = Pattern.compile(SEARCH_STRING);
049
050                Matcher matcher = pattern.matcher(line);
051
052                while (matcher.find()) {
053                    log.debug("I found the text {} and  {} and  {}\n" +
054                        "starting at index {} and ending at index {}",
055                        matcher.group(1),
056                        matcher.group(2),
057                        matcher.group(4),
058                        matcher.start(),
059                        matcher.end());
060                    name = matcher.group(1);
061                    value = Integer.parseInt(matcher.group(2));
062                    cvObject = cvModel.allCvMap().get(name);
063                    if (cvObject == null) {
064                        log.warn("Adding CV {} description \"{}\", which was in import file but not defined by the decoder definition", name, matcher.group(4));
065                        cvModel.addCV(name, false, false, false);
066                        cvObject = cvModel.allCvMap().get(name);
067                    }
068                    cvObject.setValue(value);
069                }
070            }
071            fileReader.close();
072        } catch (IOException e) {
073            log.error("Error reading file", e);
074        }
075    }
076
077}