use of org.jdom2.JDOMException in project JMRI by JMRI.
the class NameCheckAction method actionPerformed.
@SuppressWarnings("unchecked")
@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();
if (log.isDebugEnabled()) {
log.debug("located file " + file + " for XML processing");
}
// handle the file (later should be outside this thread?)
try {
Element root = readFile(file);
if (log.isDebugEnabled()) {
log.debug("parsing complete");
}
// check to see if there's a decoder element
if (root.getChild("decoder") == null) {
log.warn("Does not appear to be a decoder file");
return;
}
Iterator<Element> iter = root.getChild("decoder").getChild("variables").getDescendants(new ElementFilter("variable"));
jmri.jmrit.symbolicprog.NameFile nfile = jmri.jmrit.symbolicprog.NameFile.instance();
String warnings = "";
while (iter.hasNext()) {
Element varElement = iter.next();
// for each variable, see if can find in names file
Attribute labelAttr = varElement.getAttribute("label");
String label = null;
if (labelAttr != null) {
label = labelAttr.getValue();
}
Attribute itemAttr = varElement.getAttribute("item");
String item = null;
if (itemAttr != null) {
item = itemAttr.getValue();
}
if (log.isDebugEnabled()) {
log.debug("Variable called \"" + ((label != null) ? label : "<none>") + "\" \"" + ((item != null) ? item : "<none>"));
}
if (!(label == null ? false : nfile.checkName(label)) && !(item == null ? false : nfile.checkName(item))) {
log.warn("Variable not found: label=\"" + ((label != null) ? label : "<none>") + "\" item=\"" + ((item != null) ? label : "<none>") + "\"");
warnings += "Variable not found: label=\"" + ((label != null) ? label : "<none>") + "\" item=\"" + ((item != null) ? item : "<none>") + "\"\n";
}
}
if (!warnings.equals("")) {
JOptionPane.showMessageDialog(_who, warnings);
} else {
JOptionPane.showMessageDialog(_who, "No mismatched items found");
}
} catch (HeadlessException | IOException | JDOMException ex) {
JOptionPane.showMessageDialog(_who, "Error parsing decoder file: " + ex);
}
} else {
log.debug("XmlFileCheckAction cancelled in open dialog");
}
}
use of org.jdom2.JDOMException in project JMRI by JMRI.
the class BlockValueFile method readBlockValues.
/*
* Reads Block values from a file in the user's preferences directory
* If the file containing block values does not exist this routine returns quietly.
* If a Block named in the file does not exist currently, that entry is quietly ignored.
*/
@SuppressWarnings("unchecked")
public void readBlockValues() throws org.jdom2.JDOMException, java.io.IOException {
log.debug("entered readBlockValues");
List<String> blocks = blockManager.getSystemNameList();
// check if file exists
if (checkFile(defaultFileName)) {
// file is present,
root = rootFromName(defaultFileName);
if ((root != null) && (blocks.size() > 0)) {
// there is a file and there are Blocks defined
Element blockvalues = root.getChild("blockvalues");
if (blockvalues != null) {
// there are values defined, read and set block values if Block exists.
List<Element> blockList = blockvalues.getChildren("block");
for (int i = 0; i < blockList.size(); i++) {
if ((blockList.get(i)).getAttribute("systemname") == null) {
log.warn("unexpected null in systemName " + blockList.get(i) + " " + blockList.get(i).getAttributes());
break;
}
String sysName = blockList.get(i).getAttribute("systemname").getValue();
// get Block - ignore entry if block not found
Block b = blockManager.getBySystemName(sysName);
if (b != null) {
// Block was found, set its value
Object v = blockList.get(i).getAttribute("value").getValue();
if (blockList.get(i).getAttribute("valueClass") != null) {
if (blockList.get(i).getAttribute("valueClass").getValue().equals("jmri.jmrit.roster.RosterEntry")) {
jmri.jmrit.roster.RosterEntry re = jmri.jmrit.roster.Roster.getDefault().getEntryForId(((String) v));
if (re != null) {
v = re;
}
}
}
b.setValue(v);
// set direction if there is one
int dd = jmri.Path.NONE;
Attribute a = blockList.get(i).getAttribute("dir");
if (a != null) {
try {
dd = a.getIntValue();
} catch (org.jdom2.DataConversionException e) {
log.error("failed to convert direction attribute");
}
}
b.setDirection(dd);
}
}
}
}
}
}
use of org.jdom2.JDOMException in project JMRI by JMRI.
the class LocationManagerXml 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("{} 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;
}
LocationManager.instance().load(root);
ScheduleManager.instance().load(root);
setDirty(false);
log.debug("Locations have been loaded!");
}
use of org.jdom2.JDOMException in project JMRI by JMRI.
the class ProfileManager method readProfiles.
private void readProfiles() throws JDOMException, IOException {
try {
boolean reWrite = false;
if (!catalog.exists()) {
this.writeProfiles();
}
if (!catalog.canRead()) {
return;
}
this.readingProfiles = true;
Document doc = (new SAXBuilder()).build(catalog);
profiles.clear();
for (Element e : doc.getRootElement().getChild(PROFILES).getChildren()) {
File pp = FileUtil.getFile(e.getAttributeValue(Profile.PATH));
try {
Profile p = new Profile(pp);
this.addProfile(p);
} catch (FileNotFoundException ex) {
log.info("Cataloged profile \"{}\" not in expected location\nSearching for it in {}", e.getAttributeValue(Profile.ID), pp.getParentFile());
this.findProfiles(pp.getParentFile());
reWrite = true;
}
}
searchPaths.clear();
for (Element e : doc.getRootElement().getChild(SEARCH_PATHS).getChildren()) {
File path = FileUtil.getFile(e.getAttributeValue(Profile.PATH));
if (!searchPaths.contains(path)) {
this.addSearchPath(path);
}
if (Boolean.parseBoolean(e.getAttributeValue(DEFAULT))) {
this.defaultSearchPath = path;
}
}
if (searchPaths.isEmpty()) {
this.addSearchPath(FileUtil.getFile(FileUtil.getPreferencesPath()));
}
this.readingProfiles = false;
if (reWrite) {
this.writeProfiles();
}
this.profiles.sort(null);
} catch (JDOMException | IOException ex) {
this.readingProfiles = false;
throw ex;
}
}
use of org.jdom2.JDOMException in project JMRI by JMRI.
the class NodeIdentity method init.
private synchronized void init() {
File identityFile = this.identityFile();
if (identityFile.exists()) {
try {
Document doc = (new SAXBuilder()).build(identityFile);
String id = doc.getRootElement().getChild(NODE_IDENTITY).getAttributeValue(NODE_IDENTITY);
this.formerIdentities.clear();
doc.getRootElement().getChild(FORMER_IDENTITIES).getChildren().stream().forEach((e) -> {
this.formerIdentities.add(e.getAttributeValue(NODE_IDENTITY));
});
if (!this.validateIdentity(id)) {
log.debug("Node identity {} is invalid. Generating new node identity.", id);
this.formerIdentities.add(id);
this.getIdentity(true);
} else {
this.getIdentity(true);
}
} catch (JDOMException | IOException ex) {
log.error("Unable to read node identities: {}", ex.getLocalizedMessage());
this.getIdentity(true);
}
} else {
this.getIdentity(true);
}
}
Aggregations