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.kernel.impl.rdf.impl;
017
018import static com.google.common.base.Preconditions.checkNotNull;
019import static com.hp.hpl.jena.graph.NodeFactory.createLiteral;
020import static com.hp.hpl.jena.graph.NodeFactory.createURI;
021import static com.hp.hpl.jena.graph.Triple.create;
022import static com.hp.hpl.jena.vocabulary.RDF.type;
023import static org.fcrepo.kernel.RdfLexicon.HAS_NAMESPACE_PREFIX;
024import static org.fcrepo.kernel.RdfLexicon.HAS_NAMESPACE_URI;
025import static org.fcrepo.kernel.RdfLexicon.VOAF_VOCABULARY;
026import static org.fcrepo.kernel.impl.rdf.JcrRdfTools.getRDFNamespaceForJcrNamespace;
027import static org.slf4j.LoggerFactory.getLogger;
028
029import javax.jcr.NamespaceRegistry;
030import javax.jcr.RepositoryException;
031import javax.jcr.Session;
032
033import org.fcrepo.kernel.utils.iterators.RdfStream;
034import org.slf4j.Logger;
035
036import com.google.common.collect.ImmutableCollection;
037import com.google.common.collect.ImmutableMap;
038import com.google.common.collect.ImmutableSet;
039import com.hp.hpl.jena.graph.Node;
040import com.hp.hpl.jena.graph.Triple;
041
042/**
043 * An {@link RdfStream} that holds the namespace mappings for serializations,
044 * as well as {@link Triple}s describing those namespaces.
045 *
046 * @author ajs6f
047 * @since Oct 9, 2013
048 */
049public class NamespaceRdfContext extends RdfStream {
050
051    private static final Logger LOGGER = getLogger(NamespaceRdfContext.class);
052
053    /**
054     * Default constructor. Loads context with RDF describing namespaces in
055     * scope in the repository.
056     *
057     * @param session the session
058     * @throws RepositoryException if repository exception occurred
059     */
060    public NamespaceRdfContext(final Session session) throws RepositoryException {
061        super();
062        final NamespaceRegistry namespaceRegistry =
063            session.getWorkspace().getNamespaceRegistry();
064        checkNotNull(namespaceRegistry,
065                "Couldn't find namespace registry in repository!");
066
067        final ImmutableMap.Builder<String, String> namespaces =
068            ImmutableMap.builder();
069        final ImmutableCollection.Builder<Triple> nsTriples =
070            ImmutableSet.builder();
071        for (String prefix : namespaceRegistry.getPrefixes()) {
072            if (!prefix.isEmpty() && !prefix.equals("jcr")) {
073                final String nsURI = namespaceRegistry.getURI(prefix);
074                LOGGER.trace(
075                        "Discovered namespace prefix \"{}\" with URI \"{}\"",
076                        prefix, nsURI);
077                final String rdfNsUri = getRDFNamespaceForJcrNamespace(nsURI);
078                // first, let's put the namespace in context
079                namespaces.put(prefix, rdfNsUri);
080                LOGGER.trace("Added namespace prefix \"{}\" with URI \"{}\"",
081                        prefix, rdfNsUri);
082                final Node nsSubject = createURI(rdfNsUri);
083                // now, some triples describing this namespace
084                nsTriples.add(create(nsSubject, type.asNode(), VOAF_VOCABULARY
085                        .asNode()));
086                nsTriples.add(create(nsSubject, HAS_NAMESPACE_PREFIX.asNode(),
087                        createLiteral(prefix)));
088                nsTriples.add(create(nsSubject, HAS_NAMESPACE_URI.asNode(),
089                        createLiteral(rdfNsUri)));
090            }
091        }
092        concat(nsTriples.build()).namespaces(namespaces.build());
093    }
094}