Skip to content

Commit

Permalink
Provided first xsd:appinfo for LengthType with validator. (Issue #40)
Browse files Browse the repository at this point in the history
  • Loading branch information
wjschakel authored and wjschakel committed May 18, 2023
1 parent 4aa9aa2 commit 2975af7
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ static String reportInvalidValue(final Node xsdNode, final String value, final S
{
return "Value is empty.";
}
return reportTypeNonCompliance(xsdNode, "type", value, schema, null, null);
return reportTypeNonCompliance(xsdNode, xsdNode, "type", value, schema, null, null);
}

/**
Expand Down Expand Up @@ -109,7 +109,7 @@ static String reportInvalidAttributeValue(final Node xsdNode, final String value
{
return null;
}
return reportTypeNonCompliance(xsdNode, "type", value, schema, null, null);
return reportTypeNonCompliance(xsdNode, xsdNode, "type", value, schema, null, null);
}

/**
Expand All @@ -121,7 +121,7 @@ static String reportInvalidAttributeValue(final Node xsdNode, final String value
static List<Node> getRestrictions(final Node xsdNode, final Schema schema)
{
List<Node> restrictions = new ArrayList<>();
reportTypeNonCompliance(xsdNode, "type", null, schema, restrictions, null);
reportTypeNonCompliance(xsdNode, xsdNode, "type", null, schema, restrictions, null);
return restrictions;
}

Expand All @@ -134,13 +134,14 @@ static List<Node> getRestrictions(final Node xsdNode, final Schema schema)
static String getBaseType(final Node xsdNode, final Schema schema)
{
List<String> baseType = new ArrayList<>();
reportTypeNonCompliance(xsdNode, "type", null, schema, null, baseType);
reportTypeNonCompliance(xsdNode, xsdNode, "type", null, schema, null, baseType);
return baseType.get(0);
}

/**
* Report first encountered problem in validating the value by a type, or when {@code value = null} scan all restrictions
* and place them in the input list, and/or find the base type and store it in the base type list.
* @param appInfoNode Node; node having possible xsd:appinfo for a message.
* @param node Node; type node.
* @param attribute String; "type" on normal calls, "base" on recursive calls.
* @param value String; value.
Expand All @@ -149,8 +150,8 @@ static String getBaseType(final Node xsdNode, final Schema schema)
* @param baseType List&lt;String&gt;; may be filled with 1 base type, e.g. xsd:double.
* @return String; first encountered problem in validating the value by a type, {@code null} if there is no problem.
*/
private static String reportTypeNonCompliance(final Node node, final String attribute, final String value,
final Schema schema, final List<Node> restrictions, final List<String> baseType)
private static String reportTypeNonCompliance(final Node appInfoNode, final Node node, final String attribute,
final String value, final Schema schema, final List<Node> restrictions, final List<String> baseType)
{
String type = DocumentReader.getAttribute(node, attribute); // can request "base" on recursion
boolean isNativeType = type != null && type.startsWith("xsd:");
Expand Down Expand Up @@ -179,7 +180,8 @@ private static String reportTypeNonCompliance(final Node node, final String attr
}
if (type != null && !isNativeType)
{
String report = reportTypeNonCompliance(schema.getType(type), "base", value, schema, restrictions, baseType);
Node typeNode = schema.getType(type);
String report = reportTypeNonCompliance(typeNode, typeNode, "base", value, schema, restrictions, baseType);
if (value != null && report != null)
{
return report;
Expand All @@ -197,13 +199,13 @@ private static String reportTypeNonCompliance(final Node node, final String attr
Node extension = DocumentReader.getChild(simpleContent, "xsd:extension");
if (extension != null)
{
return reportTypeNonCompliance(extension, "base", value, schema, restrictions, baseType);
return reportTypeNonCompliance(extension, extension, "base", value, schema, restrictions, baseType);
}
return reportTypeNonCompliance(DocumentReader.getChild(simpleContent, "xsd:restriction"), "base", value, schema,
restrictions, baseType);
return reportTypeNonCompliance(appInfoNode, DocumentReader.getChild(simpleContent, "xsd:restriction"), "base",
value, schema, restrictions, baseType);
case "xsd:simpleType":
return reportTypeNonCompliance(DocumentReader.getChild(node, "xsd:restriction"), "base", value, schema,
restrictions, baseType);
return reportTypeNonCompliance(appInfoNode, DocumentReader.getChild(node, "xsd:restriction"), "base", value,
schema, restrictions, baseType);
case "xsd:element":
if (node.getChildNodes().getLength() == 0)
{
Expand All @@ -212,20 +214,20 @@ private static String reportTypeNonCompliance(final Node node, final String attr
Node complexType = DocumentReader.getChild(node, "xsd:complexType");
if (complexType != null)
{
return reportTypeNonCompliance(complexType, "type", value, schema, restrictions, baseType);
return reportTypeNonCompliance(complexType, complexType, "type", value, schema, restrictions, baseType);
}
Node simpleType = DocumentReader.getChild(node, "xsd:simpleType");
return reportTypeNonCompliance(simpleType, "type", value, schema, restrictions, baseType);
return reportTypeNonCompliance(simpleType, simpleType, "type", value, schema, restrictions, baseType);
case "xsd:attribute":
return reportTypeNonCompliance(DocumentReader.getChild(node, "xsd:simpleType"), "type", value, schema,
restrictions, baseType);
Node simpleTypeAttr = DocumentReader.getChild(node, "xsd:simpleType");
return reportTypeNonCompliance(simpleTypeAttr, simpleTypeAttr, "type", value, schema, restrictions, baseType);
case "xsd:restriction":
if (value == null && restrictions != null)
{
restrictions.add(node);
return null;
}
return reportRestrictionNonCompliance(node, value);
return reportRestrictionNonCompliance(appInfoNode, node, value);
case "xi:include":
return null;
default:
Expand Down Expand Up @@ -330,11 +332,12 @@ private static String reportNativeTypeNonCompliance(final String type, final Str

/**
* Report first encountered problem in validating the value by a restriction.
* @param appInfoNode Node; node having possible xsd:appinfo for a message.
* @param node Node; node, must be an xsd:restriction.
* @param value String; value.
* @return String; first encountered problem in validating the value by a restriction.
*/
private static String reportRestrictionNonCompliance(final Node node, final String value)
private static String reportRestrictionNonCompliance(final Node appInfoNode, final Node node, final String value)
{
Node pattern = DocumentReader.getChild(node, "xsd:pattern");
if (pattern != null)
Expand All @@ -344,7 +347,8 @@ private static String reportRestrictionNonCompliance(final Node node, final Stri
{
if (!Pattern.matches(patternString, value))
{
return "Value does not match pattern " + patternString;
String patternMessage = DocumentReader.getAnnotation(appInfoNode, "xsd:appinfo", "pattern");
return patternMessage == null ? "Value does not match pattern " + patternString : patternMessage;
}
}
catch (PatternSyntaxException exception)
Expand Down
3 changes: 3 additions & 0 deletions ots-parser-xml/src/main/resources/xsd/ots-types.xsd
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,9 @@
</xsd:simpleType>

<xsd:simpleType name="LengthType">
<xsd:annotation>
<xsd:appinfo source="pattern">Provide a valid Length, such as "3.5m".</xsd:appinfo>
</xsd:annotation>
<xsd:restriction base="xsd:string">
<xsd:pattern value="[+-]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?\s*(m|Ym|Zm|Em|Pm|Tm|Gm|Mm|km|hm|dam|dm|cm|mm|\\u03BCm|mum|nm|pm|fm|am|zm|ym|\\u212B|A|AU|Pc|ft|in|ly|mi|NM|yd)"></xsd:pattern>
</xsd:restriction>
Expand Down

0 comments on commit 2975af7

Please sign in to comment.