use of org.jdom2.output.XMLOutputter in project gocd by gocd.
the class GoControlLog method writeLogFile.
protected void writeLogFile(File file, Element element) throws IOException {
// Write the log file out, let jdom care about the encoding by using
// an OutputStream instead of a Writer.
OutputStream logStream = null;
try {
Format format = Format.getPrettyFormat();
XMLOutputter outputter = new XMLOutputter(format);
IO.mkdirFor(file);
file.setWritable(true);
logStream = new BufferedOutputStream(new FileOutputStream(file));
outputter.output(new Document(element), logStream);
} finally {
IO.close(logStream);
}
}
use of org.jdom2.output.XMLOutputter in project pcgen by PCGen.
the class DiceBagModel method saveToFile.
/**
* <p>Saves the dicebag to the specified file as a UTF-8 xml file, with the format
* specified above in {@code loadFromFile()}</p>
*
* @param file File to save to.
*/
void saveToFile(File file) {
try {
Document doc = new Document();
saveToDocument(doc);
XMLOutputter xmlOut = new XMLOutputter();
xmlOut.setFormat(Format.getPrettyFormat());
FileWriter fr = new FileWriter(file);
xmlOut.output(doc, fr);
fr.flush();
fr.close();
m_filePath = file.getPath();
m_changed = false;
} catch (Exception e) {
JOptionPane.showMessageDialog(GMGenSystem.inst, "File load error: " + file.getName());
Logging.errorPrint("File Load Error" + file.getName());
Logging.errorPrint(e.getMessage(), e);
}
}
use of org.jdom2.output.XMLOutputter in project JMRI by JMRI.
the class XmlFileValidateAction method processFile.
protected void processFile(File file) {
if (log.isDebugEnabled()) {
log.debug("located file " + file + " for XML processing");
}
// handle the file (later should be outside this thread?)
try {
xmlfile.setValidate(XmlFile.Validate.CheckDtdThenSchema);
readFile(file);
} catch (Exception ex) {
// because of XInclude, we're doing this
// again to validate the entire file
// without losing the error message
Document doc;
try {
InputStream stream = new BufferedInputStream(new FileInputStream(file));
SAXBuilder builder = new SAXBuilder("org.apache.xerces.parsers.SAXParser", false);
builder.setEntityResolver(new jmri.util.JmriLocalEntityResolver());
builder.setFeature("http://apache.org/xml/features/xinclude", true);
builder.setFeature("http://apache.org/xml/features/xinclude/fixup-base-uris", false);
builder.setFeature("http://apache.org/xml/features/validation/schema", false);
builder.setFeature("http://apache.org/xml/features/validation/schema-full-checking", false);
builder.setFeature("http://xml.org/sax/features/namespaces", true);
doc = builder.build(new BufferedInputStream(stream));
} catch (JDOMException | IOException ex2) {
showFailResults(_who, "Err(1): " + ex2);
return;
}
XMLOutputter outputter = new XMLOutputter();
outputter.setFormat(Format.getPrettyFormat().setLineSeparator(System.getProperty("line.separator")).setTextMode(Format.TextMode.PRESERVE));
StringWriter out = new StringWriter();
try {
outputter.output(doc, out);
} catch (IOException ex2) {
showFailResults(_who, "Err(4): " + ex2);
return;
}
StringReader input = new StringReader(new String(out.getBuffer()));
SAXBuilder builder = new SAXBuilder("org.apache.xerces.parsers.SAXParser", true);
builder.setEntityResolver(new jmri.util.JmriLocalEntityResolver());
builder.setFeature("http://apache.org/xml/features/xinclude", true);
builder.setFeature("http://apache.org/xml/features/xinclude/fixup-base-uris", false);
builder.setFeature("http://apache.org/xml/features/validation/schema", true);
builder.setFeature("http://apache.org/xml/features/validation/schema-full-checking", true);
builder.setFeature("http://xml.org/sax/features/namespaces", true);
try {
builder.build(input).getRootElement();
} catch (JDOMException | IOException ex2) {
showFailResults(_who, "Err(2): " + ex2);
return;
}
showFailResults(_who, "Err(3): " + ex);
return;
}
showOkResults(_who, "OK");
if (log.isDebugEnabled()) {
log.debug("parsing complete");
}
}
use of org.jdom2.output.XMLOutputter in project JMRI by JMRI.
the class SymbolicProgFrame method writeFile.
// dead class doesn't need this fixed right now
@SuppressFBWarnings(value = "REC_CATCH_EXCEPTION")
void writeFile() {
log.warn("SymbolicProgFrame writeFile invoked - is this still right, or should the LocoFile method be used?");
log.warn("Note use of VersionID attribute...");
try {
// get the file
int retVal = fco.showSaveDialog(this);
// handle selection or cancel
if (retVal != JFileChooser.APPROVE_OPTION) {
// leave early
return;
}
File file = fco.getSelectedFile();
// This is taken in large part from "Java and XML" page 368
// create root element
Element root = new Element("locomotive-config");
Document doc = jmri.jmrit.XmlFile.newDocument(root, jmri.jmrit.XmlFile.getDefaultDtdLocation() + "locomotive-config.dtd");
// add XSLT processing instruction
// <?xml-stylesheet type="text/xsl" href="XSLT/locomotive.xsl"?>
java.util.Map<String, String> m = new java.util.HashMap<String, String>();
m.put("type", "text/xsl");
m.put("href", jmri.jmrit.XmlFile.xsltLocation + "locomotive.xsl");
ProcessingInstruction p = new ProcessingInstruction("xml-stylesheet", m);
doc.addContent(0, p);
// add top-level elements
Element values;
root.addContent(// locomotive values are first item
new Element("locomotive").setAttribute("roadNumber", locoRoadNumber.getText()).setAttribute("roadName", locoRoadName.getText()).setAttribute("mfg", locoMfg.getText()).setAttribute("model", locoModel.getText()).addContent(new Element("decoder").setAttribute("model", decoderModel.getText()).setAttribute("mfg", decoderMfg.getText()).setAttribute("versionID", "").setAttribute("mfgID", "")).addContent(values = new Element("values")));
// Append a decoderDef element to values
Element decoderDef;
values.addContent(decoderDef = new Element("decoderDef"));
// add the variable values to the decoderDef Element
for (int i = 0; i < variableModel.getRowCount(); i++) {
decoderDef.addContent(new Element("varValue").setAttribute("item", variableModel.getLabel(i)).setAttribute("value", variableModel.getValString(i)));
}
// add the CV values to the values Element
for (int i = 0; i < cvModel.getRowCount(); i++) {
values.addContent(new Element("CVvalue").setAttribute("name", cvModel.getName(i)).setAttribute("value", cvModel.getValString(i)));
}
// write the result to selected file
java.io.FileOutputStream o = new java.io.FileOutputStream(file);
try {
XMLOutputter fmt = new XMLOutputter();
fmt.setFormat(Format.getPrettyFormat().setLineSeparator(System.getProperty("line.separator")).setTextMode(Format.TextMode.PRESERVE));
fmt.output(doc, o);
} finally {
o.close();
}
// mark file as OK
variableModel.setFileDirty(false);
} catch (Exception e) {
log.error(e.getLocalizedMessage(), e);
}
}
use of org.jdom2.output.XMLOutputter in project JMRI by JMRI.
the class StoreXmlVSDecoderAction method saveVSDecoderProfile.
public void saveVSDecoderProfile(java.io.File f) {
try {
Element root = new Element("VSDecoderConfig");
Document doc = XmlFile.newDocument(root, XmlFile.getDefaultDtdLocation() + "vsdecoder-config.dtd");
// add XSLT processing instruction
// <?xml-stylesheet type="text/xsl" href="XSLT/throttle-layout-config.xsl"?>
/*TODO java.util.Map<String,String> m = new java.util.HashMap<String,String>();
m.put("type", "text/xsl");
m.put("href", jmri.jmrit.XmlFile.xsltLocation + "throttle-layout-config.xsl");
ProcessingInstruction p = new ProcessingInstruction("xml-stylesheet", m);
doc.addContent(0, p); */
java.util.ArrayList<Element> children = new java.util.ArrayList<Element>(5);
for (java.util.Iterator<VSDecoder> i = VSDecoderManager.instance().getVSDecoderList().iterator(); i.hasNext(); ) {
VSDecoder vsd = i.next();
children.add(vsd.getXml());
}
// Throttle-specific stuff below. Kept for reference
/*
// throttle list window
children.add(ThrottleFrameManager.instance().getThrottlesListPanel().getXml() );
// throttle windows
for (Iterator<ThrottleWindow> i = ThrottleFrameManager.instance().getThrottleWindows(); i.hasNext();) {
ThrottleWindow tw = i.next();
Element throttleElement = tw.getXml();
children.add(throttleElement);
}
*/
// End Throttle-specific stuff.
root.setContent(children);
FileOutputStream o = new java.io.FileOutputStream(f);
try {
XMLOutputter fmt = new XMLOutputter();
fmt.setFormat(Format.getPrettyFormat().setLineSeparator(System.getProperty("line.separator")).setTextMode(Format.TextMode.PRESERVE));
fmt.output(doc, o);
} catch (IOException ex) {
log.warn("Exception in storing VSDecoder xml: " + ex);
} finally {
o.close();
}
} catch (FileNotFoundException ex) {
log.warn("Exception in storing VSDecoder xml: " + ex);
} catch (IOException ex) {
log.warn("Exception in storing VSDecoder xml: " + ex);
}
}
Aggregations