001package jmri.jmrit.operations.rollingstock.engines;
002
003import java.io.File;
004
005import org.jdom2.Document;
006import org.jdom2.Element;
007import org.jdom2.ProcessingInstruction;
008import org.slf4j.Logger;
009import org.slf4j.LoggerFactory;
010
011import jmri.InstanceManager;
012import jmri.InstanceManagerAutoDefault;
013import jmri.InstanceManagerAutoInitialize;
014import jmri.jmrit.operations.OperationsXml;
015import jmri.jmrit.operations.locations.LocationManagerXml;
016import jmri.jmrit.operations.rollingstock.RollingStockLogger;
017import jmri.jmrit.operations.setup.Setup;
018
019/**
020 * Loads and stores engines using xml files. Also loads and stores engine
021 * models, engine types, engine lengths, and engine consist names.
022 *
023 * @author Daniel Boudreau Copyright (C) 2008
024 */
025public class EngineManagerXml extends OperationsXml implements InstanceManagerAutoDefault, InstanceManagerAutoInitialize {
026
027    public EngineManagerXml() {
028    }
029
030    @Override
031    public void writeFile(String name) throws java.io.FileNotFoundException, java.io.IOException {
032        log.debug("writeFile {}", name);
033        // This is taken in large part from "Java and XML" page 368
034        File file = findFile(name);
035        if (file == null) {
036            file = new File(name);
037        }
038        // create root element
039        Element root = new Element("operations-config"); // NOI18N
040        Document doc = newDocument(root, dtdLocation + "operations-engines.dtd"); // NOI18N
041
042        // add XSLT processing instruction
043        java.util.Map<String, String> m = new java.util.HashMap<>();
044        m.put("type", "text/xsl"); // NOI18N
045        m.put("href", xsltLocation + "operations-engines.xsl"); // NOI18N
046        ProcessingInstruction p = new ProcessingInstruction("xml-stylesheet", m); // NOI18N
047        doc.addContent(0, p);
048
049        InstanceManager.getDefault(EngineModels.class).store(root);
050        InstanceManager.getDefault(EngineTypes.class).store(root);
051        InstanceManager.getDefault(EngineLengths.class).store(root);
052        InstanceManager.getDefault(ConsistManager.class).store(root);
053        InstanceManager.getDefault(EngineManager.class).store(root);
054
055        writeXML(file, doc);
056
057        // done - engine file now stored, so can't be dirty
058        setDirty(false);
059    }
060
061    /**
062     * Read the contents of a roster XML file into this object. Note that this
063     * does not clear any existing entries.
064     */
065    @Override
066    public void readFile(String name) throws org.jdom2.JDOMException, java.io.IOException {
067        // suppress rootFromName(name) warning message by checking to see if file exists
068        if (findFile(name) == null) {
069            log.debug("{} file could not be found", name);
070            return;
071        }
072        // find root
073        Element root = rootFromName(name);
074        if (root == null) {
075            log.debug("{} file could not be read", name);
076            return;
077        }
078
079        if (!root.getName().equals("operations-config")) {
080            log.warn("OperationsPro engine file corrupted");
081            return;
082        }
083        
084        InstanceManager.getDefault(EngineModels.class).load(root);
085        InstanceManager.getDefault(EngineTypes.class).load(root);
086        InstanceManager.getDefault(EngineLengths.class).load(root);
087        InstanceManager.getDefault(ConsistManager.class).load(root);
088        InstanceManager.getDefault(EngineManager.class).load(root);
089
090        log.debug("Engines have been loaded!");
091        InstanceManager.getDefault(RollingStockLogger.class).enableEngineLogging(Setup.isEngineLoggerEnabled());
092        // clear dirty bit
093        setDirty(false);
094        // clear location dirty flag, locations get modified during the loading of cars and locos
095        InstanceManager.getDefault(LocationManagerXml.class).setDirty(false);
096    }
097
098    @Override
099    public void setOperationsFileName(String name) {
100        operationsFileName = name;
101    }
102
103    @Override
104    public String getOperationsFileName() {
105        return operationsFileName;
106    }
107
108    private String operationsFileName = "OperationsEngineRoster.xml"; // NOI18N
109
110    public void dispose() {
111    }
112
113    private final static Logger log = LoggerFactory.getLogger(EngineManagerXml.class);
114
115    @Override
116    public void initialize() {
117        load();
118    }
119
120}