Search in sources :

Example 6 with PrettyPrintWriter

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

the class AbstractGmdTransformer method transform.

@Override
public BinaryContent transform(Metacard metacard, Map<String, Serializable> arguments) throws CatalogTransformerException {
    StringWriter stringWriter = new StringWriter();
    Boolean omitXmlDec = null;
    if (MapUtils.isNotEmpty(arguments)) {
        omitXmlDec = (Boolean) arguments.get(CswConstants.OMIT_XML_DECLARATION);
    }
    if (omitXmlDec == null || !omitXmlDec) {
        stringWriter.append("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n");
    }
    PrettyPrintWriter writer = new PrettyPrintWriter(stringWriter, new NoNameCoder());
    MarshallingContext context = new TreeMarshaller(writer, null, null);
    copyArgumentsToContext(context, arguments);
    converterSupplier.get().marshal(metacard, writer, context);
    ByteArrayInputStream bais = new ByteArrayInputStream(stringWriter.toString().getBytes(StandardCharsets.UTF_8));
    return new BinaryContentImpl(bais, CswRecordConverter.XML_MIME_TYPE);
}
Also used : TreeMarshaller(com.thoughtworks.xstream.core.TreeMarshaller) StringWriter(java.io.StringWriter) ByteArrayInputStream(java.io.ByteArrayInputStream) PrettyPrintWriter(com.thoughtworks.xstream.io.xml.PrettyPrintWriter) MarshallingContext(com.thoughtworks.xstream.converters.MarshallingContext) BinaryContentImpl(ddf.catalog.data.impl.BinaryContentImpl) NoNameCoder(com.thoughtworks.xstream.io.naming.NoNameCoder)

Example 7 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)

Example 8 with PrettyPrintWriter

use of com.thoughtworks.xstream.io.xml.PrettyPrintWriter in project openhab1-addons by openhab.

the class ZWaveNodeSerializer method SerializeNode.

/**
     * Serializes an XML tree of a {@link ZWaveNode}
     *
     * @param node
     *            the node to serialize
     */
public void SerializeNode(ZWaveNode node) {
    synchronized (stream) {
        // then when the binding starts it will have incomplete information!
        if (node.getNodeInitializationStage().isStaticComplete() == false) {
            logger.debug("NODE {}: Serialise aborted as static stages not complete", node.getNodeId());
            return;
        }
        File file = new File(this.folderName, String.format("node%d.xml", node.getNodeId()));
        BufferedWriter writer = null;
        logger.debug("NODE {}: Serializing to file {}", node.getNodeId(), file.getPath());
        try {
            writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF-8"));
            stream.marshal(node, new PrettyPrintWriter(writer));
            writer.flush();
        } catch (IOException e) {
            logger.error("NODE {}: Error serializing to file: {}", node.getNodeId(), e.getMessage());
        } finally {
            if (writer != null) {
                try {
                    writer.close();
                } catch (IOException e) {
                }
            }
        }
    }
}
Also used : FileOutputStream(java.io.FileOutputStream) OutputStreamWriter(java.io.OutputStreamWriter) PrettyPrintWriter(com.thoughtworks.xstream.io.xml.PrettyPrintWriter) IOException(java.io.IOException) File(java.io.File) BufferedWriter(java.io.BufferedWriter)

Example 9 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 10 with PrettyPrintWriter

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

the class TestCswRecordConverter method testMarshalBriefRecord.

@Test
public void testMarshalBriefRecord() 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.BRIEF);
    converter.marshal(metacard, writer, context);
    String xml = stringWriter.toString();
    assertThat(xml, containsString(CswConstants.CSW_BRIEF_RECORD));
    assertRecordXml(xml, metacard, BRIEF);
}
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)

Aggregations

PrettyPrintWriter (com.thoughtworks.xstream.io.xml.PrettyPrintWriter)13 MarshallingContext (com.thoughtworks.xstream.converters.MarshallingContext)7 TreeMarshaller (com.thoughtworks.xstream.core.TreeMarshaller)7 StringWriter (java.io.StringWriter)7 Metacard (ddf.catalog.data.Metacard)4 IOException (java.io.IOException)4 Matchers.containsString (org.hamcrest.Matchers.containsString)4 Test (org.junit.Test)4 XStream (com.thoughtworks.xstream.XStream)3 Writer (java.io.Writer)3 NoNameCoder (com.thoughtworks.xstream.io.naming.NoNameCoder)2 BinaryContentImpl (ddf.catalog.data.impl.BinaryContentImpl)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 FileWriter (java.io.FileWriter)2 ObjectOutputStream (java.io.ObjectOutputStream)2 PsiFragment (com.intellij.dupLocator.util.PsiFragment)1 XStreamException (com.thoughtworks.xstream.XStreamException)1 PureJavaReflectionProvider (com.thoughtworks.xstream.converters.reflection.PureJavaReflectionProvider)1 QuickWriter (com.thoughtworks.xstream.core.util.QuickWriter)1 HierarchicalStreamWriter (com.thoughtworks.xstream.io.HierarchicalStreamWriter)1