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);
}
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);
}
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);
}
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;
}
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;
}
Aggregations