This is an automated email from the ASF dual-hosted git repository.

asf-gitbox-commits pushed a commit to branch geoapi-4.0
in repository https://gitbox.apache.org/repos/asf/sis.git

commit a3fd779e1bd4d5eafb52ada554ac1666df7a18a8
Author: jsorel <[email protected]>
AuthorDate: Mon Nov 9 11:04:49 2026 +0100

    feat(GML): add flag to GML reader and write to indicate it is only a 
fragment so stream is not closed and namespace inherited
---
 .../main/org/apache/sis/gml/AbstractGMLWriter.java | 38 ++++++++++++++--
 .../main/org/apache/sis/gml/GML2Writer.java        | 12 +++++
 .../main/org/apache/sis/gml/GML3Writer.java        | 29 ++++++++++--
 .../main/org/apache/sis/gml/GMLVersion.java        | 16 +++++--
 .../main/org/apache/sis/gml/GMLWriter.java         | 53 ++++++++++++++++++----
 .../test/org/apache/sis/gml/GMLWriterTest.java     | 39 ++++++++++++++++
 6 files changed, 167 insertions(+), 20 deletions(-)

diff --git 
a/incubator/src/org.apache.sis.geometry/main/org/apache/sis/gml/AbstractGMLWriter.java
 
b/incubator/src/org.apache.sis.geometry/main/org/apache/sis/gml/AbstractGMLWriter.java
index 8dbb99f031..56da68cf08 100644
--- 
a/incubator/src/org.apache.sis.geometry/main/org/apache/sis/gml/AbstractGMLWriter.java
+++ 
b/incubator/src/org.apache.sis.geometry/main/org/apache/sis/gml/AbstractGMLWriter.java
@@ -47,6 +47,12 @@ abstract class AbstractGMLWriter implements 
GMLGeometryWriter {
      */
     protected final XMLStreamWriter writer;
 
+    /**
+     * Whether this writer writes geometry elements inside a document owned by 
the caller.
+     * In such case, this writer does not write the document header and does 
not close the cursor.
+     */
+    private final boolean fragment;
+
     /**
      * Creates a new writer using the given StAX cursor.
      * The XML document header is written immediately by this constructor.
@@ -55,8 +61,26 @@ abstract class AbstractGMLWriter implements 
GMLGeometryWriter {
      * @throws XMLStreamException if the XML document header cannot be written.
      */
     protected AbstractGMLWriter(final XMLStreamWriter writer) throws 
XMLStreamException {
-        this.writer = writer;
-        writer.writeStartDocument("UTF-8", "1.0");
+        this(writer, false);
+    }
+
+    /**
+     * Creates a new writer using the given StAX cursor, optionally as a 
fragment writer.
+     * If {@code fragment} is {@code false}, the XML document header is 
written immediately by this constructor.
+     * If {@code fragment} is {@code true}, the geometries are written inside 
a document that the caller started
+     * and will end itself. In the latter case, {@link #close()} does not 
write the end of the document and does
+     * not close the cursor.
+     *
+     * @param  writer    the StAX cursor to write to.
+     * @param  fragment  whether to write geometries inside a document owned 
by the caller.
+     * @throws XMLStreamException if the XML document header cannot be written.
+     */
+    protected AbstractGMLWriter(final XMLStreamWriter writer, final boolean 
fragment) throws XMLStreamException {
+        this.writer   = writer;
+        this.fragment = fragment;
+        if (!fragment) {
+            writer.writeStartDocument("UTF-8", "1.0");
+        }
     }
 
     /**
@@ -265,12 +289,18 @@ abstract class AbstractGMLWriter implements 
GMLGeometryWriter {
 
     /**
      * Writes the end of the XML document and closes the underlying StAX 
cursor.
+     * If this writer is a fragment writer, then this method only flushes the 
cursor,
+     * since the document is owned by the caller.
      *
      * @throws XMLStreamException if an error occurred while writing or 
closing the cursor.
      */
     @Override
     public final void close() throws XMLStreamException {
-        writer.writeEndDocument();
-        writer.close();
+        if (fragment) {
+            writer.flush();
+        } else {
+            writer.writeEndDocument();
+            writer.close();
+        }
     }
 }
