use of org.jdom2.output.Format in project mycore by MyCoRe-Org.
the class MCRQuery method buildXML.
/**
* Builds a XML representation of the query
*
* @return a XML document containing all query parameters
*/
public synchronized Document buildXML() {
if (doc == null) {
Element query = new Element("query");
query.setAttribute("maxResults", String.valueOf(maxResults));
if (sortBy != null && sortBy.size() > 0) {
Element sortByElem = new Element("sortBy");
query.addContent(sortByElem);
for (MCRSortBy sb : sortBy) {
Element ref = new Element("field");
ref.setAttribute("name", sb.getFieldName());
ref.setAttribute("order", sb.getSortOrder() ? "ascending" : "descending");
sortByElem.addContent(ref);
}
}
if (returnFields != null && returnFields.size() > 0) {
Element returns = new Element("returnFields");
returns.setText(returnFields.stream().collect(Collectors.joining(",")));
query.addContent(returns);
}
Element conditions = new Element("conditions");
query.addContent(conditions);
conditions.setAttribute("format", "xml");
conditions.addContent(cond.toXML());
doc = new Document(query);
}
return doc;
}
use of org.jdom2.output.Format in project JMRI by JMRI.
the class PaneProgPaneTest method setupDoc.
// provide a test document in the above static variables
void setupDoc() {
// create a JDOM tree with just some elements
root = new Element("programmer-config");
doc = new Document(root);
doc.setDocType(new DocType("programmer-config", "programmer-config.dtd"));
// add some elements
root.addContent(new Element("programmer").addContent(pane1 = new Element("pane").setAttribute("name", "Basic").addContent(new Element("column").addContent(new Element("display").setAttribute("item", "Primary Address")).addContent(new Element("display").setAttribute("item", "Start voltage")).addContent(new Element("display").setAttribute("item", "Normal direction of motion"))).addContent(new Element("column").addContent(new Element("display").setAttribute("item", "Address")).addContent(new Element("display").setAttribute("item", "Normal direction of motion")).addContent(new Element("display").setAttribute("item", "Normal direction of motion").setAttribute("format", "checkbox")).addContent(new Element("display").setAttribute("item", "Normal direction of motion").setAttribute("format", "radiobuttons")))).addContent(pane2 = new Element("pane").setAttribute("name", "CV").addContent(new Element("column").addContent(new Element("cvtable")))).addContent(pane3 = new Element("pane").setAttribute("name", "Other").addContent(new Element("column").addContent(new Element("display").setAttribute("item", "Address")).addContent(new Element("display").setAttribute("item", "Normal direction of motion")))));
// end of adding contents
log.debug("setupDoc complete");
}
use of org.jdom2.output.Format in project JMRI by JMRI.
the class CarManager method store.
/**
* Create an XML element to represent this Entry. This member has to remain
* synchronized with the detailed DTD in operations-cars.dtd.
* @param root The common Element for operations-cars.dtd.
*/
public void store(Element root) {
// nothing to save under
root.addContent(new Element(Xml.OPTIONS));
// options
Element values;
List<String> names = getKernelNameList();
if (Control.backwardCompatible) {
root.addContent(values = new Element(Xml.KERNELS));
for (String name : names) {
// NOI18N
String kernelNames = name + "%%";
values.addContent(kernelNames);
}
}
// new format using elements
Element kernels = new Element(Xml.NEW_KERNELS);
for (String name : names) {
Element kernel = new Element(Xml.KERNEL);
kernel.setAttribute(new Attribute(Xml.NAME, name));
kernels.addContent(kernel);
}
root.addContent(kernels);
root.addContent(values = new Element(Xml.CARS));
// add entries
List<RollingStock> carList = getByIdList();
for (RollingStock rs : carList) {
Car car = (Car) rs;
values.addContent(car.store());
}
}
use of org.jdom2.output.Format in project JMRI by JMRI.
the class OptionsFile method writeDispatcherOptions.
/*
* Writes out Dispatcher options to a file in the user's preferences directory
*/
public void writeDispatcherOptions(DispatcherFrame f) throws java.io.IOException {
log.debug("Saving Dispatcher options to file {}", defaultFileName);
dispatcher = f;
root = new Element("dispatcheroptions");
doc = newDocument(root, dtdLocation + "dispatcher-options.dtd");
// add XSLT processing instruction
// <?xml-stylesheet type="text/xsl" href="XSLT/block-values.xsl"?>
java.util.Map<String, String> m = new java.util.HashMap<String, String>();
m.put("type", "text/xsl");
m.put("href", xsltLocation + "dispatcheroptions.xsl");
org.jdom2.ProcessingInstruction p = new org.jdom2.ProcessingInstruction("xml-stylesheet", m);
doc.addContent(0, p);
// save Dispatcher Options in xml format
Element options = new Element("options");
LayoutEditor le = dispatcher.getLayoutEditor();
if (le != null) {
options.setAttribute("lename", le.getTitle());
}
options.setAttribute("useconnectivity", "" + (dispatcher.getUseConnectivity() ? "yes" : "no"));
options.setAttribute("trainsfromroster", "" + (dispatcher.getTrainsFromRoster() ? "yes" : "no"));
options.setAttribute("trainsfromtrains", "" + (dispatcher.getTrainsFromTrains() ? "yes" : "no"));
options.setAttribute("trainsfromuser", "" + (dispatcher.getTrainsFromUser() ? "yes" : "no"));
options.setAttribute("autoallocate", "" + (dispatcher.getAutoAllocate() ? "yes" : "no"));
options.setAttribute("autoturnouts", "" + (dispatcher.getAutoTurnouts() ? "yes" : "no"));
options.setAttribute("trustknownturnouts", "" + (dispatcher.getTrustKnownTurnouts() ? "yes" : "no"));
options.setAttribute("minthrottleinterval", "" + (dispatcher.getMinThrottleInterval()));
options.setAttribute("fullramptime", "" + (dispatcher.getFullRampTime()));
options.setAttribute("hasoccupancydetection", "" + (dispatcher.getHasOccupancyDetection() ? "yes" : "no"));
options.setAttribute("shortactivetrainnames", "" + (dispatcher.getShortActiveTrainNames() ? "yes" : "no"));
options.setAttribute("shortnameinblock", "" + (dispatcher.getShortNameInBlock() ? "yes" : "no"));
options.setAttribute("extracolorforallocated", "" + (dispatcher.getExtraColorForAllocated() ? "yes" : "no"));
options.setAttribute("nameinallocatedblock", "" + (dispatcher.getNameInAllocatedBlock() ? "yes" : "no"));
options.setAttribute("supportvsdecoder", "" + (dispatcher.getSupportVSDecoder() ? "yes" : "no"));
options.setAttribute("layoutscale", Scale.getShortScaleID(dispatcher.getScale()));
options.setAttribute("usescalemeters", "" + (dispatcher.getUseScaleMeters() ? "yes" : "no"));
options.setAttribute("userosterentryinblock", "" + (dispatcher.getRosterEntryInBlock() ? "yes" : "no"));
if (dispatcher.getSignalType() == 0x00) {
options.setAttribute("usesignaltype", "signalhead");
} else {
options.setAttribute("usesignaltype", "signalmast");
}
root.addContent(options);
// write out the file
try {
if (!checkFile(defaultFileName)) {
// file does not exist, create it
File file = new File(defaultFileName);
if (// create new file and check result
!file.createNewFile()) {
log.error("createNewFile failed");
}
}
// write content to file
writeXML(findFile(defaultFileName), doc);
} catch (java.io.IOException ioe) {
log.error("IO Exception " + ioe);
throw (ioe);
}
}
use of org.jdom2.output.Format in project JMRI by JMRI.
the class DefaultRouteManagerXml method loadRoutes.
/**
* Utility method to load the individual Route objects. If there's no
* additional info needed for a specific route type, invoke this with the
* parent of the set of Route elements.
*
* @param routes Element containing the Route elements to load.
*/
public void loadRoutes(Element routes) {
List<Element> routeList = routes.getChildren("route");
if (log.isDebugEnabled()) {
log.debug("Found " + routeList.size() + " routes");
}
RouteManager tm = InstanceManager.getDefault(jmri.RouteManager.class);
for (int i = 0; i < routeList.size(); i++) {
String sysName = getSystemName(routeList.get(i));
if (sysName == null) {
log.warn("unexpected null in systemName " + routeList.get(i));
break;
}
String userName = getUserName(routeList.get(i));
String cTurnout = null;
String cTurnoutState = null;
String addedDelayTxt = null;
String routeLockedTxt = null;
String cLockTurnout = null;
String cLockTurnoutState = null;
int addedDelay = 0;
if (routeList.get(i).getAttribute("controlTurnout") != null) {
cTurnout = routeList.get(i).getAttribute("controlTurnout").getValue();
}
if (routeList.get(i).getAttribute("controlTurnoutState") != null) {
cTurnoutState = routeList.get(i).getAttribute("controlTurnoutState").getValue();
}
if (routeList.get(i).getAttribute("controlLockTurnout") != null) {
cLockTurnout = routeList.get(i).getAttribute("controlLockTurnout").getValue();
}
if (routeList.get(i).getAttribute("controlLockTurnoutState") != null) {
cLockTurnoutState = routeList.get(i).getAttribute("controlLockTurnoutState").getValue();
}
if (routeList.get(i).getAttribute("addedDelay") != null) {
addedDelayTxt = routeList.get(i).getAttribute("addedDelay").getValue();
if (addedDelayTxt != null) {
addedDelay = Integer.parseInt(addedDelayTxt);
}
}
if (routeList.get(i).getAttribute("routeLocked") != null) {
routeLockedTxt = routeList.get(i).getAttribute("routeLocked").getValue();
}
if (log.isDebugEnabled()) {
log.debug("create route: (" + sysName + ")(" + (userName == null ? "<null>" : userName) + ")");
}
Route r;
try {
r = tm.provideRoute(sysName, userName);
} catch (IllegalArgumentException ex) {
log.error("failed to create Route: " + sysName);
return;
}
// load common parts
loadCommon(r, routeList.get(i));
// add control turnout if there is one
if (cTurnout != null) {
r.setControlTurnout(cTurnout);
if (cTurnoutState != null) {
if (cTurnoutState.equals("THROWN")) {
r.setControlTurnoutState(Route.ONTHROWN);
} else if (cTurnoutState.equals("CHANGE")) {
r.setControlTurnoutState(Route.ONCHANGE);
} else if (cTurnoutState.equals("VETOCLOSED")) {
r.setControlTurnoutState(Route.VETOCLOSED);
} else if (cTurnoutState.equals("VETOTHROWN")) {
r.setControlTurnoutState(Route.VETOTHROWN);
} else {
r.setControlTurnoutState(Route.ONCLOSED);
}
} else {
log.error("cTurnoutState was null!");
}
}
// set added delay
r.setRouteCommandDelay(addedDelay);
// determine if route locked
if (routeLockedTxt != null && routeLockedTxt.equals("True")) {
r.setLocked(true);
}
//add lock control turout if there is one
if (cLockTurnout != null) {
r.setLockControlTurnout(cLockTurnout);
if (cLockTurnoutState != null) {
if (cLockTurnoutState.equals("THROWN")) {
r.setLockControlTurnoutState(Route.ONTHROWN);
} else if (cLockTurnoutState.equals("CHANGE")) {
r.setLockControlTurnoutState(Route.ONCHANGE);
} else {
r.setLockControlTurnoutState(Route.ONCLOSED);
}
} else {
log.error("cLockTurnoutState was null!");
}
}
// load output turnouts if there are any - old format first (1.7.6 and before)
List<Element> routeTurnoutList = routeList.get(i).getChildren("routeTurnout");
if (routeTurnoutList.size() > 0) {
// This route has turnouts
for (int k = 0; k < routeTurnoutList.size(); k++) {
if (((routeTurnoutList.get(k))).getAttribute("systemName") == null) {
log.warn("unexpected null in systemName " + ((routeTurnoutList.get(k))) + " " + ((routeTurnoutList.get(k))).getAttributes());
break;
}
String tSysName = ((routeTurnoutList.get(k))).getAttribute("systemName").getValue();
String rState = ((routeTurnoutList.get(k))).getAttribute("state").getValue();
int tSetState = Turnout.CLOSED;
if (rState.equals("THROWN")) {
tSetState = Turnout.THROWN;
} else if (rState.equals("TOGGLE")) {
tSetState = Route.TOGGLE;
}
// Add turnout to route
r.addOutputTurnout(tSysName, tSetState);
}
}
// load output turnouts if there are any - new format
routeTurnoutList = routeList.get(i).getChildren("routeOutputTurnout");
if (routeTurnoutList.size() > 0) {
// This route has turnouts
for (int k = 0; k < routeTurnoutList.size(); k++) {
if (routeTurnoutList.get(k).getAttribute("systemName") == null) {
log.warn("unexpected null in systemName " + routeTurnoutList.get(k) + " " + routeTurnoutList.get(k).getAttributes());
break;
}
String tSysName = routeTurnoutList.get(k).getAttribute("systemName").getValue();
String rState = routeTurnoutList.get(k).getAttribute("state").getValue();
int tSetState = Turnout.CLOSED;
if (rState.equals("THROWN")) {
tSetState = Turnout.THROWN;
} else if (rState.equals("TOGGLE")) {
tSetState = Route.TOGGLE;
}
// we will not re add the turnout.
if (!r.isOutputTurnoutIncluded(tSysName)) {
// Add turnout to route
r.addOutputTurnout(tSysName, tSetState);
// determine if turnout should be locked
Turnout t = r.getOutputTurnout(k);
if (r.getLocked()) {
t.setLocked(Turnout.CABLOCKOUT + Turnout.PUSHBUTTONLOCKOUT, true);
}
}
}
}
// load output sensors if there are any - new format
routeTurnoutList = routeList.get(i).getChildren("routeOutputSensor");
if (routeTurnoutList.size() > 0) {
// This route has turnouts
for (int k = 0; k < routeTurnoutList.size(); k++) {
if (routeTurnoutList.get(k).getAttribute("systemName") == null) {
log.warn("unexpected null in systemName " + routeTurnoutList.get(k) + " " + routeTurnoutList.get(k).getAttributes());
break;
}
String tSysName = routeTurnoutList.get(k).getAttribute("systemName").getValue();
String rState = routeTurnoutList.get(k).getAttribute("state").getValue();
int tSetState = Sensor.INACTIVE;
if (rState.equals("ACTIVE")) {
tSetState = Sensor.ACTIVE;
} else if (rState.equals("TOGGLE")) {
tSetState = Route.TOGGLE;
}
// we will not re add the turnout.
if (r.isOutputSensorIncluded(tSysName)) {
break;
}
// Add turnout to route
r.addOutputSensor(tSysName, tSetState);
}
}
// load sound, script files if present
Element fileElement = routeList.get(i).getChild("routeSoundFile");
if (fileElement != null) {
// convert to absolute path name
r.setOutputSoundName(jmri.util.FileUtil.getExternalFilename(fileElement.getAttribute("name").getValue()));
}
fileElement = routeList.get(i).getChild("routeScriptFile");
if (fileElement != null) {
r.setOutputScriptName(jmri.util.FileUtil.getExternalFilename(fileElement.getAttribute("name").getValue()));
}
// load turnouts aligned sensor if there is one
fileElement = routeList.get(i).getChild("turnoutsAlignedSensor");
if (fileElement != null) {
r.setTurnoutsAlignedSensor(fileElement.getAttribute("name").getValue());
}
// load route control sensors, if there are any
List<Element> routeSensorList = routeList.get(i).getChildren("routeSensor");
if (routeSensorList.size() > 0) {
// This route has sensors
for (int k = 0; k < routeSensorList.size(); k++) {
if (routeSensorList.get(k).getAttribute("systemName") == null) {
log.warn("unexpected null in systemName " + routeSensorList.get(k) + " " + routeSensorList.get(k).getAttributes());
break;
}
// default mode
int mode = Route.ONACTIVE;
if (routeSensorList.get(k).getAttribute("mode") != null) {
String sm = routeSensorList.get(k).getAttribute("mode").getValue();
if (sm.equals("onActive")) {
mode = Route.ONACTIVE;
} else if (sm.equals("onInactive")) {
mode = Route.ONINACTIVE;
} else if (sm.equals("onChange")) {
mode = Route.ONCHANGE;
} else if (sm.equals("vetoActive")) {
mode = Route.VETOACTIVE;
} else if (sm.equals("vetoInactive")) {
mode = Route.VETOINACTIVE;
} else {
log.warn("unexpected sensor mode in route " + sysName + " was " + sm);
}
}
// Add Sensor to route
r.addSensorToRoute(routeSensorList.get(k).getAttribute("systemName").getValue(), mode);
}
}
// and start it working
r.activateRoute();
}
}
Aggregations