Search in sources :

Example 1 with KXml2Driver

use of com.thoughtworks.xstream.io.xml.KXml2Driver 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)

Example 2 with KXml2Driver

use of com.thoughtworks.xstream.io.xml.KXml2Driver in project jgnash by ccavanaugh.

the class XMLContainer method readXML.

void readXML() {
    // A file lock will be held on Windows OS when reading
    try (final Reader reader = Files.newBufferedReader(path, StandardCharsets.UTF_8)) {
        readWriteLock.writeLock().lock();
        final XStream xstream = configureXStream(new XStream(new StoredObjectReflectionProvider(objects), new KXml2Driver()));
        // Filters out any java.sql.Dates that sneaked in when saving from a relational database
        // and forces to a LocalDate
        // TODO: Remove at a later date
        xstream.alias("sql-date", LocalDate.class);
        try (final ObjectInputStream in = xstream.createObjectInputStream(reader)) {
            in.readObject();
        }
    } catch (final IOException | ClassNotFoundException e) {
        Logger.getLogger(XMLContainer.class.getName()).log(Level.SEVERE, null, e);
    } finally {
        if (!acquireFileLock()) {
            // lock the file on open
            Logger.getLogger(XMLContainer.class.getName()).severe("Could not acquire the file lock");
        }
        readWriteLock.writeLock().unlock();
    }
}
Also used : KXml2Driver(com.thoughtworks.xstream.io.xml.KXml2Driver) XStream(com.thoughtworks.xstream.XStream) Reader(java.io.Reader) IOException(java.io.IOException) ObjectInputStream(java.io.ObjectInputStream)

Example 3 with KXml2Driver

use of com.thoughtworks.xstream.io.xml.KXml2Driver in project jgnash by ccavanaugh.

the class AccountTreeXMLFactory method getStream.

private static XStream getStream() {
    final XStream xstream = new XStream(new PureJavaReflectionProvider(), new KXml2Driver()) {

        @Override
        protected MapperWrapper wrapMapper(final MapperWrapper next) {
            return new HibernateMapper(next);
        }
    };
    // gracefully ignore fields in the file that do not have object members
    xstream.ignoreUnknownElements();
    xstream.setMode(XStream.ID_REFERENCES);
    xstream.alias("Account", Account.class);
    xstream.alias("RootAccount", RootAccount.class);
    xstream.alias("CurrencyNode", CurrencyNode.class);
    xstream.alias("SecurityNode", SecurityNode.class);
    xstream.useAttributeFor(Account.class, "placeHolder");
    xstream.useAttributeFor(Account.class, "locked");
    xstream.useAttributeFor(Account.class, "visible");
    xstream.useAttributeFor(Account.class, "name");
    xstream.useAttributeFor(Account.class, "description");
    xstream.useAttributeFor(CommodityNode.class, "symbol");
    xstream.useAttributeFor(CommodityNode.class, "scale");
    xstream.useAttributeFor(CommodityNode.class, "prefix");
    xstream.useAttributeFor(CommodityNode.class, "suffix");
    xstream.useAttributeFor(CommodityNode.class, "description");
    xstream.omitField(StoredObject.class, "uuid");
    xstream.omitField(StoredObject.class, "markedForRemoval");
    // Ignore fields required for JPA
    xstream.omitField(StoredObject.class, "version");
    xstream.omitField(Account.class, "transactions");
    xstream.omitField(Account.class, "accountBalance");
    xstream.omitField(Account.class, "reconciledBalance");
    xstream.omitField(Account.class, "attributes");
    xstream.omitField(Account.class, "propertyMap");
    xstream.omitField(Account.class, "amortizeObject");
    xstream.omitField(SecurityNode.class, "historyNodes");
    xstream.omitField(SecurityNode.class, "securityHistoryEvents");
    // Filters out the hibernate
    xstream.registerConverter(new HibernateProxyConverter());
    xstream.registerConverter(new HibernatePersistentCollectionConverter(xstream.getMapper()));
    xstream.registerConverter(new HibernatePersistentMapConverter(xstream.getMapper()));
    xstream.registerConverter(new HibernatePersistentSortedMapConverter(xstream.getMapper()));
    xstream.registerConverter(new HibernatePersistentSortedSetConverter(xstream.getMapper()));
    // Converters for new Java time API
    xstream.registerConverter(new LocalDateConverter());
    xstream.registerConverter(new LocalDateTimeConverter());
    return xstream;
}
Also used : HibernatePersistentCollectionConverter(com.thoughtworks.xstream.hibernate.converter.HibernatePersistentCollectionConverter) MapperWrapper(com.thoughtworks.xstream.mapper.MapperWrapper) LocalDateConverter(jgnash.engine.xstream.LocalDateConverter) KXml2Driver(com.thoughtworks.xstream.io.xml.KXml2Driver) HibernatePersistentSortedMapConverter(com.thoughtworks.xstream.hibernate.converter.HibernatePersistentSortedMapConverter) HibernatePersistentSortedSetConverter(com.thoughtworks.xstream.hibernate.converter.HibernatePersistentSortedSetConverter) XStream(com.thoughtworks.xstream.XStream) HibernateMapper(com.thoughtworks.xstream.hibernate.mapper.HibernateMapper) HibernatePersistentMapConverter(com.thoughtworks.xstream.hibernate.converter.HibernatePersistentMapConverter) PureJavaReflectionProvider(com.thoughtworks.xstream.converters.reflection.PureJavaReflectionProvider) HibernateProxyConverter(com.thoughtworks.xstream.hibernate.converter.HibernateProxyConverter) LocalDateTimeConverter(jgnash.engine.xstream.LocalDateTimeConverter)

Aggregations

XStream (com.thoughtworks.xstream.XStream)3 KXml2Driver (com.thoughtworks.xstream.io.xml.KXml2Driver)3 PureJavaReflectionProvider (com.thoughtworks.xstream.converters.reflection.PureJavaReflectionProvider)2 IOException (java.io.IOException)2 HibernatePersistentCollectionConverter (com.thoughtworks.xstream.hibernate.converter.HibernatePersistentCollectionConverter)1 HibernatePersistentMapConverter (com.thoughtworks.xstream.hibernate.converter.HibernatePersistentMapConverter)1 HibernatePersistentSortedMapConverter (com.thoughtworks.xstream.hibernate.converter.HibernatePersistentSortedMapConverter)1 HibernatePersistentSortedSetConverter (com.thoughtworks.xstream.hibernate.converter.HibernatePersistentSortedSetConverter)1 HibernateProxyConverter (com.thoughtworks.xstream.hibernate.converter.HibernateProxyConverter)1 HibernateMapper (com.thoughtworks.xstream.hibernate.mapper.HibernateMapper)1 PrettyPrintWriter (com.thoughtworks.xstream.io.xml.PrettyPrintWriter)1 MapperWrapper (com.thoughtworks.xstream.mapper.MapperWrapper)1 ObjectInputStream (java.io.ObjectInputStream)1 ObjectOutputStream (java.io.ObjectOutputStream)1 Reader (java.io.Reader)1 Writer (java.io.Writer)1 ArrayList (java.util.ArrayList)1 Logger (java.util.logging.Logger)1 CommodityNode (jgnash.engine.CommodityNode)1 Config (jgnash.engine.Config)1