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 LOGGER = 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        LOGGER.trace("Converting identifier {} from internal to external...", inputId);
045        String result = inputId;
046        for (final String jcrNamespace : jcrNamespacesToRDFNamespaces.keySet()) {
047            LOGGER.trace("Replacing namespace: {} with: {}", jcrNamespace, jcrNamespacesToRDFNamespaces
048                    .get(jcrNamespace));
049            result = result.replace(jcrNamespace, jcrNamespacesToRDFNamespaces.get(jcrNamespace));
050        }
051        LOGGER.trace("Converted identifier {} from internal to external {}...", inputId, result);
052        return result;
053    }
054
055    /*
056     * (non-Javadoc)
057     * @see
058     * org.fcrepo.kernel.identifiers.InternalIdentifierConverter#doBackward
059     * (java.lang.String)
060     */
061    @Override
062    protected String doBackward(final String b) {
063        LOGGER.trace("Converting identifier from external to internal...");
064        String result = b;
065        for (final String rdfNamespace : rdfNamespacesToJcrNamespaces.keySet()) {
066            LOGGER.trace("Replacing namespace: {} with: {}", rdfNamespace, rdfNamespacesToJcrNamespaces
067                    .get(rdfNamespace));
068            result = result.replace(rdfNamespace, rdfNamespacesToJcrNamespaces.get(rdfNamespace));
069        }
070        return result;
071    }
072}