Search in sources :

Example 21 with DomReader

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

the class CswRecordConverterTest method testUnmarshalCswRecordWithProductAndThumbnail.

@Test
public void testUnmarshalCswRecordWithProductAndThumbnail() throws Exception {
    XStream xstream = createXstream(new WstxDriver());
    xstream.registerConverter(converter);
    InputStream is = CswRecordConverterTest.class.getResourceAsStream("/Csw_Record.xml");
    // get the URL to the thumbnail image and stick it in the xml string
    // this makes the test filesystem independent
    URL thumbnail = CswRecordConverterTest.class.getResource("/ddf_globe.png");
    String xml = null;
    if (thumbnail != null) {
        StringWriter writer = new StringWriter();
        IOUtils.copy(is, writer);
        xml = writer.toString();
        xml = xml.replace(THUMBNAIL_URL, thumbnail.toString());
    }
    xstream.alias("csw:Record", MetacardImpl.class);
    DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
    Document doc = docBuilder.parse(IOUtils.toInputStream(xml, StandardCharsets.UTF_8));
    HierarchicalStreamReader reader = new DomReader(doc);
    DataHolder holder = xstream.newDataHolder();
    Metacard mc = (Metacard) xstream.unmarshal(reader, null, holder);
    assertThat(mc, notNullValue());
    String productUrl = "http://example.com/product.pdf";
    assertThat(mc.getAttribute(Core.RESOURCE_URI).getValue(), is(productUrl));
    assertThat(mc.getThumbnail(), is(getThumbnailByteArray(thumbnail)));
}
Also used : WstxDriver(com.thoughtworks.xstream.io.xml.WstxDriver) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) DomReader(com.thoughtworks.xstream.io.xml.DomReader) XStream(com.thoughtworks.xstream.XStream) InputStream(java.io.InputStream) Matchers.containsString(org.hamcrest.Matchers.containsString) Document(org.w3c.dom.Document) URL(java.net.URL) Metacard(ddf.catalog.data.Metacard) StringWriter(java.io.StringWriter) DocumentBuilder(javax.xml.parsers.DocumentBuilder) DataHolder(com.thoughtworks.xstream.converters.DataHolder) HierarchicalStreamReader(com.thoughtworks.xstream.io.HierarchicalStreamReader) Test(org.junit.Test)

Example 22 with DomReader

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

the class CswRecordConverterTest method testUnmarshalSingleCswRecordToMetacardContentTypeMapsToFormat.

@Test
public void testUnmarshalSingleCswRecordToMetacardContentTypeMapsToFormat() throws ParserConfigurationException, IOException, SAXException {
    XStream xstream = createXstream(new WstxDriver());
    xstream.registerConverter(converter);
    xstream.alias("csw:Record", MetacardImpl.class);
    DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
    Document doc = docBuilder.parse(CswRecordConverterTest.class.getResource("/Csw_Record.xml").getPath());
    HierarchicalStreamReader reader = new DomReader(doc);
    DataHolder holder = xstream.newDataHolder();
    Metacard mc = (Metacard) xstream.unmarshal(reader, null, holder);
    assertThat(mc, notNullValue());
    assertThat(mc.getContentTypeName(), is("IMAGE-PRODUCT"));
    assertThat(mc.getAttribute(Media.FORMAT).getValue(), is("PDF"));
}
Also used : WstxDriver(com.thoughtworks.xstream.io.xml.WstxDriver) Metacard(ddf.catalog.data.Metacard) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) DomReader(com.thoughtworks.xstream.io.xml.DomReader) DocumentBuilder(javax.xml.parsers.DocumentBuilder) XStream(com.thoughtworks.xstream.XStream) DataHolder(com.thoughtworks.xstream.converters.DataHolder) HierarchicalStreamReader(com.thoughtworks.xstream.io.HierarchicalStreamReader) Document(org.w3c.dom.Document) Test(org.junit.Test)

Aggregations

HierarchicalStreamReader (com.thoughtworks.xstream.io.HierarchicalStreamReader)22 DomReader (com.thoughtworks.xstream.io.xml.DomReader)22 DocumentBuilder (javax.xml.parsers.DocumentBuilder)22 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)22 Test (org.junit.Test)22 Document (org.w3c.dom.Document)22 Metacard (ddf.catalog.data.Metacard)6 XStream (com.thoughtworks.xstream.XStream)4 DataHolder (com.thoughtworks.xstream.converters.DataHolder)4 WstxDriver (com.thoughtworks.xstream.io.xml.WstxDriver)4 UnmarshallingContext (com.thoughtworks.xstream.converters.UnmarshallingContext)2 TreeUnmarshaller (com.thoughtworks.xstream.core.TreeUnmarshaller)2 InputStream (java.io.InputStream)2 StringWriter (java.io.StringWriter)2 URL (java.net.URL)2 Matchers.containsString (org.hamcrest.Matchers.containsString)2