Search in sources :

Example 16 with PrettyPrintWriter

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

the class TestCswRecordConverter 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 17 with PrettyPrintWriter

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

the class TestCswRecordConverter 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, ElementSetType.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 18 with PrettyPrintWriter

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

the class TestGmdConverter method convert.

private String convert(Object object, boolean writeNamespaces) {
    GmdConverter converter = new GmdConverter();
    StringWriter stringWriter = new StringWriter();
    PrettyPrintWriter writer = new PrettyPrintWriter(stringWriter, new NoNameCoder());
    MarshallingContext context = new TreeMarshaller(writer, null, null);
    context.put(CswConstants.WRITE_NAMESPACES, writeNamespaces);
    converter.marshal(object, writer, context);
    return stringWriter.toString();
}
Also used : TreeMarshaller(com.thoughtworks.xstream.core.TreeMarshaller) StringWriter(java.io.StringWriter) PrettyPrintWriter(com.thoughtworks.xstream.io.xml.PrettyPrintWriter) MarshallingContext(com.thoughtworks.xstream.converters.MarshallingContext) NoNameCoder(com.thoughtworks.xstream.io.naming.NoNameCoder)

Example 19 with PrettyPrintWriter

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

the class GlossaryItemManager method saveToFile.

// TODO:RH:gloss improvement: dtd in xml files
/**
 * writes glossary to xml-file
 * prepend doc-book dtd:
 * <!DOCTYPE glossary PUBLIC "-//OASIS//DTD DocBook XML V4.1.2//EN"          "http://www.oasis-open.org/docbook/xml/4.1.2/docbookx.dtd">
 *
 * @param glossaryFile
 * @param glossaryItemArr
 */
private void saveToFile(VFSLeaf glossaryFile, ArrayList<GlossaryItem> glossaryItemArr) {
    // cdata-tags should be used instead of strings, overwrite writer.
    XStream xstream = new XStream(new XppDriver() {

        public HierarchicalStreamWriter createWriter(Writer out) {
            return new PrettyPrintWriter(out) {

                protected void writeText(QuickWriter writer, String text) {
                    if (text.contains("<") || text.contains(">") || text.contains("&")) {
                        writer.write("<![CDATA[");
                        writer.write(text);
                        writer.write("]]>");
                    } else {
                        writer.write(text);
                    }
                }
            };
        }
    });
    xstream.alias(XML_GLOSSARY_ITEM_NAME, GlossaryItem.class);
    xstream.alias(XML_REVISION_NAME, Revision.class);
    glossaryItemArr = removeEmptyGlossaryItems(glossaryItemArr);
    XStreamHelper.writeObject(xstream, glossaryFile, glossaryItemArr);
}
Also used : XppDriver(com.thoughtworks.xstream.io.xml.XppDriver) HierarchicalStreamWriter(com.thoughtworks.xstream.io.HierarchicalStreamWriter) XStream(com.thoughtworks.xstream.XStream) PrettyPrintWriter(com.thoughtworks.xstream.io.xml.PrettyPrintWriter) QuickWriter(com.thoughtworks.xstream.core.util.QuickWriter) HierarchicalStreamWriter(com.thoughtworks.xstream.io.HierarchicalStreamWriter) QuickWriter(com.thoughtworks.xstream.core.util.QuickWriter) PrettyPrintWriter(com.thoughtworks.xstream.io.xml.PrettyPrintWriter) Writer(java.io.Writer)

Example 20 with PrettyPrintWriter

use of com.thoughtworks.xstream.io.xml.PrettyPrintWriter in project weixin-java-tools by chanjarster.

the class XStreamInitializer method getInstance.

public static XStream getInstance() {
    XStream xstream = new XStream(new XppDriver() {

        @Override
        public HierarchicalStreamWriter createWriter(Writer out) {
            return new PrettyPrintWriter(out, getNameCoder()) {

                protected String PREFIX_CDATA = "<![CDATA[";

                protected String SUFFIX_CDATA = "]]>";

                protected String PREFIX_MEDIA_ID = "<MediaId>";

                protected String SUFFIX_MEDIA_ID = "</MediaId>";

                @Override
                protected void writeText(QuickWriter writer, String text) {
                    if (text.startsWith(PREFIX_CDATA) && text.endsWith(SUFFIX_CDATA)) {
                        writer.write(text);
                    } else if (text.startsWith(PREFIX_MEDIA_ID) && text.endsWith(SUFFIX_MEDIA_ID)) {
                        writer.write(text);
                    } else {
                        super.writeText(writer, text);
                    }
                }
            };
        }
    });
    xstream.ignoreUnknownElements();
    xstream.setMode(XStream.NO_REFERENCES);
    xstream.addPermission(NullPermission.NULL);
    xstream.addPermission(PrimitiveTypePermission.PRIMITIVES);
    return xstream;
}
Also used : XppDriver(com.thoughtworks.xstream.io.xml.XppDriver) HierarchicalStreamWriter(com.thoughtworks.xstream.io.HierarchicalStreamWriter) XStream(com.thoughtworks.xstream.XStream) PrettyPrintWriter(com.thoughtworks.xstream.io.xml.PrettyPrintWriter) QuickWriter(com.thoughtworks.xstream.core.util.QuickWriter) HierarchicalStreamWriter(com.thoughtworks.xstream.io.HierarchicalStreamWriter) PrettyPrintWriter(com.thoughtworks.xstream.io.xml.PrettyPrintWriter) Writer(java.io.Writer) QuickWriter(com.thoughtworks.xstream.core.util.QuickWriter)

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