Search in sources :

Example 1 with ElementFeatureWriter

use of org.geotoolkit.feature.xml.jaxp.ElementFeatureWriter in project geotoolkit by Geomatys.

the class XmlFeatureTest method testWriteSimpleFeatureElement.

@Test
public void testWriteSimpleFeatureElement() throws JAXBException, IOException, XMLStreamException, DataStoreException, ParserConfigurationException, SAXException, TransformerConfigurationException, TransformerException {
    final File temp = File.createTempFile("gml", ".xml");
    temp.deleteOnExit();
    final ElementFeatureWriter writer = new ElementFeatureWriter();
    Element r = writer.write(simpleFeatureFull, false);
    Source source = new DOMSource(r.getOwnerDocument());
    // Prepare the output file
    Result resultxml = new StreamResult(temp);
    // Write the DOM document to the file
    Transformer xformer = TransformerFactory.newInstance().newTransformer();
    xformer.transform(source, resultxml);
    String result = IOUtilities.toString(new FileInputStream(temp));
    String expResult = IOUtilities.toString(XmlFeatureTest.class.getResourceAsStream("/org/geotoolkit/feature/xml/SimpleFeatureDom.xml"));
    expResult = expResult.replace("EPSG_VERSION", EPSG_VERSION);
    expResult = expResult.replaceAll("(?i)epsg\\:\\d+\\.\\d+\\.?\\d*\\:", "epsg::");
    result = result.replaceAll("(?i)epsg\\:\\d+\\.\\d+\\.?\\d*\\:", "epsg::");
    DomCompare.compare(expResult, result);
}
Also used : ElementFeatureWriter(org.geotoolkit.feature.xml.jaxp.ElementFeatureWriter) DOMSource(javax.xml.transform.dom.DOMSource) Transformer(javax.xml.transform.Transformer) StreamResult(javax.xml.transform.stream.StreamResult) Element(org.w3c.dom.Element) File(java.io.File) DOMSource(javax.xml.transform.dom.DOMSource) Source(javax.xml.transform.Source) FileInputStream(java.io.FileInputStream) StreamResult(javax.xml.transform.stream.StreamResult) Result(javax.xml.transform.Result)

Example 2 with ElementFeatureWriter

use of org.geotoolkit.feature.xml.jaxp.ElementFeatureWriter in project geotoolkit by Geomatys.

the class XmlFeatureTest method testWriteMixedCollectionElement.

@Test
public void testWriteMixedCollectionElement() throws JAXBException, IOException, XMLStreamException, DataStoreException, ParserConfigurationException, SAXException, TransformerConfigurationException, TransformerException {
    final StringWriter temp = new StringWriter();
    final ElementFeatureWriter writer = new ElementFeatureWriter();
    Element result = writer.write(Arrays.asList(collectionSimple, collectionSimple2), false);
    Source source = new DOMSource(result.getOwnerDocument());
    // Prepare the output file
    Result resultxml = new StreamResult(temp);
    // Write the DOM document to the file
    Transformer xformer = TransformerFactory.newInstance().newTransformer();
    xformer.transform(source, resultxml);
    String s = temp.toString();
    s = s.replaceAll("timeStamp=\"[^\"]*\"", "timeStamp=\"\"");
    DomCompare.compare(IOUtilities.toString(XmlFeatureTest.class.getResourceAsStream("/org/geotoolkit/feature/xml/CollectionMixedDom.xml")), s);
}
Also used : ElementFeatureWriter(org.geotoolkit.feature.xml.jaxp.ElementFeatureWriter) DOMSource(javax.xml.transform.dom.DOMSource) Transformer(javax.xml.transform.Transformer) StringWriter(java.io.StringWriter) StreamResult(javax.xml.transform.stream.StreamResult) Element(org.w3c.dom.Element) DOMSource(javax.xml.transform.dom.DOMSource) Source(javax.xml.transform.Source) StreamResult(javax.xml.transform.stream.StreamResult) Result(javax.xml.transform.Result)

Example 3 with ElementFeatureWriter

use of org.geotoolkit.feature.xml.jaxp.ElementFeatureWriter in project geotoolkit by Geomatys.

the class XmlFeatureTest method testWriteSimplCollectionElement.

