Search in sources :

Example 71 with XStream

use of com.thoughtworks.xstream.XStream 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 72 with XStream

use of com.thoughtworks.xstream.XStream 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 73 with XStream

use of com.thoughtworks.xstream.XStream 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 74 with XStream

use of com.thoughtworks.xstream.XStream in project bamboobsc by billchen198318.

the class ApiRouteBuilder method configure.

@Override
public void configure() throws Exception {
    this.from("servlet:///api").process(new Processor() {

        @Override
        public void process(Exchange exchange) throws Exception {
            String format = StringUtils.defaultString(exchange.getIn().getHeader("format", String.class)).trim().toLowerCase();
            String visionOid = StringUtils.defaultString(exchange.getIn().getHeader("visionOid", String.class)).trim().toLowerCase();
            String startDate = StringUtils.defaultString(exchange.getIn().getHeader("startDate", String.class)).trim().toLowerCase();
            String endDate = StringUtils.defaultString(exchange.getIn().getHeader("endDate", String.class)).trim().toLowerCase();
            String startYearDate = StringUtils.defaultString(exchange.getIn().getHeader("startYearDate", String.class)).trim().toLowerCase();
            String endYearDate = StringUtils.defaultString(exchange.getIn().getHeader("endYearDate", String.class)).trim().toLowerCase();
            String frequency = StringUtils.defaultString(exchange.getIn().getHeader("frequency", String.class)).trim().toLowerCase();
            String dataFor = StringUtils.defaultString(exchange.getIn().getHeader("dataFor", String.class)).trim().toLowerCase();
            String measureDataOrganizationOid = StringUtils.defaultString(exchange.getIn().getHeader("measureDataOrganizationOid", String.class)).trim().toLowerCase();
            String measureDataEmployeeOid = StringUtils.defaultString(exchange.getIn().getHeader("measureDataEmployeeOid", String.class)).trim().toLowerCase();
            String contentFlag = StringUtils.defaultString(exchange.getIn().getHeader("contentFlag", String.class)).trim().toUpperCase();
            String responseContent = "";
            // 請參考 gsbsc-web applicationContext-STANDARD-CXF.xml
            ApiWebService apiWebService = (ApiWebService) AppContext.getBean("bsc.webservice.ApiWebService");
            BscApiServiceResponse responseObj = apiWebService.getScorecard1(visionOid, startDate, endDate, startYearDate, endYearDate, frequency, dataFor, measureDataOrganizationOid, measureDataEmployeeOid, contentFlag);
            if ("xml".equals(format)) {
                // xml
                XStream xstream = new XStream();
                xstream.setMode(XStream.NO_REFERENCES);
                xstream.alias("response", BscApiServiceResponse.class);
                responseContent = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + xstream.toXML(responseObj);
            } else {
                // json
                ObjectMapper mapper = new ObjectMapper();
                responseContent = mapper.writeValueAsString(responseObj);
            }
            exchange.getOut().setBody(responseContent);
        }
    }).to("stream:out");
}
Also used : Exchange(org.apache.camel.Exchange) Processor(org.apache.camel.Processor) ApiWebService(com.netsteadfast.greenstep.bsc.webservice.ApiWebService) BscApiServiceResponse(com.netsteadfast.greenstep.bsc.vo.BscApiServiceResponse) XStream(com.thoughtworks.xstream.XStream) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 75 with XStream

use of com.thoughtworks.xstream.XStream in project restfulie-java by caelum.

the class OpenSearchTest method setUp.

@Before
public void setUp() {
    xstream = new XStream();
    xstream.registerConverter(new DefaultUrlConverter(Restfulie.custom()));
    xstream.registerConverter(new DefaultTagsConveter());
    xstream.processAnnotations(SearchDescription.class);
}
Also used : XStream(com.thoughtworks.xstream.XStream) DefaultUrlConverter(br.com.caelum.restfulie.opensearch.conveter.DefaultUrlConverter) DefaultTagsConveter(br.com.caelum.restfulie.opensearch.conveter.DefaultTagsConveter) Before(org.junit.Before)

Aggregations

XStream (com.thoughtworks.xstream.XStream)199 Test (org.junit.Test)55 IOException (java.io.IOException)33 InputStream (java.io.InputStream)29 WstxDriver (com.thoughtworks.xstream.io.xml.WstxDriver)22 Metacard (ddf.catalog.data.Metacard)21 DomDriver (com.thoughtworks.xstream.io.xml.DomDriver)17 HashMap (java.util.HashMap)14 ByteArrayInputStream (java.io.ByteArrayInputStream)13 StaxDriver (com.thoughtworks.xstream.io.xml.StaxDriver)12 XStreamUtils.createTrustingXStream (org.kie.soup.commons.xstream.XStreamUtils.createTrustingXStream)12 Matchers.anyString (org.mockito.Matchers.anyString)11 HierarchicalStreamWriter (com.thoughtworks.xstream.io.HierarchicalStreamWriter)10 CswRecordCollection (org.codice.ddf.spatial.ogc.csw.catalog.common.CswRecordCollection)10 FileNotFoundException (java.io.FileNotFoundException)9 Writer (java.io.Writer)8 ArrayList (java.util.ArrayList)8 GmlGeometryConverter (org.codice.ddf.spatial.ogc.wfs.catalog.converter.impl.GmlGeometryConverter)8 XStreamException (com.thoughtworks.xstream.XStreamException)7 MarshallingContext (com.thoughtworks.xstream.converters.MarshallingContext)7