From 9a54f5a57e7eb3e40a65272e8a27e32c0c2c536c Mon Sep 17 00:00:00 2001 From: Aizal Khan Date: Fri, 26 Jun 2026 01:27:23 +0530 Subject: [PATCH] report length facets in JavaNotationHolderEx.validateLexical --- .../impl/values/JavaNotationHolderEx.java | 30 +++++++- .../NotationLengthFacetValidateTest.java | 77 +++++++++++++++++++ 2 files changed, 106 insertions(+), 1 deletion(-) create mode 100644 src/test/java/misc/checkin/NotationLengthFacetValidateTest.java diff --git a/src/main/java/org/apache/xmlbeans/impl/values/JavaNotationHolderEx.java b/src/main/java/org/apache/xmlbeans/impl/values/JavaNotationHolderEx.java index 6dbbd4871..95decc5ab 100644 --- a/src/main/java/org/apache/xmlbeans/impl/values/JavaNotationHolderEx.java +++ b/src/main/java/org/apache/xmlbeans/impl/values/JavaNotationHolderEx.java @@ -89,7 +89,35 @@ public static QName validateLexical(String v, SchemaType sType, ValidationContex } } - check(v, sType); + // check against length + XmlObject len = sType.getFacet(SchemaType.FACET_LENGTH); + if (len != null) + { + int m = ((XmlObjectBase)len).getBigIntegerValue().intValue(); + if (v.length() != m) + context.invalid(XmlErrorCodes.DATATYPE_LENGTH_VALID$STRING, + new Object[] { "NOTATION", v.length(), m, QNameHelper.readable(sType) }); + } + + // check against min length + XmlObject min = sType.getFacet(SchemaType.FACET_MIN_LENGTH); + if (min != null) + { + int m = ((XmlObjectBase)min).getBigIntegerValue().intValue(); + if (v.length() < m) + context.invalid(XmlErrorCodes.DATATYPE_MIN_LENGTH_VALID$STRING, + new Object[] { "NOTATION", v.length(), m, QNameHelper.readable(sType) }); + } + + // check against max length + XmlObject max = sType.getFacet(SchemaType.FACET_MAX_LENGTH); + if (max != null) + { + int m = ((XmlObjectBase)max).getBigIntegerValue().intValue(); + if (v.length() > m) + context.invalid(XmlErrorCodes.DATATYPE_MAX_LENGTH_VALID$STRING, + new Object[] { "NOTATION", v.length(), m, QNameHelper.readable(sType) }); + } return name; } diff --git a/src/test/java/misc/checkin/NotationLengthFacetValidateTest.java b/src/test/java/misc/checkin/NotationLengthFacetValidateTest.java new file mode 100644 index 000000000..c91157770 --- /dev/null +++ b/src/test/java/misc/checkin/NotationLengthFacetValidateTest.java @@ -0,0 +1,77 @@ +/* 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.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + +public class NotationLengthFacetValidateTest { + + // A maxLength facet is applied to a NOTATION type whose inherited enumeration + // still admits a longer member, so the enumeration check alone does not catch + // the length violation. + private static final String XSD = + "" + + " " + + " " + + " " + + " " + + " " + + " " + + " " + + " " + + " " + + " " + + " " + + " " + + " " + + " " + + " " + + ""; + + private static boolean validate(String notationValue) throws Exception { + SchemaTypeLoader loader = XmlBeans.loadXsd(new XmlObject[]{SchemaDocument.Factory.parse(XSD)}); + XmlObject doc = loader.parse( + "" + notationValue + "", null, null); + List errors = new ArrayList<>(); + return doc.validate(new XmlOptions().setErrorListener(errors)); + } + + @Test + void notationWithinMaxLengthValidates() throws Exception { + // 't:a' is in the enumeration and its lexical length (3) matches maxLength + assertTrue(validate("t:a")); + } + + @Test + void notationViolatingMaxLengthIsInvalid() throws Exception { + // 't:bcdefgh' is in the inherited enumeration but its lexical length (9) + // exceeds maxLength=3, so document validation must reject it + assertFalse(validate("t:bcdefgh")); + } +}