Skip to content

Commit

Permalink
only convert numeric looking things if they don't have a leading 0
Browse files Browse the repository at this point in the history
  • Loading branch information
jeanetteclark committed Dec 6, 2023
1 parent 2169118 commit 7a87866
Showing 1 changed file with 19 additions and 13 deletions.
32 changes: 19 additions & 13 deletions src/main/java/edu/ucsb/nceas/mdqengine/processor/XMLDialect.java
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,6 @@ public Result runCheck(Check check) throws XPathExpressionException {
result.setStatus(Status.ERROR);
result.setOutput(new Output(e.getMessage()));
}
//dispatcher.close();

} else {
// we just skip instead
Expand Down Expand Up @@ -361,6 +360,7 @@ private Object selectPath(Selector selector, Node contextNode) throws XPathExpre

// select one or more values from document
String selectorPath = selector.getXpath();

XPath xpath = xPathfactory.newXPath();

// combine the found namespaces and any additional ones asserted by selector.
Expand Down Expand Up @@ -454,22 +454,28 @@ else if (nodes.getLength() > 0 || selector.getSubSelector() != null) {
/*
* Retype an object based on a few simple assumptions. A "String" value is
* typically passed in. If only numeric characters are present in the String,
* then
* the object is caste to type "Number". If the string value appears to be an
* "affermative" or "negative" value (e.g. "Y", "Yes", "N", "No", ...) then the
* value is caste to "Boolean".
* then the object is caste to type "Number". If the string value appears to
* be an "affirmative" or "negative" value (e.g. "Y", "Yes", "N", "No", ...)
* then the value is caste to "Boolean".
*/
public static Object retypeObject(Object value) {
Object result = value;
// type the value correctly
if (NumberUtils.isNumber((String) value)) {
result = NumberUtils.createNumber((String) value);
} else {
// relies on this method to return null if we are not sure if it is a boolean
Boolean bool = BooleanUtils.toBooleanObject((String) value);
if (bool != null) {
result = bool;

if (value instanceof String) {
String stringValue = (String) value;
// try to type the value correctly
if (NumberUtils.isNumber(stringValue) && !stringValue.matches("^0\\d*$")) {
// If it's a valid number and doesn't start with zeros, create a Number object
result = NumberUtils.createNumber(stringValue);
} else {
// try to convert to bool
Boolean bool = BooleanUtils.toBooleanObject((String) value);
// if it worked, return the boolean, otherwise the original result is returned
if (bool != null) {
result = bool;
}
}

}

return result;
Expand Down

0 comments on commit 7a87866

Please sign in to comment.