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

pjfanning pushed a commit to branch trunk
in repository https://gitbox.apache.org/repos/asf/poi-xmlbeans.git


The following commit(s) were added to refs/heads/trunk by this push:
     new 0a573fdd assess undeclared wildcard elements against their xsi:type in 
Validator (#151)
0a573fdd is described below

commit 0a573fdd546549010f62e63425f6b1344a39304d
Author: aizu-m <[email protected]>
AuthorDate: Mon Nov 16 01:49:09 2026 +0530

    assess undeclared wildcard elements against their xsi:type in Validator 
(#151)
    
    * assess undeclared wildcard elements against their xsi:type in Validator
    
    * add XmlOptions.setValidateWildcardIgnoreXsiType to restore the old 
wildcard behaviour
    
    Undeclared elements matching a lax or strict wildcard are now assessed
    against their xsi:type. Users who relied on the previous behaviour, where
    such elements were skipped (lax) or reported as unresolved (strict)
    whatever their xsi:type, can opt back in with this validation option.
    Unlike setValidateTreatLaxAsSkip, declared elements are still validated.
    
    Co-Authored-By: Claude Opus 5.5 <[email protected]>
    
    ---------
    
    Co-authored-by: PJ Fanning <[email protected]>
    Co-authored-by: Claude Opus 5.5 <[email protected]>
---
 src/main/java/org/apache/xmlbeans/XmlOptions.java  |  37 +++++++
 .../apache/xmlbeans/impl/validator/Validator.java  |   8 +-
 .../misc/checkin/WildcardXsiTypeValidateTest.java  | 121 +++++++++++++++++++++
 3 files changed, 164 insertions(+), 2 deletions(-)

diff --git a/src/main/java/org/apache/xmlbeans/XmlOptions.java 
b/src/main/java/org/apache/xmlbeans/XmlOptions.java
index be3bddfa..725a0f37 100644
--- a/src/main/java/org/apache/xmlbeans/XmlOptions.java
+++ b/src/main/java/org/apache/xmlbeans/XmlOptions.java
@@ -140,6 +140,7 @@ public class XmlOptions implements java.io.Serializable {
         COMPILE_ANNOTATION_JAVADOC,
         VALIDATE_ON_SET,
         VALIDATE_TREAT_LAX_AS_SKIP,
+        VALIDATE_WILDCARD_IGNORE_XSI_TYPE,
         VALIDATE_STRICT,
         VALIDATE_TEXT_ONLY,
         UNSYNCHRONIZED,
@@ -1302,6 +1303,42 @@ public class XmlOptions implements java.io.Serializable {
         return hasOption(XmlOptionsKeys.VALIDATE_TREAT_LAX_AS_SKIP);
     }
 
+    /**
+     * Restores the validation of undeclared wildcard elements used before 
6.0.0.
+     * <p>
+     * By default, an element that matches a lax or strict {@code <any>} 
wildcard
+     * and has no global declaration is assessed against its {@code xsi:type}
+     * when it has one (XML Schema 1.0 cvc-assess-elt.1.2), so an unknown
+     * {@code xsi:type} or content that does not match the type is reported.
+     * With this option the {@code xsi:type} is ignored: such an element is
+     * skipped under a lax wildcard and reported as unresolved under a strict 
one,
+     * whether or not it has an {@code xsi:type}.
+     * <p>
+     * Unlike {@link #setValidateTreatLaxAsSkip()}, elements that do have a
+     * global declaration are still validated.
+     *
+     * @since 6.0.0
+     */
+    public XmlOptions setValidateWildcardIgnoreXsiType() {
+        return setValidateWildcardIgnoreXsiType(true);
+    }
+
+    /**
+     * @see #setValidateWildcardIgnoreXsiType()
+     * @since 6.0.0
+     */
+    public XmlOptions setValidateWildcardIgnoreXsiType(boolean b) {
+        return set(XmlOptionsKeys.VALIDATE_WILDCARD_IGNORE_XSI_TYPE, b);
+    }
+
+    /**
+     * @see #setValidateWildcardIgnoreXsiType()
+     * @since 6.0.0
+     */
+    public boolean isValidateWildcardIgnoreXsiType() {
+        return hasOption(XmlOptionsKeys.VALIDATE_WILDCARD_IGNORE_XSI_TYPE);
+    }
+
     /**
      * Performs additional validation checks that are disabled by
      * default for better compatibility.
diff --git a/src/main/java/org/apache/xmlbeans/impl/validator/Validator.java 
b/src/main/java/org/apache/xmlbeans/impl/validator/Validator.java
index 0f7fc97b..8cc09ede 100644
--- a/src/main/java/org/apache/xmlbeans/impl/validator/Validator.java
+++ b/src/main/java/org/apache/xmlbeans/impl/validator/Validator.java
@@ -38,6 +38,7 @@ public final class Validator
         _options = XmlOptions.maskNull(options);
         _errorListener = _options.getErrorListener();
         _treatLaxAsSkip = _options.isValidateTreatLaxAsSkip();
+        _wildcardIgnoreXsiType = _options.isValidateWildcardIgnoreXsiType();
         _strict = _options.isValidateStrict();
 
         if (_errorListener == null) {
@@ -250,7 +251,7 @@ public final class Validator
                 _localElement = _globalTypes.findElement(name);
                 elementField = _localElement;
 
-                if (elementField == null) {
+                if (elementField == null && (_wildcardIgnoreXsiType || 
event.getXsiType() == null)) {
                     if (wildcardProcess == SchemaParticle.STRICT) {
                         // KHK: cvc-complex-type.2.4c? 
cvc-assess-elt.1.1.1.3.2?
                         emitFieldError(event, 
XmlErrorCodes.ASSESS_ELEM_SCHEMA_VALID$NOT_RESOLVED,
@@ -293,7 +294,9 @@ public final class Validator
                 }
             }
 
-            elementType = elementField.getType();
+            // without a declaration the element is assessed against its
+            // xsi:type alone (cvc-assess-elt.1.2)
+            elementType = elementField == null ? XmlObject.type : 
elementField.getType();
         }
 
         assert elementType != null;
@@ -1516,6 +1519,7 @@ public final class Validator
     private int _errorState;
     private Collection<XmlError> _errorListener;
     private final boolean _treatLaxAsSkip;
+    private final boolean _wildcardIgnoreXsiType;
     private final boolean _strict;
     private final ValidatorVC _vc;
     private int _suspendErrors;
diff --git a/src/test/java/misc/checkin/WildcardXsiTypeValidateTest.java 
b/src/test/java/misc/checkin/WildcardXsiTypeValidateTest.java
new file mode 100644
index 00000000..4f77f4ed
--- /dev/null
+++ b/src/test/java/misc/checkin/WildcardXsiTypeValidateTest.java
@@ -0,0 +1,121 @@
+/*   Copyright 2004 The Apache Software Foundation
+ *
+ *   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 misc.checkin;
+
+import org.apache.xmlbeans.SchemaTypeLoader;
+import org.apache.xmlbeans.XmlBeans;
+import org.apache.xmlbeans.XmlError;
+import org.apache.xmlbeans.XmlObject;
+import org.apache.xmlbeans.XmlOptions;
+import org.apache.xmlbeans.impl.xb.xsdschema.SchemaDocument;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.ValueSource;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+public class WildcardXsiTypeValidateTest {
+
+    // u has no global declaration, so only its xsi:type can be used to assess 
it
+    private static XmlObject parse(String processContents, String child) 
throws Exception {
+        String xsd =
+            "<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema' " +
+            "xmlns:t='urn:t' targetNamespace='urn:t'>" +
+            "  <xs:element name='root'><xs:complexType><xs:sequence>" +
+            "    <xs:any namespace='##any' processContents='" + 
processContents + "'/>" +
+            "  </xs:sequence></xs:complexType></xs:element>" +
+            "  <xs:simpleType name='small'>" +
+            "    <xs:restriction base='xs:int'><xs:maxInclusive 
value='5'/></xs:restriction>" +
+            "  </xs:simpleType>" +
+            "  <xs:complexType name='ct'><xs:sequence>" +
+            "    <xs:element name='z' type='xs:int'/>" +
+            "  </xs:sequence></xs:complexType>" +
+            "</xs:schema>";
+
+        SchemaTypeLoader loader = XmlBeans.loadXsd(new 
XmlObject[]{SchemaDocument.Factory.parse(xsd)});
+        return loader.parse(
+            "<t:root xmlns:t='urn:t' 
xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>" +
+            child + "</t:root>", null, null);
+    }
+
+    private static List<XmlError> validate(XmlObject doc) {
+        List<XmlError> errors = new ArrayList<>();
+        doc.validate(new XmlOptions().setErrorListener(errors));
+        return errors;
+    }
+
+    @ParameterizedTest
+    @ValueSource(strings = {"lax", "strict"})
+    void validXsiTypeContent(String processContents) throws Exception {
+        assertTrue(validate(parse(processContents, "<u 
xsi:type='t:small'>3</u>")).isEmpty());
+        assertTrue(validate(parse(processContents, "<u 
xsi:type='t:ct'><z>1</z></u>")).isEmpty());
+    }
+
+    @ParameterizedTest
+    @ValueSource(strings = {"lax", "strict"})
+    void invalidXsiTypeContent(String processContents) throws Exception {
+        List<XmlError> errors = validate(parse(processContents, "<u 
xsi:type='t:small'>9</u>"));
+        assertEquals(1, errors.size(), errors.toString());
+        assertEquals("cvc-maxInclusive-valid", errors.get(0).getErrorCode());
+
+        errors = validate(parse(processContents, "<u 
xsi:type='t:ct'><z>x</z></u>"));
+        assertEquals(1, errors.size(), errors.toString());
+        assertEquals("decimal", errors.get(0).getErrorCode());
+
+        errors = validate(parse(processContents, "<u xsi:type='t:missing'/>"));
+        assertEquals(1, errors.size(), errors.toString());
+        assertEquals("cvc-elt.4.2", errors.get(0).getErrorCode());
+    }
+
+    @ParameterizedTest
+    @ValueSource(strings = {"lax", "strict"})
+    void undeclaredWithoutXsiType(String processContents) throws Exception {
+        XmlObject doc = parse(processContents, "<u>x</u>");
+        if ("lax".equals(processContents)) {
+            assertTrue(doc.validate());
+        } else {
+            List<XmlError> errors = validate(doc);
+            assertFalse(errors.isEmpty());
+            assertEquals("cvc-assess-elt.1.1.1.3.2", 
errors.get(0).getErrorCode());
+        }
+    }
+
+    @ParameterizedTest
+    @ValueSource(strings = {"lax", "strict"})
+    void ignoreXsiTypeOption(String processContents) throws Exception {
+        // setValidateWildcardIgnoreXsiType restores the pre-6.0.0 behaviour: 
the
+        // xsi:type of an undeclared wildcard element is not looked at
+        for (String child : new String[]{
+            "<u xsi:type='t:small'>3</u>",
+            "<u xsi:type='t:small'>9</u>",
+            "<u xsi:type='t:ct'><z>x</z></u>",
+            "<u xsi:type='t:missing'/>"}) {
+            List<XmlError> errors = new ArrayList<>();
+            parse(processContents, child).validate(
+                new 
XmlOptions().setErrorListener(errors).setValidateWildcardIgnoreXsiType());
+            if ("lax".equals(processContents)) {
+                assertTrue(errors.isEmpty(), child + " " + errors);
+            } else {
+                assertEquals(1, errors.size(), child + " " + errors);
+                assertEquals("cvc-assess-elt.1.1.1.3.2", 
errors.get(0).getErrorCode());
+            }
+        }
+    }
+}


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to