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 8a841101 apply block to substitution group members in
Validator.beginEvent (#163)
8a841101 is described below
commit 8a841101310232da6cd2cf4960bb91bd2cf6f78a
Author: aizu-m <[email protected]>
AuthorDate: Mon Nov 16 01:52:09 2026 +0530
apply block to substitution group members in Validator.beginEvent (#163)
A substitution group member was accepted in place of its head as long
as the head did not have block='substitution'. A member whose type is
derived from the head's type by extension or restriction must also be
rejected when the head element, the head's type or an intermediate
type blocks that derivation (Substitution Group OK (Transitive) 2.3).
Report such members as cvc-particle.2.3.3b.
---
.../java/org/apache/xmlbeans/XmlErrorCodes.java | 6 ++
.../apache/xmlbeans/impl/validator/Validator.java | 40 ++++++++
.../org/apache/xmlbeans/message.properties | 3 +
.../SubstitutionGroupBlockValidateTest.java | 102 +++++++++++++++++++++
4 files changed, 151 insertions(+)
diff --git a/src/main/java/org/apache/xmlbeans/XmlErrorCodes.java
b/src/main/java/org/apache/xmlbeans/XmlErrorCodes.java
index 0e367cf3..6272f076 100644
--- a/src/main/java/org/apache/xmlbeans/XmlErrorCodes.java
+++ b/src/main/java/org/apache/xmlbeans/XmlErrorCodes.java
@@ -495,6 +495,12 @@ public abstract class XmlErrorCodes
*/
public static final String PARTICLE_VALID$BLOCK_SUBSTITUTION =
"cvc-particle.2.3.3a";
+ /**
+ * cvc-particle.2.3.3b: See
+ * <a href="http://www.w3c.org/TR/xmlschema-1/#cvc-particle">XMLSchema
Structures 1.0: Element Sequence Locally Valid (Particle)</a>
+ */
+ public static final String PARTICLE_VALID$BLOCK_DERIVATION =
"cvc-particle.2.3.3b";
+
/**
* cvc-pattern-valid: See
* <a
href="http://www.w3c.org/TR/xmlschema-2/#cvc-pattern-valid">XMLSchema Datatypes
1.0: pattern valid</a>
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 8cc09ede..502a251a 100644
--- a/src/main/java/org/apache/xmlbeans/impl/validator/Validator.java
+++ b/src/main/java/org/apache/xmlbeans/impl/validator/Validator.java
@@ -287,6 +287,16 @@ public final class Validator
assert newField != null;
+ if (isSubstitutionBlocked((SchemaLocalElement)
currentParticle, newField.getType())) {
+ emitFieldError(event,
XmlErrorCodes.PARTICLE_VALID$BLOCK_DERIVATION,
+ new Object[]{QNameHelper.pretty(name)},
+ name, state._type, null,
+ XmlValidationError.ELEMENT_NOT_ALLOWED,
state._type);
+
+ _eatContent = 1;
+ return;
+ }
+
elementField = newField;
_localElement = newField;
} else {
@@ -801,6 +811,36 @@ public final class Validator
}
}
+ /**
+ * Substitution Group OK (Transitive) 2.3: a member is not part of the
+ * substitution group when its type is derived from the head's type by a
+ * method that the head element, the head's type or an intermediate type
blocks.
+ */
+ private static boolean isSubstitutionBlocked(SchemaLocalElement head,
SchemaType memberType) {
+ SchemaType headType = head.getType();
+ boolean blockExt = head.blockExtension();
+ boolean blockRest = head.blockRestriction();
+ boolean ext = false;
+ boolean rest = false;
+
+ for (SchemaType t = memberType; !t.equals(headType); ) {
+ if (t.getDerivationType() == SchemaType.DT_EXTENSION) {
+ ext = true;
+ } else {
+ rest = true;
+ }
+ t = t.getBaseType();
+ if (t == null) {
+ // head type is not on the base chain (union member), nothing
to check
+ return false;
+ }
+ blockExt |= t.blockExtension();
+ blockRest |= t.blockRestriction();
+ }
+
+ return (ext && blockExt) || (rest && blockRest);
+ }
+
private static final class State {
boolean visit(QName name) {
diff --git a/src/main/resources/org/apache/xmlbeans/message.properties
b/src/main/resources/org/apache/xmlbeans/message.properties
index 80d970ba..bc0915bb 100644
--- a/src/main/resources/org/apache/xmlbeans/message.properties
+++ b/src/main/resources/org/apache/xmlbeans/message.properties
@@ -266,6 +266,9 @@ Element not allowed: {0}
cvc-particle.2.3.3a: \
Element substitution not allowed when group head has block=''substitution'':
{0}
+cvc-particle.2.3.3b: \
+Element substitution not allowed when the group head or a base type blocks the
derivation of its type: {0}
+
cvc-pattern-valid = \
pattern valid
diff --git a/src/test/java/misc/checkin/SubstitutionGroupBlockValidateTest.java
b/src/test/java/misc/checkin/SubstitutionGroupBlockValidateTest.java
new file mode 100644
index 00000000..3e48eb5a
--- /dev/null
+++ b/src/test/java/misc/checkin/SubstitutionGroupBlockValidateTest.java
@@ -0,0 +1,102 @@
+/* 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.api.BeforeAll;
+import org.junit.jupiter.api.Test;
+
+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 SubstitutionGroupBlockValidateTest {
+
+ private static final String XSD =
+ "<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema'
xmlns:t='urn:t' " +
+ "targetNamespace='urn:t' elementFormDefault='qualified'>" +
+ " <xs:complexType name='base'><xs:sequence><xs:element name='a'
type='xs:int'/></xs:sequence></xs:complexType>" +
+ " <xs:complexType name='ext'><xs:complexContent><xs:extension
base='t:base'>" +
+ " <xs:sequence><xs:element name='b'
type='xs:int'/></xs:sequence></xs:extension></xs:complexContent></xs:complexType>"
+
+ " <xs:complexType name='res'><xs:complexContent><xs:restriction
base='t:base'>" +
+ " <xs:sequence><xs:element name='a'
type='xs:byte'/></xs:sequence></xs:restriction></xs:complexContent></xs:complexType>"
+
+ " <xs:complexType name='bbase'
block='extension'><xs:sequence><xs:element name='a'
type='xs:int'/></xs:sequence></xs:complexType>" +
+ " <xs:complexType name='bext'><xs:complexContent><xs:extension
base='t:bbase'>" +
+ " <xs:sequence><xs:element name='b'
type='xs:int'/></xs:sequence></xs:extension></xs:complexContent></xs:complexType>"
+
+ " <xs:element name='hE' type='t:base' block='extension'/>" +
+ " <xs:element name='mExt' type='t:ext' substitutionGroup='t:hE'/>" +
+ " <xs:element name='mRes' type='t:res' substitutionGroup='t:hE'/>" +
+ " <xs:element name='hR' type='t:base' block='restriction'/>" +
+ " <xs:element name='mR' type='t:res' substitutionGroup='t:hR'/>" +
+ " <xs:element name='hT' type='t:bbase'/>" +
+ " <xs:element name='mT' type='t:bext' substitutionGroup='t:hT'/>" +
+ " <xs:element name='root'><xs:complexType><xs:choice>" +
+ " <xs:element ref='t:hE'/><xs:element ref='t:hR'/><xs:element
ref='t:hT'/>" +
+ " </xs:choice></xs:complexType></xs:element>" +
+ "</xs:schema>";
+
+ private static SchemaTypeLoader loader;
+
+ @BeforeAll
+ static void compile() throws Exception {
+ loader = XmlBeans.loadXsd(new
XmlObject[]{SchemaDocument.Factory.parse(XSD)});
+ }
+
+ private static List<XmlError> validate(String content) throws Exception {
+ XmlObject doc = loader.parse("<root xmlns='urn:t'>" + content +
"</root>", null, null);
+ List<XmlError> errors = new ArrayList<>();
+ boolean valid = doc.validate(new
XmlOptions().setErrorListener(errors));
+ assertEquals(errors.isEmpty(), valid, errors.toString());
+ return errors;
+ }
+
+ private static void assertBlocked(String content) throws Exception {
+ List<XmlError> errors = validate(content);
+ assertEquals(1, errors.size(), errors.toString());
+ assertEquals("cvc-particle.2.3.3b", errors.get(0).getErrorCode());
+ }
+
+ @Test
+ void extensionMemberBlockedByHead() throws Exception {
+ assertBlocked("<mExt><a>1</a><b>2</b></mExt>");
+ }
+
+ @Test
+ void restrictionMemberBlockedByHead() throws Exception {
+ assertBlocked("<mR><a>1</a></mR>");
+ }
+
+ @Test
+ void extensionMemberBlockedByHeadType() throws Exception {
+ assertBlocked("<mT><a>1</a><b>2</b></mT>");
+ }
+
+ @Test
+ void unblockedMemberStillValid() throws Exception {
+ // block='extension' does not exclude a member derived by restriction
+ List<XmlError> errors = validate("<mRes><a>1</a></mRes>");
+ assertTrue(errors.isEmpty(), errors.toString());
+ assertFalse(validate("<mRes><a>1000</a></mRes>").isEmpty());
+ }
+}
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]