use of org.jdom2.Attribute 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.Attribute 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"));
}
}
}
use of org.jdom2.Attribute in project JMRI by JMRI.
the class AbstractLightManagerConfigXML method store.
/**
* Default implementation for storing the contents of a LightManager
*
* @param o Object to store, of type LightManager
* @return Element containing the complete info
*/
@Override
public Element store(Object o) {
Element lights = new Element("lights");
setStoreElementClass(lights);
LightManager tm = (LightManager) o;
if (tm != null) {
java.util.Iterator<String> iter = tm.getSystemNameList().iterator();
// don't return an element if there are not lights to include
if (!iter.hasNext()) {
return null;
}
// store the lights
while (iter.hasNext()) {
String sname = iter.next();
if (sname == null) {
log.error("System name null during store");
break;
}
log.debug("system name is " + sname);
Light lgt = tm.getBySystemName(sname);
Element elem = new Element("light");
elem.addContent(new Element("systemName").addContent(sname));
// store common parts
storeCommon(lgt, elem);
// write variable intensity attributes
elem.setAttribute("minIntensity", "" + lgt.getMinIntensity());
elem.setAttribute("maxIntensity", "" + lgt.getMaxIntensity());
// write transition attribute
elem.setAttribute("transitionTime", "" + lgt.getTransitionTime());
// save child lightcontrol entries
ArrayList<LightControl> lcList = lgt.getLightControlList();
Element lcElem = null;
for (int i = 0; i < lcList.size(); i++) {
LightControl lc = lcList.get(i);
if (lc != null) {
lcElem = new Element("lightcontrol");
int type = lc.getControlType();
lcElem.setAttribute("controlType", "" + type);
if (type == Light.SENSOR_CONTROL) {
lcElem.setAttribute("controlSensor", lc.getControlSensorName());
lcElem.setAttribute("sensorSense", "" + lc.getControlSensorSense());
} else if (type == Light.FAST_CLOCK_CONTROL) {
lcElem.setAttribute("fastClockOnHour", "" + lc.getFastClockOnHour());
lcElem.setAttribute("fastClockOnMin", "" + lc.getFastClockOnMin());
lcElem.setAttribute("fastClockOffHour", "" + lc.getFastClockOffHour());
lcElem.setAttribute("fastClockOffMin", "" + lc.getFastClockOffMin());
} else if (type == Light.TURNOUT_STATUS_CONTROL) {
lcElem.setAttribute("controlTurnout", lc.getControlTurnoutName());
lcElem.setAttribute("turnoutState", "" + lc.getControlTurnoutState());
} else if (type == Light.TIMED_ON_CONTROL) {
lcElem.setAttribute("timedControlSensor", lc.getControlTimedOnSensorName());
lcElem.setAttribute("duration", "" + lc.getTimedOnDuration());
}
if (type == Light.TWO_SENSOR_CONTROL) {
lcElem.setAttribute("controlSensor", lc.getControlSensorName());
lcElem.setAttribute("controlSensor2", lc.getControlSensor2Name());
lcElem.setAttribute("sensorSense", "" + lc.getControlSensorSense());
}
elem.addContent(lcElem);
}
}
lights.addContent(elem);
}
}
return lights;
}
use of org.jdom2.Attribute in project JMRI by JMRI.
the class JmriUserPreferencesManager method readPreferencesState.
private void readPreferencesState() {
Element element = this.readElement(CLASSPREFS_ELEMENT, CLASSPREFS_NAMESPACE);
if (element != null) {
element.getChildren("preferences").stream().forEach((preferences) -> {
String clazz = preferences.getAttributeValue("class");
log.debug("Reading class preferences for \"{}\"", clazz);
preferences.getChildren("multipleChoice").stream().forEach((mc) -> {
mc.getChildren("option").stream().forEach((option) -> {
int value = 0;
try {
option.getAttribute("value").getIntValue();
} catch (DataConversionException ex) {
log.error("failed to convert positional attribute");
}
this.setMultipleChoiceOption(clazz, option.getAttributeValue("item"), value);
});
});
preferences.getChildren("reminderPrompts").stream().forEach((rp) -> {
rp.getChildren("reminder").stream().forEach((reminder) -> {
log.debug("Setting preferences state \"true\" for \"{}\", \"{}\"", clazz, reminder.getText());
this.setPreferenceState(clazz, reminder.getText(), true);
});
});
});
}
}
use of org.jdom2.Attribute in project tika by apache.
the class NetCDFParser method parse.
/*
* (non-Javadoc)
*
* @see org.apache.tika.parser.Parser#parse(java.io.InputStream,
* org.xml.sax.ContentHandler, org.apache.tika.metadata.Metadata,
* org.apache.tika.parser.ParseContext)
*/
public void parse(InputStream stream, ContentHandler handler, Metadata metadata, ParseContext context) throws IOException, SAXException, TikaException {
TemporaryResources tmp = TikaInputStream.isTikaInputStream(stream) ? null : new TemporaryResources();
TikaInputStream tis = TikaInputStream.get(stream, tmp);
NetcdfFile ncFile = null;
try {
ncFile = NetcdfFile.open(tis.getFile().getAbsolutePath());
metadata.set("File-Type-Description", ncFile.getFileTypeDescription());
// first parse out the set of global attributes
for (Attribute attr : ncFile.getGlobalAttributes()) {
Property property = resolveMetadataKey(attr.getFullName());
if (attr.getDataType().isString()) {
metadata.add(property, attr.getStringValue());
} else if (attr.getDataType().isNumeric()) {
int value = attr.getNumericValue().intValue();
metadata.add(property, String.valueOf(value));
}
}
XHTMLContentHandler xhtml = new XHTMLContentHandler(handler, metadata);
xhtml.startDocument();
xhtml.newline();
xhtml.element("h1", "dimensions");
xhtml.startElement("ul");
xhtml.newline();
for (Dimension dim : ncFile.getDimensions()) {
xhtml.element("li", dim.getFullName() + " = " + dim.getLength());
}
xhtml.endElement("ul");
xhtml.element("h1", "variables");
xhtml.startElement("ul");
xhtml.newline();
for (Variable var : ncFile.getVariables()) {
xhtml.startElement("li");
xhtml.characters(var.getDataType() + " " + var.getNameAndDimensions());
xhtml.newline();
List<Attribute> attributes = var.getAttributes();
if (!attributes.isEmpty()) {
xhtml.startElement("ul");
for (Attribute element : attributes) {
xhtml.element("li", element.toString());
}
xhtml.endElement("ul");
}
xhtml.endElement("li");
}
xhtml.endElement("ul");
xhtml.endDocument();
} catch (IOException e) {
throw new TikaException("NetCDF parse error", e);
} finally {
if (ncFile != null) {
ncFile.close();
}
if (tmp != null) {
tmp.dispose();
}
}
}
Aggregations