Search in sources :

Example 31 with HierarchicalStreamReader

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

the class TestGetRecordsResponseConverter method testUnmarshalParseXmlNamespaces.

@Test
public void testUnmarshalParseXmlNamespaces() throws XmlPullParserException {
    XStream xstream = new XStream(new WstxDriver());
    xstream.setClassLoader(this.getClass().getClassLoader());
    xstream.registerConverter(new GetRecordsResponseConverter(mockProvider));
    xstream.alias("csw:GetRecordsResponse", CswRecordCollection.class);
    String xml = "<?xml version='1.0' encoding='UTF-8'?>" + "<csw:GetRecordsResponse " + "xmlns:dc=\"http://purl.org/dc/elements/1.1/\" " + "xmlns:dct=\"http://purl.org/dc/terms/\" " + "xmlns:ows=\"http://www.opengis.net/ows\" " + "xmlns:csw=\"http://www.opengis.net/cat/csw/2.0.2\" " + "version=\"2.0.2\"><csw:SearchStatus " + "timestamp=\"2014-11-11T10:53:32.152-06:00\"/>" + "<csw:SearchResults numberOfRecordsMatched=\"1\" " + "numberOfRecordsReturned=\"1\" nextRecord=\"0\" " + "recordSchema=\"http://www.opengis.net/cat/csw/2.0.2\">" + "<csw:Record>\n" + "<dc:identifier>0a2e1b1d2a3755e70a96d61e6bddbc5d</dc:identifier>" + "<dct:bibliographicCitation>0a2e1b1d2a3755e70a96d61e6bddbc5d</dct:bibliographicCitation>" + "<dc:title>US woman attacks Gauguin painting</dc:title>" + "<dct:alternative>US woman attacks Gauguin painting</dct:alternative>" + "<dc:type>video</dc:type><dc:date>2011-04-06T04:49:20.230-05:00</dc:date>" + "<dct:modified>2011-04-06T04:49:20.230-05:00</dct:modified>" + "<dct:created>2011-04-06T04:49:20.230-05:00</dct:created>" + "<dct:dateAccepted>2011-04-06T04:48:26.180-05:00</dct:dateAccepted>" + "<dct:dateCopyrighted>2011-04-06T04:48:26.180-05:00</dct:dateCopyrighted><" + "dct:dateSubmitted>2011-04-06T04:49:20.230-05:00</dct:dateSubmitted>" + "<dct:issued>2011-04-06T04:49:20.230-05:00</dct:issued>" + "<dc:publisher>ddf.distribution</dc:publisher>" + "<ows:BoundingBox crs=\"urn:x-ogc:def:crs:EPSG:6.11:4326\">\n" + "    <ows:LowerCorner>-50.5056430529222 84.0285103635943</ows:LowerCorner>" + "<ows:UpperCorner>-50.5056430529222 84.0285103635943</ows:UpperCorner>" + "</ows:BoundingBox></csw:Record><" + "/csw:SearchResults>" + "</csw:GetRecordsResponse>";
    InputStream inStream = IOUtils.toInputStream(xml);
    ArgumentCaptor<UnmarshallingContext> captor = ArgumentCaptor.forClass(UnmarshallingContext.class);
    HierarchicalStreamReader reader = new XppReader(new InputStreamReader(inStream), XmlPullParserFactory.newInstance().newPullParser());
    xstream.unmarshal(reader, null, null);
    IOUtils.closeQuietly(inStream);
    verify(mockProvider, times(1)).unmarshal(any(HierarchicalStreamReader.class), captor.capture());
    UnmarshallingContext context = captor.getValue();
    assertThat(context, notNullValue());
    assertThat(context.get(CswConstants.NAMESPACE_DECLARATIONS), instanceOf(Map.class));
    Map<String, String> namespaces = (Map) context.get(CswConstants.NAMESPACE_DECLARATIONS);
    assertThat(namespaces.get(CswConstants.XMLNS + CswConstants.NAMESPACE_DELIMITER + CswConstants.CSW_NAMESPACE_PREFIX), is(CswConstants.CSW_OUTPUT_SCHEMA));
    assertThat(namespaces.get(CswConstants.XMLNS + CswConstants.NAMESPACE_DELIMITER + CswConstants.DUBLIN_CORE_NAMESPACE_PREFIX), is(CswConstants.DUBLIN_CORE_SCHEMA));
    assertThat(namespaces.get(CswConstants.XMLNS + CswConstants.NAMESPACE_DELIMITER + CswConstants.DUBLIN_CORE_TERMS_NAMESPACE_PREFIX), is(CswConstants.DUBLIN_CORE_TERMS_SCHEMA));
    assertThat(namespaces.get(CswConstants.XMLNS + CswConstants.NAMESPACE_DELIMITER + CswConstants.OWS_NAMESPACE_PREFIX), is(CswConstants.OWS_NAMESPACE));
}
Also used : WstxDriver(com.thoughtworks.xstream.io.xml.WstxDriver) InputStreamReader(java.io.InputStreamReader) XStream(com.thoughtworks.xstream.XStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) XppReader(com.thoughtworks.xstream.io.xml.XppReader) HierarchicalStreamReader(com.thoughtworks.xstream.io.HierarchicalStreamReader) UnmarshallingContext(com.thoughtworks.xstream.converters.UnmarshallingContext) Matchers.anyString(org.mockito.Matchers.anyString) Map(java.util.Map) HashMap(java.util.HashMap) Test(org.junit.Test)

