raminqaf commented on code in PR #29520:
URL: https://github.com/apache/flink/pull/29520#discussion_r4335442978
##########
flink-table/flink-table-common/src/main/java/org/apache/flink/table/functions/BuiltInFunctionDefinitions.java:
##########
@@ -3264,6 +3266,16 @@ ANY, and(logical(LogicalTypeRoot.BOOLEAN), LITERAL)
.runtimeProvided()
.build();
+ public static final BuiltInFunctionDefinition XML_STRING =
+ BuiltInFunctionDefinition.newBuilder()
+ .name("XML_STRING")
+ .kind(SCALAR)
+
.inputTypeStrategy(sequence(logical(LogicalTypeRoot.VARIANT)))
+
.outputTypeStrategy(nullableIfArgs(explicit(DataTypes.STRING())))
+ .runtimeClass(
+
"org.apache.flink.table.runtime.functions.scalar.XmlStringFunction")
Review Comment:
just want to be aligned with @snuyanzin that this function does not use
code-gen. Correct?
##########
flink-table/flink-table-runtime/src/main/java/org/apache/flink/table/runtime/functions/VariantToXmlWriter.java:
##########
@@ -0,0 +1,784 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.apache.flink.table.runtime.functions;
+
+import org.apache.flink.annotation.Internal;
+import org.apache.flink.table.runtime.functions.XmlVariantNames.SchemaPrefix;
+import org.apache.flink.types.variant.Variant;
+
+import org.w3c.dom.DOMException;
+import org.w3c.dom.Document;
+
+import javax.annotation.Nullable;
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.parsers.ParserConfigurationException;
+import javax.xml.stream.XMLOutputFactory;
+import javax.xml.stream.XMLStreamException;
+import javax.xml.stream.XMLStreamWriter;
+
+import java.io.StringWriter;
+import java.time.Instant;
+import java.time.LocalDate;
+import java.time.LocalDateTime;
+import java.time.ZoneOffset;
+import java.util.ArrayList;
+import java.util.Base64;
+import java.util.Collections;
+import java.util.Comparator;
+import java.util.EnumSet;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+import static
org.apache.flink.table.runtime.functions.XmlVariantNames.ATTRIBUTE_PREFIX;
+import static
org.apache.flink.table.runtime.functions.XmlVariantNames.ORDER_KEY;
+import static
org.apache.flink.table.runtime.functions.XmlVariantNames.TEXT_KEY;
+import static org.apache.flink.table.runtime.functions.XmlVariantNames.XSI_NIL;
+import static
org.apache.flink.table.runtime.functions.XmlVariantNames.XSI_TYPE;
+import static
org.apache.flink.types.variant.BinaryVariantUtil.TIMESTAMP_FORMATTER;
+import static
org.apache.flink.types.variant.BinaryVariantUtil.TIMESTAMP_LTZ_FORMATTER;
+import static org.apache.flink.types.variant.BinaryVariantUtil.TIME_FORMATTER;
+import static org.apache.flink.util.Preconditions.checkArgument;
+import static org.apache.flink.util.Preconditions.checkState;
+
+/**
+ * Writes a {@link Variant} as an XML string for {@code XML_STRING}. It
reverses the mapping of
+ * {@link XmlToVariantParser}, so that parsing the XML string of a variant
from {@code PARSE_XML}
+ * gives the same variant. The mapping is described in the documentation of
{@code XML_STRING}.
+ *
+ * <p>The content of an element is written in the order of its {@code #}
field. Content without a
+ * position in the {@code #} field follows in the order of its keys. A variant
{@code NULL}, or a
+ * {@code NULL} text, is written as {@code xsi:nil="true"}, and a scalar other
than a string or a
+ * UUID gets an {@code xsi:type}, e.g. {@code xsi:type="xs:int"}. If the
document uses the xsi or xs
+ * prefix, the root element declares it with the namespace of XML Schema,
unless the variant
+ * declares it on the root element itself.
+ *
+ * <p>The XML writer of the JDK writes the tags and escapes the text. It
writes tabs and line breaks
+ * in attribute values, and carriage returns in text, as they are, so a parser
reads them back as
+ * spaces and line feeds.
+ *
+ * <p>A variant fails with an {@link IllegalArgumentException} if it has no
XML representation:
+ *
+ * <ul>
+ * <li>it isn't an object with a single field for the root element, or the
root is an array,
+ * <li>a key isn't an XML name that {@code PARSE_XML} accepts,
+ * <li>a text or an attribute value has a character that XML 1.0 doesn't
allow, e.g. U+0000,
+ * <li>an array is nested in an array,
+ * <li>an {@code @} field holds an object, an array, or a {@code NULL}, or a
{@code $} field holds
+ * an object,
+ * <li>a {@code NULL} text isn't the only content of its element, or an
element has an {@code
+ * @xsi:nil} field that is true and content,
+ * <li>a {@code #} field isn't an object, doesn't have one integer position
for each item of a
+ * key, or has a position more than once,
+ * <li>an element would need two {@code xsi:type} or two {@code xsi:nil}
attributes, e.g. an
+ * {@code @xsi:type} field and a typed text, or an {@code @xsi:nil}
field and a {@code NULL}
+ * text.
+ * </ul>
+ */
Review Comment:
Should we shorten this wall of text?
##########
flink-table/flink-table-runtime/src/test/java/org/apache/flink/table/runtime/functions/VariantToXmlWriterTest.java:
##########
@@ -0,0 +1,377 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.apache.flink.table.runtime.functions;
+
+import org.apache.flink.types.variant.BinaryVariantInternalBuilder;
+import org.apache.flink.types.variant.Variant;
+import org.apache.flink.types.variant.VariantBuilder;
+
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.Arguments;
+import org.junit.jupiter.params.provider.MethodSource;
+import org.junit.jupiter.params.provider.ValueSource;
+
+import java.io.IOException;
+import java.math.BigDecimal;
+import java.time.Instant;
+import java.time.LocalDate;
+import java.time.LocalDateTime;
+import java.time.LocalTime;
+import java.util.Arrays;
+import java.util.UUID;
+import java.util.stream.Collectors;
+import java.util.stream.IntStream;
+import java.util.stream.Stream;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+import static org.junit.jupiter.params.provider.Arguments.arguments;
+
+/** Tests for {@link VariantToXmlWriter}. */
+class VariantToXmlWriterTest {
+
+ private static final String XSI =
"xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"";
+ private static final String XSI_AND_XS = XSI + "
xmlns:xs=\"http://www.w3.org/2001/XMLSchema\"";
+
+ private static final VariantBuilder BUILDER = Variant.newBuilder();
+
+ private final XmlToVariantParser parser = new XmlToVariantParser();
+
+ //
--------------------------------------------------------------------------------------------
+ // Variants from PARSE_XML
+ //
--------------------------------------------------------------------------------------------
Review Comment:
Use `@Nested` instead of comment separation
##########
flink-table/flink-table-runtime/src/test/java/org/apache/flink/table/runtime/functions/VariantToXmlWriterTest.java:
##########
@@ -0,0 +1,377 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.apache.flink.table.runtime.functions;
+
+import org.apache.flink.types.variant.BinaryVariantInternalBuilder;
+import org.apache.flink.types.variant.Variant;
+import org.apache.flink.types.variant.VariantBuilder;
+
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.Arguments;
+import org.junit.jupiter.params.provider.MethodSource;
+import org.junit.jupiter.params.provider.ValueSource;
+
+import java.io.IOException;
+import java.math.BigDecimal;
+import java.time.Instant;
+import java.time.LocalDate;
+import java.time.LocalDateTime;
+import java.time.LocalTime;
+import java.util.Arrays;
+import java.util.UUID;
+import java.util.stream.Collectors;
+import java.util.stream.IntStream;
+import java.util.stream.Stream;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+import static org.junit.jupiter.params.provider.Arguments.arguments;
+
+/** Tests for {@link VariantToXmlWriter}. */
+class VariantToXmlWriterTest {
+
+ private static final String XSI =
"xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"";
+ private static final String XSI_AND_XS = XSI + "
xmlns:xs=\"http://www.w3.org/2001/XMLSchema\"";
+
+ private static final VariantBuilder BUILDER = Variant.newBuilder();
+
+ private final XmlToVariantParser parser = new XmlToVariantParser();
+
+ //
--------------------------------------------------------------------------------------------
+ // Variants from PARSE_XML
+ //
--------------------------------------------------------------------------------------------
+
+ @ParameterizedTest(name = "{0}")
+ @MethodSource("parsedDocuments")
+ void testWriteParsedDocument(String xml, String expectedXml) {
Review Comment:
CsvSource instead, WDYT?
##########
flink-table/flink-table-runtime/src/main/java/org/apache/flink/table/runtime/functions/VariantToXmlWriter.java:
##########
@@ -0,0 +1,784 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.apache.flink.table.runtime.functions;
+
+import org.apache.flink.annotation.Internal;
+import org.apache.flink.table.runtime.functions.XmlVariantNames.SchemaPrefix;
+import org.apache.flink.types.variant.Variant;
+
+import org.w3c.dom.DOMException;
+import org.w3c.dom.Document;
+
+import javax.annotation.Nullable;
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.parsers.ParserConfigurationException;
+import javax.xml.stream.XMLOutputFactory;
+import javax.xml.stream.XMLStreamException;
+import javax.xml.stream.XMLStreamWriter;
+
+import java.io.StringWriter;
+import java.time.Instant;
+import java.time.LocalDate;
+import java.time.LocalDateTime;
+import java.time.ZoneOffset;
+import java.util.ArrayList;
+import java.util.Base64;
+import java.util.Collections;
+import java.util.Comparator;
+import java.util.EnumSet;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+import static
org.apache.flink.table.runtime.functions.XmlVariantNames.ATTRIBUTE_PREFIX;
+import static
org.apache.flink.table.runtime.functions.XmlVariantNames.ORDER_KEY;
+import static
org.apache.flink.table.runtime.functions.XmlVariantNames.TEXT_KEY;
+import static org.apache.flink.table.runtime.functions.XmlVariantNames.XSI_NIL;
+import static
org.apache.flink.table.runtime.functions.XmlVariantNames.XSI_TYPE;
+import static
org.apache.flink.types.variant.BinaryVariantUtil.TIMESTAMP_FORMATTER;
+import static
org.apache.flink.types.variant.BinaryVariantUtil.TIMESTAMP_LTZ_FORMATTER;
+import static org.apache.flink.types.variant.BinaryVariantUtil.TIME_FORMATTER;
+import static org.apache.flink.util.Preconditions.checkArgument;
+import static org.apache.flink.util.Preconditions.checkState;
+
+/**
+ * Writes a {@link Variant} as an XML string for {@code XML_STRING}. It
reverses the mapping of
+ * {@link XmlToVariantParser}, so that parsing the XML string of a variant
from {@code PARSE_XML}
+ * gives the same variant. The mapping is described in the documentation of
{@code XML_STRING}.
+ *
+ * <p>The content of an element is written in the order of its {@code #}
field. Content without a
+ * position in the {@code #} field follows in the order of its keys. A variant
{@code NULL}, or a
+ * {@code NULL} text, is written as {@code xsi:nil="true"}, and a scalar other
than a string or a
+ * UUID gets an {@code xsi:type}, e.g. {@code xsi:type="xs:int"}. If the
document uses the xsi or xs
+ * prefix, the root element declares it with the namespace of XML Schema,
unless the variant
+ * declares it on the root element itself.
+ *
+ * <p>The XML writer of the JDK writes the tags and escapes the text. It
writes tabs and line breaks
+ * in attribute values, and carriage returns in text, as they are, so a parser
reads them back as
+ * spaces and line feeds.
+ *
+ * <p>A variant fails with an {@link IllegalArgumentException} if it has no
XML representation:
+ *
+ * <ul>
+ * <li>it isn't an object with a single field for the root element, or the
root is an array,
+ * <li>a key isn't an XML name that {@code PARSE_XML} accepts,
+ * <li>a text or an attribute value has a character that XML 1.0 doesn't
allow, e.g. U+0000,
+ * <li>an array is nested in an array,
+ * <li>an {@code @} field holds an object, an array, or a {@code NULL}, or a
{@code $} field holds
+ * an object,
+ * <li>a {@code NULL} text isn't the only content of its element, or an
element has an {@code
+ * @xsi:nil} field that is true and content,
+ * <li>a {@code #} field isn't an object, doesn't have one integer position
for each item of a
+ * key, or has a position more than once,
+ * <li>an element would need two {@code xsi:type} or two {@code xsi:nil}
attributes, e.g. an
+ * {@code @xsi:type} field and a typed text, or an {@code @xsi:nil}
field and a {@code NULL}
+ * text.
+ * </ul>
+ */
+@Internal
+public final class VariantToXmlWriter {
Review Comment:
Does it makes sense to move this class into flink-core next to the
`JsonVariantFormatter` and call it `XmlVariantFormatter`. What is your take on
this idea?
##########
flink-table/flink-table-runtime/src/main/java/org/apache/flink/table/runtime/functions/VariantToXmlWriter.java:
##########
@@ -0,0 +1,784 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.apache.flink.table.runtime.functions;
+
+import org.apache.flink.annotation.Internal;
+import org.apache.flink.table.runtime.functions.XmlVariantNames.SchemaPrefix;
+import org.apache.flink.types.variant.Variant;
+
+import org.w3c.dom.DOMException;
+import org.w3c.dom.Document;
+
+import javax.annotation.Nullable;
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.parsers.ParserConfigurationException;
+import javax.xml.stream.XMLOutputFactory;
+import javax.xml.stream.XMLStreamException;
+import javax.xml.stream.XMLStreamWriter;
+
+import java.io.StringWriter;
+import java.time.Instant;
+import java.time.LocalDate;
+import java.time.LocalDateTime;
+import java.time.ZoneOffset;
+import java.util.ArrayList;
+import java.util.Base64;
+import java.util.Collections;
+import java.util.Comparator;
+import java.util.EnumSet;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+import static
org.apache.flink.table.runtime.functions.XmlVariantNames.ATTRIBUTE_PREFIX;
+import static
org.apache.flink.table.runtime.functions.XmlVariantNames.ORDER_KEY;
+import static
org.apache.flink.table.runtime.functions.XmlVariantNames.TEXT_KEY;
+import static org.apache.flink.table.runtime.functions.XmlVariantNames.XSI_NIL;
+import static
org.apache.flink.table.runtime.functions.XmlVariantNames.XSI_TYPE;
+import static
org.apache.flink.types.variant.BinaryVariantUtil.TIMESTAMP_FORMATTER;
+import static
org.apache.flink.types.variant.BinaryVariantUtil.TIMESTAMP_LTZ_FORMATTER;
+import static org.apache.flink.types.variant.BinaryVariantUtil.TIME_FORMATTER;
+import static org.apache.flink.util.Preconditions.checkArgument;
+import static org.apache.flink.util.Preconditions.checkState;
+
+/**
+ * Writes a {@link Variant} as an XML string for {@code XML_STRING}. It
reverses the mapping of
+ * {@link XmlToVariantParser}, so that parsing the XML string of a variant
from {@code PARSE_XML}
+ * gives the same variant. The mapping is described in the documentation of
{@code XML_STRING}.
+ *
+ * <p>The content of an element is written in the order of its {@code #}
field. Content without a
+ * position in the {@code #} field follows in the order of its keys. A variant
{@code NULL}, or a
+ * {@code NULL} text, is written as {@code xsi:nil="true"}, and a scalar other
than a string or a
+ * UUID gets an {@code xsi:type}, e.g. {@code xsi:type="xs:int"}. If the
document uses the xsi or xs
+ * prefix, the root element declares it with the namespace of XML Schema,
unless the variant
+ * declares it on the root element itself.
+ *
+ * <p>The XML writer of the JDK writes the tags and escapes the text. It
writes tabs and line breaks
+ * in attribute values, and carriage returns in text, as they are, so a parser
reads them back as
+ * spaces and line feeds.
+ *
+ * <p>A variant fails with an {@link IllegalArgumentException} if it has no
XML representation:
+ *
+ * <ul>
+ * <li>it isn't an object with a single field for the root element, or the
root is an array,
+ * <li>a key isn't an XML name that {@code PARSE_XML} accepts,
+ * <li>a text or an attribute value has a character that XML 1.0 doesn't
allow, e.g. U+0000,
+ * <li>an array is nested in an array,
+ * <li>an {@code @} field holds an object, an array, or a {@code NULL}, or a
{@code $} field holds
+ * an object,
+ * <li>a {@code NULL} text isn't the only content of its element, or an
element has an {@code
+ * @xsi:nil} field that is true and content,
+ * <li>a {@code #} field isn't an object, doesn't have one integer position
for each item of a
+ * key, or has a position more than once,
+ * <li>an element would need two {@code xsi:type} or two {@code xsi:nil}
attributes, e.g. an
+ * {@code @xsi:type} field and a typed text, or an {@code @xsi:nil}
field and a {@code NULL}
+ * text.
+ * </ul>
+ */
+@Internal
+public final class VariantToXmlWriter {
Review Comment:
I haven't gone through this file in detail. But one question, are there
common functionalities we can extract between VariantToXmlWritter and
XmlToVariantParser?
##########
flink-table/flink-table-runtime/src/test/java/org/apache/flink/table/runtime/functions/VariantToXmlWriterTest.java:
##########
@@ -0,0 +1,377 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.apache.flink.table.runtime.functions;
+
+import org.apache.flink.types.variant.BinaryVariantInternalBuilder;
+import org.apache.flink.types.variant.Variant;
+import org.apache.flink.types.variant.VariantBuilder;
+
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.Arguments;
+import org.junit.jupiter.params.provider.MethodSource;
+import org.junit.jupiter.params.provider.ValueSource;
+
+import java.io.IOException;
+import java.math.BigDecimal;
+import java.time.Instant;
+import java.time.LocalDate;
+import java.time.LocalDateTime;
+import java.time.LocalTime;
+import java.util.Arrays;
+import java.util.UUID;
+import java.util.stream.Collectors;
+import java.util.stream.IntStream;
+import java.util.stream.Stream;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+import static org.junit.jupiter.params.provider.Arguments.arguments;
+
+/** Tests for {@link VariantToXmlWriter}. */
+class VariantToXmlWriterTest {
+
+ private static final String XSI =
"xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"";
+ private static final String XSI_AND_XS = XSI + "
xmlns:xs=\"http://www.w3.org/2001/XMLSchema\"";
+
+ private static final VariantBuilder BUILDER = Variant.newBuilder();
+
+ private final XmlToVariantParser parser = new XmlToVariantParser();
+
+ //
--------------------------------------------------------------------------------------------
+ // Variants from PARSE_XML
+ //
--------------------------------------------------------------------------------------------
+
+ @ParameterizedTest(name = "{0}")
+ @MethodSource("parsedDocuments")
+ void testWriteParsedDocument(String xml, String expectedXml) {
+ assertThat(VariantToXmlWriter.write(parser.parse(xml,
false))).isEqualTo(expectedXml);
+ }
+
+ static Stream<Arguments> parsedDocuments() {
+ return Stream.of(
+ // The result is the normalized document, with the content in
the order of the #
+ // field and the attributes in the order of their keys.
+ arguments(
+ "<?xml version=\"1.0\"?><!-- c --><book pages=\"320\"
id=\"1\">\n"
+ + " <title>Dune</title>\n"
+ + " This is a description\n"
+ + " <!-- c --><price>12.50</price><?pi x?>\n"
+ + "</book>",
+ "<book id=\"1\" pages=\"320\"><title>Dune</title>This
is a description"
+ + "<price>12.50</price></book>"),
+ // An empty string is an empty element.
+ arguments("<title/>", "<title></title>"),
+ // Namespace prefixes and declarations are kept.
+ arguments(
+ "<ns:a xmlns:ns=\"urn:ns\"
ns:id=\"1\"><ns:b>x</ns:b></ns:a>",
+ "<ns:a ns:id=\"1\"
xmlns:ns=\"urn:ns\"><ns:b>x</ns:b></ns:a>"),
+ // A NULL, and the null text of an element with attributes,
are nil.
+ arguments(
+ "<r><a xsi:nil=\"true\"/><b xsi:nil=\"true\"
id=\"1\">x</b></r>",
+ "<r " + XSI + "><a xsi:nil=\"true\"/><b id=\"1\"
xsi:nil=\"true\"/></r>"),
+ arguments("<a xsi:nil=\"true\"/>", "<a " + XSI + "
xsi:nil=\"true\"/>"),
+ // Typed values are written in the lexical form of their type.
+ arguments(
+ "<r>"
+ + "<a xsi:type=\"boolean\">1</a>"
+ + "<b xsi:type=\"xs:int\">+5</b>"
+ + "<c xsi:type=\"integer\">007</c>"
+ + "<d xsi:type=\"double\">1.5E3</d>"
+ + "<e xsi:type=\"time\">12:30:00.500</e>"
+ + "<f
xsi:type=\"dateTime\">2026-09-23T12:30:45.5+02:00</f>"
+ + "<g
xsi:type=\"dateTime\">2026-09-23T12:30:45</g>"
+ + "<h xsi:type=\"base64Binary\">AQ I D</h>"
+ + "</r>",
+ "<r "
+ + XSI_AND_XS
+ + "><a xsi:type=\"xs:boolean\">true</a>"
+ + "<b xsi:type=\"xs:int\">5</b>"
+ + "<c xsi:type=\"xs:decimal\">7</c>"
+ + "<d xsi:type=\"xs:double\">1500.0</d>"
+ + "<e xsi:type=\"xs:time\">12:30:00.5</e>"
+ + "<f
xsi:type=\"xs:dateTime\">2026-09-23T10:30:45.5Z</f>"
+ + "<g
xsi:type=\"xs:dateTime\">2026-09-23T12:30:45</g>"
+ + "<h xsi:type=\"xs:base64Binary\">AQID</h>"
+ + "</r>"),
+ arguments(
+ "<price xsi:type=\"decimal\"
currency=\"EUR\">13.50</price>",
+ "<price "
+ + XSI_AND_XS
+ + " currency=\"EUR\"
xsi:type=\"xs:decimal\">13.5</price>"),
+ // An xsi:type that is kept as an attribute is written as it
is.
+ arguments(
+ "<a xsi:type=\"int\">five</a>", "<a " + XSI + "
xsi:type=\"int\">five</a>"),
+ // The declarations with the namespaces of XML Schema are
dropped and declared
+ // again where needed, and other declarations are kept.
+ arguments(
+ "<r
xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"><a>x</a></r>",
+ "<r><a>x</a></r>"),
+ arguments(
+ "<xs:schema
xmlns:xs=\"http://www.w3.org/2001/XMLSchema\">"
+ + "<xs:element name=\"Book\"/></xs:schema>",
+ "<xs:schema
xmlns:xs=\"http://www.w3.org/2001/XMLSchema\">"
+ + "<xs:element
name=\"Book\"></xs:element></xs:schema>"),
+ arguments(
+ "<r xmlns:xs=\"urn:custom\"><a
xsi:type=\"int\">5</a></r>",
+ "<r " + XSI + " xmlns:xs=\"urn:custom\"><a
xsi:type=\"xs:int\">5</a></r>"),
+ // A declaration on another element doesn't apply to the root
element.
+ arguments(
+ "<r><a xmlns:xsi=\"urn:custom\"
xsi:nil=\"true\"/></r>",
+ "<r " + XSI + "><a xmlns:xsi=\"urn:custom\"
xsi:nil=\"true\"/></r>"));
+ }
+
+ /** {@code PARSE_XML(XML_STRING(v))} is {@code v} for variants from {@code
PARSE_XML}. */
+ @ParameterizedTest(name = "{0}")
+ @ValueSource(
+ strings = {
+ "<book pages=\"320\"><title>Dune</title>This is a description"
+ + "<price>12.50</price>Text in between<price
currency=\"EUR\">13.50"
+ + "</price></book>",
+ "<a><b>1</b><c>2</c><b>3</b><c>4</c>text<b>5</b></a>",
+ "<ns:a xmlns:ns=\"urn:ns\" xmlns=\"urn:default\"
ns:id=\"1\"><ns:b>x</ns:b></ns:a>",
+ "<a b=\""<&> x
\"><&>\"	x y</a>",
+ "<book><a xsi:nil=\"true\" id=\"1\"/><a>x</a><a
xsi:nil=\"true\"/></book>",
+ "<a xsi:type=\"int\">five</a>",
+ "<xs:schema xmlns:xs=\"http://www.w3.org/2001/XMLSchema\">"
+ + "<xs:element name=\"Book\"
type=\"xs:string\"/></xs:schema>",
+ "<r xmlns:xs=\"urn:custom\"><a xsi:type=\"int\">5</a></r>",
+ "<r><a xmlns:xsi=\"urn:custom\" xsi:nil=\"true\"/></r>",
+ "<r>"
+ + "<a xsi:type=\"boolean\">true</a>"
+ + "<b xsi:type=\"byte\">-1</b>"
+ + "<c xsi:type=\"short\">300</c>"
+ + "<d xsi:type=\"int\">70000</d>"
+ + "<e xsi:type=\"long\">5000000000</e>"
+ + "<f
xsi:type=\"integer\">123456789012345678901234567890</f>"
+ + "<g xsi:type=\"decimal\">-12.50</g>"
+ + "<h xsi:type=\"float\">1.1</h>"
+ + "<i xsi:type=\"double\">1.0E-300</i>"
+ + "<j xsi:type=\"date\">2026-09-23</j>"
+ + "<k xsi:type=\"time\">12:30:45.123456</k>"
+ + "<l
xsi:type=\"dateTime\">2026-09-23T12:30:45.123456</l>"
+ + "<m
xsi:type=\"dateTime\">2026-09-23T12:30:45.123456789</m>"
+ + "<n
xsi:type=\"dateTime\">2026-09-23T12:30:45.123456+02:00</n>"
+ + "<o
xsi:type=\"dateTime\">2026-09-23T12:30:45.123456789Z</o>"
+ + "<p xsi:type=\"string\">5</p>"
+ + "<q xsi:type=\"base64Binary\">AQID</q>"
+ + "</r>",
+ // The limits of the types.
+ "<r>"
+ + "<a xsi:type=\"date\">9999-12-31</a>"
+ + "<b xsi:type=\"date\">-5877641-06-23</b>"
+ + "<c
xsi:type=\"dateTime\">9999-12-31T23:59:59.999999Z</c>"
+ + "<d
xsi:type=\"dateTime\">2262-04-11T23:47:16.854775807</d>"
+ + "<e xsi:type=\"time\">23:59:59.999999</e>"
+ + "<f xsi:type=\"double\">4.9E-324</f>"
+ + "<g xsi:type=\"float\">3.4028235E38</g>"
+ + "</r>"
+ })
+ void testRoundTrip(String xml) {
+ for (boolean forceArray : new boolean[] {false, true}) {
+ final Variant variant = parser.parse(xml, forceArray);
+ final String written = VariantToXmlWriter.write(variant);
+ final Variant reparsed = parser.parse(written, forceArray);
+
assertThat(describe(reparsed)).as(written).isEqualTo(describe(variant));
+ // The written document is normalized, so writing it again gives
the same document.
+ assertThat(VariantToXmlWriter.write(reparsed)).isEqualTo(written);
+ }
+ }
+
+ /**
+ * Tabs and line breaks in attribute values, and carriage returns in text,
are written as they
+ * are, so a parser reads them back as spaces and line feeds.
+ */
+ @Test
+ void testWhitespaceThatParsersNormalize() {
+ final Variant variant = parser.parse("<a
b=\"x	y z w\">x y</a>", false);
+ assertThat(parser.parse(VariantToXmlWriter.write(variant),
false).toJson())
+ .isEqualTo("{\"a\":{\"$\":\"x\\ny\",\"@b\":\"x y z w\"}}");
+ }
+
+ //
--------------------------------------------------------------------------------------------
+ // Scalars
+ //
--------------------------------------------------------------------------------------------
+
+ @ParameterizedTest(name = "{1}")
+ @MethodSource({"typedScalars", "untypedScalars"})
+ void testWriteScalar(Variant scalar, String expectedXml) {
+ assertThat(VariantToXmlWriter.write(BUILDER.object().add("a",
scalar).build()))
+ .isEqualTo(expectedXml);
+ }
+
+ static Stream<Arguments> typedScalars() {
+ return Stream.of(
+ arguments(BUILDER.of(true), typed("boolean", "true")),
+ arguments(BUILDER.of((byte) -1), typed("byte", "-1")),
+ arguments(BUILDER.of((short) 300), typed("short", "300")),
+ arguments(BUILDER.of(70000), typed("int", "70000")),
+ arguments(BUILDER.of(5000000000L), typed("long",
"5000000000")),
+ arguments(BUILDER.of(new BigDecimal("1E+3")), typed("decimal",
"1000")),
+ arguments(BUILDER.of(1.1f), typed("float", "1.1")),
+ arguments(BUILDER.of(1.0E-300), typed("double", "1.0E-300")),
+ // XML Schema writes infinity as INF.
+ arguments(BUILDER.of(Float.NaN), typed("float", "NaN")),
+ arguments(BUILDER.of(Double.NEGATIVE_INFINITY),
typed("double", "-INF")),
+ // Years after 9999 have no + sign.
+ arguments(BUILDER.of(LocalDate.of(10000, 1, 1)), typed("date",
"10000-01-01")),
+ arguments(BUILDER.of(LocalDate.of(-1, 1, 1)), typed("date",
"-0001-01-01")),
+ // The seconds are always written.
+ arguments(BUILDER.of(LocalTime.of(12, 30)), typed("time",
"12:30:00")),
+ arguments(
+ BUILDER.of(LocalDateTime.of(2026, 9, 23, 12, 30, 45,
123_456_789)),
+ typed("dateTime", "2026-09-23T12:30:45.123456789")),
+ // A TIMESTAMP_LTZ is written in UTC.
+ arguments(
+ BUILDER.of(Instant.parse("2026-09-23T10:30:00Z")),
+ typed("dateTime", "2026-09-23T10:30:00Z")),
+ arguments(BUILDER.of(new byte[] {1, 2, 3}),
typed("base64Binary", "AQID")));
+ }
+
+ static Stream<Arguments> untypedScalars() {
+ return Stream.of(
+ arguments(BUILDER.of("x"), "<a>x</a>"),
+ // XML Schema has no type for a UUID.
+ arguments(
+
BUILDER.of(UUID.fromString("123e4567-e89b-12d3-a456-426614174000")),
+ "<a>123e4567-e89b-12d3-a456-426614174000</a>"));
+ }
+
+ private static String typed(String xsiType, String text) {
+ return String.format("<a %s xsi:type=\"xs:%s\">%s</a>", XSI_AND_XS,
xsiType, text);
+ }
+
+ //
--------------------------------------------------------------------------------------------
+ // Variants without an XML representation
+ //
--------------------------------------------------------------------------------------------
+
+ @ParameterizedTest(name = "{0}")
+ @MethodSource("variantsWithoutXmlRepresentation")
+ void testNoXmlRepresentation(String json, String expectedMessage) throws
IOException {
Review Comment:
CsvSource instead, WDYT?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]