Improved property checker.

This commit is contained in:
Christian P. MOMON 2020-10-24 01:54:15 +02:00
parent be050084d6
commit a68921c1c2
8 changed files with 268 additions and 62 deletions

View file

@ -54,11 +54,28 @@ public class PropertyCheck
* the status * the status
*/ */
public PropertyCheck(final long index, final String line, final Status 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.index = index;
this.line = line; this.line = line;
this.status = status; this.status = status;
this.comment = ""; this.comment = comment;
} }
public String getComment() public String getComment()

View file

@ -20,6 +20,7 @@ package fr.devinsy.statoolinfos.checker;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.util.Iterator;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
@ -34,11 +35,26 @@ import fr.devinsy.strings.StringsUtils;
*/ */
public class PropertyChecker public class PropertyChecker
{ {
public static final String ALL = "^.*$";
public static final String STRING = "^.+$"; 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 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 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 URL = "^(http(s)?://)?[\\w-_\\.]+(\\.\\w+)+(/.*)?$";
public static final String EMAIL = "^.*@.*$"; 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 serviceRules;
private PropertyRules federationRules; private PropertyRules federationRules;
@ -64,6 +80,7 @@ public class PropertyChecker
this.federationRules.add("federation.favicon", URL, PropertyMode.WISHED); this.federationRules.add("federation.favicon", URL, PropertyMode.WISHED);
this.federationRules.add("federation.contact.url", 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.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", URL, PropertyMode.WISHED);
this.federationRules.add("federation.legal", URL, PropertyMode.WISHED); this.federationRules.add("federation.legal", URL, PropertyMode.WISHED);
this.federationRules.add("federation.guide.user", 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.startdate", DATE, PropertyMode.WISHED);
this.federationRules.add("federation.enddate", DATE, PropertyMode.OPTIONAL); 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(); this.organizationRules = new PropertyRules();
@ -83,6 +110,7 @@ public class PropertyChecker
this.organizationRules.add("organization.description", STRING, PropertyMode.WISHED); this.organizationRules.add("organization.description", STRING, PropertyMode.WISHED);
this.organizationRules.add("organization.website", URL, PropertyMode.WISHED); this.organizationRules.add("organization.website", URL, PropertyMode.WISHED);
this.organizationRules.add("organization.logo", 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.name", STRING, PropertyMode.OPTIONAL);
this.organizationRules.add("organization.owner.website", URL, PropertyMode.OPTIONAL); this.organizationRules.add("organization.owner.website", URL, PropertyMode.OPTIONAL);
this.organizationRules.add("organization.owner.logo", 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.startdate", DATE, PropertyMode.WISHED);
this.organizationRules.add("organization.enddate", DATE, PropertyMode.OPTIONAL); 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(); this.serviceRules = new PropertyRules();
@ -109,6 +147,7 @@ public class PropertyChecker
this.serviceRules.add("service.logo", URL, PropertyMode.WISHED); this.serviceRules.add("service.logo", URL, PropertyMode.WISHED);
this.serviceRules.add("service.contact.url", 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.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.legal.url", URL, PropertyMode.WISHED);
this.serviceRules.add("service.guide.user", URL, PropertyMode.WISHED); this.serviceRules.add("service.guide.user", URL, PropertyMode.WISHED);
this.serviceRules.add("service.guide.technical", 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.license.name", STRING, PropertyMode.MANDATORY);
this.serviceRules.add("software.version", STRING, PropertyMode.WISHED); this.serviceRules.add("software.version", STRING, PropertyMode.WISHED);
this.serviceRules.add("software.source.url", URL, 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(); result = new PropertyChecks();
//
for (PropertyRule rule : rules.getMandatories())
{
boolean ended = false;
Iterator<String> 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; int lineIndex = 1;
for (String line : lines) for (String line : lines)
{ {
@ -162,33 +243,14 @@ public class PropertyChecker
{ {
String[] tokens = line.split("=", 2); String[] tokens = line.split("=", 2);
PathProperty property = new PathProperty(tokens[0].trim(), tokens[1].trim()); 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); check = new PropertyCheck(lineIndex, line, Status.VOID);
if (rule == null) if (rule == null)
{ {
if (StringUtils.startsWithAny(property.getPath(), "metrics.")) check.setStatus(Status.VOID);
{ check.setComment("Propriété inconnue");
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");
}
} }
else if (rule.isOptional()) else if (rule.isOptional())
{ {
@ -197,7 +259,7 @@ public class PropertyChecker
check.setStatus(Status.OK); check.setStatus(Status.OK);
check.setComment("OK"); check.setComment("OK");
} }
else if (property.getValue().matches(rule.getPattern())) else if (rule.checkValue(property.getValue()))
{ {
check.setStatus(Status.OK); check.setStatus(Status.OK);
check.setComment("OK"); check.setComment("OK");
@ -215,7 +277,7 @@ public class PropertyChecker
check.setStatus(Status.WARNING); check.setStatus(Status.WARNING);
check.setComment("Valeur recommandée"); check.setComment("Valeur recommandée");
} }
else if (property.getValue().matches(rule.getPattern())) else if (rule.checkValue(property.getValue()))
{ {
check.setStatus(Status.OK); check.setStatus(Status.OK);
check.setComment("OK"); check.setComment("OK");
@ -233,7 +295,7 @@ public class PropertyChecker
check.setStatus(Status.ERROR); check.setStatus(Status.ERROR);
check.setComment("Value obligatoire"); check.setComment("Value obligatoire");
} }
else if (property.getValue().matches(rule.getPattern())) else if (rule.checkValue(property.getValue()))
{ {
check.setStatus(Status.OK); check.setStatus(Status.OK);
check.setComment("OK"); check.setComment("OK");

View file

@ -34,4 +34,27 @@ public class PropertyChecks extends ArrayList<PropertyCheck>
{ {
super(); 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;
}
} }

View file

@ -18,6 +18,9 @@
*/ */
package fr.devinsy.statoolinfos.checker; package fr.devinsy.statoolinfos.checker;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/** /**
* The Class PropertyRule. * The Class PropertyRule.
*/ */
@ -30,40 +33,93 @@ public class PropertyRule
OPTIONAL OPTIONAL
} }
private String path; private Pattern pathPattern;
private String pattern; private Pattern valuePattern;
private PropertyMode mode; private PropertyMode mode;
/** /**
* Instantiates a new property rule. * Instantiates a new property rule.
* *
* @param path * @param pathRegex
* the path * the path
* @param pattern * @param valueRegex
* the pattern * the pattern
* @param mode * @param mode
* the 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.pathPattern = Pattern.compile(pathRegex);
this.pattern = pattern; this.valuePattern = Pattern.compile(valueRegex);
this.mode = mode; 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() public PropertyMode getMode()
{ {
return this.mode; 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; 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; this.valuePattern = Pattern.compile(regex);
}
public void setPattern(final String pattern)
{
this.pattern = pattern;
} }
} }

View file

@ -18,18 +18,17 @@
*/ */
package fr.devinsy.statoolinfos.checker; package fr.devinsy.statoolinfos.checker;
import java.util.HashMap; import java.util.ArrayList;
import java.util.Iterator;
import org.apache.commons.lang3.StringUtils;
import fr.devinsy.statoolinfos.checker.PropertyRule.PropertyMode; import fr.devinsy.statoolinfos.checker.PropertyRule.PropertyMode;
/** /**
* The Class PropertyRules. * The Class PropertyRuleList.
*/ */
public class PropertyRules extends HashMap<String, PropertyRule> public class PropertyRules extends ArrayList<PropertyRule>
{ {
private static final long serialVersionUID = -3830415346239101054L; private static final long serialVersionUID = 5808894132916693496L;
/** /**
* Instantiates a new property rules. * Instantiates a new property rules.
@ -51,26 +50,74 @@ public class PropertyRules extends HashMap<String, PropertyRule>
*/ */
public void add(final String path, final String pattern, final PropertyMode mode) 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 * @param path
* @return * the path
* @return the property rule
*/ */
public PropertyRule get(final String path) public PropertyRule find(final String path)
{ {
PropertyRule result; PropertyRule result;
result = super.get(StringUtils.toRootLowerCase(path)); boolean ended = false;
Iterator<PropertyRule> iterator = iterator();
if (result == null) 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);
} }
} }

View file

@ -29,8 +29,8 @@
<a id="emailLink" href="#"><img id="emailLinkImg" src="circle-icons/color/mail.png" class="disabled" title="Courriel de contact/support"/></a> <a id="emailLink" href="#"><img id="emailLinkImg" src="circle-icons/color/mail.png" class="disabled" title="Courriel de contact/support"/></a>
<a id="userDocLink" href="#"><img id="userDocLinkImg" src="circle-icons/color/bookshelf.png" class="disabled" title="Documentation"/></a> <a id="userDocLink" href="#"><img id="userDocLinkImg" src="circle-icons/color/bookshelf.png" class="disabled" title="Documentation"/></a>
<a id="technicalDocLink" href="#"><img id="technicalDocLinkImg" src="circle-icons/color/tools.png" class="disabled" title="Documentation technique"/></a> <a id="technicalDocLink" href="#"><img id="technicalDocLinkImg" src="circle-icons/color/tools.png" class="disabled" title="Documentation technique"/></a>
<a id="rawLink" href="#"><img id="rawLinkImg" src="circle-icons/mono/document.png" title="Fichier propriétés"/></a>
<a id="rawCheckLink" href="#"><img id="rawCheckLinkImg" src="circle-icons/mono/clipboard.png" title="Fichier propriétés analysé"/></a> <a id="rawCheckLink" href="#"><img id="rawCheckLinkImg" src="circle-icons/mono/clipboard.png" title="Fichier propriétés analysé"/></a>
<a id="rawLink" href="#"><img id="rawLinkImg" src="circle-icons/mono/document.png" title="Fichier propriétés"/></a>
</div> </div>
<br/> <br/>
</div> </div>

View file

@ -30,8 +30,8 @@
<a id="emailLink" href="#"><img id="emailLinkImg" src="circle-icons/color/mail.png" class="disabled" title="Courriel de contact/support"/></a> <a id="emailLink" href="#"><img id="emailLinkImg" src="circle-icons/color/mail.png" class="disabled" title="Courriel de contact/support"/></a>
<a id="userDocLink" href="#"><img id="userDocLinkImg" src="circle-icons/color/bookshelf.png" class="disabled" title="Documentation"/></a> <a id="userDocLink" href="#"><img id="userDocLinkImg" src="circle-icons/color/bookshelf.png" class="disabled" title="Documentation"/></a>
<a id="technicalDocLink" href="#"><img id="technicalDocLinkImg" src="circle-icons/color/tools.png" class="disabled" title="Documentation technique"/></a> <a id="technicalDocLink" href="#"><img id="technicalDocLinkImg" src="circle-icons/color/tools.png" class="disabled" title="Documentation technique"/></a>
<a id="rawLink" href="#"><img id="rawLinkImg" src="circle-icons/mono/document.png" title="Fichier propriétés"/></a>
<a id="rawCheckLink" href="#"><img id="rawCheckLinkImg" src="circle-icons/mono/clipboard.png" title="Fichier propriétés analysé"/></a> <a id="rawCheckLink" href="#"><img id="rawCheckLinkImg" src="circle-icons/mono/clipboard.png" title="Fichier propriétés analysé"/></a>
<a id="rawLink" href="#"><img id="rawLinkImg" src="circle-icons/mono/document.png" title="Fichier propriétés"/></a>
</div> </div>
<br/> <br/>
<div>Nombre de services : <span id="serviceCount">n/a</span></div> <div>Nombre de services : <span id="serviceCount">n/a</span></div>

View file

@ -38,8 +38,8 @@
<a id="emailLink" href="#"><img id="emailLinkImg" src="circle-icons/color/mail.png" class="disabled" title="Courriel de contact/support"/></a> <a id="emailLink" href="#"><img id="emailLinkImg" src="circle-icons/color/mail.png" class="disabled" title="Courriel de contact/support"/></a>
<a id="userDocLink" href="#"><img id="userDocLinkImg" src="circle-icons/color/bookshelf.png" class="disabled" title="Documentation"/></a> <a id="userDocLink" href="#"><img id="userDocLinkImg" src="circle-icons/color/bookshelf.png" class="disabled" title="Documentation"/></a>
<a id="technicalDocLink" href="#"><img id="technicalDocLinkImg" src="circle-icons/color/tools.png" class="disabled" title="Documentation technique"/></a> <a id="technicalDocLink" href="#"><img id="technicalDocLinkImg" src="circle-icons/color/tools.png" class="disabled" title="Documentation technique"/></a>
<a id="rawLink" href="#"><img id="rawLinkImg" src="circle-icons/mono/document.png" title="Fichier propriétés"/></a>
<a id="rawCheckLink" href="#"><img id="rawCheckLinkImg" src="circle-icons/mono/clipboard.png" title="Fichier propriétés analysé"/></a> <a id="rawCheckLink" href="#"><img id="rawCheckLinkImg" src="circle-icons/mono/clipboard.png" title="Fichier propriétés analysé"/></a>
<a id="rawLink" href="#"><img id="rawLinkImg" src="circle-icons/mono/document.png" title="Fichier propriétés"/></a>
</div> </div>
<div class="content_infos" style="margin: 5px;"> <div class="content_infos" style="margin: 5px;">
Logiciel : Logiciel :