From ba44f67b3bbc6916e1f32e06080614ac85563c3e Mon Sep 17 00:00:00 2001 From: Robert Sehr Date: Fri, 29 Nov 2024 07:46:02 +0100 Subject: [PATCH] handle sonar messages --- .../checks/TifValidationResolutionCheck.java | 2 +- .../checks/TifValidationSimpleXpathCheck.java | 24 +++++++++---------- .../checks/numbers/ValueRangeFactory.java | 20 +++++++++------- 3 files changed, 24 insertions(+), 22 deletions(-) diff --git a/module-base/src/main/java/de/intranda/goobi/plugins/checks/TifValidationResolutionCheck.java b/module-base/src/main/java/de/intranda/goobi/plugins/checks/TifValidationResolutionCheck.java index 1280f9d..1bdc788 100644 --- a/module-base/src/main/java/de/intranda/goobi/plugins/checks/TifValidationResolutionCheck.java +++ b/module-base/src/main/java/de/intranda/goobi/plugins/checks/TifValidationResolutionCheck.java @@ -16,7 +16,7 @@ import de.intranda.goobi.plugins.checks.numbers.ValueRangeFactory; public class TifValidationResolutionCheck implements TifValidationCheck { - public static String NAME = "resolution_check"; + public static final String NAME = "resolution_check"; private static Namespace jhove = Namespace.getNamespace("jhove", "http://hul.harvard.edu/ois/xml/ns/jhove"); 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 7afe9e9..264c02a 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 @@ -22,7 +22,7 @@ public class TifValidationSimpleXpathCheck implements TifValidationCheck { private static XPathFactory xFactory = XPathFactory.instance(); private Set namespaces; - private XPathExpression xpath; + private XPathExpression xpath; private String expectedValue; private String errorMessage; private Map replaceMap; @@ -37,11 +37,10 @@ public TifValidationSimpleXpathCheck(Set namespaces, String xpath, St } public static String validateXPath(String path) { - if (StringUtils.isNotBlank(path)) { - if (!path.matches("\\w+\\(.+\\)")) { - path = "string(" + path + ")"; - } + if (StringUtils.isNotBlank(path) && !path.matches("\\w+\\(.+\\)")) { + path = "string(" + path + ")"; } + return path; } @@ -53,18 +52,17 @@ private void createReplaceMap() { @Override public boolean check(Document doc) { Object value = xpath.evaluateFirst(doc); - if (value instanceof Element) { - value = ((Element) value).getTextTrim(); - } else if (value instanceof Attribute) { - value = ((Attribute) value).getValue(); - } else if (value instanceof Text) { - value = ((Text) value).getText(); + if (value instanceof Element e) { + value = e.getTextTrim(); + } else if (value instanceof Attribute a) { + value = a.getValue(); + } else if (value instanceof Text t) { + value = t.getText(); } else if (!(value instanceof String)) { value = value.toString(); } this.replaceMap.put("found", (String) value); - return this.expectedValue.equals(value) || (value != null && value instanceof String && ((String) value).matches(this.expectedValue)); - // return this.expectedValue.equals(value); + return this.expectedValue.equals(value) || (value instanceof String s && s.matches(this.expectedValue)); } @Override diff --git a/module-base/src/main/java/de/intranda/goobi/plugins/checks/numbers/ValueRangeFactory.java b/module-base/src/main/java/de/intranda/goobi/plugins/checks/numbers/ValueRangeFactory.java index 2f0b6f5..59c8d39 100644 --- a/module-base/src/main/java/de/intranda/goobi/plugins/checks/numbers/ValueRangeFactory.java +++ b/module-base/src/main/java/de/intranda/goobi/plugins/checks/numbers/ValueRangeFactory.java @@ -6,26 +6,30 @@ public class ValueRangeFactory { - public static ValueRange create(String s) throws IllegalArgumentException{ + private ValueRangeFactory() { + // private constructor to hide public default constructor + } + + public static ValueRange create(String s) throws IllegalArgumentException { ValueRange range = null; - if(s.contains(",")) { + if (s.contains(",")) { StringTokenizer tokenizer = new StringTokenizer(s, ","); range = new ArrayValueRange(); - while(tokenizer.hasMoreTokens()) { - ((ArrayValueRange)range).addValue(tokenizer.nextToken()); + while (tokenizer.hasMoreTokens()) { + ((ArrayValueRange) range).addValue(tokenizer.nextToken()); } - } else if(s.contains("-")) { + } else if (s.contains("-")) { int index = s.indexOf("-"); String start = s.substring(0, index); - String end = s.substring(index+1); + String end = s.substring(index + 1); try { double startD = Double.parseDouble(start); double endD = Double.parseDouble(end); range = new ContinuousArrayRange(startD, endD); - } catch(NullPointerException | NumberFormatException e) { + } catch (NullPointerException | NumberFormatException e) { throw new IllegalArgumentException("Cannot parse " + s + " as number range"); } - } else if(StringUtils.isNumeric(s)) { + } else if (StringUtils.isNumeric(s)) { double value = Double.parseDouble(s); range = new SingleValueRange(value); } else {