@Test
public void testWriteSimplCollectionElement() throws JAXBException, IOException, XMLStreamException, DataStoreException, ParserConfigurationException, SAXException, TransformerConfigurationException, TransformerException {
    final StringWriter temp = new StringWriter();
    final ElementFeatureWriter writer = new ElementFeatureWriter();
    Element result = writer.write(collectionSimple, false);
    Source source = new DOMSource(result.getOwnerDocument());
    // Prepare the output file
    Result resultxml = new StreamResult(temp);
    // Write the DOM document to the file
    Transformer xformer = TransformerFactory.newInstance().newTransformer();
    xformer.transform(source, resultxml);
    String expResult = IOUtilities.toString(XmlFeatureTest.class.getResourceAsStream("/org/geotoolkit/feature/xml/CollectionSimpleDom.xml"));
    expResult = expResult.replace("EPSG_VERSION", EPSG_VERSION);
    String s = temp.toString();
    s = s.replaceAll("timeStamp=\"[^\"]*\"", "timeStamp=\"\"");
    DomCompare.compare(expResult, s);
}
Also used : ElementFeatureWriter(org.geotoolkit.feature.xml.jaxp.ElementFeatureWriter) DOMSource(javax.xml.transform.dom.DOMSource) Transformer(javax.xml.transform.Transformer) StringWriter(java.io.StringWriter) StreamResult(javax.xml.transform.stream.StreamResult) Element(org.w3c.dom.Element) DOMSource(javax.xml.transform.dom.DOMSource) Source(javax.xml.transform.Source) StreamResult(javax.xml.transform.stream.StreamResult) Result(javax.xml.transform.Result)

Example 4 with ElementFeatureWriter

use of org.geotoolkit.feature.xml.jaxp.ElementFeatureWriter in project geotoolkit by Geomatys.

the class FeatureSetToComplexConverter method convert.

/**
 * {@inheritDoc}
 */
@Override
public Data convert(final FeatureSet source, final Map<String, Object> params) throws UnconvertibleObjectException {
    if (source == null) {
        throw new UnconvertibleObjectException("The output data should be defined.");
    } else if (params == null) {
        throw new UnconvertibleObjectException("Not enough information about data format");
    }
    final Object tmpMime = params.get(MIME);
    final String mime;
    if (tmpMime instanceof String) {
        mime = (String) tmpMime;
    } else {
        throw new UnconvertibleObjectException("No valid mime type given. We cannot determine output image format");
    }
    final Data complex = new Data();
    complex.setMimeType(mime);
    final Object tmpEncoding = params.get(ENCODING);
    if (tmpEncoding instanceof String) {
        complex.setEncoding((String) tmpEncoding);
    }
    final FeatureType ft;
    try {
        ft = source.getType();
    } catch (DataStoreException ex) {
        throw new UnconvertibleObjectException("Can't write acess FeatureSet type.", ex);
    }
    final String namespace = NamesExt.getNamespace(ft.getName());
    final Map<String, String> schemaLocation = new HashMap<>();
    if (WPSMimeType.APP_GEOJSON.val().equalsIgnoreCase(mime)) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        try {
            try (GeoJSONStreamWriter writer = new GeoJSONStreamWriter(baos, ft, WPSConvertersUtils.FRACTION_DIGITS);
                Stream<Feature> st = source.features(false)) {
                Iterator<Feature> iterator = st.iterator();
                while (iterator.hasNext()) {
                    Feature next = iterator.next();
                    Feature neww = writer.next();
                    FeatureExt.copy(next, neww, false);
                    writer.write();
                }
            }
            WPSConvertersUtils.addCDATAToComplex(baos.toString("UTF-8"), complex);
            complex.setSchema(null);
        } catch (DataStoreException e) {
            throw new UnconvertibleObjectException("Can't write Feature into GeoJSON output stream.", e);
        } catch (UnsupportedEncodingException e) {
            throw new UnconvertibleObjectException("Can't convert output stream into String.", e);
        }
    } else if (WPSMimeType.APP_GML.val().equalsIgnoreCase(mime) || WPSMimeType.TEXT_XML.val().equalsIgnoreCase(mime) || WPSMimeType.TEXT_GML.val().equalsIgnoreCase(mime)) {
        try {
            complex.setSchema(WPSConvertersUtils.writeSchema(ft, params));
            schemaLocation.put(namespace, complex.getSchema());
        } catch (JAXBException ex) {
            throw new UnconvertibleObjectException("Can't write FeatureType into xsd schema.", ex);
        } catch (IOException ex) {
            throw new UnconvertibleObjectException("Can't create xsd schema file.", ex);
        }
        try {
            final ElementFeatureWriter efw = new ElementFeatureWriter(schemaLocation);
            complex.getContent().add(efw.writeFeatureCollection(source, null, true, false, null, true));
        } catch (DataStoreException | ParserConfigurationException ex) {
            throw new UnconvertibleObjectException("Can't write FeatureSet into ResponseDocument.", ex);
        }
    } else
        throw new UnconvertibleObjectException("Unsupported mime-type for " + this.getClass().getName() + " : " + complex.getMimeType());
    return complex;
}
Also used : FeatureType(org.opengis.feature.FeatureType) DataStoreException(org.apache.sis.storage.DataStoreException) HashMap(java.util.HashMap) JAXBException(javax.xml.bind.JAXBException) Data(org.geotoolkit.wps.xml.v200.Data) Feature(org.opengis.feature.Feature) ElementFeatureWriter(org.geotoolkit.feature.xml.jaxp.ElementFeatureWriter) UnconvertibleObjectException(org.apache.sis.util.UnconvertibleObjectException) GeoJSONStreamWriter(org.geotoolkit.storage.geojson.GeoJSONStreamWriter)

