001/*
002 * Copyright 2015 DuraSpace, Inc.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.fcrepo.migration.foxml;
017
018import java.io.File;
019import java.io.FileInputStream;
020import java.io.IOException;
021import java.util.Properties;
022
023import com.hp.hpl.jena.update.UpdateRequest;
024
025/**
026 * Utility bean to set namespace prefixes in a SPARQL update.
027 * @author danny
028 *
029 */
030public class NamespacePrefixMapper {
031
032    private final Properties namespacePrefixes;
033
034    /**
035     * Constructor.
036     * @param namespaceFile Namespace properties file that gets injected in via Spring
037     * @throws IOException Issues loading the properties file.
038     */
039    public NamespacePrefixMapper(final File namespaceFile) throws IOException {
040        namespacePrefixes = new Properties();
041        try (final FileInputStream namespaceInputStream = new FileInputStream(namespaceFile)) {
042            namespacePrefixes.load(namespaceInputStream);
043        }
044    }
045
046    /**
047     * Declares all the namespace prefixes provided in the properties file.
048     * @param updateRequest SPARQL update query that needs declared prefixes
049     */
050    public void setPrefixes(final UpdateRequest updateRequest) {
051        namespacePrefixes.forEach((prefix,namespace) -> {
052            updateRequest.setPrefix((String) prefix, (String) namespace);
053        });
054    }
055}