Search in sources :

Example 1 with StoredObjectComparator

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");
}
Also used : ExchangeRate(jgnash.engine.ExchangeRate) Reminder(jgnash.engine.recurring.Reminder) Config(jgnash.engine.Config) XStream(com.thoughtworks.xstream.XStream) BufferedOutputStream(java.io.BufferedOutputStream) ObjectOutputStream(java.io.ObjectOutputStream) OutputStream(java.io.OutputStream) ArrayList(java.util.ArrayList) CommodityNode(jgnash.engine.CommodityNode) IOException(java.io.IOException) Logger(java.util.logging.Logger) ObjectOutputStream(java.io.ObjectOutputStream) StoredObjectComparator(jgnash.engine.StoredObjectComparator) RootAccount(jgnash.engine.RootAccount) StoredObject(jgnash.engine.StoredObject) Budget(jgnash.engine.budget.Budget) BinaryStreamDriver(com.thoughtworks.xstream.io.binary.BinaryStreamDriver) BufferedOutputStream(java.io.BufferedOutputStream) PureJavaReflectionProvider(com.thoughtworks.xstream.converters.reflection.PureJavaReflectionProvider)

Example 2 with StoredObjectComparator

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");
}
Also used : ExchangeRate(jgnash.engine.ExchangeRate) Reminder(jgnash.engine.recurring.Reminder) Config(jgnash.engine.Config) XStream(com.thoughtworks.xstream.XStream) ArrayList(java.util.ArrayList) CommodityNode(jgnash.engine.CommodityNode) IOException(java.io.IOException) Logger(java.util.logging.Logger) ObjectOutputStream(java.io.ObjectOutputStream) StoredObjectComparator(jgnash.engine.StoredObjectComparator) IOException(java.io.IOException) RootAccount(jgnash.engine.RootAccount) StoredObject(jgnash.engine.StoredObject) KXml2Driver(com.thoughtworks.xstream.io.xml.KXml2Driver) Budget(jgnash.engine.budget.Budget) PrettyPrintWriter(com.thoughtworks.xstream.io.xml.PrettyPrintWriter) PrettyPrintWriter(com.thoughtworks.xstream.io.xml.PrettyPrintWriter) Writer(java.io.Writer) PureJavaReflectionProvider(com.thoughtworks.xstream.converters.reflection.PureJavaReflectionProvider)

Aggregations

XStream (com.thoughtworks.xstream.XStream)2 PureJavaReflectionProvider (com.thoughtworks.xstream.converters.reflection.PureJavaReflectionProvider)2 IOException (java.io.IOException)2 ObjectOutputStream (java.io.ObjectOutputStream)2 ArrayList (java.util.ArrayList)2 Logger (java.util.logging.Logger)2 CommodityNode (jgnash.engine.CommodityNode)2 Config (jgnash.engine.Config)2 ExchangeRate (jgnash.engine.ExchangeRate)2 RootAccount (jgnash.engine.RootAccount)2 StoredObject (jgnash.engine.StoredObject)2 StoredObjectComparator (jgnash.engine.StoredObjectComparator)2 Budget (jgnash.engine.budget.Budget)2 Reminder (jgnash.engine.recurring.Reminder)2 BinaryStreamDriver (com.thoughtworks.xstream.io.binary.BinaryStreamDriver)1 KXml2Driver (com.thoughtworks.xstream.io.xml.KXml2Driver)1 PrettyPrintWriter (com.thoughtworks.xstream.io.xml.PrettyPrintWriter)1 BufferedOutputStream (java.io.BufferedOutputStream)1 OutputStream (java.io.OutputStream)1 Writer (java.io.Writer)1