Search in sources :

Example 21 with PrettyPrintWriter

use of com.thoughtworks.xstream.io.xml.PrettyPrintWriter in project ddf by codice.

the class CswRecordConverterTest method testMarshalRecord.

@Test
public void testMarshalRecord() throws IOException, JAXBException, SAXException, XpathException {
    Metacard metacard = getTestMetacard();
    StringWriter stringWriter = new StringWriter();
    PrettyPrintWriter writer = new PrettyPrintWriter(stringWriter);
    MarshallingContext context = new TreeMarshaller(writer, null, null);
    converter.marshal(metacard, writer, context);
    String xml = stringWriter.toString();
    assertThat(xml, containsString(CswConstants.CSW_RECORD));
    assertRecordXml(xml, metacard, FULL);
}
Also used : TreeMarshaller(com.thoughtworks.xstream.core.TreeMarshaller) Metacard(ddf.catalog.data.Metacard) StringWriter(java.io.StringWriter) PrettyPrintWriter(com.thoughtworks.xstream.io.xml.PrettyPrintWriter) MarshallingContext(com.thoughtworks.xstream.converters.MarshallingContext) Matchers.containsString(org.hamcrest.Matchers.containsString) Test(org.junit.Test)

Example 22 with PrettyPrintWriter

use of com.thoughtworks.xstream.io.xml.PrettyPrintWriter in project ddf by codice.

the class CswRecordConverterTest method testMarshalSummaryRecord.

@Test
public void testMarshalSummaryRecord() throws IOException, JAXBException, SAXException, XpathException {
    Metacard metacard = getTestMetacard();
    StringWriter stringWriter = new StringWriter();
    PrettyPrintWriter writer = new PrettyPrintWriter(stringWriter);
    MarshallingContext context = new TreeMarshaller(writer, null, null);
    context.put(CswConstants.ELEMENT_SET_TYPE, SUMMARY);
    converter.marshal(metacard, writer, context);
    String xml = stringWriter.toString();
    assertThat(xml, containsString(CswConstants.CSW_SUMMARY_RECORD));
    assertRecordXml(xml, metacard, SUMMARY);
}
Also used : TreeMarshaller(com.thoughtworks.xstream.core.TreeMarshaller) Metacard(ddf.catalog.data.Metacard) StringWriter(java.io.StringWriter) PrettyPrintWriter(com.thoughtworks.xstream.io.xml.PrettyPrintWriter) MarshallingContext(com.thoughtworks.xstream.converters.MarshallingContext) Matchers.containsString(org.hamcrest.Matchers.containsString) Test(org.junit.Test)

Example 23 with PrettyPrintWriter

use of com.thoughtworks.xstream.io.xml.PrettyPrintWriter 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(@NotNull final Collection<StoredObject> objects, @NotNull final Path path, @NotNull final DoubleConsumer percentCompleteConsumer) {
    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);
        }
    }
    percentCompleteConsumer.accept(0);
    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));
    list.addAll(query(objects, Tag.class));
    percentCompleteConsumer.accept(0.25);
    // remove any objects marked for removal
    list.removeIf(StoredObject::isMarkedForRemoval);
    // sort the list
    list.sort(new StoredObjectComparator());
    percentCompleteConsumer.accept(0.5);
    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 StaxDriver()));
        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");
    percentCompleteConsumer.accept(1);
}
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) StaxDriver(com.thoughtworks.xstream.io.xml.StaxDriver) RootAccount(jgnash.engine.RootAccount) StoredObject(jgnash.engine.StoredObject) Budget(jgnash.engine.budget.Budget) PrettyPrintWriter(com.thoughtworks.xstream.io.xml.PrettyPrintWriter) Tag(jgnash.engine.Tag) PrettyPrintWriter(com.thoughtworks.xstream.io.xml.PrettyPrintWriter) Writer(java.io.Writer) PureJavaReflectionProvider(com.thoughtworks.xstream.converters.reflection.PureJavaReflectionProvider)

Example 24 with PrettyPrintWriter

use of com.thoughtworks.xstream.io.xml.PrettyPrintWriter in project Digital by hneemann.

the class Settings method attributeChanged.

@Override
public void attributeChanged() {
    XStream xStream = Circuit.getxStream();
    try (Writer out = new OutputStreamWriter(new FileOutputStream(filename), "utf-8")) {
        out.write("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
        xStream.marshal(attributes, new PrettyPrintWriter(out));
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : XStream(com.thoughtworks.xstream.XStream) PrettyPrintWriter(com.thoughtworks.xstream.io.xml.PrettyPrintWriter) PrettyPrintWriter(com.thoughtworks.xstream.io.xml.PrettyPrintWriter)

Example 25 with PrettyPrintWriter

use of com.thoughtworks.xstream.io.xml.PrettyPrintWriter in project Digital by hneemann.

the class Resources method save.

void save(OutputStream out) throws IOException {
    XStream xStream = getxStream();
    try (Writer w = new OutputStreamWriter(out, "utf-8")) {
        w.write("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
        xStream.marshal(resourceMap, new PrettyPrintWriter(w));
    }
}
Also used : XStream(com.thoughtworks.xstream.XStream) PrettyPrintWriter(com.thoughtworks.xstream.io.xml.PrettyPrintWriter) HierarchicalStreamWriter(com.thoughtworks.xstream.io.HierarchicalStreamWriter) PrettyPrintWriter(com.thoughtworks.xstream.io.xml.PrettyPrintWriter)

Aggregations

PrettyPrintWriter (com.thoughtworks.xstream.io.xml.PrettyPrintWriter)28 XStream (com.thoughtworks.xstream.XStream)13 MarshallingContext (com.thoughtworks.xstream.converters.MarshallingContext)12 TreeMarshaller (com.thoughtworks.xstream.core.TreeMarshaller)12 StringWriter (java.io.StringWriter)12 Metacard (ddf.catalog.data.Metacard)8 Matchers.containsString (org.hamcrest.Matchers.containsString)8 Test (org.junit.Test)8 IOException (java.io.IOException)6 Writer (java.io.Writer)6 HierarchicalStreamWriter (com.thoughtworks.xstream.io.HierarchicalStreamWriter)5 FileOutputStream (java.io.FileOutputStream)4 ObjectOutputStream (java.io.ObjectOutputStream)4 QuickWriter (com.thoughtworks.xstream.core.util.QuickWriter)3 NoNameCoder (com.thoughtworks.xstream.io.naming.NoNameCoder)3 StaxDriver (com.thoughtworks.xstream.io.xml.StaxDriver)3 XppDriver (com.thoughtworks.xstream.io.xml.XppDriver)3 FileWriter (java.io.FileWriter)3 OutputStreamWriter (java.io.OutputStreamWriter)3 ArrayList (java.util.ArrayList)3