use of org.jdom2.Parent in project JMRI by JMRI.
the class SectionManagerXml method loadSections.
/**
* Utility method to load the individual Section objects. If there's no
* additional info needed for a specific Section type, invoke this with the
* parent of the set of Section elements.
*
* @param sharedSections Element containing the Section elements to load.
* @param perNodeSections Per-node Element containing the Section elements
* to load.
*/
public void loadSections(Element sharedSections, Element perNodeSections) {
List<Element> sectionList = sharedSections.getChildren("section");
if (log.isDebugEnabled()) {
log.debug("Found " + sectionList.size() + " sections");
}
SectionManager tm = InstanceManager.getDefault(jmri.SectionManager.class);
for (int i = 0; i < sectionList.size(); i++) {
String sysName = getSystemName(sectionList.get(i));
String userName = getUserName(sectionList.get(i));
Section x = tm.createNewSection(sysName, userName);
if (x != null) {
// load common part
loadCommon(x, (sectionList.get(i)));
if (sectionList.get(i).getAttribute("creationtype") != null) {
String creationType = sectionList.get(i).getAttribute("creationtype").getValue();
if (creationType.equals("userdefined")) {
x.setSectionType(Section.USERDEFINED);
} else if (creationType.equals("signalmastlogic")) {
x.setSectionType(Section.SIGNALMASTLOGIC);
}
}
if (sectionList.get(i).getAttribute("fsensorname") != null) {
String forName = (sectionList.get(i)).getAttribute("fsensorname").getValue();
x.delayedSetForwardBlockingSensorName(forName);
}
if (sectionList.get(i).getAttribute("rsensorname") != null) {
String revName = sectionList.get(i).getAttribute("rsensorname").getValue();
x.delayedSetReverseBlockingSensorName(revName);
}
if (sectionList.get(i).getAttribute("fstopsensorname") != null) {
String forName = sectionList.get(i).getAttribute("fstopsensorname").getValue();
x.delayedSetForwardStoppingSensorName(forName);
}
if (sectionList.get(i).getAttribute("rstopsensorname") != null) {
String revName = sectionList.get(i).getAttribute("rstopsensorname").getValue();
x.delayedSetReverseStoppingSensorName(revName);
}
// load block entry children
List<Element> sectionBlockList = sectionList.get(i).getChildren("blockentry");
for (int n = 0; n < sectionBlockList.size(); n++) {
Element elem = sectionBlockList.get(n);
x.delayedAddBlock(elem.getAttribute("sName").getValue());
// insert code here to verify sequence number if needed in the future
}
// load entry point children
List<Element> sectionEntryPointList = sectionList.get(i).getChildren("entrypoint");
for (int n = 0; n < sectionEntryPointList.size(); n++) {
Element elem = sectionEntryPointList.get(n);
String blockName = elem.getAttribute("toblock").getValue();
String fromBlockName = elem.getAttribute("fromblock").getValue();
String fromBlockDirection = "";
if (elem.getAttribute("fromblockdirection") != null) {
fromBlockDirection = elem.getAttribute("fromblockdirection").getValue();
}
EntryPoint ep = new EntryPoint(blockName, fromBlockName, fromBlockDirection);
//if (ep!=null) {
try {
ep.setDirection(elem.getAttribute("direction").getIntValue());
} catch (Exception e) {
log.error("Data Conversion Exception when loading direction of entry point - " + e);
}
boolean fixed = true;
if (elem.getAttribute("fixed").getValue().equals("no")) {
fixed = false;
}
ep.setFixed(fixed);
if (ep.isForwardType()) {
x.addToForwardList(ep);
} else if (ep.isReverseType()) {
x.addToReverseList(ep);
}
//}
}
}
}
}
use of org.jdom2.Parent in project JMRI by JMRI.
the class LayoutBlockManagerXml method loadLayoutBlocks.
/**
* Utility method to load the individual LayoutBlock objects. If there's no
* additional info needed for a specific layoutblock type, invoke this with
* the parent of the set of layoutblock elements.
*
* @param layoutblocks Element containing the layoutblock elements to load.
*/
public void loadLayoutBlocks(Element layoutblocks) {
LayoutBlockManager tm = InstanceManager.getDefault(LayoutBlockManager.class);
if (layoutblocks.getAttribute("blockrouting") != null) {
if (layoutblocks.getAttribute("blockrouting").getValue().equals("yes")) {
tm.enableAdvancedRouting(true);
}
}
if (layoutblocks.getAttribute("routingStablisedSensor") != null) {
try {
tm.setStabilisedSensor(layoutblocks.getAttribute("routingStablisedSensor").getValue());
} catch (jmri.JmriException e) {
}
}
List<Element> layoutblockList = layoutblocks.getChildren("layoutblock");
if (log.isDebugEnabled()) {
log.debug("Found " + layoutblockList.size() + " layoutblocks");
}
for (int i = 0; i < layoutblockList.size(); i++) {
String sysName = getSystemName(layoutblockList.get(i));
if (sysName == null) {
log.warn("unexpected null in systemName " + ((layoutblockList.get(i))) + " " + ((layoutblockList.get(i))).getAttributes());
break;
}
String userName = getUserName(layoutblockList.get(i));
LayoutBlock b = tm.createNewLayoutBlock(sysName, userName);
// load common parts
loadCommon(b, layoutblockList.get(i));
if (b != null) {
// set attributes
Color color = ColorUtil.stringToColor(((layoutblockList.get(i))).getAttribute("trackcolor").getValue());
b.setBlockTrackColor(color);
color = ColorUtil.stringToColor(((layoutblockList.get(i))).getAttribute("occupiedcolor").getValue());
b.setBlockOccupiedColor(color);
Attribute a = ((layoutblockList.get(i))).getAttribute("extracolor");
if (a != null) {
b.setBlockExtraColor(ColorUtil.stringToColor(a.getValue()));
}
a = ((layoutblockList.get(i))).getAttribute("occupancysensor");
if (a != null) {
b.setOccupancySensorName(a.getValue());
}
a = ((layoutblockList.get(i))).getAttribute("memory");
if (a != null) {
b.setMemoryName(a.getValue());
}
a = ((layoutblockList.get(i))).getAttribute("occupancysensorsense");
int sense = Sensor.ACTIVE;
try {
sense = ((layoutblockList.get(i))).getAttribute("occupiedsense").getIntValue();
} catch (org.jdom2.DataConversionException e) {
log.error("failed to convert occupiedsense attribute");
}
b.setOccupiedSense(sense);
if (((layoutblockList.get(i))).getChild("metric") != null) {
String stMetric = ((layoutblockList.get(i))).getChild("metric").getText();
try {
b.setBlockMetric(Integer.valueOf(stMetric));
} catch (java.lang.NumberFormatException e) {
log.error("failed to convert metric attribute for block " + b.getDisplayName());
}
}
}
}
}
use of org.jdom2.Parent in project JMRI by JMRI.
the class DefaultLogixManagerXml method loadLogixs.
/**
* Utility method to load the individual Logix objects. If there's no
* additional info needed for a specific logix type, invoke this with the
* parent of the set of Logix elements.
*
* @param logixs Element containing the Logix elements to load.
*/
public void loadLogixs(Element logixs) {
List<Element> logixList = logixs.getChildren("logix");
if (log.isDebugEnabled()) {
log.debug("Found " + logixList.size() + " logixs");
}
LogixManager tm = InstanceManager.getDefault(jmri.LogixManager.class);
for (int i = 0; i < logixList.size(); i++) {
String sysName = getSystemName(logixList.get(i));
if (sysName == null) {
log.warn("unexpected null in systemName " + logixList.get(i));
break;
}
String userName = getUserName(logixList.get(i));
String yesno = "";
if (logixList.get(i).getAttribute("enabled") != null) {
yesno = logixList.get(i).getAttribute("enabled").getValue();
}
if (log.isDebugEnabled()) {
log.debug("create logix: (" + sysName + ")(" + (userName == null ? "<null>" : userName) + ")");
}
Logix x = tm.createNewLogix(sysName, userName);
if (x != null) {
// load common part
loadCommon(x, logixList.get(i));
// set enabled/disabled if attribute was present
if ((yesno != null) && (!yesno.equals(""))) {
if (yesno.equals("yes")) {
x.setEnabled(true);
} else if (yesno.equals("no")) {
x.setEnabled(false);
}
}
// load conditionals, if there are any
List<Element> logixConditionalList = logixList.get(i).getChildren("logixConditional");
if (logixConditionalList.size() > 0) {
// add conditionals
for (int n = 0; n < logixConditionalList.size(); n++) {
if (logixConditionalList.get(n).getAttribute("systemName") == null) {
log.warn("unexpected null in systemName " + logixConditionalList.get(n) + " " + logixConditionalList.get(n).getAttributes());
break;
}
String cSysName = logixConditionalList.get(n).getAttribute("systemName").getValue();
int cOrder = Integer.parseInt(logixConditionalList.get(n).getAttribute("order").getValue());
// add conditional to logix
x.addConditional(cSysName, cOrder);
}
}
}
}
}
use of org.jdom2.Parent in project JMRI by JMRI.
the class AbstractTurnoutManagerConfigXML method loadTurnouts.
/**
* Utility method to load the individual Turnout objects. If there's no
* additional info needed for a specific turnout type, invoke this with the
* parent of the set of Turnout elements.
*
* @param shared Element containing the Turnout elements to load.
* @param perNode Element containing per-node Turnout data.
* @return true if succeeded
*/
@SuppressWarnings("unchecked")
public boolean loadTurnouts(Element shared, Element perNode) {
boolean result = true;
List<Element> operationList = shared.getChildren("operations");
if (operationList.size() > 1) {
log.warn("unexpected extra elements found in turnout operations list");
result = false;
}
if (operationList.size() > 0) {
TurnoutOperationManagerXml tomx = new TurnoutOperationManagerXml();
tomx.load(operationList.get(0), null);
}
List<Element> turnoutList = shared.getChildren("turnout");
if (log.isDebugEnabled()) {
log.debug("Found " + turnoutList.size() + " turnouts");
}
TurnoutManager tm = InstanceManager.turnoutManagerInstance();
try {
if (shared.getChild("defaultclosedspeed") != null) {
String closedSpeed = shared.getChild("defaultclosedspeed").getText();
if (closedSpeed != null && !closedSpeed.equals("")) {
tm.setDefaultClosedSpeed(closedSpeed);
}
}
} catch (jmri.JmriException ex) {
log.error(ex.toString());
}
try {
if (shared.getChild("defaultthrownspeed") != null) {
String thrownSpeed = shared.getChild("defaultthrownspeed").getText();
if (thrownSpeed != null && !thrownSpeed.equals("")) {
tm.setDefaultThrownSpeed(thrownSpeed);
}
}
} catch (jmri.JmriException ex) {
log.error(ex.toString());
}
for (int i = 0; i < turnoutList.size(); i++) {
Element elem = turnoutList.get(i);
String sysName = getSystemName(elem);
if (sysName == null) {
log.error("unexpected null in systemName " + elem);
result = false;
break;
}
String userName = getUserName(elem);
checkNameNormalization(sysName, userName, tm);
if (log.isDebugEnabled()) {
log.debug("create turnout: (" + sysName + ")(" + (userName == null ? "<null>" : userName) + ")");
}
Turnout t = tm.getBySystemName(sysName);
if (t == null) {
t = tm.newTurnout(sysName, userName);
//Nothing is logged in the console window as the newTurnoutFunction already does this.
} else if (userName != null) {
t.setUserName(userName);
}
// Load common parts
loadCommon(t, elem);
// now add feedback if needed
Attribute a;
a = elem.getAttribute("feedback");
if (a != null) {
try {
t.setFeedbackMode(a.getValue());
} catch (IllegalArgumentException e) {
log.error("Can not set feedback mode: '" + a.getValue() + "' for turnout: '" + sysName + "' user name: '" + (userName == null ? "" : userName) + "'");
result = false;
}
}
a = elem.getAttribute("sensor1");
if (a != null) {
try {
t.provideFirstFeedbackSensor(a.getValue());
} catch (jmri.JmriException e) {
result = false;
}
}
a = elem.getAttribute("sensor2");
if (a != null) {
try {
t.provideSecondFeedbackSensor(a.getValue());
} catch (jmri.JmriException e) {
result = false;
}
}
// check for turnout inverted
t.setInverted(getAttributeBool(elem, "inverted", false));
// check for turnout decoder
a = turnoutList.get(i).getAttribute("decoder");
if (a != null) {
t.setDecoderName(a.getValue());
}
// check for turnout lock mode
a = turnoutList.get(i).getAttribute("lockMode");
if (a != null) {
if (a.getValue().equals("both")) {
t.enableLockOperation(Turnout.CABLOCKOUT + Turnout.PUSHBUTTONLOCKOUT, true);
}
if (a.getValue().equals("cab")) {
t.enableLockOperation(Turnout.CABLOCKOUT, true);
t.enableLockOperation(Turnout.PUSHBUTTONLOCKOUT, false);
}
if (a.getValue().equals("pushbutton")) {
t.enableLockOperation(Turnout.PUSHBUTTONLOCKOUT, true);
t.enableLockOperation(Turnout.CABLOCKOUT, false);
}
}
// check for turnout locked
a = turnoutList.get(i).getAttribute("locked");
if (a != null) {
t.setLocked(Turnout.CABLOCKOUT + Turnout.PUSHBUTTONLOCKOUT, a.getValue().equals("true"));
}
// number of bits, if present - if not, defaults to 1
a = turnoutList.get(i).getAttribute("numBits");
if (a == null) {
t.setNumberOutputBits(1);
} else {
int iNum = Integer.parseInt(a.getValue());
if ((iNum == 1) || (iNum == 2)) {
t.setNumberOutputBits(iNum);
} else {
log.warn("illegal number of output bits for control of turnout " + sysName);
t.setNumberOutputBits(1);
result = false;
}
}
// control type, if present - if not, defaults to 0
a = turnoutList.get(i).getAttribute("controlType");
if (a == null) {
t.setControlType(0);
} else {
int iType = Integer.parseInt(a.getValue());
if (iType >= 0) {
t.setControlType(iType);
} else {
log.warn("illegal control type for control of turnout " + sysName);
t.setControlType(0);
result = false;
}
}
// operation stuff
List<Element> myOpList = turnoutList.get(i).getChildren("operation");
if (myOpList.size() > 0) {
if (myOpList.size() > 1) {
log.warn("unexpected extra elements found in turnout-specific operations");
result = false;
}
TurnoutOperation toper = TurnoutOperationXml.loadOperation(myOpList.get(0));
t.setTurnoutOperation(toper);
} else {
a = turnoutList.get(i).getAttribute("automate");
if (a != null) {
String str = a.getValue();
if (str.equals("Off")) {
t.setInhibitOperation(true);
} else if (!str.equals("Default")) {
t.setInhibitOperation(false);
TurnoutOperation toper = TurnoutOperationManager.getInstance().getOperation(str);
t.setTurnoutOperation(toper);
} else {
t.setInhibitOperation(false);
}
}
}
// set initial state from sensor feedback if appropriate
t.setInitialKnownStateFromFeedback();
try {
t.setDivergingSpeed("Global");
if (elem.getChild("divergingSpeed") != null) {
String speed = elem.getChild("divergingSpeed").getText();
if (speed != null && !speed.equals("") && !speed.contains("Global")) {
t.setDivergingSpeed(speed);
}
}
} catch (jmri.JmriException ex) {
log.error(ex.toString());
}
try {
t.setStraightSpeed("Global");
if (elem.getChild("straightSpeed") != null) {
String speed = elem.getChild("straightSpeed").getText();
if (speed != null && !speed.equals("") && !speed.contains("Global")) {
t.setStraightSpeed(speed);
}
}
} catch (jmri.JmriException ex) {
log.error(ex.toString());
}
}
return result;
}
use of org.jdom2.Parent in project JMRI by JMRI.
the class AbstractAudioManagerConfigXML method loadAudio.
/**
* Utility method to load the individual Audio objects. If there's no
* additional info needed for a specific Audio type, invoke this with the
* parent of the set of Audio elements.
*
* @param audio Element containing the Audio elements to load.
*/
@SuppressWarnings("unchecked")
public void loadAudio(Element audio) {
AudioManager am = InstanceManager.getDefault(jmri.AudioManager.class);
// Count number of loaded Audio objects
int loadedObjects = 0;
// Load buffers first
List<Element> audioList = audio.getChildren("audiobuffer");
if (log.isDebugEnabled()) {
log.debug("Found " + audioList.size() + " Audio Buffer objects");
}
for (int i = 0; i < audioList.size(); i++) {
Element e = audioList.get(i);
String sysName = getSystemName(e);
if (sysName == null) {
log.warn("unexpected null in systemName " + (e) + " " + (e).getAttributes());
break;
}
String userName = getUserName(e);
if (log.isDebugEnabled()) {
log.debug("create Audio: (" + sysName + ")(" + (userName == null ? "<null>" : userName) + ")");
}
try {
AudioBuffer ab = (AudioBuffer) am.newAudio(sysName, userName);
// load common parts
loadCommon(ab, e);
// load sub-type specific parts
// Transient objects for reading child elements
Element ce;
String value;
if ((ce = e.getChild("url")) != null) {
ab.setURL(ce.getValue());
}
if ((ce = e.getChild("looppoint")) != null) {
if ((value = ce.getAttributeValue("start")) != null) {
ab.setStartLoopPoint(Integer.parseInt(value));
}
if ((value = ce.getAttributeValue("end")) != null) {
ab.setEndLoopPoint(Integer.parseInt(value));
}
}
if ((ce = e.getChild("streamed")) != null) {
ab.setStreamed(ce.getValue().equals("yes"));
}
} catch (AudioException ex) {
log.error("Error loading AudioBuffer (" + sysName + "): " + ex);
}
}
loadedObjects += audioList.size();
// Now load sources
audioList = audio.getChildren("audiosource");
if (log.isDebugEnabled()) {
log.debug("Found " + audioList.size() + " Audio Source objects");
}
for (int i = 0; i < audioList.size(); i++) {
Element e = audioList.get(i);
String sysName = getSystemName(e);
if (sysName == null) {
log.warn("unexpected null in systemName " + (e) + " " + (e).getAttributes());
break;
}
String userName = getUserName(e);
if (log.isDebugEnabled()) {
log.debug("create Audio: (" + sysName + ")(" + (userName == null ? "<null>" : userName) + ")");
}
try {
AudioSource as = (AudioSource) am.newAudio(sysName, userName);
// load common parts
loadCommon(as, e);
// load sub-type specific parts
// Transient objects for reading child elements
Element ce;
String value;
if ((ce = e.getChild("position")) != null) {
as.setPosition(new Vector3f(Float.parseFloat(ce.getAttributeValue("x")), Float.parseFloat(ce.getAttributeValue("y")), Float.parseFloat(ce.getAttributeValue("z"))));
}
if ((ce = e.getChild("velocity")) != null) {
as.setVelocity(new Vector3f(Float.parseFloat(ce.getAttributeValue("x")), Float.parseFloat(ce.getAttributeValue("y")), Float.parseFloat(ce.getAttributeValue("z"))));
}
if ((ce = e.getChild("assignedbuffer")) != null) {
if (ce.getValue().length() != 0 && !ce.getValue().equals("null")) {
as.setAssignedBuffer(ce.getValue());
}
}
if ((ce = e.getChild("gain")) != null && ce.getValue().length() != 0) {
as.setGain(Float.parseFloat(ce.getValue()));
}
if ((ce = e.getChild("pitch")) != null && ce.getValue().length() != 0) {
as.setPitch(Float.parseFloat(ce.getValue()));
}
if ((ce = e.getChild("distances")) != null) {
if ((value = ce.getAttributeValue("ref")) != null) {
as.setReferenceDistance(Float.parseFloat(value));
}
if ((value = ce.getAttributeValue("max")) != null) {
as.setMaximumDistance(Float.parseFloat(value));
}
}
if ((ce = e.getChild("loops")) != null) {
if ((value = ce.getAttributeValue("min")) != null) {
as.setMinLoops(Integer.parseInt(value));
}
if ((value = ce.getAttributeValue("max")) != null) {
as.setMaxLoops(Integer.parseInt(value));
}
// if ((value = ce.getAttributeValue("mindelay"))!=null)
// as.setMinLoopDelay(Integer.parseInt(value));
// if ((value = ce.getAttributeValue("maxdelay"))!=null)
// as.setMaxLoopDelay(Integer.parseInt(value));
}
if ((ce = e.getChild("fadetimes")) != null) {
if ((value = ce.getAttributeValue("in")) != null) {
as.setFadeIn(Integer.parseInt(value));
}
if ((value = ce.getAttributeValue("out")) != null) {
as.setFadeOut(Integer.parseInt(value));
}
}
if ((ce = e.getChild("dopplerfactor")) != null && ce.getValue().length() != 0) {
as.setDopplerFactor(Float.parseFloat(ce.getValue()));
}
if ((ce = e.getChild("positionrelative")) != null) {
as.setPositionRelative(ce.getValue().equals("yes"));
}
} catch (AudioException ex) {
log.error("Error loading AudioSource (" + sysName + "): " + ex);
}
}
loadedObjects += audioList.size();
// Finally, load Listeners if needed
if (loadedObjects > 0) {
audioList = audio.getChildren("audiolistener");
if (log.isDebugEnabled()) {
log.debug("Found " + audioList.size() + " Audio Listener objects");
}
for (int i = 0; i < audioList.size(); i++) {
Element e = audioList.get(i);
String sysName = getSystemName(e);
if (sysName == null) {
log.warn("unexpected null in systemName " + (e) + " " + (e).getAttributes());
break;
}
String userName = getUserName(e);
if (log.isDebugEnabled()) {
log.debug("create Audio: (" + sysName + ")(" + (userName == null ? "<null>" : userName) + ")");
}
try {
AudioListener al = (AudioListener) am.newAudio(sysName, userName);
// load common parts
loadCommon(al, e);
// load sub-type specific parts
// Transient object for reading child elements
Element ce;
if ((ce = e.getChild("position")) != null) {
al.setPosition(new Vector3f(Float.parseFloat(ce.getAttributeValue("x")), Float.parseFloat(ce.getAttributeValue("y")), Float.parseFloat(ce.getAttributeValue("z"))));
}
if ((ce = e.getChild("velocity")) != null) {
al.setVelocity(new Vector3f(Float.parseFloat(ce.getAttributeValue("x")), Float.parseFloat(ce.getAttributeValue("y")), Float.parseFloat(ce.getAttributeValue("z"))));
}
if ((ce = e.getChild("orientation")) != null) {
al.setOrientation(new Vector3f(Float.parseFloat(ce.getAttributeValue("atX")), Float.parseFloat(ce.getAttributeValue("atY")), Float.parseFloat(ce.getAttributeValue("atZ"))), new Vector3f(Float.parseFloat(ce.getAttributeValue("upX")), Float.parseFloat(ce.getAttributeValue("upY")), Float.parseFloat(ce.getAttributeValue("upZ"))));
}
if ((ce = e.getChild("gain")) != null) {
al.setGain(Float.parseFloat(ce.getValue()));
}
if ((ce = e.getChild("metersperunit")) != null) {
al.setMetersPerUnit(Float.parseFloat((ce.getValue())));
}
} catch (AudioException ex) {
log.error("Error loading AudioListener (" + sysName + "): " + ex);
}
}
Attribute a;
if ((a = audio.getAttribute("distanceattenuated")) != null) {
am.getActiveAudioFactory().setDistanceAttenuated(a.getValue().equals("yes"));
}
}
}
Aggregations