From bc733504fadc99747fd7879eea9da1a4b1c17da5 Mon Sep 17 00:00:00 2001 From: CarstenHollmann Date: Fri, 18 Nov 2022 11:26:29 +0100 Subject: [PATCH] Fix encoding of Inspire observations for streaming --- .../feature/FeatureServiceConstantsTest.java | 15 +++++++ .../encode/InspireOmObservationEncoder.java | 13 +++--- .../InspireOmObservationXmlStreamWriter.java | 45 +++++++++++++++++++ 3 files changed, 67 insertions(+), 6 deletions(-) create mode 100644 svalbard/xmlbeans/src/main/java/org/n52/svalbard/encode/InspireOmObservationXmlStreamWriter.java diff --git a/shetland/arcgis/src/test/java/org/n52/shetland/arcgis/service/feature/FeatureServiceConstantsTest.java b/shetland/arcgis/src/test/java/org/n52/shetland/arcgis/service/feature/FeatureServiceConstantsTest.java index 16341b651..e17e898ea 100644 --- a/shetland/arcgis/src/test/java/org/n52/shetland/arcgis/service/feature/FeatureServiceConstantsTest.java +++ b/shetland/arcgis/src/test/java/org/n52/shetland/arcgis/service/feature/FeatureServiceConstantsTest.java @@ -1,3 +1,18 @@ +/* + * Copyright (C) 2015-2022 52°North Spatial Information Research GmbH + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package org.n52.shetland.arcgis.service.feature; import static org.junit.jupiter.api.Assertions.assertEquals; diff --git a/svalbard/xmlbeans/src/main/java/org/n52/svalbard/encode/InspireOmObservationEncoder.java b/svalbard/xmlbeans/src/main/java/org/n52/svalbard/encode/InspireOmObservationEncoder.java index db8693fe1..d0dccf66e 100644 --- a/svalbard/xmlbeans/src/main/java/org/n52/svalbard/encode/InspireOmObservationEncoder.java +++ b/svalbard/xmlbeans/src/main/java/org/n52/svalbard/encode/InspireOmObservationEncoder.java @@ -15,7 +15,6 @@ */ package org.n52.svalbard.encode; -import java.io.IOException; import java.io.OutputStream; import java.util.Collections; import java.util.HashSet; @@ -48,8 +47,7 @@ * @since 1.0.0 * */ -public class InspireOmObservationEncoder - extends AbstractXmlEncoder +public class InspireOmObservationEncoder extends AbstractXmlEncoder implements ObservationEncoder, StreamingEncoder { private static final Set ENCODER_KEYS = @@ -87,10 +85,13 @@ public void encode(Object element, OutputStream outputStream, EncodingContext ct .with(XmlEncoderFlags.XML_OPTIONS, (Supplier) this::getXmlOptions), outputStream, (OmObservation) element).write(); } else { - // writeIndent(encodingValues.getIndent(), outputStream); - encode(element, ctx).save(outputStream, getXmlOptions()); + new InspireOmObservationXmlStreamWriter( + ctx.with(EncoderFlags.ENCODER_REPOSITORY, getEncoderRepository()) + .with(XmlEncoderFlags.XML_OPTIONS, (Supplier) this::getXmlOptions), + outputStream, (OmObservation) element, this).write(); + } - } catch (IOException | XMLStreamException e) { + } catch (XMLStreamException e) { throw new EncodingException("Error while writing element to stream!", e); } } diff --git a/svalbard/xmlbeans/src/main/java/org/n52/svalbard/encode/InspireOmObservationXmlStreamWriter.java b/svalbard/xmlbeans/src/main/java/org/n52/svalbard/encode/InspireOmObservationXmlStreamWriter.java new file mode 100644 index 000000000..3d9377ef1 --- /dev/null +++ b/svalbard/xmlbeans/src/main/java/org/n52/svalbard/encode/InspireOmObservationXmlStreamWriter.java @@ -0,0 +1,45 @@ +/* + * Copyright (C) 2015-2022 52°North Spatial Information Research GmbH + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.n52.svalbard.encode; + +import java.io.OutputStream; + +import javax.xml.stream.XMLStreamException; + +import org.n52.shetland.ogc.om.OmObservation; +import org.n52.svalbard.encode.exception.EncodingException; +import org.n52.svalbard.write.XmlStreamWriter; + +import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; + +@SuppressFBWarnings({"EI_EXPOSE_REP2"}) +public class InspireOmObservationXmlStreamWriter extends XmlStreamWriter { + + + private InspireOmObservationEncoder encoder; + + public InspireOmObservationXmlStreamWriter(EncodingContext context, OutputStream outputStream, + OmObservation element, InspireOmObservationEncoder encoder) throws XMLStreamException { + super(context, outputStream, element); + this.encoder = encoder; + } + + @Override + public void write() throws XMLStreamException, EncodingException { + rawText(encoder.encode(getElement(), getContext()).xmlText(getXmlOptions().setSaveNoXmlDecl())); + } + +}