This is an automated email from the ASF dual-hosted git repository.
coheigea pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/ws-xmlschema.git
The following commit(s) were added to refs/heads/master by this push:
new 711a7765 Explicitly catch NumberFormatException in
XmlSchemaElementValidator (#123)
711a7765 is described below
commit 711a7765333f4070ddb93d2e90f53eb1a03b3ec8
Author: Colm O hEigeartaigh <[email protected]>
AuthorDate: Fri Aug 21 09:52:46 2026 +0100
Explicitly catch NumberFormatException in XmlSchemaElementValidator (#123)
---
.../schema/docpath/XmlSchemaElementValidator.java | 37 ++++++--
.../docpath/TestXmlSchemaElementValidator.java | 98 ++++++++++++++++++++++
2 files changed, 129 insertions(+), 6 deletions(-)
diff --git
a/xmlschema-walker/src/main/java/org/apache/ws/commons/schema/docpath/XmlSchemaElementValidator.java
b/xmlschema-walker/src/main/java/org/apache/ws/commons/schema/docpath/XmlSchemaElementValidator.java
index 972aef58..11e2c836 100644
---
a/xmlschema-walker/src/main/java/org/apache/ws/commons/schema/docpath/XmlSchemaElementValidator.java
+++
b/xmlschema-walker/src/main/java/org/apache/ws/commons/schema/docpath/XmlSchemaElementValidator.java
@@ -522,7 +522,7 @@ final class XmlSchemaElementValidator {
if ((rangeFacets != null) && !rangeFacets.isEmpty()) {
for (XmlSchemaRestriction rangeFacet : rangeFacets) {
- compareTo = getBigDecimalOf(rangeFacet.getValue());
+ compareTo = getBigDecimalOf(name, rangeType,
rangeFacet.getValue());
final int comparison = value.compareTo(compareTo);
switch (rangeType) {
@@ -554,7 +554,8 @@ final class XmlSchemaElementValidator {
}
}
- private static BigDecimal getBigDecimalOf(Object numericValue) {
+ private static BigDecimal getBigDecimalOf(String name,
XmlSchemaRestriction.Type rangeType,
+ Object numericValue) throws
ValidationException {
BigDecimal newValue = null;
if (numericValue instanceof BigDecimal) {
@@ -573,7 +574,13 @@ final class XmlSchemaElementValidator {
newValue = new BigDecimal(((Number)numericValue).longValue());
} else if (numericValue instanceof String) {
- newValue = new BigDecimal(numericValue.toString());
+ try {
+ newValue = new BigDecimal(numericValue.toString());
+ } catch (NumberFormatException e) {
+ throw new ValidationException(name + " has a " + rangeType
+ + " restriction with a malformed
numeric value \""
+ + numericValue + "\".", e);
+ }
} else {
throw new
IllegalArgumentException(numericValue.getClass().getName()
@@ -608,7 +615,13 @@ final class XmlSchemaElementValidator {
if (lengthFacets != null) {
for (XmlSchemaRestriction lengthFacet : lengthFacets) {
- lengthRestriction =
Integer.parseInt(lengthFacet.getValue().toString());
+ try {
+ lengthRestriction =
Integer.parseInt(lengthFacet.getValue().toString());
+ } catch (NumberFormatException e) {
+ throw new ValidationException(name + " has a " + facetType
+ + " restriction with a
malformed numeric value \""
+ + lengthFacet.getValue() +
"\".", e);
+ }
switch (facetType) {
case LENGTH:
@@ -660,7 +673,13 @@ final class XmlSchemaElementValidator {
if (lengthFacets != null) {
for (XmlSchemaRestriction lengthFacet : lengthFacets) {
- lengthRestriction =
Integer.parseInt(lengthFacet.getValue().toString());
+ try {
+ lengthRestriction =
Integer.parseInt(lengthFacet.getValue().toString());
+ } catch (NumberFormatException e) {
+ throw new ValidationException(name + " has a " + facetType
+ + " restriction with a
malformed numeric value \""
+ + lengthFacet.getValue() +
"\".", e);
+ }
switch (facetType) {
case LENGTH:
@@ -712,7 +731,13 @@ final class XmlSchemaElementValidator {
if (digitsFacets != null) {
for (XmlSchemaRestriction digitsFacet : digitsFacets) {
- numDigits =
Integer.parseInt(digitsFacet.getValue().toString());
+ try {
+ numDigits =
Integer.parseInt(digitsFacet.getValue().toString());
+ } catch (NumberFormatException e) {
+ throw new ValidationException(name + " has a " + facetType
+ + " restriction with a
malformed numeric value \""
+ + digitsFacet.getValue() +
"\".", e);
+ }
switch (facetType) {
case DIGITS_FRACTION:
satisfied = (value.scale() <= numDigits);
diff --git
a/xmlschema-walker/src/test/java/org/apache/ws/commons/schema/docpath/TestXmlSchemaElementValidator.java
b/xmlschema-walker/src/test/java/org/apache/ws/commons/schema/docpath/TestXmlSchemaElementValidator.java
index 2fb899ae..0bd21021 100644
---
a/xmlschema-walker/src/test/java/org/apache/ws/commons/schema/docpath/TestXmlSchemaElementValidator.java
+++
b/xmlschema-walker/src/test/java/org/apache/ws/commons/schema/docpath/TestXmlSchemaElementValidator.java
@@ -1346,6 +1346,59 @@ public class TestXmlSchemaElementValidator {
XmlSchemaElementValidator.validateContent(stateMachine, "128",
nsContext);
}
+ @Test(expected = ValidationException.class)
+ public void testInvalidStringLengthFacetValue() throws Exception {
+ HashMap<XmlSchemaRestriction.Type, List<XmlSchemaRestriction>> facets
= new HashMap<XmlSchemaRestriction.Type, List<XmlSchemaRestriction>>();
+
+ facets.put(XmlSchemaRestriction.Type.LENGTH,
+ Collections
+ .<XmlSchemaRestriction> singletonList(new
XmlSchemaRestriction(
+
XmlSchemaRestriction.Type.LENGTH,
+
"bogus", false)));
+
+ XmlSchemaTypeInfo typeInfo = new
XmlSchemaTypeInfo(XmlSchemaBaseSimpleType.STRING, facets);
+
+ XmlSchemaStateMachineNode stateMachine = new
XmlSchemaStateMachineNode(xmlElement, null, typeInfo);
+
+ XmlSchemaElementValidator.validateContent(stateMachine, "123",
nsContext);
+ }
+
+ @Test(expected = ValidationException.class)
+ public void testInvalidListLengthFacetValue() throws Exception {
+ XmlSchemaTypeInfo baseType = new
XmlSchemaTypeInfo(XmlSchemaBaseSimpleType.STRING);
+
+ HashMap<XmlSchemaRestriction.Type, List<XmlSchemaRestriction>>
listFacets = new HashMap<XmlSchemaRestriction.Type,
List<XmlSchemaRestriction>>();
+
+ listFacets.put(XmlSchemaRestriction.Type.LENGTH,
+ Collections
+ .<XmlSchemaRestriction> singletonList(new
XmlSchemaRestriction(
+
XmlSchemaRestriction.Type.LENGTH,
+
"bogus", false)));
+
+ XmlSchemaTypeInfo listType = new XmlSchemaTypeInfo(baseType,
listFacets);
+
+ XmlSchemaStateMachineNode stateMachine = new
XmlSchemaStateMachineNode(xmlElement, null, listType);
+
+ XmlSchemaElementValidator.validateContent(stateMachine, "1 2 3",
nsContext);
+ }
+
+ @Test(expected = ValidationException.class)
+ public void testInvalidDigitsFacetValue() throws Exception {
+ HashMap<XmlSchemaRestriction.Type, List<XmlSchemaRestriction>> facets
= new HashMap<XmlSchemaRestriction.Type, List<XmlSchemaRestriction>>();
+
+ facets.put(XmlSchemaRestriction.Type.DIGITS_TOTAL,
+ Collections
+ .<XmlSchemaRestriction> singletonList(new
XmlSchemaRestriction(
+
XmlSchemaRestriction.Type.DIGITS_TOTAL,
+
"bogus", false)));
+
+ XmlSchemaTypeInfo type = new
XmlSchemaTypeInfo(XmlSchemaBaseSimpleType.DECIMAL, facets);
+
+ XmlSchemaStateMachineNode stateMachine = new
XmlSchemaStateMachineNode(xmlElement, null, type);
+
+ XmlSchemaElementValidator.validateContent(stateMachine, "12345",
nsContext);
+ }
+
@Test
public void testValidStringNoFacets() throws Exception {
XmlSchemaTypeInfo typeInfo = new
XmlSchemaTypeInfo(XmlSchemaBaseSimpleType.STRING);
@@ -1754,6 +1807,51 @@ public class TestXmlSchemaElementValidator {
XmlSchemaElementValidator.validateContent(stateMachine, "fail!",
nsContext);
}
+ @Test
+ public void testUnionSkipsMemberWithMalformedFacet() throws Exception {
+ HashMap<XmlSchemaRestriction.Type, List<XmlSchemaRestriction>>
malformedFacets = new HashMap<XmlSchemaRestriction.Type,
List<XmlSchemaRestriction>>();
+
+ malformedFacets.put(XmlSchemaRestriction.Type.INCLUSIVE_MAX,
+ Collections
+ .<XmlSchemaRestriction> singletonList(new
XmlSchemaRestriction(
+
XmlSchemaRestriction.Type.INCLUSIVE_MAX,
+
"bogus", false)));
+
+ ArrayList<XmlSchemaTypeInfo> unionTypes = new
ArrayList<XmlSchemaTypeInfo>(2);
+
+ unionTypes.add(new XmlSchemaTypeInfo(XmlSchemaBaseSimpleType.DECIMAL,
malformedFacets));
+ unionTypes.add(new XmlSchemaTypeInfo(XmlSchemaBaseSimpleType.STRING));
+
+ XmlSchemaTypeInfo unionType = new XmlSchemaTypeInfo(unionTypes);
+
+ XmlSchemaStateMachineNode stateMachine = new
XmlSchemaStateMachineNode(xmlElement, null, unionType);
+
+ // The DECIMAL member's malformed facet must not abort the whole union
check.
+ XmlSchemaElementValidator.validateContent(stateMachine,
"not-a-number", nsContext);
+ }
+
+ @Test(expected = ValidationException.class)
+ public void testUnionAllMembersFailIncludingMalformedFacet() throws
Exception {
+ HashMap<XmlSchemaRestriction.Type, List<XmlSchemaRestriction>>
malformedFacets = new HashMap<XmlSchemaRestriction.Type,
List<XmlSchemaRestriction>>();
+
+ malformedFacets.put(XmlSchemaRestriction.Type.INCLUSIVE_MAX,
+ Collections
+ .<XmlSchemaRestriction> singletonList(new
XmlSchemaRestriction(
+
XmlSchemaRestriction.Type.INCLUSIVE_MAX,
+
"bogus", false)));
+
+ ArrayList<XmlSchemaTypeInfo> unionTypes = new
ArrayList<XmlSchemaTypeInfo>(2);
+
+ unionTypes.add(new XmlSchemaTypeInfo(XmlSchemaBaseSimpleType.DECIMAL,
malformedFacets));
+ unionTypes.add(new XmlSchemaTypeInfo(XmlSchemaBaseSimpleType.BOOLEAN));
+
+ XmlSchemaTypeInfo unionType = new XmlSchemaTypeInfo(unionTypes);
+
+ XmlSchemaStateMachineNode stateMachine = new
XmlSchemaStateMachineNode(xmlElement, null, unionType);
+
+ XmlSchemaElementValidator.validateContent(stateMachine,
"not-a-number", nsContext);
+ }
+
@Test
public void testNillableElement() throws Exception {
XmlSchemaElement element = new XmlSchemaElement(xmlSchema, false);