Search in sources :

Example 1 with XStreamJVM9

use of jgnash.engine.xstream.XStreamJVM9 in project jgnash by ccavanaugh.

the class XStreamFactory method getInstance.

static XStream getInstance() {
    final XStream xstream = new XStreamJVM9(new PureJavaReflectionProvider(), new StaxDriver());
    xstream.alias("Message", Message.class);
    xstream.alias("MessageProperty", MessageProperty.class);
    return xstream;
}
Also used : StaxDriver(com.thoughtworks.xstream.io.xml.StaxDriver) XStreamJVM9(jgnash.engine.xstream.XStreamJVM9) XStream(com.thoughtworks.xstream.XStream) PureJavaReflectionProvider(com.thoughtworks.xstream.converters.reflection.PureJavaReflectionProvider)

Example 2 with XStreamJVM9

use of jgnash.engine.xstream.XStreamJVM9 in project jgnash by ccavanaugh.

the class AccountTreeXMLFactory method getStream.

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

        @Override
        protected MapperWrapper wrapMapper(final MapperWrapper next) {
            return new HibernateMapper(next);
        }
    };
    // configure XStream security
    xstream.addPermission(NoTypePermission.NONE);
    xstream.addPermission(PrimitiveTypePermission.PRIMITIVES);
    xstream.addPermission(ArrayTypePermission.ARRAYS);
    xstream.addPermission(new WildcardTypePermission(new String[] { "java.**", "jgnash.engine.**" }));
    // gracefully ignore fields in the file that do not have object members
    xstream.ignoreUnknownElements();
    xstream.setMode(XStream.ID_REFERENCES);
    // use date instead of local-date by default
    xstream.alias("date", LocalDate.class);
    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()));
    return xstream;
}
Also used : HibernatePersistentCollectionConverter(com.thoughtworks.xstream.hibernate.converter.HibernatePersistentCollectionConverter) StaxDriver(com.thoughtworks.xstream.io.xml.StaxDriver) MapperWrapper(com.thoughtworks.xstream.mapper.MapperWrapper) XStreamJVM9(jgnash.engine.xstream.XStreamJVM9) WildcardTypePermission(com.thoughtworks.xstream.security.WildcardTypePermission) 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)

Example 3 with XStreamJVM9

use of jgnash.engine.xstream.XStreamJVM9 in project jgnash by ccavanaugh.

the class BinaryXStreamTest method testFile.

@Test
void testFile() {
    final List<String> stringData = new ArrayList<>();
    for (int i = 0; i < 100; i++) {
        stringData.add(UUID.randomUUID().toString());
    }
    final List<Integer> integerData = stringData.stream().map(String::hashCode).collect(Collectors.toList());
    try {
        tempFile = Files.createTempFile("test", "");
    } catch (IOException e) {
        fail(e.toString());
    }
    try (final OutputStream fos = Files.newOutputStream(tempFile)) {
        BinaryStreamDriver bsd = new BinaryStreamDriver();
        XStream xstream = new XStreamJVM9(new PureJavaReflectionProvider(), bsd);
        try (ObjectOutputStream out = xstream.createObjectOutputStream(fos)) {
            out.writeObject(stringData);
            out.writeObject(integerData);
        }
    } catch (IOException e) {
        fail(e.toString());
    }
    assertTrue(FileMagic.isBinaryXStreamFile(tempFile));
    assertFalse(FileMagic.isOfxV2(tempFile));
    try (InputStream fis = Files.newInputStream(tempFile)) {
        BinaryStreamDriver bsd = new BinaryStreamDriver();
        XStream xstream = new XStreamJVM9(new PureJavaReflectionProvider(), bsd);
        try (ObjectInputStream in = xstream.createObjectInputStream(fis)) {
            List<?> strings = (List<?>) in.readObject();
            List<?> integers = (List<?>) in.readObject();
            assertArrayEquals(strings.toArray(), stringData.toArray());
            assertArrayEquals(integers.toArray(), integerData.toArray());
        } catch (ClassNotFoundException e) {
            fail(e.toString());
        }
    } catch (IOException e) {
        fail(e.toString());
    }
}
Also used : XStreamJVM9(jgnash.engine.xstream.XStreamJVM9) XStream(com.thoughtworks.xstream.XStream) ObjectInputStream(java.io.ObjectInputStream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) ObjectOutputStream(java.io.ObjectOutputStream) ArrayList(java.util.ArrayList) IOException(java.io.IOException) ObjectOutputStream(java.io.ObjectOutputStream) BinaryStreamDriver(com.thoughtworks.xstream.io.binary.BinaryStreamDriver) ArrayList(java.util.ArrayList) List(java.util.List) PureJavaReflectionProvider(com.thoughtworks.xstream.converters.reflection.PureJavaReflectionProvider) ObjectInputStream(java.io.ObjectInputStream) Test(org.junit.jupiter.api.Test)

Aggregations

PureJavaReflectionProvider (com.thoughtworks.xstream.converters.reflection.PureJavaReflectionProvider)3 XStreamJVM9 (jgnash.engine.xstream.XStreamJVM9)3 XStream (com.thoughtworks.xstream.XStream)2 StaxDriver (com.thoughtworks.xstream.io.xml.StaxDriver)2 HibernatePersistentCollectionConverter (com.thoughtworks.xstream.hibernate.converter.HibernatePersistentCollectionConverter)1 HibernatePersistentMapConverter (com.thoughtworks.xstream.hibernate.converter.HibernatePersistentMapConverter)1 HibernateProxyConverter (com.thoughtworks.xstream.hibernate.converter.HibernateProxyConverter)1 HibernateMapper (com.thoughtworks.xstream.hibernate.mapper.HibernateMapper)1 BinaryStreamDriver (com.thoughtworks.xstream.io.binary.BinaryStreamDriver)1 MapperWrapper (com.thoughtworks.xstream.mapper.MapperWrapper)1 WildcardTypePermission (com.thoughtworks.xstream.security.WildcardTypePermission)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 ObjectInputStream (java.io.ObjectInputStream)1 ObjectOutputStream (java.io.ObjectOutputStream)1 OutputStream (java.io.OutputStream)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Test (org.junit.jupiter.api.Test)1