This is an automated email from the ASF dual-hosted git repository. coheigea pushed a commit to branch coheigea/walker-noname in repository https://gitbox.apache.org/repos/asf/ws-xmlschema.git
commit 7b64be01e85b59c968a37ced44700295a50296a9 Author: Colm O hEigeartaigh <[email protected]> AuthorDate: Fri Sep 25 09:46:14 2026 +0100 Refuse an element declaration with no name and no ref in the walker --- .../ws/commons/schema/walker/XmlSchemaWalker.java | 10 +++ .../docpath/TestStateMachineOddDeclarations.java | 91 ++++++++++++++++++++++ 2 files changed, 101 insertions(+) diff --git a/xmlschema-walker/src/main/java/org/apache/ws/commons/schema/walker/XmlSchemaWalker.java b/xmlschema-walker/src/main/java/org/apache/ws/commons/schema/walker/XmlSchemaWalker.java index 59f575c1..671d095f 100644 --- a/xmlschema-walker/src/main/java/org/apache/ws/commons/schema/walker/XmlSchemaWalker.java +++ b/xmlschema-walker/src/main/java/org/apache/ws/commons/schema/walker/XmlSchemaWalker.java @@ -220,6 +220,16 @@ public final class XmlSchemaWalker { } private void walkElement(XmlSchemaElement element) { + if (getElementQName(element) == null) { + /* + * Neither a name nor a reference, which the schema reader accepts: it keeps only the + * type of a declaration carrying both ref and type, which XML Schema forbids. There + * is no element to walk, and a visitor keeping its books by QName cannot take one. + */ + throw new XmlSchemaException("An element declaration has neither a name nor a ref, so it" + + " cannot be walked. A declaration carrying both ref and" + + " type, which XML Schema forbids, is read as having neither."); + } element = getElement(element, false); final XmlSchemaElement substGroupElem = element; diff --git a/xmlschema-walker/src/test/java/org/apache/ws/commons/schema/docpath/TestStateMachineOddDeclarations.java b/xmlschema-walker/src/test/java/org/apache/ws/commons/schema/docpath/TestStateMachineOddDeclarations.java new file mode 100644 index 00000000..9617e220 --- /dev/null +++ b/xmlschema-walker/src/test/java/org/apache/ws/commons/schema/docpath/TestStateMachineOddDeclarations.java @@ -0,0 +1,91 @@ +/** + * 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 org.apache.ws.commons.schema.docpath; + +import java.io.StringReader; + +import javax.xml.namespace.QName; + +import org.apache.ws.commons.schema.XmlSchemaCollection; +import org.apache.ws.commons.schema.XmlSchemaElement; +import org.apache.ws.commons.schema.XmlSchemaException; +import org.apache.ws.commons.schema.walker.XmlSchemaWalker; + +import org.junit.Assert; +import org.junit.Test; + +/** + * Element declarations the schema reader accepts although XML Schema forbids them are refused by + * the walker with its documented exception, not a NullPointerException from the state machine + * generator. + */ +public class TestStateMachineOddDeclarations extends Assert { + + private static final String PREFIX = + "<xs:schema xmlns:xs=\"http://www.w3.org/2001/XMLSchema\"" + + " xmlns:tns=\"urn:odd\" targetNamespace=\"urn:odd\">" + + "<xs:element name=\"target\" type=\"xs:string\"/>" + + "<xs:element name=\"root\"><xs:complexType><xs:sequence>"; + + private static final String SUFFIX = "</xs:sequence></xs:complexType></xs:element></xs:schema>"; + + /* + * ref and type together, as a W3C test schema has it. The reader keeps only the type. + */ + @Test + public void testElementWithRefAndTypeIsRefused() { + assertRefused("<xs:element ref=\"tns:target\" type=\"xs:int\"/>"); + } + + @Test + public void testElementWithNeitherNameNorRefIsRefused() { + assertRefused("<xs:element type=\"xs:string\"/>"); + } + + @Test + public void testElementWithRefStillWalks() { + final XmlSchemaCollection collection = read("<xs:element ref=\"tns:target\"/>"); + final XmlSchemaStateMachineGenerator generator = new XmlSchemaStateMachineGenerator(); + new XmlSchemaWalker(collection, generator).walk(root(collection)); + assertNotNull(generator.getStateMachineNodesByQName().get(new QName("urn:odd", "target"))); + } + + private static void assertRefused(String particles) { + final XmlSchemaCollection collection = read(particles); + try { + new XmlSchemaWalker(collection, new XmlSchemaStateMachineGenerator()).walk(root(collection)); + fail("The declaration names no element and should be refused."); + } catch (XmlSchemaException expected) { + assertTrue(expected.getMessage(), expected.getMessage().contains("neither a name nor a ref")); + } + } + + private static XmlSchemaCollection read(String particles) { + final XmlSchemaCollection collection = new XmlSchemaCollection(); + collection.read(new StringReader(PREFIX + particles + SUFFIX)); + return collection; + } + + private static XmlSchemaElement root(XmlSchemaCollection collection) { + final XmlSchemaElement root = collection.getElementByQName(new QName("urn:odd", "root")); + assertNotNull(root); + return root; + } +}
