Skip to content

Commit 5f45f76

Browse files
committed
Pull request #275: Tolerate wrongly reported content types for remote shape files if file name can be used
Merge in ITB/shacl-validator from development to master * commit '28c6aa12dac195f269e1c23da3695e520d168926': Tolerate wrongly reported content types for remote shape files if file name can be used
2 parents 4bcad85 + 28c6aa1 commit 5f45f76

File tree

2 files changed

+31
-3
lines changed

2 files changed

+31
-3
lines changed

shaclvalidator-common/src/main/java/eu/europa/ec/itb/shacl/util/ShaclValidatorUtils.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,19 @@
1919
import eu.europa.ec.itb.shacl.validation.FileManager;
2020
import eu.europa.ec.itb.shacl.validation.ReportSpecs;
2121
import eu.europa.ec.itb.shacl.validation.SHACLReportHandler;
22+
import eu.europa.ec.itb.validation.commons.FileInfo;
2223
import eu.europa.ec.itb.validation.commons.LocalisationHelper;
2324
import eu.europa.ec.itb.validation.commons.ReportPair;
25+
import org.apache.jena.atlas.web.ContentType;
2426
import org.apache.jena.query.Query;
2527
import org.apache.jena.query.QueryExecution;
2628
import org.apache.jena.query.QueryExecutionFactory;
2729
import org.apache.jena.query.QueryFactory;
2830
import org.apache.jena.rdf.model.Model;
2931
import org.apache.jena.rdf.model.RDFNode;
3032
import org.apache.jena.rdf.model.Statement;
33+
import org.apache.jena.riot.Lang;
34+
import org.apache.jena.riot.RDFLanguages;
3135
import org.slf4j.Logger;
3236
import org.slf4j.LoggerFactory;
3337
import org.springframework.util.MimeType;
@@ -115,6 +119,31 @@ public static String handleEquivalentContentSyntaxes(String contentSyntax) {
115119
return contentSyntax;
116120
}
117121

122+
/**
123+
* Determine the RDF language for the provided file.
124+
*
125+
* @param fileInfo The file information.
126+
* @return The language.
127+
*/
128+
public static Lang determineRdfLanguage(FileInfo fileInfo) {
129+
// First check content type.
130+
String contentType = handleEquivalentContentSyntaxes(fileInfo.getType());
131+
if (isRdfContentSyntax(contentType)) {
132+
// This is an RDF content type.
133+
return RDFLanguages.contentTypeToLang(contentType);
134+
} else {
135+
// Not an RDF content type.
136+
ContentType guessedContentType = RDFLanguages.guessContentType(fileInfo.getFile().getName());
137+
if (guessedContentType != null) {
138+
// We guessed the content type from the file name.
139+
return RDFLanguages.contentTypeToLang(guessedContentType);
140+
} else {
141+
// Use the original content type for a best-effort guess.
142+
return RDFLanguages.contentTypeToLang(fileInfo.getType());
143+
}
144+
}
145+
}
146+
118147
/**
119148
* Make sure badly reported content types are correctly handled.
120149
*

shaclvalidator-common/src/main/java/eu/europa/ec/itb/shacl/validation/SHACLValidator.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,7 @@
6666
import java.io.*;
6767
import java.util.*;
6868

69-
import static eu.europa.ec.itb.shacl.util.ShaclValidatorUtils.getStatementSafe;
70-
import static eu.europa.ec.itb.shacl.util.ShaclValidatorUtils.handleEquivalentContentSyntaxes;
69+
import static eu.europa.ec.itb.shacl.util.ShaclValidatorUtils.*;
7170
import static eu.europa.ec.itb.shacl.validation.SHACLResources.VALIDATION_REPORT;
7271
import static org.apache.jena.riot.lang.LangJSONLD11.JSONLD_OPTIONS;
7372

@@ -414,7 +413,7 @@ private Model getShapesModel(List<FileInfo> shaclFiles) {
414413
if (specs.isLogProgress()) {
415414
LOG.info("Validating against [{}]", shaclFile.getFile().getName());
416415
}
417-
Lang rdfLanguage = processRdfLanguage(RDFLanguages.contentTypeToLang(handleEquivalentContentSyntaxes(shaclFile.getType())));
416+
Lang rdfLanguage = processRdfLanguage(determineRdfLanguage(shaclFile));
418417
if (rdfLanguage == null) {
419418
throw new ValidatorException("validator.label.exception.unableToDetermineShaclContentType");
420419
}

0 commit comments

Comments
 (0)