From b27ef9f51b16d6f61c5273c90f9ce38f503acc62 Mon Sep 17 00:00:00 2001 From: Robert Sehr Date: Tue, 3 Dec 2024 08:54:27 +0100 Subject: [PATCH] added new validation types --- .../plugins/TifValidationConfiguration.java | 1 - .../checks/TifValidationSimpleXpathCheck.java | 68 +++++++++++++++++-- 2 files changed, 62 insertions(+), 7 deletions(-) diff --git a/module-base/src/main/java/de/intranda/goobi/plugins/TifValidationConfiguration.java b/module-base/src/main/java/de/intranda/goobi/plugins/TifValidationConfiguration.java index c6a0d8f..4a4bb90 100644 --- a/module-base/src/main/java/de/intranda/goobi/plugins/TifValidationConfiguration.java +++ b/module-base/src/main/java/de/intranda/goobi/plugins/TifValidationConfiguration.java @@ -91,7 +91,6 @@ private void readChecks() { break; } } - } //ADD NEW CHECKS HERE diff --git a/module-base/src/main/java/de/intranda/goobi/plugins/checks/TifValidationSimpleXpathCheck.java b/module-base/src/main/java/de/intranda/goobi/plugins/checks/TifValidationSimpleXpathCheck.java index 54e65d1..6cd851e 100644 --- a/module-base/src/main/java/de/intranda/goobi/plugins/checks/TifValidationSimpleXpathCheck.java +++ b/module-base/src/main/java/de/intranda/goobi/plugins/checks/TifValidationSimpleXpathCheck.java @@ -4,8 +4,9 @@ import java.util.Map; import java.util.Set; -import org.apache.commons.lang.StringUtils; -import org.apache.commons.lang.text.StrSubstitutor; +import org.apache.commons.lang3.StringUtils; +import org.apache.commons.lang3.math.NumberUtils; +import org.apache.commons.text.StringSubstitutor; import org.jdom2.Attribute; import org.jdom2.Document; import org.jdom2.Element; @@ -27,8 +28,9 @@ public class TifValidationSimpleXpathCheck implements TifValidationCheck { private String errorMessage; private Map replaceMap; - // possible values: equals, greater, lesser, exists, not exists, same + // possible values: equals, greater, lesser, exists, not exists, same, multiple private String checkType = "equals"; + private String otherXpath; public TifValidationSimpleXpathCheck(Set namespaces, String xpath, String expectedValue, String errorMessage) { super(); @@ -69,11 +71,65 @@ public boolean check(Document doc) { value = value.toString(); } this.replaceMap.put("found", (String) value); - + String val = (String) value; switch (checkType) { case "exists": return true; - + case "greater": + + if (NumberUtils.isCreatable(val)) { + if (val.contains(".")) { + double expected = Double.parseDouble(expectedValue); + double d = Double.parseDouble(val); + return expected <= d; + } else { + int expected = Integer.parseInt(expectedValue); + int i = Integer.parseInt(val); + return expected <= i; + } + } + // no number + return false; + case "lesser": + if (NumberUtils.isCreatable(val)) { + if (val.contains(".")) { + double expected = Double.parseDouble(expectedValue); + double d = Double.parseDouble(val); + return expected >= d; + } else { + int expected = Integer.parseInt(expectedValue); + int i = Integer.parseInt(val); + return expected >= i; + } + } + // no number + return false; + case "same": + XPathExpression o = xFactory.compile(otherXpath, Filters.fpassthrough(), null, namespaces); + Object otherValue = o.evaluateFirst(doc); + if (otherValue instanceof Element e) { + otherValue = e.getTextTrim(); + } else if (otherValue instanceof Attribute a) { + otherValue = a.getValue(); + } else if (otherValue instanceof Text t) { + otherValue = t.getText(); + } else if (!(otherValue instanceof String)) { + otherValue = otherValue.toString(); + } + return value.equals(otherValue); + case "multiple": + if (NumberUtils.isCreatable(val)) { + int expexted = Integer.parseInt(expectedValue); + int actual; + if (val.contains(".")) { + actual = (int) Double.parseDouble(val); + } else { + actual = Integer.parseInt(val); + } + return actual % expexted == 0; + } + // no number + return false; case "equals": { return this.expectedValue.equals(value) || (value instanceof String s && s.matches(this.expectedValue)); } @@ -86,7 +142,7 @@ public boolean check(Document doc) { @Override public String getFormattedError(String imageName) { this.replaceMap.put("image", imageName); - return StrSubstitutor.replace(errorMessage, replaceMap); + return StringSubstitutor.replace(errorMessage, replaceMap); } }