diff --git 
a/incubator/src/org.apache.sis.geometry/main/org/apache/sis/gml/GML2Writer.java 
b/incubator/src/org.apache.sis.geometry/main/org/apache/sis/gml/GML2Writer.java
index 1bf3728b2b..3ab8736982 100644
--- 
a/incubator/src/org.apache.sis.geometry/main/org/apache/sis/gml/GML2Writer.java
+++ 
b/incubator/src/org.apache.sis.geometry/main/org/apache/sis/gml/GML2Writer.java
@@ -56,6 +56,18 @@ public final class GML2Writer extends AbstractGMLWriter {
         super(writer);
     }
 
+    /**
+     * Creates a new writer using the given StAX cursor, optionally as a 
fragment writer
+     * which writes geometries inside a document owned by the caller.
+     *
+     * @param  writer    the StAX cursor to write to.
+     * @param  fragment  whether to write geometries inside a document owned 
by the caller.
+     * @throws XMLStreamException if the XML document header cannot be written.
+     */
+    GML2Writer(final XMLStreamWriter writer, final boolean fragment) throws 
XMLStreamException {
+        super(writer, fragment);
+    }
+
     /**
      * Creates a new writer for the given output stream.
      *
diff --git 
a/incubator/src/org.apache.sis.geometry/main/org/apache/sis/gml/GML3Writer.java 
b/incubator/src/org.apache.sis.geometry/main/org/apache/sis/gml/GML3Writer.java
index 8eb061e45d..2372d01d89 100644
--- 
a/incubator/src/org.apache.sis.geometry/main/org/apache/sis/gml/GML3Writer.java
+++ 
b/incubator/src/org.apache.sis.geometry/main/org/apache/sis/gml/GML3Writer.java
@@ -61,6 +61,12 @@ import org.apache.sis.storage.DataStoreReferencingException;
  * @author  Johann Sorel (Geomatys)
  */
 public final class GML3Writer extends AbstractGMLWriter {
+    /**
+     * The namespace of the written elements: either the GML 3.2 namespace,
+     * or the unversioned namespace used by GML 3.0 and 3.1.
+     */
+    private final String namespace;
+
     /**
      * Creates a new writer using the given StAX cursor.
      * The XML document header is written immediately by this constructor.
@@ -69,7 +75,22 @@ public final class GML3Writer extends AbstractGMLWriter {
      * @throws XMLStreamException if the XML document header cannot be written.
      */
     public GML3Writer(final XMLStreamWriter writer) throws XMLStreamException {
-        super(writer);
+        this(writer, GML3Tags.NAMESPACE_3, false);
+    }
+
+    /**
+     * Creates a new writer using the given StAX cursor, writing elements in 
the given namespace.
+     * The namespace shall be either {@link GML3Tags#NAMESPACE_3} for GML 3.2,
+     * or {@link GML2Tags#NAMESPACE_2} for GML 3.0 and 3.1.
+     *
+     * @param  writer     the StAX cursor to write to.
+     * @param  namespace  the namespace of the elements to write.
+     * @param  fragment   whether to write geometries inside a document owned 
by the caller.
+     * @throws XMLStreamException if the XML document header cannot be written.
+     */
+    GML3Writer(final XMLStreamWriter writer, final String namespace, final 
boolean fragment) throws XMLStreamException {
+        super(writer, fragment);
+        this.namespace = namespace;
     }
 
     /**
@@ -84,15 +105,15 @@ public final class GML3Writer extends AbstractGMLWriter {
 
     @Override
     protected GMLVersion getVersion() {
-        return GMLVersion.V3;
+        return GML3Tags.NAMESPACE_3.equals(namespace) ? GMLVersion.V3 : 
GMLVersion.V3_1;
     }
 
     /**
-     * Returns the GML 3.2 namespace.
+     * Returns the GML 3.2 namespace, or the unversioned GML 3.1 namespace.
      */
     @Override
     protected String namespace() {
-        return GML3Tags.NAMESPACE_3;
+        return namespace;
     }
 
     /**
diff --git 
a/incubator/src/org.apache.sis.geometry/main/org/apache/sis/gml/GMLVersion.java 
b/incubator/src/org.apache.sis.geometry/main/org/apache/sis/gml/GMLVersion.java
index 98d5a67d2c..048c566460 100644
--- 
a/incubator/src/org.apache.sis.geometry/main/org/apache/sis/gml/GMLVersion.java
+++ 
b/incubator/src/org.apache.sis.geometry/main/org/apache/sis/gml/GMLVersion.java
@@ -19,8 +19,9 @@ package org.apache.sis.gml;
 
 /**
  * The GML version that {@link GMLWriter} should target.
- * There is a single {@link #V3} value covering GML 3.0, 3.1 and 3.2, since
- * {@link GML3Writer} always emits canonical GML 3.2 output regardless of 
intent.
+ * The {@link #V3} value covers GML 3.0, 3.1 and 3.2 when reading, and emits 
canonical GML 3.2 output
+ * when writing. The {@link #V3_1} value emits the same GML 3 encoding in the 
unversioned namespace
+ * of GML 3.0 and 3.1, as required by some standards such as OGC Filter 
Encoding 1.1.
  *
  * @author  Johann Sorel (Geomatys)
  */
@@ -31,7 +32,14 @@ public enum GMLVersion {
     V2,
 
     /**
-     * GML 3 (3.0, 3.1 or 3.2)
+     * GML 3 (3.0, 3.1 or 3.2). Writers emit GML 3.2.
      */
-    V3
+    V3,
+
+    /**
+     * GML 3.1, written in the unversioned {@code http://www.opengis.net/gml} 
namespace.
+     * This is the GML version used by OGC Filter Encoding 1.1 and Symbology 
Encoding 1.1.
+     * Readers report GML 3.0 and 3.1 documents as {@link #V3}.
+     */
+    V3_1
 }
diff --git 
a/incubator/src/org.apache.sis.geometry/main/org/apache/sis/gml/GMLWriter.java 
b/incubator/src/org.apache.sis.geometry/main/org/apache/sis/gml/GMLWriter.java
index 614fdd1255..2a6d0f8e3c 100644
--- 
a/incubator/src/org.apache.sis.geometry/main/org/apache/sis/gml/GMLWriter.java
+++ 
b/incubator/src/org.apache.sis.geometry/main/org/apache/sis/gml/GMLWriter.java
@@ -17,6 +17,7 @@
 package org.apache.sis.gml;
 
 import java.io.OutputStream;
+import javax.xml.stream.XMLOutputFactory;
 import javax.xml.stream.XMLStreamException;
 import javax.xml.stream.XMLStreamWriter;
 import org.opengis.referencing.crs.CoordinateReferenceSystem;
@@ -53,11 +54,7 @@ public final class GMLWriter implements AutoCloseable {
      * @throws XMLStreamException if the XML document header cannot be written.
      */
     public GMLWriter(final XMLStreamWriter writer, final GMLVersion version) 
throws XMLStreamException {
-        switch (version) {
-            case V2: delegate = new GML2Writer(writer); break;
-            case V3: delegate = new GML3Writer(writer); break;
-            default: throw new AssertionError(version);
-        }
+        delegate = create(writer, version, false);
     }
 
     /**
@@ -68,10 +65,50 @@ public final class GMLWriter implements AutoCloseable {
      * @throws XMLStreamException if the StAX cursor cannot be created.
      */
     public GMLWriter(final OutputStream out, final GMLVersion version) throws 
XMLStreamException {
+        this(XMLOutputFactory.newInstance().createXMLStreamWriter(out, 
"UTF-8"), version);
+    }
+
+    /**
+     * Creates a new writer delegating to the given concrete writer.
+     */
+    private GMLWriter(final GMLGeometryWriter delegate) {
+        this.delegate = delegate;
+    }
+
+    /**
+     * Creates a writer of geometry elements inside a XML document owned by 
the caller.
+     * The returned writer does not write the XML document header, and its 
{@link #close()}
+     * method neither writes the end of the document nor closes the given 
cursor.
+     * This is useful for writing geometries embedded in other XML documents,
+     * for example in the spatial operators of a filter.
+     *
+     * <p>Each geometry element declares the GML namespace as its default 
namespace,
+     * so the enclosing document does not need to declare the GML 
namespace.</p>
+     *
+     * @param  writer   the StAX cursor to write to, positioned where the 
geometry shall be written.
+     * @param  version  the GML version to target.
+     * @return a writer of geometry elements inside the given document.
+     * @throws XMLStreamException if the writer cannot be created.
+     */
+    public static GMLWriter fragment(final XMLStreamWriter writer, final 
GMLVersion version) throws XMLStreamException {
+        return new GMLWriter(create(writer, version, true));
+    }
+
+    /**
+     * Creates the concrete writer for the given GML version.
+     *
+     * @param  writer    the StAX cursor to write to.
+     * @param  version   the GML version to target.
+     * @param  fragment  whether to write geometries inside a document owned 
by the caller.
+     */
+    private static GMLGeometryWriter create(final XMLStreamWriter writer, 
final GMLVersion version, final boolean fragment)
+            throws XMLStreamException
+    {
         switch (version) {
-            case V2: delegate = new GML2Writer(out); break;
-            case V3: delegate = new GML3Writer(out); break;
-            default: throw new AssertionError(version);
+            case V2:   return new GML2Writer(writer, fragment);
+            case V3:   return new GML3Writer(writer, GML3Tags.NAMESPACE_3, 
fragment);
+            case V3_1: return new GML3Writer(writer, GML2Tags.NAMESPACE_2, 
fragment);
+            default:   throw new AssertionError(version);
         }
     }
 
diff --git 
a/incubator/src/org.apache.sis.geometry/test/org/apache/sis/gml/GMLWriterTest.java
 
b/incubator/src/org.apache.sis.geometry/test/org/apache/sis/gml/GMLWriterTest.java
index d9ab6c5b78..1545333f24 100644
--- 
a/incubator/src/org.apache.sis.geometry/test/org/apache/sis/gml/GMLWriterTest.java
+++ 
b/incubator/src/org.apache.sis.geometry/test/org/apache/sis/gml/GMLWriterTest.java
@@ -17,7 +17,10 @@
 package org.apache.sis.gml;
 
 import java.io.ByteArrayOutputStream;
+import java.io.StringWriter;
 import java.nio.charset.StandardCharsets;
+import javax.xml.stream.XMLOutputFactory;
+import javax.xml.stream.XMLStreamWriter;
 import org.opengis.referencing.crs.CoordinateReferenceSystem;
 import org.apache.sis.geometries.GeometryFactory;
 import org.apache.sis.geometries.Point;
@@ -27,6 +30,7 @@ import org.apache.sis.referencing.CRS;
 
 // Test dependencies
 import org.junit.jupiter.api.Test;
+import static org.junit.jupiter.api.Assertions.*;
 
 
 /**
@@ -76,4 +80,39 @@ public final class GMLWriterTest extends TestCase {
     public void testWriteGML3Version() throws Exception {
         assertXmlEquals(TestData.V3.openStream(TestData.POINT), 
write(GMLVersion.V3), "xmlns:*");
     }
+
+    /**
+     * Tests that {@link GMLVersion#V3_1} writes the GML 3 encoding in the 
unversioned namespace.
+     */
+    @Test
+    public void testWriteGML31Version() throws Exception {
+        final String xml = write(GMLVersion.V3_1);
+        assertTrue(xml.contains("\"http://www.opengis.net/gml\"";), xml);
+        assertFalse(xml.contains("http://www.opengis.net/gml/3.2";), xml);
+        assertTrue(xml.contains("<pos>"), xml);
+    }
+
+    /**
+     * Tests writing a geometry inside a document owned by the caller.
+     * The fragment writer shall neither write a second XML header nor close 
the document.
+     */
+    @Test
+    public void testFragment() throws Exception {
+        final Point g = GeometryFactory.DEFAULT.createPoint(
+                
GeometryFactory.DEFAULT.createDataPoints(NDArrays.of(SampleSystem.of(wgs84), 
10.0, 20.0)));
+        final var buffer = new StringWriter();
+        final XMLStreamWriter xml = 
XMLOutputFactory.newInstance().createXMLStreamWriter(buffer);
+        xml.writeStartDocument();
+        xml.writeStartElement("container");
+        try (GMLWriter writer = GMLWriter.fragment(xml, GMLVersion.V2)) {
+            writer.writeGeometry(g);
+        }
+        xml.writeEndElement();      // Shall still be possible after the 
fragment writer has been closed.
+        xml.writeEndDocument();
+        xml.close();
+        final String text = buffer.toString();
+        assertEquals(text.indexOf("<?xml"), text.lastIndexOf("<?xml"), text);
+        assertTrue(text.contains("<container><Point 
xmlns=\"http://www.opengis.net/gml\"";), text);
+        assertTrue(text.endsWith("</Point></container>"), text);
+    }
 }

Reply via email to