001package jmri.jmrit.operations.routes;
002
003import java.io.File;
004import jmri.InstanceManager;
005import jmri.InstanceManagerAutoDefault;
006import jmri.InstanceManagerAutoInitialize;
007import jmri.jmrit.operations.OperationsXml;
008import org.jdom2.Document;
009import org.jdom2.Element;
010import org.jdom2.ProcessingInstruction;
011import org.slf4j.Logger;
012import org.slf4j.LoggerFactory;
013
014/**
015 * Loads and stores routes using xml files.
016 *
017 * @author Daniel Boudreau Copyright (C) 2008, 2009
018 */
019public class RouteManagerXml extends OperationsXml implements InstanceManagerAutoDefault, InstanceManagerAutoInitialize {
020
021    public RouteManagerXml() {
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-routes.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-routes.xsl"); // NOI18N
040        ProcessingInstruction p = new ProcessingInstruction("xml-stylesheet", m); // NOI18N
041        doc.addContent(0, p);
042
043        InstanceManager.getDefault(RouteManager.class).store(root);
044
045        writeXML(file, doc);
046
047        // done - route file now stored, so can't be dirty
048        setDirty(false);
049    }
050
051    /**
052     * Read the contents of a roster XML file into this object. Note that this
053     * does not clear any existing entries.
054     */
055    @Override
056    public void readFile(String name) throws org.jdom2.JDOMException, java.io.IOException {
057        // suppress rootFromName(name) warning message by checking to see if file exists
058        if (findFile(name) == null) {
059            log.debug("{} file could not be found", name);
060            return;
061        }
062        // find root
063        Element root = rootFromName(name);
064        if (root == null) {
065            log.debug("{} file could not be read", name);
066            return;
067        }
068        
069        if (!root.getName().equals("operations-config")) {
070            log.warn("OperationsPro route file corrupted");
071            return;
072        }
073
074        InstanceManager.getDefault(RouteManager.class).load(root);
075
076        // clear dirty bit
077        setDirty(false);
078    }
079
080    @Override
081    public void setOperationsFileName(String name) {
082        operationsFileName = name;
083    }
084
085    @Override
086    public String getOperationsFileName() {
087        return operationsFileName;
088    }
089
090    private String operationsFileName = "OperationsRouteRoster.xml"; // NOI18N
091
092    public void dispose() {
093    }
094
095    private final static Logger log = LoggerFactory.getLogger(RouteManagerXml.class);
096
097    @Override
098    public void initialize() {
099        load();
100    }
101}