Search in sources :

Example 11 with XmlFile

use of jmri.jmrit.XmlFile in project JMRI by JMRI.

the class AbstractWiThrottlePreferences method save.

public void save() {
    if (fileName == null) {
        return;
    }
    XmlFile xmlFile = new XmlFile() {
    };
    xmlFile.makeBackupFile(fileName);
    File file = new File(fileName);
    try {
        File parentDir = file.getParentFile();
        if (!parentDir.exists()) {
            if (!parentDir.mkdir()) {
                log.warn("Could not create parent directory for prefs file :" + fileName);
                return;
            }
        }
        if (file.createNewFile()) {
            log.debug("Creating new WiThrottle prefs file: " + fileName);
        }
    } catch (Exception ea) {
        log.error("Could not create WiThrottle preferences file.");
    }
    try {
        Element root = new Element("withrottle-prefs");
        Document doc = XmlFile.newDocument(root);
        root.setContent(store());
        xmlFile.writeXML(file, doc);
    } catch (Exception eb) {
        log.warn("Exception in storing WiThrottle xml: " + eb);
    }
}
Also used : XmlFile(jmri.jmrit.XmlFile) Element(org.jdom2.Element) Document(org.jdom2.Document) File(java.io.File) XmlFile(jmri.jmrit.XmlFile)

Example 12 with XmlFile

use of jmri.jmrit.XmlFile in project JMRI by JMRI.

the class WarrantPreferences method save.

public void save() {
    if (_fileName == null) {
        log.error("_fileName null. Could not create warrant preferences file.");
        return;
    }
    XmlFile xmlFile = new XmlFile() {
    };
    xmlFile.makeBackupFile(_fileName);
    File file = new File(_fileName);
    try {
        File parentDir = file.getParentFile();
        if (!parentDir.exists()) {
            if (!parentDir.mkdir()) {
                log.warn("Could not create parent directory for prefs file :{}", _fileName);
                return;
            }
        }
        if (file.createNewFile()) {
            log.debug("Creating new warrant prefs file: {}", _fileName);
        }
    } catch (IOException ea) {
        log.error("Could not create warrant preferences file at {}.", _fileName, ea);
    }
    try {
        Element root = new Element("warrantPreferences");
        Document doc = XmlFile.newDocument(root);
        if (store(root)) {
            xmlFile.writeXML(file, doc);
        }
    } catch (IOException eb) {
        log.warn("Exception in storing warrant xml: {}", eb);
    }
}
Also used : XmlFile(jmri.jmrit.XmlFile) Element(org.jdom2.Element) IOException(java.io.IOException) Document(org.jdom2.Document) File(java.io.File) XmlFile(jmri.jmrit.XmlFile)

Example 13 with XmlFile

use of jmri.jmrit.XmlFile in project JMRI by JMRI.

the class ThrottleFrame method saveThrottle.

private void saveThrottle(String sfile) {
    // Save throttle: title / window position
    // as strongly linked to extended throttles and roster presence, do not save function buttons and background window as they're stored in the roster entry
    XmlFile xf = new XmlFile() {
    };
    // odd syntax is due to XmlFile being abstract
    xf.makeBackupFile(sfile);
    File file = new File(sfile);
    try {
        //The file does not exist, create it before writing
        File parentDir = file.getParentFile();
        if (!parentDir.exists()) {
            if (// make directory and check result
            !parentDir.mkdir()) {
                log.error("could not make parent directory");
            }
        }
        if (// create file, check success
        !file.createNewFile()) {
            log.error("createNewFile failed");
        }
    } catch (Exception exp) {
        log.error("Exception while writing the throttle file, may not be complete: " + exp);
    }
    try {
        Element root = new Element("throttle-config");
        Document doc = XmlFile.newDocument(root, XmlFile.getDefaultDtdLocation() + "throttle-config.dtd");
        // add XSLT processing instruction
        // <?xml-stylesheet type="text/xsl" href="XSLT/throttle.xsl"?>
        /*   java.util.Map<String,String> m = new java.util.HashMap<String,String>();
             m.put("type", "text/xsl");
             m.put("href", jmri.jmrit.XmlFile.xsltLocation+"throttle.xsl");
             ProcessingInstruction p = new ProcessingInstruction("xml-stylesheet", m);
             doc.addContent(0,p);*/
        Element throttleElement = getXml();
        //   throttleElement.getChild("AddressPanel").removeChild("locoaddress");
        if (// don't save function buttons labels, they're in roster entry 
        (this.getRosterEntry() != null) && (getDefaultThrottleFolder() + addressPanel.getRosterEntry().getId().trim() + ".xml").compareTo(sfile) == 0) {
            throttleElement.getChild("FunctionPanel").removeChildren("FunctionButton");
        }
        root.setContent(throttleElement);
        xf.writeXML(file, doc);
        setLastUsedSaveFile(sfile);
    } catch (Exception ex) {
        log.warn("Exception while storing throttle xml: " + ex);
    }
}
Also used : XmlFile(jmri.jmrit.XmlFile) Element(org.jdom2.Element) Document(org.jdom2.Document) File(java.io.File) XmlFile(jmri.jmrit.XmlFile)

Example 14 with XmlFile

