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 80df884e validate non-special xsi attributes in store-side
Validate.doAttrs (#146)
80df884e is described below
commit 80df884eb3c2f853ec06091e10166b5d8b371025
Author: aizu-m <[email protected]>
AuthorDate: Tue Nov 3 15:19:01 2026 +0530
validate non-special xsi attributes in store-side Validate.doAttrs (#146)
---
.../org/apache/xmlbeans/impl/store/Validate.java | 11 ++-
.../misc/checkin/XsiAttributeValidateTest.java | 78 ++++++++++++++++++++++
2 files changed, 88 insertions(+), 1 deletion(-)
diff --git a/src/main/java/org/apache/xmlbeans/impl/store/Validate.java
b/src/main/java/org/apache/xmlbeans/impl/store/Validate.java
index e8664486..6d157b37 100644
--- a/src/main/java/org/apache/xmlbeans/impl/store/Validate.java
+++ b/src/main/java/org/apache/xmlbeans/impl/store/Validate.java
@@ -113,7 +113,7 @@ final class Validate implements ValidatorListener.Event
{
do
{
- if (_cur.isNormalAttr() && !_cur.getUri().equals( Locale._xsi
))
+ if (_cur.isNormalAttr() && !isSpecialXsiAttr( _cur.getName() ))
_sink.nextEvent( ValidatorListener.ATTR, this );
}
while ( _cur.toNextAttr() );
@@ -124,6 +124,15 @@ final class Validate implements ValidatorListener.Event
_sink.nextEvent( ValidatorListener.ENDATTRS, this );
}
+ // Only xsi:type, xsi:nil, xsi:schemaLocation and
xsi:noNamespaceSchemaLocation are exempt
+ // from attribute validation (cvc-complex-type.3.2); any other xsi
attribute must be declared
+ private static boolean isSpecialXsiAttr ( QName name )
+ {
+ return
+ name.equals( Locale._xsiType ) || name.equals( Locale._xsiNil ) ||
+ name.equals( Locale._xsiLoc ) || name.equals( Locale._xsiNoLoc );
+ }
+
private void emitText ( )
{
assert _cur.isText();
diff --git a/src/test/java/misc/checkin/XsiAttributeValidateTest.java
b/src/test/java/misc/checkin/XsiAttributeValidateTest.java
new file mode 100644
index 00000000..05690d3c
--- /dev/null
+++ b/src/test/java/misc/checkin/XsiAttributeValidateTest.java
@@ -0,0 +1,78 @@
+/* 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.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 XsiAttributeValidateTest {
+
+ private static final 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:attribute name='a'
type='xs:int'/></xs:complexType>" +
+ " </xs:element>" +
+ " <xs:element name='open'>" +
+ " <xs:complexType><xs:anyAttribute
processContents='skip'/></xs:complexType>" +
+ " </xs:element>" +
+ "</xs:schema>";
+
+ private static boolean validate(String instance, List<XmlError> errors)
throws Exception {
+ SchemaTypeLoader loader = XmlBeans.loadXsd(new
XmlObject[]{SchemaDocument.Factory.parse(XSD)});
+ XmlObject doc = loader.parse(instance, null, null);
+ return doc.validate(new XmlOptions().setErrorListener(errors));
+ }
+
+ @Test
+ void undeclaredXsiAttributeIsReported() throws Exception {
+ List<XmlError> errors = new ArrayList<>();
+ assertFalse(validate(
+ "<t:root xmlns:t='urn:t'
xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' " +
+ "a='1' xsi:foo='x'/>", errors));
+ assertEquals(1, errors.size(), errors.toString());
+ assertEquals("cvc-complex-type.3.2.1", errors.get(0).getErrorCode());
+ }
+
+ @Test
+ void specialXsiAttributesAreStillAllowed() throws Exception {
+ List<XmlError> errors = new ArrayList<>();
+ assertTrue(validate(
+ "<t:root xmlns:t='urn:t'
xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' " +
+ "xsi:schemaLocation='urn:t t.xsd'
xsi:noNamespaceSchemaLocation='n.xsd' a='1'/>",
+ errors), errors.toString());
+ }
+
+ @Test
+ void xsiAttributeMatchesWildcard() throws Exception {
+ List<XmlError> errors = new ArrayList<>();
+ assertTrue(validate(
+ "<t:open xmlns:t='urn:t'
xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:foo='x'/>",
+ errors), errors.toString());
+ }
+}
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]