use of com.thoughtworks.xstream.converters.MarshallingContext in project ddf by codice.
the class TestCswTransformProvider method testMarshalNoTransformers.
@Test(expected = ConversionException.class)
public void testMarshalNoTransformers() throws Exception {
when(mockMetacardManager.getTransformerBySchema(anyString())).thenReturn(null);
StringWriter stringWriter = new StringWriter();
HierarchicalStreamWriter writer = new WstxDriver().createWriter(stringWriter);
CswTransformProvider provider = new CswTransformProvider(mockMetacardManager, null);
MarshallingContext context = new TreeMarshaller(writer, null, null);
provider.marshal(getMetacard(), writer, context);
}
use of com.thoughtworks.xstream.converters.MarshallingContext 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.converters.MarshallingContext 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.converters.MarshallingContext in project ddf by codice.
the class CswRecordConverter method transform.
@Override
public BinaryContent transform(Metacard metacard, Map<String, Serializable> arguments) throws CatalogTransformerException {
if (StringUtils.isNotBlank(metacard.getMetadata())) {
// Check if the metadata is csw:Record
try {
StringReader xml = new StringReader(metacard.getMetadata());
XMLEventReader reader = factory.createXMLEventReader(xml);
boolean rootFound = false;
while (reader.hasNext() && !rootFound) {
XMLEvent event = reader.nextEvent();
if (event.isStartElement()) {
rootFound = true;
QName name = event.asStartElement().getName();
if (StringUtils.equals(CswConstants.CSW_RECORD_LOCAL_NAME, name.getLocalPart()) && StringUtils.equals(CswConstants.CSW_OUTPUT_SCHEMA, name.getNamespaceURI())) {
return new BinaryContentImpl(IOUtils.toInputStream(metacard.getMetadata()), XML_MIME_TYPE);
}
}
}
} catch (Exception e) {
// Ignore and proceed with the transform.
}
}
StringWriter stringWriter = new StringWriter();
Boolean omitXmlDec = null;
if (arguments != null) {
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);
MarshallingContext context = new TreeMarshaller(writer, null, null);
context.put(CswConstants.WRITE_NAMESPACES, true);
copyArgumentsToContext(context, arguments);
this.marshal(metacard, writer, context);
BinaryContent transformedContent;
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(stringWriter.toString().getBytes(StandardCharsets.UTF_8));
transformedContent = new BinaryContentImpl(byteArrayInputStream, XML_MIME_TYPE);
return transformedContent;
}
use of com.thoughtworks.xstream.converters.MarshallingContext 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);
}
Aggregations