From a68921c1c2a420580104946492a871f5076e8e5c Mon Sep 17 00:00:00 2001 From: "Christian P. MOMON" Date: Sat, 24 Oct 2020 01:54:15 +0200 Subject: [PATCH] Improved property checker. --- .../statoolinfos/checker/PropertyCheck.java | 19 ++- .../statoolinfos/checker/PropertyChecker.java | 112 ++++++++++++++---- .../statoolinfos/checker/PropertyChecks.java | 23 ++++ .../statoolinfos/checker/PropertyRule.java | 93 ++++++++++++--- .../statoolinfos/checker/PropertyRules.java | 77 +++++++++--- .../statoolinfos/htmlize/federation.xhtml | 2 +- .../statoolinfos/htmlize/organization.xhtml | 2 +- .../statoolinfos/htmlize/service.xhtml | 2 +- 8 files changed, 268 insertions(+), 62 deletions(-) diff --git a/src/fr/devinsy/statoolinfos/checker/PropertyCheck.java b/src/fr/devinsy/statoolinfos/checker/PropertyCheck.java index f8a852c..c16b9b1 100644 --- a/src/fr/devinsy/statoolinfos/checker/PropertyCheck.java +++ b/src/fr/devinsy/statoolinfos/checker/PropertyCheck.java @@ -54,11 +54,28 @@ public class PropertyCheck * the status */ public PropertyCheck(final long index, final String line, final Status status) + { + this(index, line, status, ""); + } + + /** + * Instantiates a new property check. + * + * @param index + * the index + * @param line + * the line + * @param status + * the status + * @param comment + * the comment + */ + public PropertyCheck(final long index, final String line, final Status status, final String comment) { this.index = index; this.line = line; this.status = status; - this.comment = ""; + this.comment = comment; } public String getComment() diff --git a/src/fr/devinsy/statoolinfos/checker/PropertyChecker.java b/src/fr/devinsy/statoolinfos/checker/PropertyChecker.java index 2973e70..aebe317 100644 --- a/src/fr/devinsy/statoolinfos/checker/PropertyChecker.java +++ b/src/fr/devinsy/statoolinfos/checker/PropertyChecker.java @@ -20,6 +20,7 @@ package fr.devinsy.statoolinfos.checker; import java.io.File; import java.io.IOException; +import java.util.Iterator; import org.apache.commons.lang3.StringUtils; @@ -34,11 +35,26 @@ import fr.devinsy.strings.StringsUtils; */ public class PropertyChecker { + public static final String ALL = "^.*$"; public static final String STRING = "^.+$"; public static final String DATETIME = "\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}(\\.\\d+)?"; public static final String DATE = "^(\\d{4}-\\d{2}-\\d{2}|\\d{2}/\\d{2}/\\d{4}|\\d{2}/\\d{4})"; public static final String URL = "^(http(s)?://)?[\\w-_\\.]+(\\.\\w+)+(/.*)?$"; public static final String EMAIL = "^.*@.*$"; + public static final String NUMERIC = "^\\d+$"; + public static final String NUMERICS = "^\\d*(,\\d*)*$"; + public static final String MONTHS = "^\\d*(,\\d*){0,11}$"; + public static final String WEEKS = "^\\d*(,\\d*){0,52}$"; + public static final String DAYS = "^\\d*(,\\d*){0,365}$"; + + public static final String SUBS = "^subs\\.\\S+$"; + public static final String METRICS_NAME = "^metrics\\.\\S+\\.name$"; + public static final String METRICS_DESCRIPTION = "^metrics\\.\\S+\\.description$"; + public static final String METRICS_YEAR = "^metrics\\.\\S+\\.\\d{4}$"; + public static final String METRICS_MONTHS = "^metrics\\.\\S+\\.\\d{4}\\.months$"; + public static final String METRICS_WEEKS = "^metrics\\.\\S+\\.\\d{4}\\.weeks$"; + public static final String METRICS_DAYS = "^metrics\\.\\S+\\.\\d{4}\\.days$"; + public static final String CRAWL = "^crawl\\.\\S+$"; private PropertyRules serviceRules; private PropertyRules federationRules; @@ -64,6 +80,7 @@ public class PropertyChecker this.federationRules.add("federation.favicon", URL, PropertyMode.WISHED); this.federationRules.add("federation.contact.url", URL, PropertyMode.WISHED); this.federationRules.add("federation.contact.email", EMAIL, PropertyMode.WISHED); + this.federationRules.add("^federation\\.socialnetworks\\.\\S+$", URL, PropertyMode.WISHED); this.federationRules.add("federation.legal.url", URL, PropertyMode.WISHED); this.federationRules.add("federation.legal", URL, PropertyMode.WISHED); this.federationRules.add("federation.guide.user", URL, PropertyMode.WISHED); @@ -71,6 +88,16 @@ public class PropertyChecker this.federationRules.add("federation.startdate", DATE, PropertyMode.WISHED); this.federationRules.add("federation.enddate", DATE, PropertyMode.OPTIONAL); + this.federationRules.add(SUBS, URL, PropertyMode.OPTIONAL); + this.federationRules.add(METRICS_NAME, STRING, PropertyMode.OPTIONAL); + this.federationRules.add(METRICS_DESCRIPTION, STRING, PropertyMode.OPTIONAL); + this.federationRules.add(METRICS_YEAR, NUMERIC, PropertyMode.OPTIONAL); + this.federationRules.add(METRICS_MONTHS, MONTHS, PropertyMode.OPTIONAL); + this.federationRules.add(METRICS_WEEKS, WEEKS, PropertyMode.OPTIONAL); + this.federationRules.add(METRICS_DAYS, DAYS, PropertyMode.OPTIONAL); + + // this.federationRules.add(CRAWL, ALL, PropertyMode.MANDATORY); + // this.organizationRules = new PropertyRules(); @@ -83,6 +110,7 @@ public class PropertyChecker this.organizationRules.add("organization.description", STRING, PropertyMode.WISHED); this.organizationRules.add("organization.website", URL, PropertyMode.WISHED); this.organizationRules.add("organization.logo", URL, PropertyMode.WISHED); + this.organizationRules.add("^organization\\.socialnetworks\\.\\S+$", URL, PropertyMode.WISHED); this.organizationRules.add("organization.owner.name", STRING, PropertyMode.OPTIONAL); this.organizationRules.add("organization.owner.website", URL, PropertyMode.OPTIONAL); this.organizationRules.add("organization.owner.logo", URL, PropertyMode.OPTIONAL); @@ -95,6 +123,16 @@ public class PropertyChecker this.organizationRules.add("organization.startdate", DATE, PropertyMode.WISHED); this.organizationRules.add("organization.enddate", DATE, PropertyMode.OPTIONAL); + this.organizationRules.add(SUBS, URL, PropertyMode.OPTIONAL); + this.organizationRules.add(METRICS_NAME, STRING, PropertyMode.OPTIONAL); + this.organizationRules.add(METRICS_DESCRIPTION, STRING, PropertyMode.OPTIONAL); + this.organizationRules.add(METRICS_YEAR, NUMERIC, PropertyMode.OPTIONAL); + this.organizationRules.add(METRICS_MONTHS, MONTHS, PropertyMode.OPTIONAL); + this.organizationRules.add(METRICS_WEEKS, WEEKS, PropertyMode.OPTIONAL); + this.organizationRules.add(METRICS_DAYS, DAYS, PropertyMode.OPTIONAL); + + this.organizationRules.add(CRAWL, ALL, PropertyMode.MANDATORY); + // this.serviceRules = new PropertyRules(); @@ -109,6 +147,7 @@ public class PropertyChecker this.serviceRules.add("service.logo", URL, PropertyMode.WISHED); this.serviceRules.add("service.contact.url", URL, PropertyMode.WISHED); this.serviceRules.add("service.contact.email", EMAIL, PropertyMode.WISHED); + this.serviceRules.add("^service\\.socialnetworks\\.\\S+$", URL, PropertyMode.WISHED); this.serviceRules.add("service.legal.url", URL, PropertyMode.WISHED); this.serviceRules.add("service.guide.user", URL, PropertyMode.WISHED); this.serviceRules.add("service.guide.technical", URL, PropertyMode.WISHED); @@ -124,6 +163,16 @@ public class PropertyChecker this.serviceRules.add("software.license.name", STRING, PropertyMode.MANDATORY); this.serviceRules.add("software.version", STRING, PropertyMode.WISHED); this.serviceRules.add("software.source.url", URL, PropertyMode.WISHED); + + // this.serviceRules.add(SUBS, URL, PropertyMode.MANDATORY); + this.serviceRules.add(METRICS_NAME, STRING, PropertyMode.OPTIONAL); + this.serviceRules.add(METRICS_DESCRIPTION, STRING, PropertyMode.OPTIONAL); + this.serviceRules.add(METRICS_YEAR, NUMERIC, PropertyMode.OPTIONAL); + this.serviceRules.add(METRICS_MONTHS, MONTHS, PropertyMode.OPTIONAL); + this.serviceRules.add(METRICS_WEEKS, WEEKS, PropertyMode.OPTIONAL); + this.serviceRules.add(METRICS_DAYS, DAYS, PropertyMode.OPTIONAL); + + this.serviceRules.add(CRAWL, ALL, PropertyMode.MANDATORY); } /** @@ -139,6 +188,38 @@ public class PropertyChecker result = new PropertyChecks(); + // + for (PropertyRule rule : rules.getMandatories()) + { + boolean ended = false; + Iterator iterator = lines.iterator(); + while (!ended) + { + if (iterator.hasNext()) + { + String line = iterator.next(); + + if ((!StringUtils.isBlank(line)) && (!line.startsWith("#"))) + { + String[] tokens = line.split("=", 2); + PathProperty property = new PathProperty(tokens[0].trim(), tokens[1].trim()); + + if (rule.checkPath(property.getPath())) + { + ended = true; + } + } + } + else + { + ended = true; + PropertyCheck check = new PropertyCheck(0, rule.getPathPattern(), Status.ERROR, "Propriété manquante"); + result.add(check); + } + } + } + + // int lineIndex = 1; for (String line : lines) { @@ -162,33 +243,14 @@ public class PropertyChecker { String[] tokens = line.split("=", 2); PathProperty property = new PathProperty(tokens[0].trim(), tokens[1].trim()); - PropertyRule rule = rules.get(property.getPath()); + PropertyRule rule = rules.find(property.getPath()); check = new PropertyCheck(lineIndex, line, Status.VOID); if (rule == null) { - if (StringUtils.startsWithAny(property.getPath(), "metrics.")) - { - check.setStatus(Status.OK); - check.setComment("OK"); - } - else if (StringUtils.startsWithAny(property.getPath(), "subs.")) - { - check.setStatus(Status.OK); - check.setComment("OK"); - } - else if (StringUtils.startsWithAny(property.getPath(), "crawl.")) - { - check.setStatus(Status.OK); - check.setComment("OK"); - } - else - { - - check.setStatus(Status.VOID); - check.setComment("Propriété inconnue"); - } + check.setStatus(Status.VOID); + check.setComment("Propriété inconnue"); } else if (rule.isOptional()) { @@ -197,7 +259,7 @@ public class PropertyChecker check.setStatus(Status.OK); check.setComment("OK"); } - else if (property.getValue().matches(rule.getPattern())) + else if (rule.checkValue(property.getValue())) { check.setStatus(Status.OK); check.setComment("OK"); @@ -215,7 +277,7 @@ public class PropertyChecker check.setStatus(Status.WARNING); check.setComment("Valeur recommandée"); } - else if (property.getValue().matches(rule.getPattern())) + else if (rule.checkValue(property.getValue())) { check.setStatus(Status.OK); check.setComment("OK"); @@ -233,7 +295,7 @@ public class PropertyChecker check.setStatus(Status.ERROR); check.setComment("Value obligatoire"); } - else if (property.getValue().matches(rule.getPattern())) + else if (rule.checkValue(property.getValue())) { check.setStatus(Status.OK); check.setComment("OK"); diff --git a/src/fr/devinsy/statoolinfos/checker/PropertyChecks.java b/src/fr/devinsy/statoolinfos/checker/PropertyChecks.java index b44f770..8d69b29 100644 --- a/src/fr/devinsy/statoolinfos/checker/PropertyChecks.java +++ b/src/fr/devinsy/statoolinfos/checker/PropertyChecks.java @@ -34,4 +34,27 @@ public class PropertyChecks extends ArrayList { super(); } + + /** + * Extract active lines. + * + * @return the property checks + */ + public PropertyChecks extractActiveLines() + { + PropertyChecks result; + + result = new PropertyChecks(); + + for (PropertyCheck check : this) + { + if (!check.getLine().matches("^(|#\\s?[^\\[].*)$")) + { + result.add(check); + } + } + + // + return result; + } } \ No newline at end of file diff --git a/src/fr/devinsy/statoolinfos/checker/PropertyRule.java b/src/fr/devinsy/statoolinfos/checker/PropertyRule.java index fb1c5b6..a949122 100644 --- a/src/fr/devinsy/statoolinfos/checker/PropertyRule.java +++ b/src/fr/devinsy/statoolinfos/checker/PropertyRule.java @@ -18,6 +18,9 @@ */ package fr.devinsy.statoolinfos.checker; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + /** * The Class PropertyRule. */ @@ -30,40 +33,93 @@ public class PropertyRule OPTIONAL } - private String path; - private String pattern; + private Pattern pathPattern; + private Pattern valuePattern; private PropertyMode mode; /** * Instantiates a new property rule. * - * @param path + * @param pathRegex * the path - * @param pattern + * @param valueRegex * the pattern * @param mode * the mode */ - public PropertyRule(final String path, final String pattern, final PropertyMode mode) + public PropertyRule(final String pathRegex, final String valueRegex, final PropertyMode mode) { - this.path = path; - this.pattern = pattern; + this.pathPattern = Pattern.compile(pathRegex); + this.valuePattern = Pattern.compile(valueRegex); this.mode = mode; } + /** + * @param value + * @return + */ + public boolean checkPath(final String value) + { + boolean result; + + Matcher matcher = this.pathPattern.matcher(value); + result = matcher.matches(); + + // + return result; + } + + /** + * Check. + * + * @param value + * the value + * @return true, if successful + */ + public boolean checkValue(final String value) + { + boolean result; + + Matcher matcher = this.valuePattern.matcher(value); + result = matcher.matches(); + + // + return result; + } + public PropertyMode getMode() { return this.mode; } - public String getPath() + /** + * Gets the path pattern. + * + * @return the path pattern + */ + public String getPathPattern() { - return this.path; + String result; + + result = this.pathPattern.pattern(); + + // + return result; } - public String getPattern() + /** + * Gets the pattern string. + * + * @return the pattern string + */ + public String getValuePattern() { - return this.pattern; + String result; + + result = this.valuePattern.pattern(); + + // + return result; } /** @@ -137,13 +193,14 @@ public class PropertyRule this.mode = mode; } - public void setPath(final String path) + /** + * Sets the pattern. + * + * @param valuePattern + * the new pattern + */ + public void setPattern(final String regex) { - this.path = path; - } - - public void setPattern(final String pattern) - { - this.pattern = pattern; + this.valuePattern = Pattern.compile(regex); } } \ No newline at end of file diff --git a/src/fr/devinsy/statoolinfos/checker/PropertyRules.java b/src/fr/devinsy/statoolinfos/checker/PropertyRules.java index 134e66f..86e019e 100644 --- a/src/fr/devinsy/statoolinfos/checker/PropertyRules.java +++ b/src/fr/devinsy/statoolinfos/checker/PropertyRules.java @@ -18,18 +18,17 @@ */ package fr.devinsy.statoolinfos.checker; -import java.util.HashMap; - -import org.apache.commons.lang3.StringUtils; +import java.util.ArrayList; +import java.util.Iterator; import fr.devinsy.statoolinfos.checker.PropertyRule.PropertyMode; /** - * The Class PropertyRules. + * The Class PropertyRuleList. */ -public class PropertyRules extends HashMap +public class PropertyRules extends ArrayList { - private static final long serialVersionUID = -3830415346239101054L; + private static final long serialVersionUID = 5808894132916693496L; /** * Instantiates a new property rules. @@ -51,26 +50,74 @@ public class PropertyRules extends HashMap */ public void add(final String path, final String pattern, final PropertyMode mode) { - PropertyRule rule = new PropertyRule(path, pattern, mode); + String targetPath; + if (path.startsWith("^")) + { + targetPath = path; + } + else + { + targetPath = path.replace(".", "\\."); + } - this.put(StringUtils.toRootLowerCase(path), rule); + PropertyRule rule = new PropertyRule(targetPath, pattern, mode); + + this.add(rule); } /** + * Find. + * * @param path - * @return + * the path + * @return the property rule */ - public PropertyRule get(final String path) + public PropertyRule find(final String path) { PropertyRule result; - result = super.get(StringUtils.toRootLowerCase(path)); - - if (result == null) + boolean ended = false; + Iterator iterator = iterator(); + result = null; + while (!ended) { - if (path.matches("^[^\\.]+\\.socialnetworks\\..+$")) + if (iterator.hasNext()) { - result = new PropertyRule("*.socialnetworks.*", PropertyChecker.URL, PropertyMode.WISHED); + PropertyRule rule = iterator.next(); + + if (rule.checkPath(path)) + { + ended = true; + result = rule; + } + } + else + { + ended = true; + result = null; + } + } + + // + return result; + } + + /** + * Gets the mandatories. + * + * @return the mandatories + */ + public PropertyRules getMandatories() + { + PropertyRules result; + + result = new PropertyRules(); + + for (PropertyRule rule : this) + { + if (rule.isMandatory()) + { + result.add(rule); } } diff --git a/src/fr/devinsy/statoolinfos/htmlize/federation.xhtml b/src/fr/devinsy/statoolinfos/htmlize/federation.xhtml index 59c54cd..64a3f7f 100644 --- a/src/fr/devinsy/statoolinfos/htmlize/federation.xhtml +++ b/src/fr/devinsy/statoolinfos/htmlize/federation.xhtml @@ -29,8 +29,8 @@ - +
diff --git a/src/fr/devinsy/statoolinfos/htmlize/organization.xhtml b/src/fr/devinsy/statoolinfos/htmlize/organization.xhtml index f9328cf..1929a07 100644 --- a/src/fr/devinsy/statoolinfos/htmlize/organization.xhtml +++ b/src/fr/devinsy/statoolinfos/htmlize/organization.xhtml @@ -30,8 +30,8 @@ - +
Nombre de services : n/a
diff --git a/src/fr/devinsy/statoolinfos/htmlize/service.xhtml b/src/fr/devinsy/statoolinfos/htmlize/service.xhtml index 632cebe..105dd8f 100644 --- a/src/fr/devinsy/statoolinfos/htmlize/service.xhtml +++ b/src/fr/devinsy/statoolinfos/htmlize/service.xhtml @@ -38,8 +38,8 @@ - +