This is an automated email from the ASF dual-hosted git repository. coheigea pushed a commit to branch coheigea/IllegalArgumentException in repository https://gitbox.apache.org/repos/asf/ws-xmlschema.git
commit 96fac232405ff12d555f649efb0fe33cf7985fc1 Author: Colm O hEigeartaigh <[email protected]> AuthorDate: Wed Aug 26 11:30:39 2026 +0100 Properly throw XMLSchemaException in some places --- .../org/apache/ws/commons/schema/EnumUtil.java | 7 +- .../apache/ws/commons/schema/SchemaBuilder.java | 8 ++- .../ws/commons/schema/XmlSchemaCollection.java | 8 +-- .../src/test/java/tests/ExceptionContractTest.java | 82 ++++++++++++++++++++++ 4 files changed, 98 insertions(+), 7 deletions(-) diff --git a/xmlschema-core/src/main/java/org/apache/ws/commons/schema/EnumUtil.java b/xmlschema-core/src/main/java/org/apache/ws/commons/schema/EnumUtil.java index aa79127a..e5a3a3ff 100644 --- a/xmlschema-core/src/main/java/org/apache/ws/commons/schema/EnumUtil.java +++ b/xmlschema-core/src/main/java/org/apache/ws/commons/schema/EnumUtil.java @@ -30,7 +30,12 @@ final class EnumUtil { } static <T extends Enum<T>> T valueOf(Class<T> enumClass, String name) { - return Enum.valueOf(enumClass, name.toUpperCase(Locale.ENGLISH)); + try { + return Enum.valueOf(enumClass, name.toUpperCase(Locale.ENGLISH)); + } catch (IllegalArgumentException e) { + throw new XmlSchemaException("Invalid value \"" + name + "\" for " + + enumClass.getSimpleName() + ".", e); + } } } diff --git a/xmlschema-core/src/main/java/org/apache/ws/commons/schema/SchemaBuilder.java b/xmlschema-core/src/main/java/org/apache/ws/commons/schema/SchemaBuilder.java index 66418f23..bbd215fa 100644 --- a/xmlschema-core/src/main/java/org/apache/ws/commons/schema/SchemaBuilder.java +++ b/xmlschema-core/src/main/java/org/apache/ws/commons/schema/SchemaBuilder.java @@ -584,7 +584,7 @@ public class SchemaBuilder { if (isEmpty(uri)) { valid = isEmpty(pSchema.getSyntacticalTargetNamespace()); } else { - valid = pSchema.getSyntacticalTargetNamespace().equals(uri); + valid = uri.equals(pSchema.getSyntacticalTargetNamespace()); } if (!valid) { throw new XmlSchemaException("An imported schema was announced to have the namespace " @@ -931,7 +931,7 @@ public class SchemaBuilder { } if (uri == null || Constants.NULL_NS_URI.equals(uri)) { - throw new IllegalStateException("The prefix " + prefix + " is not bound."); + throw new XmlSchemaException("The prefix " + prefix + " is not bound."); } localName = pName.substring(offset + 1); } @@ -1355,6 +1355,10 @@ public class SchemaBuilder { } if (constraintEl.hasAttribute("refer")) { + if (!(constraint instanceof XmlSchemaKeyref)) { + throw new XmlSchemaException("A \"refer\" attribute is only permitted on xs:keyref," + + " not on xs:" + constraintEl.getLocalName() + "."); + } String name = constraintEl.getAttribute("refer"); ((XmlSchemaKeyref)constraint).refer = getRefQName(name, constraintEl); } diff --git a/xmlschema-core/src/main/java/org/apache/ws/commons/schema/XmlSchemaCollection.java b/xmlschema-core/src/main/java/org/apache/ws/commons/schema/XmlSchemaCollection.java index dfe59fb3..1383ee30 100644 --- a/xmlschema-core/src/main/java/org/apache/ws/commons/schema/XmlSchemaCollection.java +++ b/xmlschema-core/src/main/java/org/apache/ws/commons/schema/XmlSchemaCollection.java @@ -714,10 +714,10 @@ public final class XmlSchemaCollection { void addSchema(SchemaKey pKey, XmlSchema pSchema) { if (schemas.containsKey(pKey)) { throw - new IllegalStateException("A schema with target namespace " - + pKey.getNamespace() - + " and system ID " - + pKey.getSystemId() + " is already present."); + new XmlSchemaException("A schema with target namespace " + + pKey.getNamespace() + + " and system ID " + + pKey.getSystemId() + " is already present."); } schemas.put(pKey, pSchema); } diff --git a/xmlschema-core/src/test/java/tests/ExceptionContractTest.java b/xmlschema-core/src/test/java/tests/ExceptionContractTest.java new file mode 100644 index 00000000..5e029f05 --- /dev/null +++ b/xmlschema-core/src/test/java/tests/ExceptionContractTest.java @@ -0,0 +1,82 @@ +/** + * 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 tests; + +import java.io.StringReader; + +import org.apache.ws.commons.schema.XmlSchemaCollection; +import org.apache.ws.commons.schema.XmlSchemaException; + +import org.junit.Assert; +import org.junit.Test; + +public class ExceptionContractTest extends Assert { + + private void assertRejectedWithXmlSchemaException(String schemaBody) { + String schema = + "<xs:schema xmlns:xs=\"http://www.w3.org/2001/XMLSchema\"" + + " targetNamespace=\"urn:contract\">" + schemaBody + "</xs:schema>"; + XmlSchemaCollection collection = new XmlSchemaCollection(); + try { + collection.read(new StringReader(schema)); + fail("The crafted schema should have been rejected: " + schemaBody); + } catch (XmlSchemaException expected) { + // Expected documented failure surface. + } + } + + @Test + public void testUndeclaredPrefixInTypeReference() { + assertRejectedWithXmlSchemaException("<xs:element name=\"a\" type=\"nope:T\"/>"); + } + + @Test + public void testReferOnKeyConstraint() { + assertRejectedWithXmlSchemaException( + "<xs:element name=\"a\" type=\"xs:string\">" + + "<xs:key name=\"k\" refer=\"xs:b\">" + + "<xs:selector xpath=\".\"/><xs:field xpath=\"@id\"/>" + + "</xs:key></xs:element>"); + } + + @Test + public void testInvalidFormAttributeValue() { + assertRejectedWithXmlSchemaException( + "<xs:element name=\"a\"><xs:complexType>" + + "<xs:attribute name=\"x\" form=\"bogus\"/>" + + "</xs:complexType></xs:element>"); + } + + @Test + public void testInvalidUseAttributeValue() { + assertRejectedWithXmlSchemaException( + "<xs:element name=\"a\"><xs:complexType>" + + "<xs:attribute name=\"x\" use=\"bogus\"/>" + + "</xs:complexType></xs:element>"); + } + + @Test + public void testInvalidProcessContentsValue() { + assertRejectedWithXmlSchemaException( + "<xs:element name=\"a\"><xs:complexType><xs:sequence>" + + "<xs:any processContents=\"bogus\"/>" + + "</xs:sequence></xs:complexType></xs:element>"); + } +} \ No newline at end of file
