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();
}
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();
}
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;
}
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);
}
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());
}
Aggregations