001package jmri.jmrit.operations.setup;
002
003import java.io.File;
004
005import org.jdom2.*;
006import org.slf4j.Logger;
007import org.slf4j.LoggerFactory;
008
009import jmri.*;
010import jmri.jmrit.operations.OperationsXml;
011import jmri.jmrit.operations.trains.*;
012
013/**
014 * Loads and stores the operation setup using xml files.
015 *
016 * @author Daniel Boudreau Copyright (C) 2008, 2010
017 */
018public class OperationsSetupXml extends OperationsXml implements InstanceManagerAutoDefault, InstanceManagerAutoInitialize {
019
020    public OperationsSetupXml() {
021    }
022
023    @Override
024    public void writeFile(String name) throws java.io.FileNotFoundException, java.io.IOException {
025        log.debug("writeFile {}", name);
026        // This is taken in large part from "Java and XML" page 368
027        File file = findFile(name);
028        if (file == null) {
029            file = new File(name);
030        }
031        // create root element
032        Element root = new Element("operations-config"); // NOI18N
033        Document doc = newDocument(root, dtdLocation + "operations-config.dtd"); // NOI18N
034
035        // add XSLT processing instruction
036        java.util.Map<String, String> m = new java.util.HashMap<String, String>();
037        m.put("type", "text/xsl"); // NOI18N
038        m.put("href", xsltLocation + "operations-config.xsl"); // NOI18N
039        ProcessingInstruction p = new ProcessingInstruction("xml-stylesheet", m); // NOI18N
040        doc.addContent(0, p);
041
042        // add top-level elements
043        root.addContent(Setup.store());
044        // add manifest header text strings
045        root.addContent(TrainManifestHeaderText.store());
046        // add manifest text strings
047        root.addContent(TrainManifestText.store());
048        // add switch list text strings
049        root.addContent(TrainSwitchListText.store());
050        // add control elements
051        root.addContent(Control.store());
052
053        writeXML(file, doc);
054
055        // done, so can't be dirty
056        setDirty(false);
057    }
058
059    @Override
060    public void readFile(String name) throws org.jdom2.JDOMException, java.io.IOException {
061        // suppress rootFromName(name) warning message by checking to see if file exists
062        if (findFile(name) == null) {
063            log.debug("{} file could not be found", name);
064            return;
065        }
066        // find root
067        Element root = rootFromName(name);
068        if (root == null) {
069            log.debug("{} file could not be read", name);
070            return;
071        }
072        
073        if (!root.getName().equals("operations-config")) {
074            log.warn("OperationsPro settings file corrupted");
075            return;
076        }
077        
078        Setup.load(root);
079        // load manifest header text strings
080        TrainManifestHeaderText.load(root);
081        // load manifest text strings
082        TrainManifestText.load(root);
083        // load switch list text strings
084        TrainSwitchListText.load(root);
085        // load control settings
086        Control.load(root);
087        
088        InstanceManager.getDefault(TrainLogger.class).enableTrainLogging(Setup.isTrainLoggerEnabled());
089
090        // clear dirty bit
091        setDirty(false);
092    }
093
094    @Override
095    public void setOperationsFileName(String name) {
096        operationsFileName = name;
097    }
098
099    @Override
100    public String getOperationsFileName() {
101        return operationsFileName;
102    }
103
104    private String operationsFileName = "Operations.xml"; // NOI18N
105
106    private final static Logger log = LoggerFactory.getLogger(OperationsSetupXml.class);
107
108    @Override
109    public void initialize() {
110        load();
111    }
112}