001/*
002 * Licensed to DuraSpace under one or more contributor license agreements.
003 * See the NOTICE file distributed with this work for additional information
004 * regarding copyright ownership.
005 *
006 * DuraSpace licenses this file to you under the Apache License,
007 * Version 2.0 (the "License"); you may not use this file except in
008 * compliance with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.fcrepo.kernel.modeshape.identifiers;
019
020import static org.fcrepo.kernel.modeshape.rdf.JcrRdfTools.jcrNamespacesToRDFNamespaces;
021import static org.fcrepo.kernel.modeshape.rdf.JcrRdfTools.rdfNamespacesToJcrNamespaces;
022import static org.slf4j.LoggerFactory.getLogger;
023
024import org.fcrepo.kernel.api.identifiers.InternalIdentifierConverter;
025import org.slf4j.Logger;
026
027/**
028 * A simple {@link InternalIdentifierConverter} that replaces internal JCR
029 * namespaces with external namespaces, and replaces the term for content.
030 *
031 * @author ajs6f
032 * @since Apr 1, 2014
033 */
034public class NamespaceConverter extends InternalIdentifierConverter {
035
036    private static final Logger LOGGER = getLogger(NamespaceConverter.class);
037
038    /*
039     * (non-Javadoc)
040     * @see
041     * org.fcrepo.kernel.api.identifiers.InternalIdentifierConverter#doForward(
042     * java.lang.String)
043     */
044    @Override
045    protected String doForward(final String inputId) {
046        LOGGER.trace("Converting identifier {} from internal to external...", inputId);
047        String result = inputId;
048        for (final String jcrNamespace : jcrNamespacesToRDFNamespaces.keySet()) {
049            LOGGER.trace("Replacing namespace: {} with: {}", jcrNamespace, jcrNamespacesToRDFNamespaces
050                    .get(jcrNamespace));
051            result = result.replace(jcrNamespace, jcrNamespacesToRDFNamespaces.get(jcrNamespace));
052        }
053        LOGGER.trace("Converted identifier {} from internal to external {}...", inputId, result);
054        return result;
055    }
056
057    /*
058     * (non-Javadoc)
059     * @see
060     * org.fcrepo.kernel.api.identifiers.InternalIdentifierConverter#doBackward
061     * (java.lang.String)
062     */
063    @Override
064    protected String doBackward(final String b) {
065        LOGGER.trace("Converting identifier from external to internal...");
066        String result = b;
067        for (final String rdfNamespace : rdfNamespacesToJcrNamespaces.keySet()) {
068            LOGGER.trace("Replacing namespace: {} with: {}", rdfNamespace, rdfNamespacesToJcrNamespaces
069                    .get(rdfNamespace));
070            result = result.replace(rdfNamespace, rdfNamespacesToJcrNamespaces.get(rdfNamespace));
071        }
072        return result;
073    }
074}