Example 5 with ElementFeatureWriter

use of org.geotoolkit.feature.xml.jaxp.ElementFeatureWriter in project geotoolkit by Geomatys.

the class FeatureToComplexConverter method convert.

/**
 * {@inheritDoc}
 */
@Override
public Data convert(Feature source, Map<String, Object> params) throws UnconvertibleObjectException {
    if (source == null) {
        throw new UnconvertibleObjectException("The output data should be defined.");
    } else if (params == null) {
        throw new UnconvertibleObjectException("Not enough information about data format");
    }
    final Object tmpMime = params.get(MIME);
    final String mime;
    if (tmpMime instanceof String) {
        mime = (String) tmpMime;
    } else {
        throw new UnconvertibleObjectException("No valid mime type given. We cannot determine output image format");
    }
    final Data complex = new Data();
    complex.setMimeType(mime);
    final Object tmpEncoding = params.get(ENCODING);
    if (tmpEncoding instanceof String) {
        complex.setEncoding((String) tmpEncoding);
    }
    final FeatureType ft = source.getType();
    final String namespace = NamesExt.getNamespace(ft.getName());
    final Map<String, String> schemaLocation = new HashMap<>();
    if (WPSMimeType.APP_GEOJSON.val().equalsIgnoreCase(complex.getMimeType())) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        try {
            try (GeoJSONStreamWriter writer = new GeoJSONStreamWriter(baos, ft, WPSConvertersUtils.FRACTION_DIGITS)) {
                Feature next = writer.next();
                FeatureExt.copy(source, next, true);
                writer.write();
            }
            WPSConvertersUtils.addCDATAToComplex(baos.toString("UTF-8"), complex);
            complex.setSchema(null);
        } catch (DataStoreException e) {
            throw new UnconvertibleObjectException("Can't write Feature into GeoJSON output stream.", e);
        } catch (UnsupportedEncodingException e) {
            throw new UnconvertibleObjectException("Can't convert output stream into String.", e);
        }
    } else if (WPSMimeType.APP_GML.val().equalsIgnoreCase(complex.getMimeType()) || WPSMimeType.TEXT_XML.val().equalsIgnoreCase(complex.getMimeType()) || WPSMimeType.TEXT_GML.val().equalsIgnoreCase(complex.getMimeType())) {
        try {
            complex.setSchema(WPSConvertersUtils.writeSchema(ft, params));
            schemaLocation.put(namespace, complex.getSchema());
        } catch (JAXBException ex) {
            throw new UnconvertibleObjectException("Can't write FeatureType into xsd schema.", ex);
        } catch (IOException ex) {
            throw new UnconvertibleObjectException("Can't create xsd schema file.", ex);
        }
        try {
            final ElementFeatureWriter efw = new ElementFeatureWriter(schemaLocation);
            complex.getContent().add(efw.writeFeature(source, null, true));
        } catch (ParserConfigurationException ex) {
            throw new UnconvertibleObjectException("Can't write FeatureCollection into ResponseDocument.", ex);
        }
    } else
        throw new UnconvertibleObjectException("Unsupported mime-type for " + this.getClass().getName() + " : " + complex.getMimeType());
    return complex;
}
Also used : FeatureType(org.opengis.feature.FeatureType) DataStoreException(org.apache.sis.storage.DataStoreException) HashMap(java.util.HashMap) JAXBException(javax.xml.bind.JAXBException) Data(org.geotoolkit.wps.xml.v200.Data) Feature(org.opengis.feature.Feature) ElementFeatureWriter(org.geotoolkit.feature.xml.jaxp.ElementFeatureWriter) UnconvertibleObjectException(org.apache.sis.util.UnconvertibleObjectException) GeoJSONStreamWriter(org.geotoolkit.storage.geojson.GeoJSONStreamWriter) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException)

Aggregations

ElementFeatureWriter (org.geotoolkit.feature.xml.jaxp.ElementFeatureWriter)6 HashMap (java.util.HashMap)3 JAXBException (javax.xml.bind.JAXBException)3 Result (javax.xml.transform.Result)3 Source (javax.xml.transform.Source)3 Transformer (javax.xml.transform.Transformer)3 DOMSource (javax.xml.transform.dom.DOMSource)3 StreamResult (javax.xml.transform.stream.StreamResult)3 DataStoreException (org.apache.sis.storage.DataStoreException)3 UnconvertibleObjectException (org.apache.sis.util.UnconvertibleObjectException)3 GeoJSONStreamWriter (org.geotoolkit.storage.geojson.GeoJSONStreamWriter)3 Data (org.geotoolkit.wps.xml.v200.Data)3 Feature (org.opengis.feature.Feature)3 FeatureType (org.opengis.feature.FeatureType)3 Element (org.w3c.dom.Element)3 StringWriter (java.io.StringWriter)2 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)1