use of org.jdom2.JDOMException in project JMRI by JMRI.
the class XmlFileCheckAction method actionPerformed.
@Override
public void actionPerformed(ActionEvent e) {
if (fci == null) {
fci = jmri.jmrit.XmlFile.userFileChooser("XML files", "xml");
}
// request the filename from an open dialog
fci.rescanCurrentDirectory();
int retVal = fci.showOpenDialog(_who);
// handle selection or cancel
if (retVal == JFileChooser.APPROVE_OPTION) {
File file = fci.getSelectedFile();
log.debug("located file {} for XML processing", file);
// handle the file (later should be outside this thread?)
try {
xmlfile.setValidate(XmlFile.Validate.None);
readFile(file);
JOptionPane.showMessageDialog(_who, "OK");
} catch (IOException | JDOMException ex) {
JOptionPane.showMessageDialog(_who, "Error: " + ex);
}
log.debug("parsing complete");
} else {
log.info("XmlFileCheckAction cancelled in open dialog");
}
}
use of org.jdom2.JDOMException in project JMRI by JMRI.
the class TrainManagerXml method readFile.
/**
* Read the contents of a roster XML file into this object. Note that this
* does not clear any existing entries.
*/
@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(name + " file could not be found");
// set flag, could be the first time
fileLoaded = true;
return;
}
// find root
Element root = rootFromName(name);
if (root == null) {
log.debug(name + " file could not be read");
return;
}
TrainManager.instance().load(root);
TrainScheduleManager.instance().load(root);
// set flag trains are loaded
fileLoaded = true;
AutomationManager.instance().load(root);
// now load train icons on panels
TrainManager.instance().loadTrainIcons();
// loading complete run startup scripts
TrainManager.instance().runStartUpScripts();
log.debug("Trains have been loaded!");
TrainLogger.instance().enableTrainLogging(Setup.isTrainLoggerEnabled());
// clear dirty flag
setDirty(false);
}
use of org.jdom2.JDOMException in project JMRI by JMRI.
the class DecoderIndexFile method updateIndexIfNeeded.
/**
* Check whether the user's version of the decoder index file needs to be
* updated; if it does, then forces the update.
*
* @return true is the index should be reloaded because it was updated
* @throws org.jdom2.JDOMException if unable to parse decoder index
* @throws java.io.IOException if unable to read decoder index
*/
static boolean updateIndexIfNeeded() throws org.jdom2.JDOMException, java.io.IOException {
// get version from master index; if not found, give up
String masterVersion = null;
DecoderIndexFile masterXmlFile = new DecoderIndexFile();
URL masterFile = FileUtil.findURL("xml/" + defaultDecoderIndexFilename(), FileUtil.Location.INSTALLED);
if (masterFile == null) {
return false;
}
Element masterRoot = masterXmlFile.rootFromURL(masterFile);
if (masterRoot.getChild("decoderIndex") != null) {
if (masterRoot.getChild("decoderIndex").getAttribute("version") != null) {
masterVersion = masterRoot.getChild("decoderIndex").getAttribute("version").getValue();
}
log.debug("master version found, is " + masterVersion);
} else {
return false;
}
// get from user index. Unless they are equal, force an update.
// note we find this file via the search path; if not exists, so that
// the master is found, we still do the right thing (nothing).
String userVersion = null;
DecoderIndexFile userXmlFile = new DecoderIndexFile();
Element userRoot = userXmlFile.rootFromName(defaultDecoderIndexFilename());
if (userRoot.getChild("decoderIndex") != null) {
if (userRoot.getChild("decoderIndex").getAttribute("version") != null) {
userVersion = userRoot.getChild("decoderIndex").getAttribute("version").getValue();
}
log.debug("user version found, is " + userVersion);
}
if (masterVersion != null && masterVersion.equals(userVersion)) {
return false;
}
// force the update, with the version number located earlier is available
if (masterVersion != null) {
instance().fileVersion = Integer.parseInt(masterVersion);
}
forceCreationOfNewIndex();
// and force it to be used
return true;
}
use of org.jdom2.JDOMException in project JMRI by JMRI.
the class TrainInfoFile method readTrainInfo.
/*
* Reads Dispatcher TrainInfo from a file in the user's preferences directory
* If the file containing Dispatcher TrainInfo does not exist this routine returns quietly.
* "name" is assumed to have the .xml or .XML extension already included
*/
public TrainInfo readTrainInfo(String name) throws org.jdom2.JDOMException, java.io.IOException {
log.debug("entered readTrainInfo for {}", name);
TrainInfo tInfo = null;
// check if file exists
if (checkFile(fileLocation + name)) {
// file is present.
tInfo = new TrainInfo();
root = rootFromName(fileLocation + name);
if (root != null) {
// there is a file
Element traininfo = root.getChild("traininfo");
if (traininfo != null) {
// there are train info options defined, read them
if (traininfo.getAttribute("transitname") != null) {
// there is a transit name selected
tInfo.setTransitName(traininfo.getAttribute("transitname").getValue());
} else {
log.error("Transit name missing when reading TrainInfoFile " + name);
}
if (traininfo.getAttribute("trainname") != null) {
// there is a transit name selected
tInfo.setTrainName(traininfo.getAttribute("trainname").getValue());
} else {
log.error("Train name missing when reading TrainInfoFile " + name);
}
if (traininfo.getAttribute("dccaddress") != null) {
tInfo.setDCCAddress(traininfo.getAttribute("dccaddress").getValue());
} else {
log.error("DCC Address missing when reading TrainInfoFile " + name);
}
if (traininfo.getAttribute("trainintransit") != null) {
tInfo.setTrainInTransit(true);
if (traininfo.getAttribute("trainintransit").getValue().equals("no")) {
tInfo.setTrainInTransit(false);
}
} else {
log.error("Train in Transit check box missing when reading TrainInfoFile " + name);
}
if (traininfo.getAttribute("startblockname") != null) {
// there is a transit name selected
tInfo.setStartBlockName(traininfo.getAttribute("startblockname").getValue());
} else {
log.error("Start block name missing when reading TrainInfoFile " + name);
}
if (traininfo.getAttribute("endblockname") != null) {
// there is a transit name selected
tInfo.setDestinationBlockName(traininfo.getAttribute("endblockname").getValue());
} else {
log.error("Destination block name missing when reading TrainInfoFile " + name);
}
if (traininfo.getAttribute("trainfromroster") != null) {
tInfo.setTrainFromRoster(true);
if (traininfo.getAttribute("trainfromroster").getValue().equals("no")) {
tInfo.setTrainFromRoster(false);
}
}
if (traininfo.getAttribute("trainfromtrains") != null) {
tInfo.setTrainFromTrains(true);
if (traininfo.getAttribute("trainfromtrains").getValue().equals("no")) {
tInfo.setTrainFromTrains(false);
}
}
if (traininfo.getAttribute("trainfromuser") != null) {
tInfo.setTrainFromUser(true);
if (traininfo.getAttribute("trainfromuser").getValue().equals("no")) {
tInfo.setTrainFromUser(false);
}
}
if (traininfo.getAttribute("priority") != null) {
tInfo.setPriority(Integer.parseInt(traininfo.getAttribute("priority").getValue()));
} else {
log.error("Priority missing when reading TrainInfoFile " + name);
}
if (traininfo.getAttribute("allocatealltheway") != null) {
tInfo.setAllocateAllTheWay(true);
if (traininfo.getAttribute("allocatealltheway").getValue().equals("no")) {
tInfo.setAllocateAllTheWay(false);
}
}
if (traininfo.getAttribute("resetwhendone") != null) {
tInfo.setResetWhenDone(true);
if (traininfo.getAttribute("resetwhendone").getValue().equals("no")) {
tInfo.setResetWhenDone(false);
}
if (traininfo.getAttribute("delayedrestart") != null) {
switch(traininfo.getAttribute("delayedrestart").getValue()) {
case "no":
tInfo.setDelayedRestart(ActiveTrain.NODELAY);
break;
case "sensor":
tInfo.setDelayedRestart(ActiveTrain.SENSORDELAY);
if (traininfo.getAttribute("delayedrestartsensor") != null) {
tInfo.setRestartSensorName(traininfo.getAttribute("delayedrestartsensor").getValue());
}
break;
case "timed":
tInfo.setDelayedRestart(ActiveTrain.TIMEDDELAY);
if (traininfo.getAttribute("delayedrestarttime") != null) {
tInfo.setRestartDelayMin((int) traininfo.getAttribute("delayedrestarttime").getLongValue());
}
break;
default:
break;
}
}
}
if (traininfo.getAttribute("reverseatend") != null) {
tInfo.setReverseAtEnd(true);
if (traininfo.getAttribute("reverseatend").getValue().equals("no")) {
tInfo.setReverseAtEnd(false);
}
}
if (traininfo.getAttribute("delayedstart") != null) {
switch(traininfo.getAttribute("delayedstart").getValue()) {
case "no":
tInfo.setDelayedStart(ActiveTrain.NODELAY);
break;
case "sensor":
tInfo.setDelayedStart(ActiveTrain.SENSORDELAY);
break;
default:
//This covers the old versions of the file with "yes"
tInfo.setDelayedStart(ActiveTrain.TIMEDDELAY);
break;
}
}
if (traininfo.getAttribute("departuretimehr") != null) {
tInfo.setDepartureTimeHr(Integer.parseInt(traininfo.getAttribute("departuretimehr").getValue()));
}
if (traininfo.getAttribute("departuretimemin") != null) {
tInfo.setDepartureTimeMin(Integer.parseInt(traininfo.getAttribute("departuretimemin").getValue()));
}
if (traininfo.getAttribute("delayedSensor") != null) {
tInfo.setDelaySensorName(traininfo.getAttribute("delayedSensor").getValue());
}
if (traininfo.getAttribute("traintype") != null) {
tInfo.setTrainType(traininfo.getAttribute("traintype").getValue());
}
if (traininfo.getAttribute("autorun") != null) {
tInfo.setAutoRun(true);
if (traininfo.getAttribute("autorun").getValue().equals("no")) {
tInfo.setAutoRun(false);
}
}
if (traininfo.getAttribute("loadatstartup") != null) {
tInfo.setLoadAtStartup(true);
if (traininfo.getAttribute("loadatstartup").getValue().equals("no")) {
tInfo.setLoadAtStartup(false);
}
}
// here retrieve items related only to automatically run trains if present
if (traininfo.getAttribute("speedfactor") != null) {
tInfo.setSpeedFactor(Float.parseFloat(traininfo.getAttribute("speedfactor").getValue()));
}
if (traininfo.getAttribute("maxspeed") != null) {
tInfo.setMaxSpeed(Float.parseFloat(traininfo.getAttribute("maxspeed").getValue()));
}
if (traininfo.getAttribute("ramprate") != null) {
tInfo.setRampRate(traininfo.getAttribute("ramprate").getValue());
}
if (traininfo.getAttribute("resistancewheels") != null) {
tInfo.setResistanceWheels(true);
if (traininfo.getAttribute("resistancewheels").getValue().equals("no")) {
tInfo.setResistanceWheels(false);
}
}
if (traininfo.getAttribute("runinreverse") != null) {
tInfo.setRunInReverse(true);
if (traininfo.getAttribute("runinreverse").getValue().equals("no")) {
tInfo.setRunInReverse(false);
}
}
if (traininfo.getAttribute("sounddecoder") != null) {
tInfo.setSoundDecoder(true);
if (traininfo.getAttribute("sounddecoder").getValue().equals("no")) {
tInfo.setSoundDecoder(false);
}
}
if (traininfo.getAttribute("maxtrainlength") != null) {
tInfo.setMaxTrainLength(Float.parseFloat(traininfo.getAttribute("maxtrainlength").getValue()));
}
if (traininfo.getAttribute("terminatewhendone") != null) {
tInfo.setTerminateWhenDone(false);
if (traininfo.getAttribute("terminatewhendone").getValue().equals("yes")) {
tInfo.setTerminateWhenDone(true);
}
}
}
}
}
return tInfo;
}
use of org.jdom2.JDOMException in project JMRI by JMRI.
the class XmlFileTest method testProcessPI.
public void testProcessPI() throws org.jdom2.JDOMException, java.io.IOException {
// Document from test file
Document doc;
Element e;
FileInputStream fs = new FileInputStream(new File("java/test/jmri/jmrit/XmlFileTest_PI.xml"));
try {
// argument controls validation
SAXBuilder builder = XmlFile.getBuilder(XmlFile.Validate.None);
doc = builder.build(new BufferedInputStream(fs));
Assert.assertNotNull("Original Document found", doc);
e = doc.getRootElement();
Assert.assertNotNull("Original root element found", e);
XmlFile x = new XmlFile() {
};
Document d = x.processInstructions(doc);
Assert.assertNotNull(d);
// test transform changes <contains> element to <content>
e = d.getRootElement();
Assert.assertNotNull("Transformed root element found", e);
Assert.assertTrue("Transformed root element is right type", e.getName().equals("top"));
Assert.assertTrue("Old element gone", e.getChild("contains") == null);
Assert.assertTrue("New element there", e.getChild("content") != null);
Assert.assertTrue("New element has content", e.getChild("content").getChildren().size() == 2);
} catch (java.io.IOException ex) {
throw ex;
} catch (org.jdom2.JDOMException ex) {
throw ex;
} finally {
fs.close();
}
}
Aggregations