use of jgnash.engine.StoredObjectComparator in project jgnash by ccavanaugh.
the class BinaryContainer method writeBinary.
/**
* Writes an XML file given a collection of StoredObjects. TrashObjects and
* objects marked for removal are not written. If the file already exists,
* it will be overwritten.
*
* @param objects Collection of StoredObjects to write
* @param path file to write
*/
static synchronized void writeBinary(@NotNull final Collection<StoredObject> objects, @NotNull final Path path) {
final Logger logger = Logger.getLogger(BinaryContainer.class.getName());
if (!Files.exists(path.getParent())) {
try {
Files.createDirectories(path.getParent());
logger.info("Created missing directories");
} catch (final IOException e) {
logger.log(Level.SEVERE, e.getLocalizedMessage(), e);
}
}
createBackup(path);
List<StoredObject> list = new ArrayList<>();
list.addAll(query(objects, Budget.class));
list.addAll(query(objects, Config.class));
list.addAll(query(objects, CommodityNode.class));
list.addAll(query(objects, ExchangeRate.class));
list.addAll(query(objects, RootAccount.class));
list.addAll(query(objects, Reminder.class));
// remove any objects marked for removal
list.removeIf(StoredObject::isMarkedForRemoval);
// sort the list
list.sort(new StoredObjectComparator());
logger.info("Writing Binary file");
try (final OutputStream os = new BufferedOutputStream(Files.newOutputStream(path))) {
final XStream xstream = configureXStream(new XStreamOut(new PureJavaReflectionProvider(), new BinaryStreamDriver()));
try (final ObjectOutputStream out = xstream.createObjectOutputStream(os)) {
out.writeObject(list);
out.flush();
}
// forcibly flush before letting go of the resources to help older windows systems write correctly
os.flush();
} catch (IOException e) {
logger.log(Level.SEVERE, e.getLocalizedMessage(), e);
}
logger.info("Writing Binary file complete");
}
use of jgnash.engine.StoredObjectComparator in project jgnash by ccavanaugh.
the class XMLContainer method writeXML.
/**
* Writes an XML file given a collection of StoredObjects. TrashObjects and
* objects marked for removal are not written. If the file already exists,
* it will be overwritten.
*
* @param objects Collection of StoredObjects to write
* @param path file to write
*/
static synchronized void writeXML(final Collection<StoredObject> objects, final Path path) {
Logger logger = Logger.getLogger(XMLContainer.class.getName());
if (!Files.exists(path.getParent())) {
try {
Files.createDirectories(path.getParent());
logger.info("Created missing directories");
} catch (final IOException e) {
logger.log(Level.SEVERE, e.getLocalizedMessage(), e);
}
}
createBackup(path);
List<StoredObject> list = new ArrayList<>();
list.addAll(query(objects, Budget.class));
list.addAll(query(objects, Config.class));
list.addAll(query(objects, CommodityNode.class));
list.addAll(query(objects, ExchangeRate.class));
list.addAll(query(objects, RootAccount.class));
list.addAll(query(objects, Reminder.class));
// remove any objects marked for removal
list.removeIf(StoredObject::isMarkedForRemoval);
// sort the list
list.sort(new StoredObjectComparator());
logger.info("Writing XML file");
try (final Writer writer = Files.newBufferedWriter(path, StandardCharsets.UTF_8)) {
writer.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
writer.write("<?fileFormat " + Engine.CURRENT_MAJOR_VERSION + "." + Engine.CURRENT_MINOR_VERSION + "?>\n");
final XStream xstream = configureXStream(new XStreamOut(new PureJavaReflectionProvider(), new KXml2Driver()));
try (final ObjectOutputStream out = xstream.createObjectOutputStream(new PrettyPrintWriter(writer))) {
out.writeObject(list);
// forcibly flush before letting go of the resources to help older windows systems write correctly
out.flush();
} catch (final Exception e) {
logger.log(Level.SEVERE, e.getLocalizedMessage(), e);
}
} catch (final IOException e) {
logger.log(Level.SEVERE, e.getLocalizedMessage(), e);
}
logger.info("Writing XML file complete");
}
Aggregations