use of org.jdom2.Content in project JMRI by JMRI.
the class MatrixSignalMastXml method store.
/**
* Default implementation for storing the contents of a
* MatrixSignalMastManager
*
* @param o Object to store, of type MatrixSignalMast
* @return e Element containing the complete info
*/
@Override
public Element store(Object o) {
// from mast p to XML
MatrixSignalMast p = (MatrixSignalMast) o;
Element e = new Element("matrixsignalmast");
e.setAttribute("class", this.getClass().getName());
// include content
e.addContent(new Element("systemName").addContent(p.getSystemName()));
// username, comment & properties
storeCommon(p, e);
// mast properties:
Element unlit = new Element("unlit");
if (p.allowUnLit()) {
unlit.setAttribute("allowed", "yes");
unlit.addContent(new Element("bitString").addContent(p.getUnLitChars()));
} else {
unlit.setAttribute("allowed", "no");
}
e.addContent(unlit);
List<String> outputs = p.getOutputs();
// convert char[] to xml-storable simple String
// max. 5 outputs (either: turnouts (bean names) [or ToDo: DCC addresses (numbers)]
// spotted by FindBugs as to never be null (check on creation of MatrixMast)
Element outps = new Element("outputs");
int i = 1;
for (String _output : outputs) {
String key = ("output" + i);
Element outp = new Element("output");
outp.setAttribute("matrixCol", key);
// get name (Turnout)
outp.addContent(p.getOutputName(i));
outps.addContent(outp);
i++;
}
if (outputs.size() != 0) {
e.addContent(outps);
}
// string of max. 6 chars "001010" describing matrix row per aspect
SignalAppearanceMap appMap = p.getAppearanceMap();
if (appMap != null) {
Element bss = new Element("bitStrings");
java.util.Enumeration<String> aspects = appMap.getAspects();
while (aspects.hasMoreElements()) {
String key = aspects.nextElement();
Element bs = new Element("bitString");
bs.setAttribute("aspect", key);
bs.addContent(p.getBitstring(key));
bss.addContent(bs);
}
e.addContent(bss);
}
List<String> disabledAspects = p.getDisabledAspects();
if (disabledAspects != null) {
Element el = new Element("disabledAspects");
for (String aspect : disabledAspects) {
Element ele = new Element("disabledAspect");
ele.addContent(aspect);
el.addContent(ele);
}
if (disabledAspects.size() != 0) {
e.addContent(el);
}
}
return e;
}
use of org.jdom2.Content in project JMRI by JMRI.
the class XmlFile method addDefaultInfo.
/**
* Add default information to the XML before writing it out.
* <P>
* Currently, this is identification information as an XML comment. This
* includes: <UL>
* <LI>The JMRI version used <LI>Date of writing <LI>A CVS id string, in
* case the file gets checked in or out </UL>
* <P>
* It may be necessary to extend this to check whether the info is already
* present, e.g. if re-writing a file.
*
* @param root The root element of the document that will be written.
*/
public static void addDefaultInfo(Element root) {
String content = "Written by JMRI version " + jmri.Version.name() + " on " + (new Date()).toString() + " $Id$";
Comment comment = new Comment(content);
root.addContent(comment);
}
use of org.jdom2.Content in project JMRI by JMRI.
the class TrainInfoFile method writeTrainInfo.
/*
* Writes out Dispatcher options to a file in the user's preferences directory
*/
public void writeTrainInfo(TrainInfo tf, String name) throws java.io.IOException {
log.debug("entered writeTrainInfo");
root = new Element("traininfofile");
doc = newDocument(root, dtdLocation + "dispatcher-traininfo.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<>();
m.put("type", "text/xsl");
m.put("href", xsltLocation + "dispatcher-traininfo.xsl");
org.jdom2.ProcessingInstruction p = new org.jdom2.ProcessingInstruction("xml-stylesheet", m);
doc.addContent(0, p);
// save Dispatcher TrainInfo in xml format
Element traininfo = new Element("traininfo");
traininfo.setAttribute("transitname", tf.getTransitName());
traininfo.setAttribute("trainname", tf.getTrainName());
traininfo.setAttribute("dccaddress", tf.getDCCAddress());
traininfo.setAttribute("trainintransit", "" + (tf.getTrainInTransit() ? "yes" : "no"));
traininfo.setAttribute("startblockname", tf.getStartBlockName());
traininfo.setAttribute("endblockname", tf.getDestinationBlockName());
traininfo.setAttribute("trainfromroster", "" + (tf.getTrainFromRoster() ? "yes" : "no"));
traininfo.setAttribute("trainfromtrains", "" + (tf.getTrainFromTrains() ? "yes" : "no"));
traininfo.setAttribute("trainfromuser", "" + (tf.getTrainFromUser() ? "yes" : "no"));
traininfo.setAttribute("priority", Integer.toString(tf.getPriority()));
traininfo.setAttribute("resetwhendone", "" + (tf.getResetWhenDone() ? "yes" : "no"));
switch(tf.getDelayedRestart()) {
case ActiveTrain.SENSORDELAY:
traininfo.setAttribute("delayedrestart", "sensor");
traininfo.setAttribute("delayedrestartsensor", tf.getRestartSensorName());
break;
case ActiveTrain.TIMEDDELAY:
traininfo.setAttribute("delayedrestart", "timed");
traininfo.setAttribute("delayedrestarttime", Integer.toString(tf.getRestartDelayMin()));
break;
default:
traininfo.setAttribute("delayedrestart", "no");
break;
}
traininfo.setAttribute("reverseatend", "" + (tf.getReverseAtEnd() ? "yes" : "no"));
if (tf.getDelayedStart() == ActiveTrain.TIMEDDELAY) {
traininfo.setAttribute("delayedstart", "timed");
} else if (tf.getDelayedStart() == ActiveTrain.SENSORDELAY) {
traininfo.setAttribute("delayedstart", "sensor");
if (tf.getDelaySensorName() != null) {
traininfo.setAttribute("delayedSensor", tf.getDelaySensorName());
}
}
traininfo.setAttribute("terminatewhendone", (tf.getTerminateWhenDone() ? "yes" : "no"));
traininfo.setAttribute("departuretimehr", Integer.toString(tf.getDepartureTimeHr()));
traininfo.setAttribute("departuretimemin", Integer.toString(tf.getDepartureTimeMin()));
traininfo.setAttribute("traintype", tf.getTrainType());
traininfo.setAttribute("autorun", "" + (tf.getAutoRun() ? "yes" : "no"));
traininfo.setAttribute("loadatstartup", "" + (tf.getLoadAtStartup() ? "yes" : "no"));
traininfo.setAttribute("allocatealltheway", "" + (tf.getAllocateAllTheWay() ? "yes" : "no"));
// here save items related to automatically running active trains
traininfo.setAttribute("speedfactor", Float.toString(tf.getSpeedFactor()));
traininfo.setAttribute("maxspeed", Float.toString(tf.getMaxSpeed()));
traininfo.setAttribute("ramprate", tf.getRampRate());
traininfo.setAttribute("resistancewheels", "" + (tf.getResistanceWheels() ? "yes" : "no"));
traininfo.setAttribute("runinreverse", "" + (tf.getRunInReverse() ? "yes" : "no"));
traininfo.setAttribute("sounddecoder", "" + (tf.getSoundDecoder() ? "yes" : "no"));
traininfo.setAttribute("maxtrainlength", Float.toString(tf.getMaxTrainLength()));
root.addContent(traininfo);
// write out the file
try {
if (!checkFile(fileLocation + name)) {
// file does not exist, create it
File file = new File(fileLocation + name);
if (// create file and check result
!file.createNewFile()) {
log.error("createNewFile failed");
}
}
// write content to file
writeXML(findFile(fileLocation + name), doc);
} catch (java.io.IOException ioe) {
log.error("IO Exception " + ioe);
throw (ioe);
}
}
use of org.jdom2.Content in project JMRI by JMRI.
the class DefaultSignalAppearanceMap method loadMap.
static DefaultSignalAppearanceMap loadMap(String signalSystemName, String aspectMapName) {
DefaultSignalAppearanceMap map = new DefaultSignalAppearanceMap("map:" + signalSystemName + ":" + aspectMapName);
maps.put("map:" + signalSystemName + ":" + aspectMapName, map);
String path = "signals/" + signalSystemName + "/appearance-" + aspectMapName + ".xml";
URL file = FileUtil.findURL(path, "resources", "xml");
if (file == null) {
log.error("appearance file (xml/{}) doesn't exist", path);
throw new IllegalArgumentException("appearance file (xml/" + path + ") doesn't exist");
}
jmri.jmrit.XmlFile xf = new jmri.jmrit.XmlFile() {
};
Element root;
try {
root = xf.rootFromURL(file);
// get appearances
List<Element> l = root.getChild("appearances").getChildren("appearance");
// find all appearances, include them by aspect name,
log.debug(" reading {} aspectname elements", l.size());
for (int i = 0; i < l.size(); i++) {
String name = l.get(i).getChild("aspectname").getText();
if (log.isDebugEnabled()) {
log.debug("aspect name " + name);
}
// add 'show' sub-elements as ints
List<Element> c = l.get(i).getChildren("show");
int[] appearances = new int[c.size()];
for (int j = 0; j < c.size(); j++) {
// note: includes setting name; redundant, but needed
int ival;
String sval = c.get(j).getText().toUpperCase();
if (sval.equals("LUNAR")) {
ival = SignalHead.LUNAR;
} else if (sval.equals("GREEN")) {
ival = SignalHead.GREEN;
} else if (sval.equals("YELLOW")) {
ival = SignalHead.YELLOW;
} else if (sval.equals("RED")) {
ival = SignalHead.RED;
} else if (sval.equals("FLASHLUNAR")) {
ival = SignalHead.FLASHLUNAR;
} else if (sval.equals("FLASHGREEN")) {
ival = SignalHead.FLASHGREEN;
} else if (sval.equals("FLASHYELLOW")) {
ival = SignalHead.FLASHYELLOW;
} else if (sval.equals("FLASHRED")) {
ival = SignalHead.FLASHRED;
} else if (sval.equals("DARK")) {
ival = SignalHead.DARK;
} else {
log.error("found invalid content: {}", sval);
throw new JDOMException("invalid content: " + sval);
}
appearances[j] = ival;
}
map.addAspect(name, appearances);
List<Element> img = l.get(i).getChildren("imagelink");
loadImageMaps(img, name, map);
// now add the rest of the attributes
java.util.Hashtable<String, String> hm = new java.util.Hashtable<String, String>();
List<Element> a = l.get(i).getChildren();
for (int j = 0; j < a.size(); j++) {
String key = a.get(j).getName();
String value = a.get(j).getText();
hm.put(key, value);
}
map.aspectAttributeMap.put(name, hm);
}
loadSpecificMap(signalSystemName, aspectMapName, map, root);
loadAspectRelationMap(signalSystemName, aspectMapName, map, root);
log.debug("loading complete");
} catch (java.io.IOException | org.jdom2.JDOMException e) {
log.error("error reading file " + file.getPath(), e);
return null;
}
return map;
}
use of org.jdom2.Content in project jspwiki by apache.
the class CreoleRenderer method renderElement.
/**
* Renders an element into the StringBuilder given
* @param ce
* @param sb
*/
private void renderElement(Element ce, StringBuilder sb) {
String endEl = EMPTY_STRING;
for (int i = 0; i < ELEMENTS.length; i += 3) {
if (ELEMENTS[i].equals(ce.getName())) {
sb.append(ELEMENTS[i + 1]);
endEl = ELEMENTS[i + 2];
}
}
if (UL.equals(ce.getName())) {
m_listCount++;
m_listChar = '*';
} else if (OL.equals(ce.getName())) {
m_listCount++;
m_listChar = '#';
} else if (LI.equals(ce.getName())) {
for (int i = 0; i < m_listCount; i++) sb.append(m_listChar);
sb.append(ONE_SPACE);
} else if (A.equals(ce.getName())) {
String href = ce.getAttributeValue(HREF_ATTRIBUTE);
String text = ce.getText();
if (href.equals(text)) {
sb.append(HREF_START + href + HREF_END);
} else {
sb.append(HREF_START + href + HREF_DELIMITER + text + HREF_END);
}
// Do not render anything else
return;
} else if (PRE.equals(ce.getName())) {
sb.append(PRE_START);
sb.append(ce.getText());
sb.append(PRE_END);
return;
}
//
for (Iterator<Content> i = ce.getContent().iterator(); i.hasNext(); ) {
Content c = i.next();
if (c instanceof PluginContent) {
PluginContent pc = (PluginContent) c;
if (pc.getPluginName().equals(PLUGIN_IMAGE)) {
sb.append(IMG_START + pc.getParameter(PARAM_SRC) + IMG_END);
} else {
m_plugins.add(pc);
sb.append(PLUGIN_START + pc.getPluginName() + ONE_SPACE + m_plugins.size() + PLUGIN_END);
}
} else if (c instanceof Text) {
sb.append(((Text) c).getText());
} else if (c instanceof Element) {
renderElement((Element) c, sb);
}
}
if (UL.equals(ce.getName()) || OL.equals(ce.getName())) {
m_listCount--;
} else if (P.equals(ce.getName())) {
sb.append(LINEBREAK);
}
sb.append(endEl);
}
Aggregations