001/**
002 * The contents of this file are subject to the license and copyright
003 * detailed in the LICENSE and NOTICE files at the root of the source
004 * tree.
005 *
006 */
007package org.fcrepo.migration.foxml;
008
009import org.apache.jena.update.UpdateRequest;
010
011import java.io.BufferedInputStream;
012import java.io.File;
013import java.io.FileInputStream;
014import java.io.IOException;
015import java.io.InputStream;
016import java.util.Properties;
017
018/**
019 * Utility bean to set namespace prefixes in a SPARQL update.
020 * @author danny
021 *
022 */
023public class NamespacePrefixMapper {
024
025    private final Properties namespacePrefixes;
026
027    /**
028     * Constructor.
029     * @param namespaceFile Namespace properties file that gets injected in via Spring
030     * @throws IOException Issues loading the properties file.
031     */
032    public NamespacePrefixMapper(final File namespaceFile) throws IOException {
033        namespacePrefixes = new Properties();
034        try (final InputStream namespaceInputStream = new BufferedInputStream(new FileInputStream(namespaceFile))) {
035            namespacePrefixes.load(namespaceInputStream);
036        }
037    }
038
039    /**
040     * Declares all the namespace prefixes provided in the properties file.
041     * @param updateRequest SPARQL update query that needs declared prefixes
042     */
043    public void setPrefixes(final UpdateRequest updateRequest) {
044        namespacePrefixes.forEach((prefix,namespace) -> {
045            updateRequest.setPrefix((String) prefix, (String) namespace);
046        });
047    }
048}