RuleInfo.java: Added RuleInfo class for structured results | ValidatorController.java: Updated how results are returned (more structured) & Added fair guidelines (as separate option and called with any other guideline) & Added unused and untested method for validating uploaded file.

This commit is contained in:
Konstantina Galouni 2022-11-30 12:28:56 +02:00
parent 85494a6dac
commit e28198e1d7
3 changed files with 204 additions and 10 deletions

View File

@ -4,7 +4,7 @@
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<groupId>eu.dnetlib</groupId>
<artifactId>validator-api</artifactId>
<version>1.0-SNAPSHOT</version>
<name>uoa-validator-api</name>

View File

@ -2,19 +2,20 @@ package eu.dnetlib.validatorapi.controllers;
import eu.dnetlib.validator2.validation.XMLApplicationProfile;
import eu.dnetlib.validator2.validation.guideline.Guideline;
import eu.dnetlib.validator2.validation.guideline.openaire.AbstractOpenAireProfile;
import eu.dnetlib.validator2.validation.guideline.openaire.DataArchiveGuidelinesV2Profile;
import eu.dnetlib.validator2.validation.guideline.openaire.LiteratureGuidelinesV3Profile;
import eu.dnetlib.validator2.validation.guideline.openaire.LiteratureGuidelinesV4Profile;
import eu.dnetlib.validator2.validation.guideline.StandardResult;
import eu.dnetlib.validator2.validation.guideline.openaire.*;
import eu.dnetlib.validatorapi.entities.RuleInfo;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.w3c.dom.Document;
import org.xml.sax.InputSource;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.StringReader;
import java.io.*;
import java.nio.file.Files;
import java.util.*;
import java.util.stream.Collectors;
@ -24,8 +25,117 @@ public class ValidatorController {
private final Logger log = LogManager.getLogger(this.getClass());
@RequestMapping(value = {"/validate"}, method = RequestMethod.POST)
public List<String> validate(@RequestParam(name = "guidelines") String guidelinesProfileName, @RequestBody String xml) {
// public List<String> validate(@RequestParam(name = "guidelines") String guidelinesProfileName, @RequestBody String xml) {
public List<RuleInfo> validate(@RequestParam(name = "guidelines") String guidelinesProfileName, @RequestBody String xml) {
log.debug(xml);
List<String> resultMessages = new ArrayList<>();
List<RuleInfo> resultRules = new ArrayList<>();
AbstractOpenAireProfile profile = null;
AbstractOpenAireProfile fairProfile = null;
if(guidelinesProfileName.equals("dataArchiveGuidelinesV2Profile")) {
profile = new DataArchiveGuidelinesV2Profile();
fairProfile = new FAIR_Data_GuidelinesProfile();
} else if(guidelinesProfileName.equals("literatureGuidelinesV3Profile")) {
profile = new LiteratureGuidelinesV3Profile();
fairProfile = new FAIR_Data_GuidelinesProfile();
} else if(guidelinesProfileName.equals("literatureGuidelinesV4Profile")) {
profile = new LiteratureGuidelinesV4Profile();
fairProfile = new FAIR_Data_GuidelinesProfile();
} else if(guidelinesProfileName.equals("fairDataGuidelinesProfile")) {
fairProfile = new FAIR_Data_GuidelinesProfile();
}
if(profile == null && fairProfile == null) {
log.error("Exception: No valid guidelines");
new Exception("No valid guidelines");
}
Map<String, Double> scorePerDoc = new LinkedHashMap<>();
// String file = "/home/konstantina/projects/validator-api/src/main/resources/Record_21811.xml";
XMLApplicationProfile.ValidationResult result = null;
try {
// log.debug("Processing " + file);
// Document doc = DOMBuilder.parse(new FileReader(file), false, true, true);
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(new InputSource(new StringReader(xml)));
if(profile != null) {
log.debug("Max score: " + profile.maxScore());
result = profile.validate("id", doc);
Date date = new Date();
scorePerDoc.put("input-" + date.toString(), result.score());
Map<String, Guideline.Result> results = result.results();
log.debug("\n\nPrinting results...");
for (Map.Entry entry : results.entrySet()) {
log.debug(entry.getKey() + " = " + entry.getValue());
resultMessages.add(entry.getKey() + " = " + entry.getValue());
RuleInfo ruleInfo = new RuleInfo();
ruleInfo.setName(entry.getKey().toString());
Guideline.Result res = (Guideline.Result) entry.getValue();
ruleInfo.setErrors((List<String>) res.errors());
ruleInfo.setWarnings((List<String>) res.warnings());
ruleInfo.setInternalError(res.internalError());
ruleInfo.setScore(res.score());
ruleInfo.setStatus(res.status());
resultRules.add(ruleInfo);
}
}
if(fairProfile != null) {
log.debug("Max score: " + fairProfile.maxScore());
result = fairProfile.validate("id", doc);
Date date = new Date();
scorePerDoc.put("input-"+date.toString(), result.score());
Map<String, Guideline.Result> results = result.results();
log.debug("\n\nPrinting FAIR results...");
for (Map.Entry entry : results.entrySet()) {
if (!entry.getValue().toString().contains("eu.dnetlib.validator2")) {
log.debug(entry.getKey() + " = " + entry.getValue());
resultMessages.add(entry.getKey() + " = " + entry.getValue());
RuleInfo ruleInfo = new RuleInfo();
ruleInfo.setName(entry.getKey().toString());
Guideline.Result res = (Guideline.Result) entry.getValue();
ruleInfo.setErrors((List<String>) res.errors());
ruleInfo.setWarnings((List<String>) res.warnings());
ruleInfo.setInternalError(res.internalError());
ruleInfo.setScore(res.score());
ruleInfo.setStatus(res.status());
resultRules.add(ruleInfo);
}
}
}
}
catch(Exception e) {
log.debug(e.getMessage());
log.debug(e);
e.printStackTrace();
resultMessages.add("Error processing input");
}
String printout = scorePerDoc.entrySet().stream().
map(entry -> {
resultMessages.add("score: " + entry.getValue());
return entry.getValue() + ": " + entry.getKey();
}).collect(Collectors.joining("\n"));
log.debug("\n\nPrinting scorePerDoc...");
log.debug(printout);
// return resultMessages;
return resultRules;
}
@RequestMapping(method = RequestMethod.POST, value = {"/validate-file"})
public List<String> validateXmlImport(@RequestParam("guidelines") String guidelinesProfileName, @RequestParam("file") MultipartFile multipartFile) throws IOException {
String filePath = "src/main/resources/xmlFile.tmp";
File file = new File(filePath);
multipartFile.transferTo(file);
List<String> resultMessages = new ArrayList<>();
AbstractOpenAireProfile profile = null;
@ -45,10 +155,10 @@ public class ValidatorController {
XMLApplicationProfile.ValidationResult result = null;
try {
// log.debug("Processing " + file);
// Document doc = DOMBuilder.parse(new FileReader(file), false, true, true);
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(new InputSource(new StringReader(xml)));
// Document doc = db.parse(new InputSource(new StringReader(xml)));
Document doc = db.parse(filePath);
result = profile.validate("id", doc);
Date date = new Date();
scorePerDoc.put("input-"+date.toString(), result.score());
@ -73,7 +183,9 @@ public class ValidatorController {
log.debug("\n\nPrinting scorePerDoc...");
log.debug(printout);
Files.deleteIfExists(file.toPath());
return resultMessages;
}
}
}

View File

@ -0,0 +1,82 @@
package eu.dnetlib.validatorapi.entities;
import eu.dnetlib.validator2.engine.Status;
import eu.dnetlib.validator2.validation.guideline.Guideline;
import eu.dnetlib.validator2.validation.guideline.StandardResult;
import java.util.List;
public class RuleInfo {
String name;
String description;
// Guideline.Result result;
List<String> warnings;
List<String> errors;
String internalError;
Status status;
int score;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public List<String> getWarnings() {
return warnings;
}
public void setWarnings(List<String> warnings) {
this.warnings = warnings;
}
public List<String> getErrors() {
return errors;
}
public void setErrors(List<String> errors) {
this.errors = errors;
}
public String getInternalError() {
return internalError;
}
public void setInternalError(String internalError) {
this.internalError = internalError;
}
public Status getStatus() {
return status;
}
public void setStatus(Status status) {
this.status = status;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
// public StandardResult getResult() {
// return result;
// }
//
// public void setResult(StandardResult result) {
// this.result = result;
// }
}