use of org.jdom2.input.SAXBuilder in project JMRI by JMRI.
the class ProfileManager method export.
/**
* Export the {@link jmri.profile.Profile} to a zip file.
*
* @param profile The profile to export
* @param target The file to export the profile into
* @param exportExternalUserFiles If the User Files are not within the
* profile directory, should they be
* included?
* @param exportExternalRoster It the roster is not within the profile
* directory, should it be included?
* @throws java.io.IOException if unable to write a file during the
* export
* @throws org.jdom2.JDOMException if unable to create a new profile
* configuration file in the exported
* Profile
*/
public void export(Profile profile, File target, boolean exportExternalUserFiles, boolean exportExternalRoster) throws IOException, JDOMException {
if (!target.exists() && !target.createNewFile()) {
throw new IOException("Unable to create file " + target);
}
// NOI18N
String tempDirPath = System.getProperty("java.io.tmpdir") + File.separator + "JMRI" + System.currentTimeMillis();
FileUtil.createDirectory(tempDirPath);
File tempDir = new File(tempDirPath);
File tempProfilePath = new File(tempDir, profile.getPath().getName());
FileUtil.copy(profile.getPath(), tempProfilePath);
// NOI18N
File config = new File(tempProfilePath, "ProfileConfig.xml");
Document doc = (new SAXBuilder()).build(config);
if (exportExternalUserFiles) {
FileUtil.copy(new File(FileUtil.getUserFilesPath()), tempProfilePath);
// NOI18N
Element fileLocations = doc.getRootElement().getChild("fileLocations");
for (Element fl : fileLocations.getChildren()) {
if (fl.getAttribute("defaultUserLocation") != null) {
// NOI18N
// NOI18N
fl.setAttribute("defaultUserLocation", "profile:");
}
}
}
if (exportExternalRoster) {
// NOI18N
FileUtil.copy(new File(Roster.getDefault().getRosterIndexPath()), new File(tempProfilePath, "roster.xml"));
// NOI18N
FileUtil.copy(new File(Roster.getDefault().getRosterLocation(), "roster"), new File(tempProfilePath, "roster"));
// NOI18N
Element roster = doc.getRootElement().getChild("roster");
// NOI18N
roster.removeAttribute("directory");
}
if (exportExternalUserFiles || exportExternalRoster) {
try (FileWriter fw = new FileWriter(config)) {
XMLOutputter fmt = new XMLOutputter();
fmt.setFormat(Format.getPrettyFormat().setLineSeparator(System.getProperty("line.separator")).setTextMode(Format.TextMode.PRESERVE));
fmt.output(doc, fw);
}
}
try (FileOutputStream out = new FileOutputStream(target);
ZipOutputStream zip = new ZipOutputStream(out)) {
this.exportDirectory(zip, tempProfilePath, tempProfilePath.getPath());
}
FileUtil.delete(tempDir);
}
use of org.jdom2.input.SAXBuilder in project JMRI by JMRI.
the class XmlFileTest method testProcessPI.
public void testProcessPI() throws org.jdom2.JDOMException, java.io.IOException {
// Document from test file
Document doc;
Element e;
FileInputStream fs = new FileInputStream(new File("java/test/jmri/jmrit/XmlFileTest_PI.xml"));
try {
// argument controls validation
SAXBuilder builder = XmlFile.getBuilder(XmlFile.Validate.None);
doc = builder.build(new BufferedInputStream(fs));
Assert.assertNotNull("Original Document found", doc);
e = doc.getRootElement();
Assert.assertNotNull("Original root element found", e);
XmlFile x = new XmlFile() {
};
Document d = x.processInstructions(doc);
Assert.assertNotNull(d);
// test transform changes <contains> element to <content>
e = d.getRootElement();
Assert.assertNotNull("Transformed root element found", e);
Assert.assertTrue("Transformed root element is right type", e.getName().equals("top"));
Assert.assertTrue("Old element gone", e.getChild("contains") == null);
Assert.assertTrue("New element there", e.getChild("content") != null);
Assert.assertTrue("New element has content", e.getChild("content").getChildren().size() == 2);
} catch (java.io.IOException ex) {
throw ex;
} catch (org.jdom2.JDOMException ex) {
throw ex;
} finally {
fs.close();
}
}
use of org.jdom2.input.SAXBuilder in project pcgen by PCGen.
the class Initiative method loadINIT.
/**
* Perform initial loading
* @param initFile
* @param comp
*/
public void loadINIT(File initFile, PCGenMessageHandler comp) {
try {
SAXBuilder builder = new SAXBuilder();
Document character = builder.build(initFile);
loadFromDocument(character, comp);
} catch (Exception e) {
JOptionPane.showMessageDialog(JOptionPane.getFrameForComponent(this), "File load error: " + initFile.getName());
Logging.errorPrint("File Load Error" + initFile.getName());
Logging.errorPrint(e.getMessage(), e);
}
}
use of org.jdom2.input.SAXBuilder in project pcgen by PCGen.
the class DiceBagModel method loadFromFile.
/**
* <p>Loads the data from the specified file. The document must be a valid
* xml document with the following form:</p>
* <p>
* <code>
* <dice-bag name="[Some name]">
* <dice-roll>[a dice expression]</dice-roll>
* </dice-bag>
* </code>
* </p>
*
* @param file The file to open from.
*/
private void loadFromFile(File file) {
try {
SAXBuilder builder = new SAXBuilder();
Document doc = builder.build(file);
m_filePath = file.getPath();
loadFromDocument(doc);
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.input.SAXBuilder in project pcgen by PCGen.
the class TravelMethodFactory method load.
// ### Factory methods ###
public static Vector<TravelMethod> load(File datadir) {
//Create a new list for the travel methods
Vector<TravelMethod> tms = new Vector<>();
File path = new File(datadir, DIR_TRAVELMETHODS);
if (path.isDirectory()) {
File[] dataFiles = path.listFiles(new XMLFilter());
SAXBuilder builder = new SAXBuilder();
for (int i = 0; i < dataFiles.length; i++) {
try {
Document methodSet = builder.build(dataFiles[i]);
DocType dt = methodSet.getDocType();
if (dt.getElementName().equals(XML_ELEMENT_TRAVEL)) {
//Do work here
TravelMethod tm = TravelMethodFactory.create(methodSet);
tms.add(tm);
}
} catch (Exception e) {
Logging.errorPrint(e.getMessage(), e);
}
}
} else {
//$NON-NLS-1$
Logging.errorPrintLocalised("in_plugin_overland_noDatafile", path.getPath());
}
return tms;
}
Aggregations