Search in sources :

Example 61 with JDOMException

use of org.jdom2.JDOMException in project JMRI by JMRI.

the class OperationsSetupXml method readFile.

@Override
public void readFile(String name) throws org.jdom2.JDOMException, java.io.IOException {
    // suppress rootFromName(name) warning message by checking to see if file exists
    if (findFile(name) == null) {
        log.debug("{} file could not be found", name);
        return;
    }
    // find root
    Element root = rootFromName(name);
    if (root == null) {
        log.debug("{} file could not be read", name);
        return;
    }
    Setup.load(root);
    // load manifest header text strings
    TrainManifestHeaderText.load(root);
    // load manifest text strings
    TrainManifestText.load(root);
    // load switch list text strings
    TrainSwitchListText.load(root);
    // load control settings
    Control.load(root);
}
Also used : Element(org.jdom2.Element)

Example 62 with JDOMException

use of org.jdom2.JDOMException in project JMRI by JMRI.

the class RosterEntry method updateFile.

/**
     * Write the contents of this RosterEntry back to a file, preserving all
     * existing decoder CV content.
     * <p>
     * This writes the file back in place, with the same decoder-specific
     * content.
     */
public void updateFile() {
    LocoFile df = new LocoFile();
    String fullFilename = LocoFile.getFileLocation() + getFileName();
    // read in the content
    try {
        mRootElement = df.rootFromName(fullFilename);
    } catch (JDOMException | IOException e) {
        log.error("Exception while loading loco XML file: " + getFileName() + " exception: " + e);
    }
    try {
        File f = new File(fullFilename);
        // do backup
        df.makeBackupFile(LocoFile.getFileLocation() + getFileName());
        // and finally write the file
        df.writeFile(f, mRootElement, this.store());
    } catch (Exception e) {
        log.error("error during locomotive file output", e);
        try {
            JOptionPane.showMessageDialog(null, ResourceBundle.getBundle("jmri.jmrit.roster.JmritRosterBundle").getString("ErrorSavingText") + "\n" + e.getMessage(), ResourceBundle.getBundle("jmri.jmrit.roster.JmritRosterBundle").getString("ErrorSavingTitle"), JOptionPane.ERROR_MESSAGE);
        } catch (HeadlessException he) {
        // silently ignore inability to display dialog
        }
    }
}
Also used : HeadlessException(java.awt.HeadlessException) IOException(java.io.IOException) JDOMException(org.jdom2.JDOMException) File(java.io.File) JDOMException(org.jdom2.JDOMException) HeadlessException(java.awt.HeadlessException) ParseException(java.text.ParseException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException)

Example 63 with JDOMException

use of org.jdom2.JDOMException in project JMRI by JMRI.

the class SpeedometerFrame method doLoad.

