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");
}
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();
}
}
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;
}
Aggregations