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 502a251a5..e9c37f448 100644 --- a/src/main/java/org/apache/xmlbeans/impl/validator/Validator.java +++ b/src/main/java/org/apache/xmlbeans/impl/validator/Validator.java @@ -1051,14 +1051,42 @@ private String validateSimpleType( XmlWhitespace.collapse( field.getDefaultText(), type.getWhiteSpaceRule()); - if (!validateSimpleType(type, fixedValue, event)) { + // The fixed value was already resolved against the schema at compile + // time, so compare against that cooked value. Reparsing its lexical + // form here would resolve the schema's prefixes against the instance, + // which is also why only the fallback that has to reparse it needs the + // lexical check. + XmlObject def; + + try { + def = field.getDefaultValue(); + } catch (XmlValueOutOfRangeException e) { + def = null; + } + + if (def == null && !validateSimpleType(type, fixedValue, event)) { return null; } - XmlObject val = type.newValue(value); - XmlObject def = type.newValue(fixedValue); + // Comparing the instance value against the fixed value may have to + // resolve QName or NOTATION prefixes in the instance value, so push + // the namespace context the same way the enumeration comparisons do. + boolean valueEquals; + + NamespaceContext.push(new NamespaceContext(event)); + + try { + XmlObject val = type.newValue(value); + + valueEquals = + val.valueEquals(def == null ? type.newValue(fixedValue) : def); + } catch (XmlValueOutOfRangeException e) { + valueEquals = false; + } finally { + NamespaceContext.pop(); + } - if (!val.valueEquals(def)) { + if (!valueEquals) { // TODO (dutta) - make this more verbose if (field.isAttribute()) { // KHK: check for is cvc-complex-type.3.1 or cvc-au diff --git a/src/test/java/misc/checkin/FixedQNameValidateTest.java b/src/test/java/misc/checkin/FixedQNameValidateTest.java new file mode 100644 index 000000000..bcf76ec6c --- /dev/null +++ b/src/test/java/misc/checkin/FixedQNameValidateTest.java @@ -0,0 +1,93 @@ +/* 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 misc.checkin; + +import org.apache.xmlbeans.SchemaTypeLoader; +import org.apache.xmlbeans.XmlBeans; +import org.apache.xmlbeans.impl.xb.xsdschema.SchemaDocument; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + +public class FixedQNameValidateTest { + + private static final String XSI = "xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'"; + + private static final String XSD = + "" + + " " + + " " + + " " + + " " + + " " + + ""; + + private static SchemaTypeLoader loader; + + @BeforeAll + static void compileSchema() throws Exception { + loader = XmlBeans.loadXsd(SchemaDocument.Factory.parse(XSD)); + } + + private static boolean validates(String instance) throws Exception { + return loader.parse(instance, null, null).validate(); + } + + @Test + void fixedQNameElementValidates() throws Exception { + // used to throw XmlValueOutOfRangeException out of validate(): the fixed + // value comparison called newValue() with no namespace context pushed + assertTrue(validates("p:foo")); + } + + @Test + void fixedQNameAttributeValidates() throws Exception { + assertTrue(validates("")); + } + + @Test + void mismatchedFixedQNameIsReported() throws Exception { + // a wrong value is reported as invalid, not raised as an exception + assertFalse(validates("p:bar")); + assertFalse(validates("")); + } + + @Test + void fixedQNameComparedByValueNotPrefix() throws Exception { + // the schema's own prefix is never looked up in the instance: the fixed + // namespace reached through a different instance prefix still matches, + // with 'p' undeclared on the instance + assertTrue(validates("z:foo")); + assertTrue(validates("")); + // rebinding the prefix to another namespace does not match the fixed value + assertFalse(validates("p:foo")); + } + + @Test + void fixedQNameComparedAcrossXsiType() throws Exception { + // xsi:type narrows the instance type, so the two sides of the comparison + // come from different types: the compiled fixed value keeps the field's + // declared xs:QName while the instance value is built from t:qname + assertTrue(validates( + "z:foo")); + assertFalse(validates( + "z:bar")); + } +}