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