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);
}
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
}
}
}
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");
}
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);
}
}
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;
}
Aggregations