Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,12 @@ private 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);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -584,7 +584,7 @@ public void validate(XmlSchema pSchema) {
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 "
Expand Down Expand Up @@ -931,7 +931,7 @@ private QName getRefQName(String pName, NamespaceContext pContext) {
}

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);
}
Expand Down Expand Up @@ -1355,6 +1355,10 @@ private XmlSchemaComplexContentRestriction handleComplexContentRestriction(XmlSc
}

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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -714,10 +714,10 @@ public String toString() {
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);
}
Expand Down
82 changes: 82 additions & 0 deletions xmlschema-core/src/test/java/tests/ExceptionContractTest.java
Original file line number Diff line number Diff line change
@@ -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>");
}
}
Loading