use of eu.europeana.metis.dereference.vocimport.exception.VocabularyImportException in project metis-framework by europeana.
the class VocabularyCollectionMavenRule method execute.
@Override
public void execute(EnforcerRuleHelper enforcerRuleHelper) throws EnforcerRuleException {
// Get the environment: the log and the project.
final Log log = enforcerRuleHelper.getLog();
final MavenProject project;
try {
project = enforcerRuleHelper.getComponent(MavenProject.class);
} catch (ComponentLookupException e) {
throw new EnforcerRuleException("Could not retrieve the project properties.", e);
}
// Get the vocabulary directory file
final Path baseDirectory = project.getBasedir().toPath();
final Path vocabularyDirectory = baseDirectory.resolve(vocabularyDirectoryFile);
// Prepare validation
final VocabularyCollectionImporter importer = new VocabularyCollectionImporterFactory().createImporter(baseDirectory, vocabularyDirectory);
final VocabularyCollectionValidatorImpl validator = new VocabularyCollectionValidatorImpl(importer, lenientOnLackOfExamples, lenientOnMappingTestFailures, lenientOnExampleRetrievalFailures);
log.info("");
log.info("Validating vocabulary collection: " + importer.getDirectoryLocation().toString());
// Perform validation
try {
validator.validate(vocabulary -> log.info(" Vocabulary found: " + vocabulary.getName()), log::warn);
} catch (VocabularyImportException e) {
log.error(e.getMessage());
throw new EnforcerRuleException("Vocabulary collection validation failed.", e);
}
// Done
log.info("Finished validating vocabulary collection.");
}
use of eu.europeana.metis.dereference.vocimport.exception.VocabularyImportException in project metis-framework by europeana.
the class VocabularyCollectionValidatorImpl method testExample.
private void testExample(IncomingRecordToEdmConverter converter, String example, String suffix, boolean isCounterExample, String readableMetadataLocation, Consumer<String> warningReceiver) throws VocabularyImportException {
// Retrieve the example - is not null.
final String exampleContent;
try {
exampleContent = new RdfRetriever().retrieve(example, suffix);
} catch (IOException | URISyntaxException e) {
final String message = getTestErrorMessage(example, isCounterExample, readableMetadataLocation, "could not be retrieved", e);
processTestError(message, lenientOnExampleRetrievalFailures, warningReceiver, e);
return;
}
// Convert the example
final String result;
try {
result = converter.convert(exampleContent, example);
} catch (TransformerException e) {
final String message = getTestErrorMessage(example, isCounterExample, readableMetadataLocation, "could not be mapped", e);
processTestError(message, lenientOnMappingTestFailures, warningReceiver, e);
return;
}
// Check whether the example yielded a mapped entity or not
if (StringUtils.isNotBlank(result) && isCounterExample) {
final String message = getTestErrorMessage(example, isCounterExample, readableMetadataLocation, "yielded a mapped result, but is expected not to", null);
processTestError(message, lenientOnMappingTestFailures, warningReceiver, null);
} else if (StringUtils.isBlank(result) && !isCounterExample) {
final String message = getTestErrorMessage(example, isCounterExample, readableMetadataLocation, "did not yield a mapped result, but is expected to", null);
processTestError(message, lenientOnMappingTestFailures, warningReceiver, null);
}
// Check whether the example yielded valid XML
if (StringUtils.isNotBlank(result)) {
try {
EnrichmentBaseConverter.convertToEnrichmentBase(result);
} catch (JAXBException e) {
final String message = getTestErrorMessage(example, isCounterExample, readableMetadataLocation, "did not yield a valid XML", e);
throw new VocabularyImportException(message, e);
}
}
}
use of eu.europeana.metis.dereference.vocimport.exception.VocabularyImportException in project metis-framework by europeana.
the class VocabularyCollectionImporterImpl method loadVocabulary.
private Vocabulary loadVocabulary(Location metadataLocation, Location mappingLocation, ObjectMapper mapper) throws VocabularyImportException {
// Read the metadata file.
final VocabularyMetadata metadata;
try (final InputStream input = metadataLocation.read()) {
metadata = mapper.readValue(input, VocabularyMetadata.class);
} catch (IOException e) {
throw new VocabularyImportException("Could not read vocabulary metadata at [" + metadataLocation + "].", e);
}
// Read the mapping file.
final String mapping;
try (final InputStream input = mappingLocation.read()) {
mapping = IOUtils.toString(input, StandardCharsets.UTF_8);
} catch (IOException e) {
throw new VocabularyImportException("Could not read vocabulary mapping at [" + mappingLocation + "].", e);
}
// Compile the vocabulary.
return Vocabulary.builder().setName(metadata.getName()).setTypes(metadata.getTypes()).setPaths(metadata.getPaths()).setParentIterations(metadata.getParentIterations()).setSuffix(metadata.getSuffix()).setExamples(metadata.getExamples()).setCounterExamples(metadata.getCounterExamples()).setTransformation(mapping).setReadableMetadataLocation(metadataLocation.toString()).setReadableMappingLocation(mappingLocation.toString()).build();
}
use of eu.europeana.metis.dereference.vocimport.exception.VocabularyImportException in project metis-framework by europeana.
the class VocabularyCollectionImporterImpl method importVocabularies.
@Override
public Iterable<VocabularyLoader> importVocabularies() throws VocabularyImportException {
// Obtain the directory entries.
final ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
final VocabularyDirectoryEntry[] directoryEntries;
try (final InputStream input = directoryLocation.read()) {
directoryEntries = mapper.readValue(input, VocabularyDirectoryEntry[].class);
} catch (IOException e) {
throw new VocabularyImportException("Could not read vocabulary directory at [" + directoryLocation + "].", e);
}
// Compile the vocabulary loaders
final List<VocabularyLoader> result = new ArrayList<>(directoryEntries.length);
for (VocabularyDirectoryEntry entry : directoryEntries) {
final Location metadataLocation = directoryLocation.resolve(entry.getMetadata());
final Location mappingLocation = directoryLocation.resolve(entry.getMapping());
result.add(() -> loadVocabulary(metadataLocation, mappingLocation, mapper));
}
// Done
return result;
}
Aggregations