Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 32 additions & 4 deletions src/main/java/org/apache/xmlbeans/impl/validator/Validator.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
93 changes: 93 additions & 0 deletions src/test/java/misc/checkin/FixedQNameValidateTest.java
Original file line number Diff line number Diff line change
@@ -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 =
"<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema' xmlns:t='urn:t' xmlns:p='urn:p' " +
"targetNamespace='urn:t' elementFormDefault='qualified'>" +
" <xs:simpleType name='qname'><xs:restriction base='xs:QName'/></xs:simpleType>" +
" <xs:element name='qn' type='xs:QName' fixed='p:foo'/>" +
" <xs:element name='withAttr'>" +
" <xs:complexType><xs:attribute name='a' type='xs:QName' fixed='p:foo'/></xs:complexType>" +
" </xs:element>" +
"</xs:schema>";

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("<t:qn xmlns:t='urn:t' xmlns:p='urn:p'>p:foo</t:qn>"));
}

@Test
void fixedQNameAttributeValidates() throws Exception {
assertTrue(validates("<t:withAttr xmlns:t='urn:t' xmlns:p='urn:p' a='p:foo'/>"));
}

@Test
void mismatchedFixedQNameIsReported() throws Exception {
// a wrong value is reported as invalid, not raised as an exception
assertFalse(validates("<t:qn xmlns:t='urn:t' xmlns:p='urn:p'>p:bar</t:qn>"));
assertFalse(validates("<t:withAttr xmlns:t='urn:t' xmlns:p='urn:p' a='p:bar'/>"));
}

@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("<t:qn xmlns:t='urn:t' xmlns:z='urn:p'>z:foo</t:qn>"));
assertTrue(validates("<t:withAttr xmlns:t='urn:t' xmlns:z='urn:p' a='z:foo'/>"));
// rebinding the prefix to another namespace does not match the fixed value
assertFalse(validates("<t:qn xmlns:t='urn:t' xmlns:p='urn:other'>p:foo</t:qn>"));
}

@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(
"<t:qn xmlns:t='urn:t' " + XSI + " xmlns:z='urn:p' xsi:type='t:qname'>z:foo</t:qn>"));
assertFalse(validates(
"<t:qn xmlns:t='urn:t' " + XSI + " xmlns:z='urn:p' xsi:type='t:qname'>z:bar</t:qn>"));
}
}
Loading