Search in sources :

Example 1 with TreeMarshaller

use of com.thoughtworks.xstream.core.TreeMarshaller in project ddf by codice.

the class TestCswTransformProvider method testMarshalOtherSchema.

@Test
public void testMarshalOtherSchema() throws Exception {
    when(mockMetacardManager.getTransformerByProperty(TransformerManager.SCHEMA, OTHER_SCHEMA)).thenReturn(mockMetacardTransformer);
    when(mockMetacardTransformer.transform(any(Metacard.class), any(Map.class))).thenReturn(new BinaryContentImpl(IOUtils.toInputStream(getRecord()), new MimeType(MediaType.APPLICATION_XML)));
    StringWriter stringWriter = new StringWriter();
    HierarchicalStreamWriter writer = new WstxDriver().createWriter(stringWriter);
    CswTransformProvider provider = new CswTransformProvider(mockMetacardManager, null);
    MarshallingContext context = new TreeMarshaller(writer, null, null);
    context.put(CswConstants.TRANSFORMER_LOOKUP_KEY, TransformerManager.SCHEMA);
    context.put(CswConstants.TRANSFORMER_LOOKUP_VALUE, OTHER_SCHEMA);
    ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class);
    provider.marshal(getMetacard(), writer, context);
    // Verify the context arguments were set correctly
    verify(mockMetacardManager, times(1)).getTransformerByProperty(captor.capture(), captor.capture());
    String outputSchema = captor.getValue();
    assertThat(outputSchema, is(OTHER_SCHEMA));
}
Also used : WstxDriver(com.thoughtworks.xstream.io.xml.WstxDriver) TreeMarshaller(com.thoughtworks.xstream.core.TreeMarshaller) Metacard(ddf.catalog.data.Metacard) StringWriter(java.io.StringWriter) HierarchicalStreamWriter(com.thoughtworks.xstream.io.HierarchicalStreamWriter) BinaryContentImpl(ddf.catalog.data.impl.BinaryContentImpl) MarshallingContext(com.thoughtworks.xstream.converters.MarshallingContext) Matchers.anyString(org.mockito.Matchers.anyString) HashMap(java.util.HashMap) Map(java.util.Map) MimeType(javax.activation.MimeType) Test(org.junit.Test)

Example 2 with TreeMarshaller

use of com.thoughtworks.xstream.core.TreeMarshaller 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);
}
Also used : WstxDriver(com.thoughtworks.xstream.io.xml.WstxDriver) TreeMarshaller(com.thoughtworks.xstream.core.TreeMarshaller) StringWriter(java.io.StringWriter) HierarchicalStreamWriter(com.thoughtworks.xstream.io.HierarchicalStreamWriter) MarshallingContext(com.thoughtworks.xstream.converters.MarshallingContext) Test(org.junit.Test)

Example 3 with TreeMarshaller

use of com.thoughtworks.xstream.core.TreeMarshaller 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 4 with TreeMarshaller

use of com.thoughtworks.xstream.core.TreeMarshaller in project ddf by codice.

the class CswTransformProviderTest method testMarshalCswRecord.

@Test
public void testMarshalCswRecord() throws Exception {
    when(mockMetacardManager.getTransformerBySchema(CswConstants.CSW_OUTPUT_SCHEMA)).thenReturn(mockCswRecordConverter);
    when(mockCswRecordConverter.transform(any(Metacard.class), any(Map.class))).thenReturn(new BinaryContentImpl(IOUtils.toInputStream(getRecord(), StandardCharsets.UTF_8), new MimeType(MediaType.APPLICATION_XML)));
    StringWriter stringWriter = new StringWriter();
    HierarchicalStreamWriter writer = new WstxDriver().createWriter(stringWriter);
    CswTransformProvider provider = new CswTransformProvider(mockMetacardManager, null);
    MarshallingContext context = new TreeMarshaller(writer, null, null);
    ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class);
    provider.marshal(getMetacard(), writer, context);
    // Verify the context arguments were set correctly
    verify(mockMetacardManager, times(1)).getTransformerBySchema(captor.capture());
    String outputSchema = captor.getValue();
    assertThat(outputSchema, is(CswConstants.CSW_OUTPUT_SCHEMA));
}
Also used : WstxDriver(com.thoughtworks.xstream.io.xml.WstxDriver) TreeMarshaller(com.thoughtworks.xstream.core.TreeMarshaller) Metacard(ddf.catalog.data.Metacard) StringWriter(java.io.StringWriter) HierarchicalStreamWriter(com.thoughtworks.xstream.io.HierarchicalStreamWriter) BinaryContentImpl(ddf.catalog.data.impl.BinaryContentImpl) MarshallingContext(com.thoughtworks.xstream.converters.MarshallingContext) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) HashMap(java.util.HashMap) Map(java.util.Map) MimeType(javax.activation.MimeType) Test(org.junit.Test)

Example 5 with TreeMarshaller

use of com.thoughtworks.xstream.core.TreeMarshaller 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(), StandardCharsets.UTF_8), 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;
}
Also used : QName(javax.xml.namespace.QName) BinaryContentImpl(ddf.catalog.data.impl.BinaryContentImpl) BinaryContent(ddf.catalog.data.BinaryContent) XStreamException(com.thoughtworks.xstream.XStreamException) CatalogTransformerException(ddf.catalog.transform.CatalogTransformerException) MimeTypeParseException(javax.activation.MimeTypeParseException) IOException(java.io.IOException) TreeMarshaller(com.thoughtworks.xstream.core.TreeMarshaller) StringWriter(java.io.StringWriter) ByteArrayInputStream(java.io.ByteArrayInputStream) StringReader(java.io.StringReader) XMLEvent(javax.xml.stream.events.XMLEvent) XMLEventReader(javax.xml.stream.XMLEventReader) PrettyPrintWriter(com.thoughtworks.xstream.io.xml.PrettyPrintWriter) MarshallingContext(com.thoughtworks.xstream.converters.MarshallingContext)

Aggregations

MarshallingContext (com.thoughtworks.xstream.converters.MarshallingContext)18 TreeMarshaller (com.thoughtworks.xstream.core.TreeMarshaller)18 StringWriter (java.io.StringWriter)18 Test (org.junit.Test)14 PrettyPrintWriter (com.thoughtworks.xstream.io.xml.PrettyPrintWriter)12 Metacard (ddf.catalog.data.Metacard)12 Matchers.containsString (org.hamcrest.Matchers.containsString)8 HierarchicalStreamWriter (com.thoughtworks.xstream.io.HierarchicalStreamWriter)6 WstxDriver (com.thoughtworks.xstream.io.xml.WstxDriver)6 BinaryContentImpl (ddf.catalog.data.impl.BinaryContentImpl)6 HashMap (java.util.HashMap)4 Map (java.util.Map)4 MimeType (javax.activation.MimeType)4 NoNameCoder (com.thoughtworks.xstream.io.naming.NoNameCoder)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)2 Matchers.anyString (org.mockito.Matchers.anyString)2 XStreamException (com.thoughtworks.xstream.XStreamException)1 BinaryContent (ddf.catalog.data.BinaryContent)1 CatalogTransformerException (ddf.catalog.transform.CatalogTransformerException)1