use of org.jdom2.Document in project JMRI by JMRI.
the class XmlFile method writeXML.
/**
* Write a File as XML.
*
* @throws FileNotFoundException if file not found
* @param file File to be created.
* @param doc Document to be written out. This should never be null.
*/
public void writeXML(File file, Document doc) throws IOException, FileNotFoundException {
// ensure parent directory exists
if (file.getParent() != null) {
FileUtil.createDirectory(file.getParent());
}
// write the result to selected file
try (FileOutputStream o = new FileOutputStream(file)) {
XMLOutputter fmt = new XMLOutputter();
fmt.setFormat(Format.getPrettyFormat().setLineSeparator(System.getProperty("line.separator")).setTextMode(Format.TextMode.TRIM_FULL_WHITE));
fmt.output(doc, o);
o.flush();
}
}
use of org.jdom2.Document in project JMRI by JMRI.
the class XmlFile method newDocument.
/**
* Create the Document object to store a particular root Element.
*
* @param root Root element of the final document
* @param dtd name of an external DTD
* @return new Document, with root installed
*/
public static Document newDocument(Element root, String dtd) {
Document doc = new Document(root);
doc.setDocType(new DocType(root.getName(), dtd));
addDefaultInfo(root);
return doc;
}
use of org.jdom2.Document in project JMRI by JMRI.
the class XmlFile method newDocument.
/**
* Create the Document object to store a particular root Element, without a
* DocType DTD (e.g. for using a schema)
*
* @param root Root element of the final document
* @return new Document, with root installed
*/
public static Document newDocument(Element root) {
Document doc = new Document(root);
addDefaultInfo(root);
return doc;
}
use of org.jdom2.Document in project JMRI by JMRI.
the class LocationManagerXml method writeFile.
@Override
public void writeFile(String name) throws java.io.FileNotFoundException, java.io.IOException {
log.debug("writeFile {}", name);
// This is taken in large part from "Java and XML" page 368
File file = findFile(name);
if (file == null) {
file = new File(name);
}
// create root element
// NOI18N
Element root = new Element("operations-config");
// NOI18N
Document doc = newDocument(root, dtdLocation + "operations-locations.dtd");
// add XSLT processing instruction
java.util.Map<String, String> m = new java.util.HashMap<String, String>();
// NOI18N
m.put("type", "text/xsl");
// NOI18N
m.put("href", xsltLocation + "operations-locations.xsl");
// NOI18N
ProcessingInstruction p = new ProcessingInstruction("xml-stylesheet", m);
doc.addContent(0, p);
LocationManager.instance().store(root);
ScheduleManager.instance().store(root);
writeXML(file, doc);
// done - location file now stored, so can't be dirty
setDirty(false);
}
use of org.jdom2.Document in project JMRI by JMRI.
the class CarManagerXml method writeFile.
@Override
public void writeFile(String name) throws java.io.FileNotFoundException, java.io.IOException {
log.debug("writeFile {}", name);
// This is taken in large part from "Java and XML" page 368
File file = findFile(name);
if (file == null) {
file = new File(name);
}
// create root element
// NOI18N
Element root = new Element("operations-config");
// NOI18N
Document doc = newDocument(root, dtdLocation + "operations-cars.dtd");
// add XSLT processing instruction
java.util.Map<String, String> m = new java.util.HashMap<String, String>();
// NOI18N
m.put("type", "text/xsl");
// NOI18N
m.put("href", xsltLocation + "operations-cars.xsl");
// NOI18N
ProcessingInstruction p = new ProcessingInstruction("xml-stylesheet", m);
doc.addContent(0, p);
// note all comments line feeds have been changed to processor directives
CarRoads.instance().store(root);
CarTypes.instance().store(root);
CarColors.instance().store(root);
CarLengths.instance().store(root);
CarOwners.instance().store(root);
CarLoads.instance().store(root);
CarManager.instance().store(root);
writeXML(file, doc);
// done - car file now stored, so can't be dirty
setDirty(false);
}
Aggregations