Search in sources :

Example 1 with VocabularyImportException

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.");
}
Also used : Path(java.nio.file.Path) MavenProject(org.apache.maven.project.MavenProject) Log(org.apache.maven.plugin.logging.Log) EnforcerRuleException(org.apache.maven.enforcer.rule.api.EnforcerRuleException) ComponentLookupException(org.codehaus.plexus.component.repository.exception.ComponentLookupException) VocabularyImportException(eu.europeana.metis.dereference.vocimport.exception.VocabularyImportException)

Example 2 with VocabularyImportException

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);
        }
    }
}
Also used : RdfRetriever(eu.europeana.metis.dereference.RdfRetriever) JAXBException(javax.xml.bind.JAXBException) VocabularyImportException(eu.europeana.metis.dereference.vocimport.exception.VocabularyImportException) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) TransformerException(javax.xml.transform.TransformerException)

Example 3 with VocabularyImportException

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();
}
Also used : InputStream(java.io.InputStream) VocabularyMetadata(eu.europeana.metis.dereference.vocimport.model.VocabularyMetadata) VocabularyImportException(eu.europeana.metis.dereference.vocimport.exception.VocabularyImportException) IOException(java.io.IOException)

Example 4 with VocabularyImportException

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;
}
Also used : InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) YAMLFactory(com.fasterxml.jackson.dataformat.yaml.YAMLFactory) VocabularyDirectoryEntry(eu.europeana.metis.dereference.vocimport.model.VocabularyDirectoryEntry) VocabularyLoader(eu.europeana.metis.dereference.vocimport.model.VocabularyLoader) VocabularyImportException(eu.europeana.metis.dereference.vocimport.exception.VocabularyImportException) IOException(java.io.IOException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Location(eu.europeana.metis.dereference.vocimport.model.Location)

Aggregations

VocabularyImportException (eu.europeana.metis.dereference.vocimport.exception.VocabularyImportException)4 IOException (java.io.IOException)3 InputStream (java.io.InputStream)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 YAMLFactory (com.fasterxml.jackson.dataformat.yaml.YAMLFactory)1 RdfRetriever (eu.europeana.metis.dereference.RdfRetriever)1 Location (eu.europeana.metis.dereference.vocimport.model.Location)1 VocabularyDirectoryEntry (eu.europeana.metis.dereference.vocimport.model.VocabularyDirectoryEntry)1 VocabularyLoader (eu.europeana.metis.dereference.vocimport.model.VocabularyLoader)1 VocabularyMetadata (eu.europeana.metis.dereference.vocimport.model.VocabularyMetadata)1 URISyntaxException (java.net.URISyntaxException)1 Path (java.nio.file.Path)1 ArrayList (java.util.ArrayList)1 JAXBException (javax.xml.bind.JAXBException)1 TransformerException (javax.xml.transform.TransformerException)1 EnforcerRuleException (org.apache.maven.enforcer.rule.api.EnforcerRuleException)1 Log (org.apache.maven.plugin.logging.Log)1 MavenProject (org.apache.maven.project.MavenProject)1 ComponentLookupException (org.codehaus.plexus.component.repository.exception.ComponentLookupException)1