001package jmri.jmrit.withrottle;
002
003import java.io.File;
004import java.io.IOException;
005import jmri.jmrit.XmlFile;
006import org.jdom2.Document;
007import org.jdom2.Element;
008import org.jdom2.JDOMException;
009import org.slf4j.Logger;
010import org.slf4j.LoggerFactory;
011
012/**
013 * @author Brett Hoffman Copyright (C) 2010
014 */
015abstract public class AbstractWiThrottlePreferences {
016
017    private String fileName;
018
019    public void openFile(String fileName) {
020        this.fileName = fileName;
021        AbstractWiThrottlePreferencesXml prefsXml = new AbstractWiThrottlePreferencesXml();
022        File file = new File(this.fileName);
023        Element root;
024        try {
025            root = prefsXml.rootFromFile(file);
026        } catch (java.io.FileNotFoundException ea) {
027            log.info("Could not find WiThrottle preferences file ({}).  Normal if preferences have not been saved before.", fileName);
028            root = null;
029        } catch (IOException | JDOMException eb) {
030            log.error("Exception while loading throttles preferences:", eb);
031            root = null;
032        }
033        if (root != null) {
034            Element child = root.getChild("WiThrottlePreferences"); // NOI18N
035            if ( child == null ) {
036                log.error("WiThrottle Preferences not loaded, no WiThrottlePreferences element in {}", fileName );
037                return;
038            }
039            load(child);
040        }
041    }
042
043    abstract void load( @javax.annotation.Nonnull Element child);
044
045    abstract Element store();
046
047    public void save() {
048        if (fileName == null) {
049            return;
050        }
051
052        XmlFile xmlFile = new XmlFile() {
053        };
054        xmlFile.makeBackupFile(fileName);
055        File file = new File(fileName);
056        try {
057            File parentDir = file.getParentFile();
058            if (!parentDir.exists()) {
059                if (!parentDir.mkdir()) {
060                    log.warn("Could not create parent directory for prefs file :{}", fileName);
061                    return;
062                }
063            }
064            if (file.createNewFile()) {
065                log.debug("Creating new WiThrottle prefs file: {}", fileName);
066            }
067        } catch (IOException ea) {
068            log.error("Could not create WiThrottle preferences file.");
069        }
070
071        try {
072            Element root = new Element("withrottle-prefs");
073            Document doc = XmlFile.newDocument(root);
074            root.setContent(store());
075            xmlFile.writeXML(file, doc);
076        } catch (IOException eb) {
077            log.warn("Exception in storing WiThrottle xml:", eb);
078        }
079    }
080
081    public AbstractWiThrottlePreferences() {
082    }
083
084    public static class AbstractWiThrottlePreferencesXml extends XmlFile {
085    }
086
087    private final static Logger log = LoggerFactory.getLogger(AbstractWiThrottlePreferences.class);
088
089}