Search in sources :

Example 1 with QuickWriter

use of com.thoughtworks.xstream.core.util.QuickWriter in project openolat by klemens.

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 2 with QuickWriter

use of com.thoughtworks.xstream.core.util.QuickWriter 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 3 with QuickWriter

use of com.thoughtworks.xstream.core.util.QuickWriter 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

XStream (com.thoughtworks.xstream.XStream)3 QuickWriter (com.thoughtworks.xstream.core.util.QuickWriter)3 HierarchicalStreamWriter (com.thoughtworks.xstream.io.HierarchicalStreamWriter)3 PrettyPrintWriter (com.thoughtworks.xstream.io.xml.PrettyPrintWriter)3 XppDriver (com.thoughtworks.xstream.io.xml.XppDriver)3 Writer (java.io.Writer)3