Search in sources :

Example 1 with MarshallerImpl

use of org.jvnet.ogc.gml.v_3_1_1.jts.MarshallerImpl in project ddf by codice.

the class CswFilterFactory method convertGeometry.

@SuppressWarnings("unchecked")
private JAXBElement<? extends AbstractGeometryType> convertGeometry(Geometry geometry) {
    geometry.setUserData(CswConstants.SRS_NAME);
    JAXBElement<? extends AbstractGeometryType> abstractGeometry = null;
    try {
        Map<String, String> geoConverterProps = new HashMap<String, String>();
        geoConverterProps.put(CswJTSToGML311GeometryConverter.USE_POS_LIST_GEO_CONVERTER_PROP_KEY, String.valueOf(this.isSetUsePosList));
        JTSToGML311GeometryConverter converter = new CswJTSToGML311GeometryConverter(geoConverterProps);
        Marshaller marshaller = new MarshallerImpl(JAXB_CONTEXT.createMarshaller(), converter);
        StringWriter writer = new StringWriter();
        marshaller.marshal(geometry, writer);
        String xmlGeo = writer.toString();
        LOGGER.debug("Geometry as XML: {}", xmlGeo);
        XMLInputFactory xmlInputFactory = XMLInputFactory.newFactory();
        xmlInputFactory.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, false);
        xmlInputFactory.setProperty(XMLInputFactory.SUPPORT_DTD, false);
        xmlInputFactory.setProperty(XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES, false);
        XMLStreamReader xmlStreamReader = xmlInputFactory.createXMLStreamReader(new StringReader(xmlGeo));
        Unmarshaller unmarshaller = JAXB_CONTEXT.createUnmarshaller();
        Object object = unmarshaller.unmarshal(xmlStreamReader);
        LOGGER.debug("Unmarshalled as => {}", object);
        if (object instanceof JAXBElement) {
            abstractGeometry = (JAXBElement<? extends AbstractGeometryType>) object;
        } else {
            LOGGER.debug("Unable to cast to JAXBElement<? extends AbstractGeometryType>.  Object is of type [{}].", object.getClass().getName());
        }
    } catch (JAXBException | XMLStreamException e) {
        LOGGER.debug("Unable to unmarshal geometry [{}]", geometry.getClass().getName(), e);
    }
    return abstractGeometry;
}
Also used : JTSToGML311GeometryConverter(org.jvnet.ogc.gml.v_3_1_1.jts.JTSToGML311GeometryConverter) Marshaller(javax.xml.bind.Marshaller) MarshallerImpl(org.jvnet.ogc.gml.v_3_1_1.jts.MarshallerImpl) XMLStreamReader(javax.xml.stream.XMLStreamReader) HashMap(java.util.HashMap) JAXBException(javax.xml.bind.JAXBException) JAXBElement(javax.xml.bind.JAXBElement) StringWriter(java.io.StringWriter) XMLStreamException(javax.xml.stream.XMLStreamException) StringReader(java.io.StringReader) Unmarshaller(javax.xml.bind.Unmarshaller) XMLInputFactory(javax.xml.stream.XMLInputFactory)

Aggregations

StringReader (java.io.StringReader)1 StringWriter (java.io.StringWriter)1 HashMap (java.util.HashMap)1 JAXBElement (javax.xml.bind.JAXBElement)1 JAXBException (javax.xml.bind.JAXBException)1 Marshaller (javax.xml.bind.Marshaller)1 Unmarshaller (javax.xml.bind.Unmarshaller)1 XMLInputFactory (javax.xml.stream.XMLInputFactory)1 XMLStreamException (javax.xml.stream.XMLStreamException)1 XMLStreamReader (javax.xml.stream.XMLStreamReader)1 JTSToGML311GeometryConverter (org.jvnet.ogc.gml.v_3_1_1.jts.JTSToGML311GeometryConverter)1 MarshallerImpl (org.jvnet.ogc.gml.v_3_1_1.jts.MarshallerImpl)1