Search in sources :

Example 6 with HierarchicalStreamWriter

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

the class TestCswMarshallHelper method testWriteAllFields.

@Test
public void testWriteAllFields() {
    HierarchicalStreamWriter writer = mock(HierarchicalStreamWriter.class);
    MarshallingContext context = mock(MarshallingContext.class);
    Attribute attribute = mock(Attribute.class);
    when(attribute.getValues()).thenReturn(Arrays.asList(new String[] { "TEST1", "TEST2", "TEST3" }));
    MetacardImpl metacard = mock(MetacardImpl.class);
    MetacardType metacardType = mock(MetacardType.class);
    when(metacard.getMetacardType()).thenReturn(metacardType);
    when(metacard.getAttribute(any(String.class))).thenReturn(attribute);
    Set<AttributeDescriptor> attributeDescriptors = new HashSet<>();
    AttributeDescriptor ad = mock(AttributeDescriptor.class);
    when(ad.isMultiValued()).thenReturn(true);
    when(ad.getName()).thenReturn(CswConstants.CSW_SOURCE_QNAME.toString());
    attributeDescriptors.add(ad);
    when(metacardType.getAttributeDescriptors()).thenReturn(attributeDescriptors);
    CswMarshallHelper.writeAllFields(writer, context, metacard);
    verify(writer, times(3)).startNode(any(String.class));
    verify(writer, times(3)).setValue(any(String.class));
    verify(writer, times(3)).endNode();
}
Also used : HierarchicalStreamWriter(com.thoughtworks.xstream.io.HierarchicalStreamWriter) Attribute(ddf.catalog.data.Attribute) AttributeDescriptor(ddf.catalog.data.AttributeDescriptor) MarshallingContext(com.thoughtworks.xstream.converters.MarshallingContext) MetacardImpl(ddf.catalog.data.impl.MetacardImpl) MetacardType(ddf.catalog.data.MetacardType) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 7 with HierarchicalStreamWriter

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

the class TestCswMarshallHelper method testWriteTemporalData.

@Test
public void testWriteTemporalData() {
    HierarchicalStreamWriter writer = mock(HierarchicalStreamWriter.class);
    MarshallingContext context = mock(MarshallingContext.class);
    MetacardImpl metacard = mock(MetacardImpl.class);
    DateTime effectiveDate = new DateTime(2015, 01, 01, 13, 15);
    DateTime expirationDate = new DateTime(2015, 06, 01, 10, 45);
    when(metacard.getEffectiveDate()).thenReturn(effectiveDate.toDate());
    when(metacard.getExpirationDate()).thenReturn(expirationDate.toDate());
    CswMarshallHelper.writeTemporalData(writer, context, metacard);
    verify(writer, times(1)).startNode(any(String.class));
    verify(writer, times(1)).setValue(any(String.class));
    verify(writer, times(1)).endNode();
}
Also used : HierarchicalStreamWriter(com.thoughtworks.xstream.io.HierarchicalStreamWriter) MarshallingContext(com.thoughtworks.xstream.converters.MarshallingContext) MetacardImpl(ddf.catalog.data.impl.MetacardImpl) DateTime(org.joda.time.DateTime) Test(org.junit.Test)

Example 8 with HierarchicalStreamWriter

use of com.thoughtworks.xstream.io.HierarchicalStreamWriter 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 9 with HierarchicalStreamWriter

use of com.thoughtworks.xstream.io.HierarchicalStreamWriter in project spring-framework by spring-projects.

the class XStreamMarshaller method marshalDomNode.

// Marshalling
@Override
protected void marshalDomNode(Object graph, Node node) throws XmlMappingException {
    HierarchicalStreamWriter streamWriter;
    if (node instanceof Document) {
        streamWriter = new DomWriter((Document) node, this.nameCoder);
    } else if (node instanceof Element) {
        streamWriter = new DomWriter((Element) node, node.getOwnerDocument(), this.nameCoder);
    } else {
        throw new IllegalArgumentException("DOMResult contains neither Document nor Element");
    }
    doMarshal(graph, streamWriter, null);
}
Also used : DomWriter(com.thoughtworks.xstream.io.xml.DomWriter) HierarchicalStreamWriter(com.thoughtworks.xstream.io.HierarchicalStreamWriter) Element(org.w3c.dom.Element) Document(org.w3c.dom.Document)

Example 10 with HierarchicalStreamWriter

use of com.thoughtworks.xstream.io.HierarchicalStreamWriter in project spring-framework by spring-projects.

the class XStreamMarshallerTests method jsonDriver.

@Test
public void jsonDriver() throws Exception {
    marshaller.setStreamDriver(new JsonHierarchicalStreamDriver() {

        @Override
        public HierarchicalStreamWriter createWriter(Writer writer) {
            return new JsonWriter(writer, JsonWriter.DROP_ROOT_MODE, new JsonWriter.Format(new char[0], new char[0], JsonWriter.Format.SPACE_AFTER_LABEL | JsonWriter.Format.COMPACT_EMPTY_ELEMENT));
        }
    });
    Writer writer = new StringWriter();
    marshaller.marshal(flight, new StreamResult(writer));
    assertEquals("Invalid result", "{\"flightNumber\": 42}", writer.toString());
}
Also used : StringWriter(java.io.StringWriter) StreamResult(javax.xml.transform.stream.StreamResult) HierarchicalStreamWriter(com.thoughtworks.xstream.io.HierarchicalStreamWriter) JsonHierarchicalStreamDriver(com.thoughtworks.xstream.io.json.JsonHierarchicalStreamDriver) JsonWriter(com.thoughtworks.xstream.io.json.JsonWriter) HierarchicalStreamWriter(com.thoughtworks.xstream.io.HierarchicalStreamWriter) JsonWriter(com.thoughtworks.xstream.io.json.JsonWriter) XMLEventWriter(javax.xml.stream.XMLEventWriter) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) Writer(java.io.Writer) StringWriter(java.io.StringWriter) Test(org.junit.Test)

Aggregations

HierarchicalStreamWriter (com.thoughtworks.xstream.io.HierarchicalStreamWriter)11 Test (org.junit.Test)7 MarshallingContext (com.thoughtworks.xstream.converters.MarshallingContext)6 XStream (com.thoughtworks.xstream.XStream)4 StringWriter (java.io.StringWriter)4 Writer (java.io.Writer)4 TreeMarshaller (com.thoughtworks.xstream.core.TreeMarshaller)3 WstxDriver (com.thoughtworks.xstream.io.xml.WstxDriver)3 XppDriver (com.thoughtworks.xstream.io.xml.XppDriver)3 Map (java.util.Map)3 NoNameCoder (com.thoughtworks.xstream.io.naming.NoNameCoder)2 CompactWriter (com.thoughtworks.xstream.io.xml.CompactWriter)2 Metacard (ddf.catalog.data.Metacard)2 BinaryContentImpl (ddf.catalog.data.impl.BinaryContentImpl)2 MetacardImpl (ddf.catalog.data.impl.MetacardImpl)2 HashMap (java.util.HashMap)2 MimeType (javax.activation.MimeType)2 DateTimeConverter (org.apache.camel.component.salesforce.api.utils.DateTimeConverter)2 Matchers.anyString (org.mockito.Matchers.anyString)2 ObjectWriter (com.fasterxml.jackson.databind.ObjectWriter)1