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.identifiers;
017
018import static org.fcrepo.kernel.impl.rdf.JcrRdfTools.jcrNamespacesToRDFNamespaces;
019import static org.fcrepo.kernel.impl.rdf.JcrRdfTools.rdfNamespacesToJcrNamespaces;
020import static org.slf4j.LoggerFactory.getLogger;
021
022import org.fcrepo.kernel.identifiers.InternalIdentifierConverter;
023import org.slf4j.Logger;
024
025/**
026 * A simple {@link InternalIdentifierConverter} that replaces internal JCR
027 * namespaces with external namespaces, and replaces the term for content.
028 *
029 * @author ajs6f
030 * @since Apr 1, 2014
031 */
032public class NamespaceConverter extends InternalIdentifierConverter {
033
034    private static final Logger log = getLogger(NamespaceConverter.class);
035
036    /*
037     * (non-Javadoc)
038     * @see
039     * org.fcrepo.kernel.identifiers.InternalIdentifierConverter#doForward(
040     * java.lang.String)
041     */
042    @Override
043    protected String doForward(final String inputId) {
044        log.trace("Converting identifier {} from internal to external...", inputId);
045        String result = inputId;
046        for (final String jcrNamespace : jcrNamespacesToRDFNamespaces.keySet()) {
047            log.trace("Replacing namespace: {} with: {}", jcrNamespace, jcrNamespacesToRDFNamespaces.get(jcrNamespace));
048            result = result.replace(jcrNamespace, jcrNamespacesToRDFNamespaces.get(jcrNamespace));
049        }
050        log.trace("Converted identifier {} from internal to external {}...", inputId, result);
051        return result;
052    }
053
054    /*
055     * (non-Javadoc)
056     * @see
057     * org.fcrepo.kernel.identifiers.InternalIdentifierConverter#doBackward
058     * (java.lang.String)
059     */
060    @Override
061    protected String doBackward(final String b) {
062        log.trace("Converting identifier from external to internal...");
063        String result = b;
064        for (final String rdfNamespace : rdfNamespacesToJcrNamespaces.keySet()) {
065            log.trace("Replacing namespace: {} with: {}", rdfNamespace, rdfNamespacesToJcrNamespaces.get(rdfNamespace));
066            result = result.replace(rdfNamespace, rdfNamespacesToJcrNamespaces.get(rdfNamespace));
067        }
068        return result;
069    }
070}