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 */
006package org.fcrepo.kernel.api.rdf;
007
008import java.io.File;
009import java.io.IOException;
010import java.util.HashMap;
011import java.util.Map;
012
013import org.fcrepo.kernel.api.utils.AutoReloadingConfiguration;
014
015import com.fasterxml.jackson.core.type.TypeReference;
016import com.fasterxml.jackson.databind.ObjectMapper;
017import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
018
019/**
020 * Registry of RDF namespaces
021 *
022 * @author bbpennel
023 */
024public class RdfNamespaceRegistry extends AutoReloadingConfiguration {
025
026    private Map<String, String> namespaces;
027
028    /**
029     * Load the namespace prefix to URI configuration file
030     */
031    @Override
032    protected synchronized void loadConfiguration() throws IOException {
033        final ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
034        final TypeReference<HashMap<String, String>> typeRef = new TypeReference<HashMap<String, String>>() {};
035        namespaces = mapper.readValue(new File(configPath), typeRef);
036    }
037
038    /**
039     * Get the mapping of namespace prefixes to URIs
040     *
041     * @return map of namespace prefixes to URIs, or an empty map if no mapping was provided.
042     */
043    public Map<String, String> getNamespaces() {
044        if (namespaces == null) {
045            namespaces = new HashMap<>();
046        }
047        return namespaces;
048    }
049
050    /**
051     * Set the mapping of namespace prefixes to URIs
052     *
053     * @param namespaces mapping of namespaces
054     */
055    public void setNamespaces(final Map<String, String> namespaces) {
056        this.namespaces = namespaces;
057    }
058}