private void doLoad() {
    log.debug("Check if there's anything to load");
    SpeedometerXml x = new SpeedometerXml();
    File file = x.getFile(false);
    if (file == null) {
        log.debug("Nothing to load");
        return;
    }
    log.debug("Start loading speedometer settings...");
    // Find root
    Element root;
    try {
        root = x.rootFromFile(file);
        if (root == null) {
            log.debug("File could not be read");
            return;
        }
        // First read configuration
        if (root.getChild("configuration") != null) {
            @SuppressWarnings("unchecked") List<Element> l = root.getChild("configuration").getChildren();
            if (log.isDebugEnabled()) {
                log.debug("readFile sees " + l.size() + " configurations");
            }
            for (int i = 0; i < l.size(); i++) {
                Element e = l.get(i);
                if (log.isDebugEnabled()) {
                    log.debug("Configuration " + e.getName() + " value " + e.getValue());
                }
                if (e.getName().equals("useMetric")) {
                    setUnitsMetric(e.getValue().equals("yes") ? true : false);
                }
            }
        }
        // Now read sensor information
        if (root.getChild("sensors") != null) {
            @SuppressWarnings("unchecked") List<Element> l = root.getChild("sensors").getChildren("sensor");
            if (log.isDebugEnabled()) {
                log.debug("readFile sees " + l.size() + " sensors");
            }
            for (int i = 0; i < l.size(); i++) {
                Element e = l.get(i);
                String sensorType = e.getChild("type").getText();
                if (sensorType.equals("StartSensor")) {
                    startSensor.setText(e.getChild("sensorName").getText());
                    boolean trigger = e.getChild("trigger").getValue().equals("entry");
                    startOnEntry.setSelected(trigger);
                    startOnExit.setSelected(!trigger);
                } else if (sensorType.equals("StopSensor1")) {
                    stopSensor1.setText(e.getChild("sensorName").getText());
                    boolean trigger = e.getChild("trigger").getValue().equals("entry");
                    stopOnEntry1.setSelected(trigger);
                    stopOnExit1.setSelected(!trigger);
                    distance1.setText(IntlUtilities.valueOf(Float.parseFloat(e.getChild("distance").getText())));
                } else if (sensorType.equals("StopSensor2")) {
                    stopSensor2.setText(e.getChild("sensorName").getText());
                    boolean trigger = e.getChild("trigger").getValue().equals("entry");
                    stopOnEntry2.setSelected(trigger);
                    stopOnExit2.setSelected(!trigger);
                    distance2.setText(IntlUtilities.valueOf(Float.parseFloat(e.getChild("distance").getText())));
                } else {
                    log.warn("Unknown sensor type: " + sensorType);
                }
            }
        }
    } catch (JDOMException ex) {
        log.error("File invalid: " + ex);
    } catch (IOException ex) {
        log.error("Error reading file: " + ex);
    }
    log.debug("...done");
}
Also used : Element(org.jdom2.Element) IOException(java.io.IOException) JDOMException(org.jdom2.JDOMException) File(java.io.File) XmlFile(jmri.jmrit.XmlFile)

Example 64 with JDOMException

use of org.jdom2.JDOMException in project JMRI by JMRI.

the class ConnectionConfigManager method savePreferences.

private synchronized void savePreferences(Profile profile, boolean shared) {
    Element element = new Element(CONNECTIONS, NAMESPACE);
    connections.stream().forEach((o) -> {
        log.debug("Saving connection {} ({})...", o.getConnectionName(), shared);
        Element e = ConfigXmlManager.elementFromObject(o, shared);
        if (e != null) {
            element.addContent(e);
        }
    });
    // save connections, or save an empty connections element if user removed all connections
    try {
        ProfileUtils.getAuxiliaryConfiguration(profile).putConfigurationFragment(JDOMUtil.toW3CElement(element), shared);
    } catch (JDOMException ex) {
        log.error("Unable to create create XML", ex);
    }
}
Also used : Element(org.jdom2.Element) JDOMException(org.jdom2.JDOMException)

Example 65 with JDOMException

use of org.jdom2.JDOMException 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

Element (org.jdom2.Element)60 JDOMException (org.jdom2.JDOMException)34 IOException (java.io.IOException)29 Document (org.jdom2.Document)24 File (java.io.File)22 SAXBuilder (org.jdom2.input.SAXBuilder)22 XmlFile (jmri.jmrit.XmlFile)12 ArrayList (java.util.ArrayList)9 List (java.util.List)8 URL (java.net.URL)7 FileNotFoundException (java.io.FileNotFoundException)6 HashMap (java.util.HashMap)5 BufferedInputStream (java.io.BufferedInputStream)4 FileInputStream (java.io.FileInputStream)4 NoProcessSpecifiedException (de.hpi.bpt.scylla.creation.SimulationConfiguration.SimulationConfigurationCreator.NoProcessSpecifiedException)3 NotAuthorizedToOverrideException (de.hpi.bpt.scylla.creation.SimulationConfiguration.SimulationConfigurationCreator.NotAuthorizedToOverrideException)3 HeadlessException (java.awt.HeadlessException)3 Attribute (org.jdom2.Attribute)3 ScalingFileChooser (de.hpi.bpt.scylla.GUI.ScalingFileChooser)2 PropertyChangeEvent (java.beans.PropertyChangeEvent)2