use of org.jdom2.Element in project JMRI by JMRI.
the class BlockManagerXml method store.
/**
* Store the contents of a BlockManager.
*
* @param o Object to store, of type BlockManager
* @return Element containing the complete info
*/
@Override
public Element store(Object o) {
Element blocks = new Element("blocks");
setStoreElementClass(blocks);
BlockManager tm = (BlockManager) o;
if (tm != null) {
java.util.Iterator<String> iter = tm.getSystemNameList().iterator();
// don't return an element if there are not blocks to include
if (!iter.hasNext()) {
return null;
}
blocks.addContent(new Element("defaultspeed").addContent(tm.getDefaultSpeed()));
// write out first set of blocks without contents
while (iter.hasNext()) {
try {
String sname = iter.next();
if (sname == null) {
log.error("System name null during store");
} else {
Block b = tm.getBySystemName(sname);
// the following null check is to catch a null pointer exception that sometimes was found to happen
if (b == null) {
log.error("Null block during store - sname = " + sname);
} else {
Element elem = new Element("block");
elem.addContent(new Element("systemName").addContent(sname));
// the following null check is to catch a null pointer exception that sometimes was found to happen
String uname = b.getUserName();
if ((uname != null) && (!uname.equals(""))) {
elem.addContent(new Element("userName").addContent(b.getUserName()));
}
if (log.isDebugEnabled()) {
log.debug("initial store Block " + sname);
}
// and put this element out
blocks.addContent(elem);
}
}
} catch (Exception e) {
log.error(e.toString());
}
}
// write out again with contents
iter = tm.getSystemNameList().iterator();
while (iter.hasNext()) {
String sname = iter.next();
if (sname == null) {
log.error("System name null during store skipped for this block");
} else {
Block b = tm.getBySystemName(sname);
// the following null check is to catch a null pointer exception that sometimes was found to happen
if (b == null) {
log.error("Null Block during store - second store skipped for this block - " + sname);
} else {
String uname = b.getUserName();
if (uname == null) {
uname = "";
}
Element elem = new Element("block");
elem.addContent(new Element("systemName").addContent(sname));
if (log.isDebugEnabled()) {
log.debug("second store Block " + sname + ":" + uname);
}
// store length and curvature attributes
elem.setAttribute("length", Float.toString(b.getLengthMm()));
elem.setAttribute("curve", Integer.toString(b.getCurvature()));
// store common parts
storeCommon(b, elem);
if ((!b.getBlockSpeed().equals("")) && !b.getBlockSpeed().contains("Global")) {
elem.addContent(new Element("speed").addContent(b.getBlockSpeed()));
}
String perm = "no";
if (b.getPermissiveWorking()) {
perm = "yes";
}
elem.addContent(new Element("permissive").addContent(perm));
// Add content. First, the sensor.
if (b.getNamedSensor() != null) {
elem.addContent(new Element("occupancysensor").addContent(b.getNamedSensor().getName()));
}
if (b.getDeniedBlocks().size() > 0) {
Element denied = new Element("deniedBlocks");
b.getDeniedBlocks().forEach((deniedBlock) -> {
denied.addContent(new Element("block").addContent(deniedBlock));
});
elem.addContent(denied);
}
// Now the Reporter
Reporter r = b.getReporter();
if (r != null) {
Element re = new Element("reporter");
re.setAttribute("systemName", r.getSystemName());
re.setAttribute("useCurrent", b.isReportingCurrent() ? "yes" : "no");
elem.addContent(re);
}
if (tm.isSavedPathInfo()) {
// then the paths
List<Path> paths = b.getPaths();
for (int i = 0; i < paths.size(); i++) {
addPath(elem, paths.get(i));
}
// and put this element out
}
blocks.addContent(elem);
}
}
}
}
return blocks;
}
use of org.jdom2.Element in project JMRI by JMRI.
the class BlockManagerXml method addPath.
void addPath(Element e, Path p) {
// for now, persist two directions and a bean setting
Element pe = new Element("path");
pe.setAttribute("todir", "" + p.getToBlockDirection());
pe.setAttribute("fromdir", "" + p.getFromBlockDirection());
if (p.getBlock() != null) {
pe.setAttribute("block", "" + p.getBlock().getSystemName());
}
List<BeanSetting> l = p.getSettings();
if (l != null) {
for (int i = 0; i < l.size(); i++) {
addBeanSetting(pe, l.get(i));
}
}
e.addContent(pe);
}
use of org.jdom2.Element in project JMRI by JMRI.
the class BlockManagerXml method loadBlock.
/**
* Utility method to load the individual Block objects.
*
* @param element Element containing one block
* @throws jmri.configurexml.JmriConfigureXmlException if element contains
* malformed or
* schematically invalid
* XMl
*/
public void loadBlock(Element element) throws JmriConfigureXmlException {
String sysName = getSystemName(element);
String userName = getUserName(element);
if (log.isDebugEnabled()) {
log.debug("defined Block: (" + sysName + ")(" + (userName == null ? "<null>" : userName) + ")");
}
Block block = InstanceManager.getDefault(jmri.BlockManager.class).getBlock(sysName);
if (block == null) {
// create it if doesn't exist
InstanceManager.getDefault(jmri.BlockManager.class).createNewBlock(sysName, userName);
block = InstanceManager.getDefault(jmri.BlockManager.class).getBlock(sysName);
}
if (block == null) {
log.error("Unable to load block with system name " + sysName + " and username of " + (userName == null ? "<null>" : userName));
return;
}
if (userName != null) {
block.setUserName(userName);
}
if (element.getAttribute("length") != null) {
// load length in millimeters
block.setLength(Float.parseFloat(element.getAttribute("length").getValue()));
}
if (element.getAttribute("curve") != null) {
// load curve attribute
block.setCurvature(Integer.parseInt((element.getAttribute("curve")).getValue()));
}
try {
block.setBlockSpeed("Global");
if (element.getChild("speed") != null) {
String speed = element.getChild("speed").getText();
if (speed != null && !speed.equals("") && !speed.contains("Global")) {
block.setBlockSpeed(speed);
}
}
} catch (jmri.JmriException ex) {
log.error(ex.toString());
}
if (element.getChild("permissive") != null) {
boolean permissive = false;
if (element.getChild("permissive").getText().equals("yes")) {
permissive = true;
}
block.setPermissiveWorking(permissive);
}
Element deniedBlocks = element.getChild("deniedBlocks");
if (deniedBlocks != null) {
List<Element> denyBlock = deniedBlocks.getChildren("block");
for (Element deny : denyBlock) {
block.addBlockDenyList(deny.getText());
}
}
// load common parts
loadCommon(block, element);
// load sensor if present
List<Element> sensors = element.getChildren("sensor");
if (sensors.size() > 1) {
log.error("More than one sensor present: " + sensors.size());
}
if (sensors.size() == 1) {
//Old method of saving sensors
if (sensors.get(0).getAttribute("systemName") != null) {
String name = sensors.get(0).getAttribute("systemName").getValue();
if (!name.equals("")) {
block.setSensor(name);
}
}
}
if (element.getChild("occupancysensor") != null) {
String name = element.getChild("occupancysensor").getText();
if (!name.equals("")) {
block.setSensor(name);
}
}
// load Reporter if present
List<Element> reporters = element.getChildren("reporter");
if (reporters.size() > 1) {
log.error("More than one reporter present: " + reporters.size());
}
if (reporters.size() == 1) {
// Reporter
String name = reporters.get(0).getAttribute("systemName").getValue();
try {
Reporter reporter = InstanceManager.getDefault(jmri.ReporterManager.class).provideReporter(name);
block.setReporter(reporter);
block.setReportingCurrent(reporters.get(0).getAttribute("useCurrent").getValue().equals("yes"));
} catch (IllegalArgumentException ex) {
log.warn("failed to create Reporter \"{}\" during Block load", name);
}
}
// load paths if present
List<Element> paths = element.getChildren("path");
int startSize = block.getPaths().size();
int loadCount = 0;
for (int i = 0; i < paths.size(); i++) {
Element path = paths.get(i);
if (loadPath(block, path)) {
loadCount++;
}
}
if (startSize > 0 && loadCount > 0) {
log.warn("Added " + loadCount++ + " paths to block " + sysName + " that already had " + startSize + " blocks.");
}
if (startSize + loadCount != block.getPaths().size()) {
log.error("Started with " + startSize + " paths in block " + sysName + ", added " + loadCount + " but final count is " + block.getPaths().size() + "; something not right.");
}
}
use of org.jdom2.Element in project JMRI by JMRI.
the class ConfigXmlManager method addUserPrefsStore.
protected void addUserPrefsStore(Element root) {
for (int i = 0; i < uplist.size(); i++) {
Object o = uplist.get(i);
Element e = elementFromObject(o);
if (e != null) {
root.addContent(e);
}
}
}
use of org.jdom2.Element in project JMRI by JMRI.
the class BlockManagerXml method loadBeanSetting.
/**
* Load BeanSetting into an existing Path.
*
* @param path Path to receive BeanSetting
* @param element Element containing beansetting information
*/
public void loadBeanSetting(Path path, Element element) {
int setting = 0;
try {
setting = element.getAttribute("setting").getIntValue();
} catch (org.jdom2.DataConversionException e) {
log.error("Could not parse beansetting attribute");
}
List<Element> turnouts = element.getChildren("turnout");
if (turnouts.size() != 1) {
log.error("invalid number of turnout element children");
}
String name = turnouts.get(0).getAttribute("systemName").getValue();
try {
Turnout t = InstanceManager.turnoutManagerInstance().provideTurnout(name);
BeanSetting bs = new BeanSetting(t, name, setting);
path.addSetting(bs);
} catch (IllegalArgumentException ex) {
log.warn("failed to create Turnout \"{}\" during Block load", name);
}
}
Aggregations