Example 32 with HierarchicalStreamReader

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

the class TestCswTransformProvider method testUnmarshalCswRecordCoordinateOrder.

@Test
public void testUnmarshalCswRecordCoordinateOrder() throws Exception {
    when(mockInputManager.getTransformerBySchema(CswConstants.CSW_OUTPUT_SCHEMA)).thenReturn(mockCswRecordConverter);
    HierarchicalStreamReader reader = new WstxDriver().createReader(new StringReader(getRecord()));
    CswTransformProvider provider = new CswTransformProvider(null, mockInputManager);
    UnmarshallingContext context = new TreeUnmarshaller(null, null, null, null);
    context.put(CswConstants.AXIS_ORDER_PROPERTY, CswAxisOrder.LAT_LON);
    ArgumentCaptor<HierarchicalStreamReader> readerArgumentCaptor = ArgumentCaptor.forClass(HierarchicalStreamReader.class);
    ArgumentCaptor<UnmarshallingContext> unmarshallingContextArgumentCaptor = ArgumentCaptor.forClass(UnmarshallingContext.class);
    provider.unmarshal(reader, context);
    // Verify that CswRecordConverter unmarshal was called
    verify(mockCswRecordConverter, times(1)).unmarshal(readerArgumentCaptor.capture(), unmarshallingContextArgumentCaptor.capture());
    HierarchicalStreamReader hierarchicalStreamReader = readerArgumentCaptor.getValue();
    UnmarshallingContext unmarshallingContext = unmarshallingContextArgumentCaptor.getValue();
    // Verify that reader and context are passed to the CswRecordConverter correctly
    assertThat(hierarchicalStreamReader, is(reader));
    assertThat(unmarshallingContext, is(context));
    assertThat(context.get(CswConstants.AXIS_ORDER_PROPERTY), is(CswAxisOrder.LAT_LON));
}
Also used : WstxDriver(com.thoughtworks.xstream.io.xml.WstxDriver) StringReader(java.io.StringReader) HierarchicalStreamReader(com.thoughtworks.xstream.io.HierarchicalStreamReader) UnmarshallingContext(com.thoughtworks.xstream.converters.UnmarshallingContext) TreeUnmarshaller(com.thoughtworks.xstream.core.TreeUnmarshaller) Test(org.junit.Test)

Example 33 with HierarchicalStreamReader

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

the class TestCswRecordConverter method testUnmarshalWriteNamespaces.

@Test
public void testUnmarshalWriteNamespaces() throws IOException, SAXException, XmlPullParserException {
    XStream xstream = new XStream(new XppDriver());
    xstream.registerConverter(converter);
    xstream.alias("Record", MetacardImpl.class);
    xstream.alias("csw:Record", MetacardImpl.class);
    InputStream is = IOUtils.toInputStream(getRecordNoNamespaceDeclaration());
    HierarchicalStreamReader reader = new XppReader(new InputStreamReader(is), XmlPullParserFactory.newInstance().newPullParser());
    DataHolder args = xstream.newDataHolder();
    Map<String, String> namespaces = new HashMap<>();
    namespaces.put(CswConstants.XMLNS + CswConstants.NAMESPACE_DELIMITER + CswConstants.CSW_NAMESPACE_PREFIX, CswConstants.CSW_OUTPUT_SCHEMA);
    namespaces.put(CswConstants.XMLNS + CswConstants.NAMESPACE_DELIMITER + CswConstants.DUBLIN_CORE_NAMESPACE_PREFIX, CswConstants.DUBLIN_CORE_SCHEMA);
    namespaces.put(CswConstants.XMLNS + CswConstants.NAMESPACE_DELIMITER + CswConstants.DUBLIN_CORE_TERMS_NAMESPACE_PREFIX, CswConstants.DUBLIN_CORE_TERMS_SCHEMA);
    namespaces.put(CswConstants.XMLNS + CswConstants.NAMESPACE_DELIMITER + CswConstants.OWS_NAMESPACE_PREFIX, CswConstants.OWS_NAMESPACE);
    args.put(CswConstants.NAMESPACE_DECLARATIONS, namespaces);
    Metacard mc = (Metacard) xstream.unmarshal(reader, null, args);
    Metacard expectedMetacard = getTestMetacard();
    assertThat(mc, notNullValue());
    assertThat(mc.getContentTypeName(), is(mc.getContentTypeName()));
    assertThat(mc.getCreatedDate(), is(expectedMetacard.getCreatedDate()));
    assertThat(mc.getEffectiveDate(), is(expectedMetacard.getEffectiveDate()));
    assertThat(mc.getId(), is(expectedMetacard.getId()));
    assertThat(mc.getModifiedDate(), is(expectedMetacard.getModifiedDate()));
    assertThat(mc.getTitle(), is(expectedMetacard.getTitle()));
    assertThat(mc.getResourceURI(), is(expectedMetacard.getResourceURI()));
    XMLUnit.setIgnoreWhitespace(true);
    assertXMLEqual(mc.getMetadata(), getControlRecord());
}
Also used : Metacard(ddf.catalog.data.Metacard) InputStreamReader(java.io.InputStreamReader) XppDriver(com.thoughtworks.xstream.io.xml.XppDriver) HashMap(java.util.HashMap) XStream(com.thoughtworks.xstream.XStream) InputStream(java.io.InputStream) XppReader(com.thoughtworks.xstream.io.xml.XppReader) DataHolder(com.thoughtworks.xstream.converters.DataHolder) HierarchicalStreamReader(com.thoughtworks.xstream.io.HierarchicalStreamReader) Matchers.containsString(org.hamcrest.Matchers.containsString) Test(org.junit.Test)

