use of org.jdom2.JDOMException in project JMRI by JMRI.
the class SchemaTestBase method validate.
@Test
public void validate() {
Assume.assumeFalse("Ignoring schema validation.", Boolean.getBoolean("jmri.skipschematests"));
XmlFile.setDefaultValidate(XmlFile.Validate.CheckDtdThenSchema);
XmlFile xf = new XmlFileImpl();
try {
xf.rootFromFile(file);
if (!this.pass) {
Assert.fail("Validation of \"" + file.getPath() + "\" should have failed");
}
} catch (IOException | JDOMException ex) {
// throw unexpected errors
if (this.pass) {
Assert.fail("Failed to validate \"" + file.getPath() + "\" due to: " + ex);
}
}
}
use of org.jdom2.JDOMException in project JMRI by JMRI.
the class DecoderIndexFile method readFile.
/**
* Read the contents of a decoderIndex XML file into this object. Note that
* this does not clear any existing entries; reset the instance to do that.
*
* @param name the name of the decoder index file
* @throws org.jdom2.JDOMException if unable to parse to decoder index file
* @throws java.io.IOException if unable to read decoder index file
*/
void readFile(String name) throws org.jdom2.JDOMException, java.io.IOException {
if (log.isDebugEnabled()) {
log.debug("readFile " + name);
}
// read file, find root
Element root = rootFromName(name);
// decode type, invoke proper processing routine if a decoder file
if (root.getChild("decoderIndex") != null) {
if (root.getChild("decoderIndex").getAttribute("version") != null) {
fileVersion = Integer.parseInt(root.getChild("decoderIndex").getAttribute("version").getValue());
}
log.debug("found fileVersion of " + fileVersion);
readMfgSection(root.getChild("decoderIndex"));
readFamilySection(root.getChild("decoderIndex"));
} else {
log.error("Unrecognized decoderIndex file contents in file: " + name);
}
}
use of org.jdom2.JDOMException in project JMRI by JMRI.
the class DecoderIndexFile method writeFile.
public void writeFile(String name, DecoderIndexFile oldIndex, String[] files) throws java.io.IOException {
if (log.isDebugEnabled()) {
log.debug("writeFile " + name);
}
// This is taken in large part from "Java and XML" page 368
File file = new File(FileUtil.getUserFilesPath() + name);
// create root element and document
Element root = new Element("decoderIndex-config");
root.setAttribute("noNamespaceSchemaLocation", "http://jmri.org/xml/schema/decoder.xsd", org.jdom2.Namespace.getNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance"));
Document doc = newDocument(root);
// add XSLT processing instruction
// <?xml-stylesheet type="text/xsl" href="XSLT/DecoderID.xsl"?>
java.util.Map<String, String> m = new java.util.HashMap<>();
m.put("type", "text/xsl");
m.put("href", xsltLocation + "DecoderID.xsl");
ProcessingInstruction p = new ProcessingInstruction("xml-stylesheet", m);
doc.addContent(0, p);
// add top-level elements
Element index;
root.addContent(index = new Element("decoderIndex"));
index.setAttribute("version", Integer.toString(fileVersion));
log.debug("version written to file as " + fileVersion);
// add mfg list from existing DecoderIndexFile item
Element mfgList = new Element("mfgList");
// copy dates from original mfgList element
if (oldIndex.nmraListDate != null) {
mfgList.setAttribute("nmraListDate", oldIndex.nmraListDate);
}
if (oldIndex.updated != null) {
mfgList.setAttribute("updated", oldIndex.updated);
}
if (oldIndex.lastAdd != null) {
mfgList.setAttribute("lastadd", oldIndex.lastAdd);
}
// We treat "NMRA" special...
Element mfg = new Element("manufacturer");
mfg.setAttribute("mfg", "NMRA");
mfg.setAttribute("mfgID", "999");
mfgList.addContent(mfg);
// start working on the rest of the entries
List<String> keys = new ArrayList<>(oldIndex._mfgIdFromNameHash.keySet());
Collections.sort(keys);
for (Object item : keys) {
String mfgName = (String) item;
if (!mfgName.equals("NMRA")) {
mfg = new Element("manufacturer");
mfg.setAttribute("mfg", mfgName);
mfg.setAttribute("mfgID", oldIndex._mfgIdFromNameHash.get(mfgName));
mfgList.addContent(mfg);
}
}
// add family list by scanning files
Element familyList = new Element("familyList");
for (String fileName : files) {
DecoderFile d = new DecoderFile();
try {
Element droot = d.rootFromName(DecoderFile.fileLocation + fileName);
Element family = droot.getChild("decoder").getChild("family").clone();
family.setAttribute("file", fileName);
familyList.addContent(family);
} catch (org.jdom2.JDOMException exj) {
log.error("could not parse " + fileName + ": " + exj.getMessage());
} catch (java.io.FileNotFoundException exj) {
log.error("could not read " + fileName + ": " + exj.getMessage());
} catch (IOException exj) {
log.error("other exception while dealing with " + fileName + ": " + exj.getMessage());
}
}
index.addContent(mfgList);
index.addContent(familyList);
writeXML(file, doc);
// force a read of the new file next time
resetInstance();
}
use of org.jdom2.JDOMException in project JMRI by JMRI.
the class ConsistFile method readFile.
/**
* Read all consists from a file.
*
* @param fileName path to file
* @throws org.jdom2.JDOMException if unable to parse consists
* @throws java.io.IOException if unable to read file
*/
@SuppressWarnings("unchecked")
public void readFile(String fileName) throws JDOMException, IOException {
if (checkFile(fileName)) {
Element root = rootFromName(fileName);
Element roster;
if (root == null) {
log.warn("consist file could not be read");
return;
}
roster = root.getChild("roster");
if (roster == null) {
log.debug("consist file does not contain a roster entry");
return;
}
Iterator<Element> consistIterator = root.getDescendants(new ElementFilter("consist"));
try {
Element consist;
do {
consist = consistIterator.next();
consistFromXml(consist);
} while (consistIterator.hasNext());
} catch (NoSuchElementException nde) {
log.debug("end of consist list");
}
} else {
log.info("Consist file does not exist. One will be created if necessary.");
}
}
use of org.jdom2.JDOMException in project JMRI by JMRI.
the class OptionsFile method readDispatcherOptions.
/*
* Reads Dispatcher Options from a file in the user's preferences directory
* If the file containing Dispatcher Options does not exist this routine returns quietly.
*/
public void readDispatcherOptions(DispatcherFrame f) throws org.jdom2.JDOMException, java.io.IOException {
// check if file exists
if (checkFile(defaultFileName)) {
// file is present,
log.debug("Reading Dispatcher options from file {}", defaultFileName);
root = rootFromName(defaultFileName);
dispatcher = f;
if (root != null) {
// there is a file
Element options = root.getChild("options");
if (options != null) {
// there are options defined, read and set Dispatcher options
if (options.getAttribute("lename") != null) {
// there is a layout editor name selected
String leName = options.getAttribute("lename").getValue();
// get list of Layout Editor panels
ArrayList<LayoutEditor> layoutEditorList = jmri.jmrit.display.PanelMenu.instance().getLayoutEditorPanelList();
if (layoutEditorList.size() == 0) {
log.warn("Dispatcher options specify a Layout Editor panel that is not present.");
} else {
boolean found = false;
for (int i = 0; i < layoutEditorList.size(); i++) {
if (leName.equals(layoutEditorList.get(i).getTitle())) {
found = true;
dispatcher.setLayoutEditor(layoutEditorList.get(i));
}
}
if (!found) {
log.warn("Layout Editor panel - " + leName + " - not found.");
}
}
}
if (options.getAttribute("usesignaltype") != null) {
dispatcher.setSignalType(0x00);
if (options.getAttribute("usesignaltype").getValue().equals("signalmast")) {
dispatcher.setSignalType(0x01);
}
}
if (options.getAttribute("useconnectivity") != null) {
dispatcher.setUseConnectivity(true);
if (options.getAttribute("useconnectivity").getValue().equals("no")) {
dispatcher.setUseConnectivity(false);
}
}
if (options.getAttribute("trainsfromroster") != null) {
dispatcher.setTrainsFromRoster(true);
if (options.getAttribute("trainsfromroster").getValue().equals("no")) {
dispatcher.setTrainsFromRoster(false);
}
}
if (options.getAttribute("trainsfromtrains") != null) {
dispatcher.setTrainsFromTrains(true);
if (options.getAttribute("trainsfromtrains").getValue().equals("no")) {
dispatcher.setTrainsFromTrains(false);
}
}
if (options.getAttribute("trainsfromuser") != null) {
dispatcher.setTrainsFromUser(true);
if (options.getAttribute("trainsfromuser").getValue().equals("no")) {
dispatcher.setTrainsFromUser(false);
}
}
if (options.getAttribute("autoallocate") != null) {
dispatcher.setAutoAllocate(false);
if (options.getAttribute("autoallocate").getValue().equals("yes")) {
dispatcher.setAutoAllocate(true);
}
}
if (options.getAttribute("autoturnouts") != null) {
dispatcher.setAutoTurnouts(true);
if (options.getAttribute("autoturnouts").getValue().equals("no")) {
dispatcher.setAutoTurnouts(false);
}
}
if (options.getAttribute("trustknownturnouts") != null) {
dispatcher.setTrustKnownTurnouts(false);
if (options.getAttribute("trustknownturnouts").getValue().equals("yes")) {
dispatcher.setTrustKnownTurnouts(true);
}
}
if (options.getAttribute("minthrottleinterval") != null) {
String s = (options.getAttribute("minthrottleinterval")).getValue();
dispatcher.setMinThrottleInterval(Integer.parseInt(s));
}
if (options.getAttribute("fullramptime") != null) {
String s = (options.getAttribute("fullramptime")).getValue();
dispatcher.setFullRampTime(Integer.parseInt(s));
}
if (options.getAttribute("hasoccupancydetection") != null) {
dispatcher.setHasOccupancyDetection(true);
if (options.getAttribute("hasoccupancydetection").getValue().equals("no")) {
dispatcher.setHasOccupancyDetection(false);
}
}
if (options.getAttribute("shortactivetrainnames") != null) {
dispatcher.setShortActiveTrainNames(true);
if (options.getAttribute("shortactivetrainnames").getValue().equals("no")) {
dispatcher.setShortActiveTrainNames(false);
}
}
if (options.getAttribute("shortnameinblock") != null) {
dispatcher.setShortNameInBlock(true);
if (options.getAttribute("shortnameinblock").getValue().equals("no")) {
dispatcher.setShortNameInBlock(false);
}
}
if (options.getAttribute("extracolorforallocated") != null) {
dispatcher.setExtraColorForAllocated(true);
if (options.getAttribute("extracolorforallocated").getValue().equals("no")) {
dispatcher.setExtraColorForAllocated(false);
}
}
if (options.getAttribute("nameinallocatedblock") != null) {
dispatcher.setNameInAllocatedBlock(true);
if (options.getAttribute("nameinallocatedblock").getValue().equals("no")) {
dispatcher.setNameInAllocatedBlock(false);
}
}
if (options.getAttribute("supportvsdecoder") != null) {
dispatcher.setSupportVSDecoder(true);
if (options.getAttribute("supportvsdecoder").getValue().equals("no")) {
dispatcher.setSupportVSDecoder(false);
}
}
if (options.getAttribute("layoutscale") != null) {
String s = (options.getAttribute("layoutscale")).getValue();
for (int i = 1; i <= Scale.NUM_SCALES; i++) {
if (Scale.getShortScaleID(i).equals(s)) {
dispatcher.setScale(i);
}
}
}
if (options.getAttribute("usescalemeters") != null) {
dispatcher.setUseScaleMeters(true);
if (options.getAttribute("usescalemeters").getValue().equals("no")) {
dispatcher.setUseScaleMeters(false);
}
}
if (options.getAttribute("userosterentryinblock") != null) {
dispatcher.setRosterEntryInBlock(false);
if (options.getAttribute("userosterentryinblock").getValue().equals("yes")) {
dispatcher.setRosterEntryInBlock(true);
}
}
}
}
} else {
log.debug("No Dispatcher options file found at {}, using defaults", defaultFileName);
}
}
Aggregations