use of jmri.jmrit.XmlFile in project JMRI by JMRI.

the class ThrottleFrame method loadThrottle.

public void loadThrottle(String sfile) {
    if (sfile == null) {
        JFileChooser fileChooser = jmri.jmrit.XmlFile.userFileChooser(Bundle.getMessage("PromptXmlFileTypes"), "xml");
        fileChooser.setCurrentDirectory(new File(getDefaultThrottleFolder()));
        fileChooser.setDialogType(JFileChooser.OPEN_DIALOG);
        java.io.File file = LoadXmlConfigAction.getFile(fileChooser);
        if (file == null) {
            return;
        }
        sfile = file.getAbsolutePath();
        if (sfile == null) {
            return;
        }
    }
    boolean switchAfter = false;
    if (!isEditMode) {
        switchMode();
        switchAfter = true;
    }
    try {
        XmlFile xf = new XmlFile() {
        };
        // odd syntax is due to XmlFile being abstract
        File f = new File(sfile);
        Element root = xf.rootFromFile(f);
        Element conf = root.getChild("ThrottleFrame");
        // File looks ok
        setLastUsedSaveFile(sfile);
        // close all existing Jynstruments
        Component[] cmps = getComponents();
        for (int i = 0; i < cmps.length; i++) {
            try {
                if (cmps[i] instanceof JInternalFrame) {
                    JInternalFrame jyf = (JInternalFrame) cmps[i];
                    Component[] cmps2 = jyf.getContentPane().getComponents();
                    for (int j = 0; j < cmps2.length; j++) {
                        if (cmps2[j] instanceof Jynstrument) {
                            ((Jynstrument) cmps2[j]).exit();
                            jyf.dispose();
                        }
                    }
                }
            } catch (Exception ex) {
                log.debug("Got exception (no panic) " + ex);
            }
        }
        // and finally load all preferences
        setXml(conf);
    } catch (Exception ex) {
        if (log.isDebugEnabled()) {
            log.debug("Loading throttle exception: " + ex.getMessage());
        }
    }
    //     checkPosition();
    if (switchAfter) {
        switchMode();
    }
    return;
}
Also used : Jynstrument(jmri.jmrit.jython.Jynstrument) XmlFile(jmri.jmrit.XmlFile) Element(org.jdom2.Element) File(java.io.File) Point(java.awt.Point) JFileChooser(javax.swing.JFileChooser) JComponent(javax.swing.JComponent) Component(java.awt.Component) File(java.io.File) XmlFile(jmri.jmrit.XmlFile) JInternalFrame(javax.swing.JInternalFrame)

Example 15 with XmlFile

use of jmri.jmrit.XmlFile in project JMRI by JMRI.

the class DefaultSignalSystemManager method makeBean.

SignalSystem makeBean(String name) {
    //First check to see if the bean is in the default system directory
    URL path = FileUtil.findURL("xml/signals/" + name + "/aspects.xml", FileUtil.Location.INSTALLED);
    log.debug("load from {}", path);
    XmlFile xf = new AspectFile();
    if (path != null) {
        try {
            Element root = xf.rootFromURL(path);
            DefaultSignalSystem s = new DefaultSignalSystem(name);
            loadBean(s, root);
            return s;
        } catch (IOException | JDOMException e) {
            log.error("Could not parse aspect file \"{}\" due to: {}", path, e);
        }
    }
    //if the file doesn't exist or fails the load from the default location then try the user directory
    path = FileUtil.findURL("signals/" + name + "/aspects.xml", FileUtil.Location.USER, "xml", "resources");
    log.debug("load from {}", path);
    if (path != null) {
        xf = new AspectFile();
        try {
            Element root = xf.rootFromURL(path);
            DefaultSignalSystem s = new DefaultSignalSystem(name);
            loadBean(s, root);
            return s;
        } catch (IOException | JDOMException e) {
            log.error("Could not parse aspect file \"{}\" due to: {}", path, e);
        }
    }
    return null;
}
Also used : DefaultSignalSystem(jmri.implementation.DefaultSignalSystem) XmlFile(jmri.jmrit.XmlFile) Element(org.jdom2.Element) IOException(java.io.IOException) JDOMException(org.jdom2.JDOMException) URL(java.net.URL)

Aggregations

XmlFile (jmri.jmrit.XmlFile)15 Element (org.jdom2.Element)10 File (java.io.File)8 Document (org.jdom2.Document)6 IOException (java.io.IOException)5 JDOMException (org.jdom2.JDOMException)3 Attribute (org.jdom2.Attribute)2 SuppressFBWarnings (edu.umd.cs.findbugs.annotations.SuppressFBWarnings)1 BorderLayout (java.awt.BorderLayout)1 Component (java.awt.Component)1 Point (java.awt.Point)1 ActionEvent (java.awt.event.ActionEvent)1 ActionListener (java.awt.event.ActionListener)1 URL (java.net.URL)1 ZipEntry (java.util.zip.ZipEntry)1 ZipFile (java.util.zip.ZipFile)1 JButton (javax.swing.JButton)1 JComponent (javax.swing.JComponent)1 JFileChooser (javax.swing.JFileChooser)1 JInternalFrame (javax.swing.JInternalFrame)1