Example 34 with HierarchicalStreamReader

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

the class GmdTransformer method handleTransform.

private Metacard handleTransform(InputStream inputStream, String id) throws CatalogTransformerException {
    String xml;
    XstreamPathValueTracker pathValueTracker;
    try (TemporaryFileBackedOutputStream temporaryFileBackedOutputStream = new TemporaryFileBackedOutputStream()) {
        IOUtils.copy(inputStream, temporaryFileBackedOutputStream);
        byteArray = temporaryFileBackedOutputStream.asByteSource();
        try (InputStream xmlSourceInputStream = getSourceInputStream()) {
            xml = IOUtils.toString(xmlSourceInputStream);
        }
        argumentHolder.put(XstreamPathConverter.PATH_KEY, buildPaths());
        XMLStreamReader streamReader = xmlFactory.createXMLStreamReader(new StringReader(xml));
        HierarchicalStreamReader reader = new StaxReader(new QNameMap(), streamReader);
        pathValueTracker = (XstreamPathValueTracker) xstream.unmarshal(reader, null, argumentHolder);
        Metacard metacard = toMetacard(pathValueTracker, id);
        metacard.setAttribute(new AttributeImpl(Core.METADATA, xml));
        return metacard;
    } catch (XStreamException | XMLStreamException | IOException e) {
        throw new CatalogTransformerException(TRANSFORM_EXCEPTION_MSG, e);
    } finally {
        IOUtils.closeQuietly(inputStream);
    }
}
Also used : StaxReader(com.thoughtworks.xstream.io.xml.StaxReader) XMLStreamReader(javax.xml.stream.XMLStreamReader) TemporaryFileBackedOutputStream(org.codice.ddf.platform.util.TemporaryFileBackedOutputStream) InputStream(java.io.InputStream) AttributeImpl(ddf.catalog.data.impl.AttributeImpl) XstreamPathValueTracker(org.codice.ddf.spatial.ogc.csw.catalog.converter.XstreamPathValueTracker) CatalogTransformerException(ddf.catalog.transform.CatalogTransformerException) IOException(java.io.IOException) Metacard(ddf.catalog.data.Metacard) XStreamException(com.thoughtworks.xstream.XStreamException) XMLStreamException(javax.xml.stream.XMLStreamException) StringReader(java.io.StringReader) HierarchicalStreamReader(com.thoughtworks.xstream.io.HierarchicalStreamReader) QNameMap(com.thoughtworks.xstream.io.xml.QNameMap)

Aggregations

HierarchicalStreamReader (com.thoughtworks.xstream.io.HierarchicalStreamReader)34 Test (org.junit.Test)28 DomReader (com.thoughtworks.xstream.io.xml.DomReader)13 DocumentBuilder (javax.xml.parsers.DocumentBuilder)13 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)13 Document (org.w3c.dom.Document)13 StringReader (java.io.StringReader)11 UnmarshallingContext (com.thoughtworks.xstream.converters.UnmarshallingContext)8 InputStream (java.io.InputStream)8 TreeUnmarshaller (com.thoughtworks.xstream.core.TreeUnmarshaller)7 WstxDriver (com.thoughtworks.xstream.io.xml.WstxDriver)6 XppReader (com.thoughtworks.xstream.io.xml.XppReader)6 HashMap (java.util.HashMap)6 XStream (com.thoughtworks.xstream.XStream)5 Metacard (ddf.catalog.data.Metacard)5 Serializable (java.io.Serializable)5 DataHolder (com.thoughtworks.xstream.converters.DataHolder)4 QNameMap (com.thoughtworks.xstream.io.xml.QNameMap)4 StaxReader (com.thoughtworks.xstream.io.xml.StaxReader)4 IOException (java.io.IOException)4