use of org.jdom2.output.Format in project JMRI by JMRI.
the class PaneProgPane method getRepresentation.
/**
* Get a GUI representation of a particular variable for display.
*
* @param name Name used to look up the Variable object
* @param var XML Element which might contain a "format" attribute to be
* used in the {@link VariableValue#getNewRep} call from the
* Variable object; "tooltip" elements are also processed here.
* @return JComponent representing this variable
*/
public JComponent getRepresentation(String name, Element var) {
int i = _varModel.findVarIndex(name);
VariableValue variable = _varModel.getVariable(i);
JComponent rep = null;
String format = "default";
Attribute attr;
if ((attr = var.getAttribute("format")) != null && attr.getValue() != null) {
format = attr.getValue();
}
if (i >= 0) {
rep = getRep(i, format);
rep.setMaximumSize(rep.getPreferredSize());
// set tooltip if specified here & not overridden by defn in Variable
String tip = LocaleSelector.getAttribute(var, "tooltip");
if (rep.getToolTipText() != null) {
tip = rep.getToolTipText();
}
rep.setToolTipText(modifyToolTipText(tip, variable));
}
return rep;
}
use of org.jdom2.output.Format in project JMRI by JMRI.
the class ThrottleFrame method setXml.
/**
* Set the preferences based on the XML Element.
* <ul>
* <li> Window prefs
* <li> Frame title
* <li> ControlPanel
* <li> FunctionPanel
* <li> AddressPanel
* </ul>
*
*
* @param e The Element for this object.
*/
@SuppressWarnings("unchecked")
public void setXml(Element e) {
if (e == null) {
return;
}
String sfile = e.getAttributeValue("ThrottleXMLFile");
if (sfile != null) {
loadThrottle(FileUtil.getExternalFilename(sfile));
return;
}
boolean switchAfter = false;
if (!isEditMode) {
switchMode();
switchAfter = true;
}
int bSize = 23;
// Get InternalFrame border size
if (e.getAttribute("border") != null) {
bSize = Integer.parseInt((e.getAttribute("border").getValue()));
}
if (// Old format
e.getChild("window") != null) {
throttleWindow.setXml(e);
}
Element controlPanelElement = e.getChild("ControlPanel");
controlPanel.setXml(controlPanelElement);
if (((javax.swing.plaf.basic.BasicInternalFrameUI) controlPanel.getUI()).getNorthPane() != null) {
((javax.swing.plaf.basic.BasicInternalFrameUI) controlPanel.getUI()).getNorthPane().setPreferredSize(new Dimension(0, bSize));
}
Element functionPanelElement = e.getChild("FunctionPanel");
functionPanel.setXml(functionPanelElement);
if (((javax.swing.plaf.basic.BasicInternalFrameUI) controlPanel.getUI()).getNorthPane() != null) {
((javax.swing.plaf.basic.BasicInternalFrameUI) functionPanel.getUI()).getNorthPane().setPreferredSize(new Dimension(0, bSize));
}
Element addressPanelElement = e.getChild("AddressPanel");
addressPanel.setXml(addressPanelElement);
if (((javax.swing.plaf.basic.BasicInternalFrameUI) controlPanel.getUI()).getNorthPane() != null) {
((javax.swing.plaf.basic.BasicInternalFrameUI) addressPanel.getUI()).getNorthPane().setPreferredSize(new Dimension(0, bSize));
}
List<Element> jinsts = e.getChildren("Jynstrument");
if ((jinsts != null) && (jinsts.size() > 0)) {
for (int i = 0; i < jinsts.size(); i++) {
JInternalFrame jif = ynstrument(FileUtil.getExternalFilename(jinsts.get(i).getAttributeValue("JynstrumentFolder")));
Element window = jinsts.get(i).getChild("window");
if (jif != null) {
if (window != null) {
WindowPreferences.setPreferences(jif, window);
}
Component[] cmps2 = jif.getContentPane().getComponents();
int j = 0;
while ((j < cmps2.length) && (!(cmps2[j] instanceof Jynstrument))) {
j++;
}
if ((j < cmps2.length) && (cmps2[j] instanceof Jynstrument) && (jinsts.get(i) != null)) {
((Jynstrument) cmps2[j]).setXml(jinsts.get(i));
}
jif.repaint();
}
}
}
setFrameTitle();
if (switchAfter) {
switchMode();
}
}
use of org.jdom2.output.Format in project JMRI by JMRI.
the class Steam1Sound method setXml.
@Override
public void setXml(Element e, VSDFile vf) {
Element el;
String fn, n;
S1Notch sb;
// Handle the common stuff.
super.setXml(e, vf);
//log.debug("Steam EngineSound: {}", e.getAttribute("name").getValue());
_soundName = this.getName();
log.debug("Steam1: name: {}, soundName: {}", this.getName(), _soundName);
// Required values
top_speed = Integer.parseInt(e.getChildText("top-speed"));
log.debug("top speed forward: {} MPH", top_speed);
driver_diameter_float = Float.parseFloat(e.getChildText("driver-diameter-float"));
log.debug("driver diameter: {} inches", driver_diameter_float);
num_cylinders = Integer.parseInt(e.getChildText("cylinders"));
log.debug("Number of cylinders defined: {}", num_cylinders);
// Optional value
// Allows to adjust speed.
// Optional value.
n = e.getChildText("exponent");
if (n != null) {
exponent = Float.parseFloat(n);
} else {
// default
exponent = 1.0f;
}
log.debug("exponent: {}", exponent);
// Optional value
// Defines how many rpms in 0.5 seconds will trigger decel actions like braking.
n = e.getChildText("decel-trigger-rpms");
if (n != null) {
decel_trigger_rpms = Integer.parseInt(n);
} else {
// Default (need a value)
decel_trigger_rpms = 999;
}
log.debug("number of rpms to trigger decelerating actions: {}", decel_trigger_rpms);
// Get the sounds.
// Note: each sound must have equal attributes, e.g. 16-bit, 44100 Hz
// Get the files and create a buffer and byteBuffer for each file.
// For each notch there must be <num_cylinders * 2> chuff files.
notch_sounds = new HashMap<>();
// notch number (index)
int i = 0;
// chuff or coast number (index)
int j = 0;
// Sound sample format
int fmt = 0;
// notch number (visual)
int nn = 1;
// Get the notch-sounds.
Iterator<Element> itr = (e.getChildren("s1notch-sound")).iterator();
while (itr.hasNext()) {
el = itr.next();
sb = new S1Notch();
sb.setNotch(nn);
// Get the chuff sounds.
List<Element> elist = el.getChildren("notch-file");
j = 0;
for (Element fe : elist) {
fn = fe.getText();
log.debug("notch: {}, file: {}", nn, fn);
AudioBuffer b = S1Notch.getBuffer(vf, fn, _soundName + "_NOTCH_" + i + "_" + j + "_", _soundName + "_NOTCH_" + i + "_" + j + "_");
log.debug("buffer created: {}, name: {}, format: {}", b, b.getSystemName(), b.getFormat());
sb.addChuffBuffer(b);
if (fmt == 0) {
// Get the format of the (first) WAV file.
// Since all WAV files of the notches MUST have the same format,
// I asume this format for all WAV files for now.
fmt = AudioUtil.getWavFormat(S1Notch.getWavStream(vf, fn));
log.debug("fmt: {}", fmt);
}
ByteBuffer data = AudioUtil.getWavData(S1Notch.getWavStream(vf, fn));
sb.addChuffData(data);
j++;
}
log.debug("Number of chuff sounds for notch {} defined: {}", nn, j);
// Create a filler Buffer for queueing and a ByteBuffer for length modification.
fn = el.getChildText("notchfiller-file");
if (fn != null) {
log.debug("notch filler file: {}", fn);
AudioBuffer bnf = S1Notch.getBuffer(vf, el.getChildText("notchfiller-file"), _soundName + "_NOTCHFILLER_" + i + "_", _soundName + "_NOTCHFILLER_" + i + "_");
log.debug("buffer created: {}, name: {}, format: {}", bnf, bnf.getSystemName(), bnf.getFormat());
sb.setNotchFillerBuffer(bnf);
sb.setNotchFillerData(AudioUtil.getWavData(S1Notch.getWavStream(vf, fn)));
} else {
log.debug("no notchfiller available.");
sb.setNotchFillerBuffer(null);
}
// VSDFile validation makes sure that there is at least one notch.
if (nn == 1) {
// Get the coasting sounds.
j = 0;
List<Element> elistc = el.getChildren("coast-file");
for (Element fe : elistc) {
fn = fe.getText();
log.debug("coasting file: {}", fn);
AudioBuffer bc = S1Notch.getBuffer(vf, fn, _soundName + "_COAST_" + j + "_", _soundName + "_COAST_" + j + "_");
log.debug("buffer created: {}, name: {}, format: {}", bc, bc.getSystemName(), bc.getFormat());
// WAV in Buffer for queueing.
sb.addCoastBuffer(bc);
ByteBuffer datac = AudioUtil.getWavData(S1Notch.getWavStream(vf, fn));
// WAV data in ByteBuffer for length modification.
sb.addCoastData(datac);
j++;
}
log.debug("Number of coasting sounds for notch {} defined: {}", nn, j);
// Create a filler Buffer for queueing and a ByteBuffer for length modification.
fn = el.getChildText("coastfiller-file");
if (fn != null) {
log.debug("coasting filler file: {}", fn);
AudioBuffer bcf = S1Notch.getBuffer(vf, fn, _soundName + "_COASTFILLER_", _soundName + "_COASTFILLER_");
log.debug("buffer created: {}, name: {}, format: {}", bcf, bcf.getSystemName(), bcf.getFormat());
sb.setCoastFillerBuffer(bcf);
sb.setCoastFillerData(AudioUtil.getWavData(S1Notch.getWavStream(vf, fn)));
} else {
log.debug("no coastfiller available.");
sb.setCoastFillerBuffer(null);
}
// serve well for that purpose. These buffers are bound to notch 1.
for (int jk = 0; jk < 10; jk++) {
AudioBuffer bh = S1Notch.getBufferHelper(_soundName + "_BUFFERHELPER_" + jk, _soundName + "_BUFFERHELPER_" + jk);
log.debug("buffer helper created: {}, name: {}", bh, bh.getSystemName());
sb.addHelper(bh);
}
}
sb.setMinLimit(Integer.parseInt(el.getChildText("min-rpm")));
sb.setMaxLimit(Integer.parseInt(el.getChildText("max-rpm")));
sb.setBufferFmt(fmt);
log.debug("sample format for notch {}: {}", nn, fmt);
// Store in the list.
notch_sounds.put(nn, sb);
i++;
nn++;
}
log.debug("Number of notches defined: {}", notch_sounds.size());
// Get the trigger sounds.
// Note: other than notch sounds, trigger sounds can have different attributes.
trigger_sounds = new HashMap<>();
// Get the idle sound.
el = e.getChild("idle-sound");
if (el != null) {
fn = el.getChild("sound-file").getValue();
log.debug("idle sound: {}", fn);
idle_sound = new SoundBite(vf, fn, _soundName + "Idle", _soundName + "Idle");
// Handle gain
idle_sound.setGain(setXMLGain(el));
log.debug("idle sound gain: {}", idle_sound.getGain());
idle_sound.setLooped(true);
idle_sound.setFadeTimes(500, 500);
trigger_sounds.put("idle", idle_sound);
log.debug("trigger sound idle: {}", trigger_sounds.get("idle"));
}
// Get the brake sound.
el = e.getChild("brake-sound");
if (el != null) {
fn = el.getChild("sound-file").getValue();
log.debug("brake sound: {}", fn);
brake_sound = new SoundBite(vf, fn, _soundName + "Brake", _soundName + "Brake");
brake_sound.setGain(setXMLGain(el));
log.debug("brake sound gain: {}", brake_sound.getGain());
brake_sound.setLooped(false);
brake_sound.setFadeTimes(500, 500);
trigger_sounds.put("brake", brake_sound);
log.debug("trigger sound brake: {}", trigger_sounds.get("brake"));
}
// Get the pre-arrival sound
el = e.getChild("pre-arrival-sound");
if (el != null) {
fn = el.getChild("sound-file").getValue();
log.debug("pre-arrival sound: {}", fn);
pre_arrival_sound = new SoundBite(vf, fn, _soundName + "Pre-arrival", _soundName + "Pre-arrival");
pre_arrival_sound.setGain(setXMLGain(el));
log.debug("pre-arrival sound gain: {}", pre_arrival_sound.getGain());
pre_arrival_sound.setLooped(false);
pre_arrival_sound.setFadeTimes(500, 500);
log.debug("getGain pre-arrival-sound: {}", pre_arrival_sound.getGain());
trigger_sounds.put("pre_arrival", pre_arrival_sound);
log.debug("trigger sound pre_arrival: {}", trigger_sounds.get("pre_arrival"));
}
// Kick-start the loop thread.
this.startThread();
}
use of org.jdom2.output.Format in project dq-easy-cloud by dq-open-cloud.
the class XmlUtilsTest method testAddXmlElement.
/**
* 新增节点
* @throws Exception
*/
@Test
public void testAddXmlElement() throws Exception {
// 获取根元素
Element root = doc.getRootElement();
// 设置新增的person的信息
Element newEle = new Element("result");
newEle.setAttribute("column", "update_by");
newEle.setAttribute("jdbcType", "VARCHAR");
newEle.setAttribute("property", "updateBy");
Element resultMap = root.getChild("resultMap");
List<Element> results = resultMap.getChildren("result");
System.out.println("result.size" + results.size());
results.add(0, newEle);
// 设置新增的person的信息
Element newEle1 = new Element("result");
newEle1.setAttribute("column", "update_by");
newEle1.setAttribute("jdbcType", "VARCHAR");
newEle1.setAttribute("property", "updateBy");
System.out.println("result.size" + results.size());
results.add(0, newEle1);
Format format = Format.getRawFormat();
format.setEncoding("UTF-8");
format.setTextMode(TextMode.PRESERVE);
XMLOutputter out = new XMLOutputter(format);
// 写文件
out.output(doc, new FileWriter(xmlFile));
}
use of org.jdom2.output.Format in project scylla by bptlab.
the class SimulationManager method run.
/**
* parses input, runs DesmoJ simulation experiment, writes BPS output logs
*/
public String run() {
try {
SAXBuilder builder = new SAXBuilder();
if (globalConfigurationFilename == null || globalConfigurationFilename.isEmpty()) {
throw new ScyllaValidationException("No global configuration provided.");
} else {
// parse global configuration XML
Document gcDoc = builder.build(globalConfigurationFilename);
Element gcRootElement = gcDoc.getRootElement();
GlobalConfigurationParser globalConfigurationParser = new GlobalConfigurationParser(this);
globalConfiguration = globalConfigurationParser.parse(gcDoc.getRootElement());
String fileNameWithoutExtension = // filename.lastIndexOf("\\") +
globalConfigurationFilename.substring(// 1,
globalConfigurationFilename.lastIndexOf(Scylla.FILEDELIM) + 1, globalConfigurationFilename.lastIndexOf(".xml"));
globalConfiguration.setFileNameWithoutExtension(fileNameWithoutExtension);
// plugins to parse global configuration
GlobalConfigurationParserPluggable.runPlugins(this, globalConfiguration, gcRootElement);
DateTimeUtils.setZoneId(globalConfiguration.getZoneId());
}
CommonProcessElementsParser cpeParser = new CommonProcessElementsParser(this);
for (String filename : processModelFilenames) {
Document pmDoc = builder.build(filename);
Element pmRootElement = pmDoc.getRootElement();
// parse common process elements from XML (BPMN)
CommonProcessElements commonProcessElementsFromFile = cpeParser.parse(pmRootElement);
String fileNameWithoutExtension = // filename.lastIndexOf("\\") + 1,
filename.substring(filename.lastIndexOf(Scylla.FILEDELIM) + 1, filename.lastIndexOf(".bpmn"));
commonProcessElementsFromFile.setBpmnFileNameWithoutExtension(fileNameWithoutExtension);
// plugins to parse common process elements
CommonProcessElementsParserPluggable.runPlugins(this, commonProcessElementsFromFile, pmRootElement);
// parse process model(s) from XML (BPMN)
ProcessModelParser pmParser = new ProcessModelParser(this);
pmParser.setCommonProcessElements(commonProcessElementsFromFile);
ProcessModel processModelFromFile = pmParser.parse(pmDoc.getRootElement());
String processId = processModelFromFile.getId();
if (processModels.containsKey(processId)) {
throw new ScyllaValidationException("Duplicate process model with id " + processId + ".");
}
// plugins to parse process model(s)
ProcessModelParserPluggable.runPlugins(this, processModelFromFile, pmRootElement);
processModels.put(processId, processModelFromFile);
commonProcessElements.put(processId, commonProcessElementsFromFile);
}
SimulationConfigurationParser simParser = new SimulationConfigurationParser(this);
// parse each simulation configuration XML
for (String filename : simulationConfigurationFilenames) {
Document scDoc = builder.build(filename);
SimulationConfiguration simulationConfigurationFromFile = simParser.parse(scDoc.getRootElement());
String processId = simulationConfigurationFromFile.getProcessModel().getId();
if (simulationConfigurations.containsKey(processId)) {
throw new ScyllaValidationException("Multiple simulation configurations for process with id " + processId + ".");
}
// plugins to parse simulation configuration
SimulationConfigurationParserPluggable.runPlugins(this, simulationConfigurationFromFile, scDoc);
simulationConfigurations.put(processId, simulationConfigurationFromFile);
}
} catch (JDOMException | IOException | ScyllaValidationException e) {
DebugLogger.error(e.getMessage());
e.printStackTrace();
}
// TODO validate resources in process models (i.e. check if they are all covered in resource data)
TimeUnit epsilon = TimeUnit.SECONDS;
DateTimeUtils.setReferenceTimeUnit(epsilon);
String experimentName = Long.toString((new Date()).getTime());
Experiment.setEpsilon(epsilon);
Experiment exp = new Experiment(experimentName, experimentOutputFolder);
exp.setShowProgressBar(false);
// XXX each simulation configuration may have its own seed
Long randomSeed = globalConfiguration.getRandomSeed();
if (randomSeed != null) {
exp.setSeedGenerator(randomSeed);
} else {
exp.setSeedGenerator((new Random()).nextLong());
}
SimulationModel sm = new SimulationModel(null, globalConfiguration, commonProcessElements, processModels, simulationConfigurations, enableBpsLogging, enableDesLogging);
sm.connectToExperiment(exp);
int lambda = 1;
if (sm.getEndDateTime() != null) {
// have to use time which is slightly after intended end time (epsilon)
// otherwise the AbortProcessSimulationEvent(s) may not fire
long simulationDuration = DateTimeUtils.getDuration(sm.getStartDateTime(), sm.getEndDateTime());
TimeInstant simulationTimeInstant = new TimeInstant(simulationDuration + lambda, epsilon);
exp.stop(simulationTimeInstant);
exp.tracePeriod(new TimeInstant(0), simulationTimeInstant);
exp.debugPeriod(new TimeInstant(0), simulationTimeInstant);
} else {
exp.traceOn(new TimeInstant(0));
exp.debugOn(new TimeInstant(0));
}
if (!enableDesLogging) {
exp.debugOff(new TimeInstant(0));
exp.traceOff(new TimeInstant(0));
}
exp.start();
exp.report();
exp.finish();
try {
// log process execution
// log resources, process, tasks
StringBuilder strb = new StringBuilder(globalConfigurationFilename.substring(0, globalConfigurationFilename.lastIndexOf(Scylla.FILEDELIM) + 1));
outputPath = strb.substring(0, strb.lastIndexOf(Scylla.FILEDELIM) + 1) + "output_" + new SimpleDateFormat("yy_MM_dd_HH_mm_ss").format(new Date()) + Scylla.FILEDELIM;
File outputPathFolder = new File(outputPath);
if (!outputPathFolder.exists())
outputPathFolder.mkdir();
OutputLoggerPluggable.runPlugins(sm, outputPath);
} catch (IOException e) {
e.printStackTrace();
}
return outputPath;
}
Aggregations