Skip to content

Commit

Permalink
More translators and unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ajs6f committed Apr 2, 2014
1 parent 01b07c9 commit bca6ac2
Show file tree
Hide file tree
Showing 5 changed files with 258 additions and 16 deletions.
Expand Up @@ -44,18 +44,16 @@ public class ExternalIdentifierTranslator extends IdentifierTranslator<Resource>
@Inject
private List<InternalIdentifierTranslator> translationChain;

private Converter<String, String> accumulatedForwardTranslator;

private Converter<String, String> accumulatedReverseTranslator;
private Converter<String, String> forward, reverse = identity();

@Override
protected Resource doForward(final String a) {
return doRdfForward(accumulatedForwardTranslator.convert(a));
return doRdfForward(forward.convert(a));
}

@Override
protected String doBackward(final Resource a) {
return accumulatedReverseTranslator.convert(doRdfBackward(a));
return reverse.convert(doRdfBackward(a));
}

protected Resource doRdfForward(final String a) {
Expand All @@ -67,29 +65,25 @@ protected String doRdfBackward(final Resource a) {
}

/**
* We accumulate the translators once and store the resulting calculation.
* We fold the list of translators once in each direction and store the
* resulting calculation.
*/
@PostConstruct
public void simpleFoldLikeAccumulation() {
Converter<String, String> accumulatedForward = identity();
public void accumulateTranslations() {
for (final InternalIdentifierTranslator t : translationChain) {
accumulatedForward = accumulatedForward.andThen(t);
forward = forward.andThen(t);
}
accumulatedForwardTranslator = accumulatedForward;
Converter<String, String> accumulatedReverse = identity();
for (final InternalIdentifierTranslator t : Lists.reverse(translationChain)) {
accumulatedReverse = accumulatedReverse.andThen(t.reverse());
reverse = reverse.andThen(t.reverse());
}
accumulatedReverseTranslator = accumulatedReverse;
}


/**
* @param chain the translationChain to use
* @param chain the translation chain to use
*/
public void setTranslationChain(final List<InternalIdentifierTranslator> chain) {
this.translationChain = chain;
simpleFoldLikeAccumulation();
accumulateTranslations();
}

}
@@ -0,0 +1,80 @@
/**
* Copyright 2013 DuraSpace, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
*
*/

package org.fcrepo.kernel.identifiers;

import static org.fcrepo.jcr.FedoraJcrTypes.FCR_CONTENT;
import static org.fcrepo.kernel.rdf.JcrRdfTools.jcrNamespacesToRDFNamespaces;
import static org.fcrepo.kernel.rdf.JcrRdfTools.rdfNamespacesToJcrNamespaces;
import static org.modeshape.jcr.api.JcrConstants.JCR_CONTENT;
import static org.slf4j.LoggerFactory.getLogger;

import org.slf4j.Logger;

/**
* A simple {@link InternalIdentifierTranslator} that replaces internal JCR
* namespaces with external namespaces, and replaces the term for content.
*
* @author ajs6f
* @date Apr 1, 2014
*/
public class NamespaceTranslator extends InternalIdentifierTranslator {

private static final Logger log = getLogger(NamespaceTranslator.class);

/*
* (non-Javadoc)
* @see
* org.fcrepo.kernel.identifiers.InternalIdentifierTranslator#doForward(
* java.lang.String)
*/
@Override
protected String doForward(final String a) {
log.debug("Converting identifier from internal to external...");
String result = a;
for (final String jcrNamespace : jcrNamespacesToRDFNamespaces.keySet()) {
log.debug("Replacing namespace: {} with: {}", jcrNamespace, jcrNamespacesToRDFNamespaces.get(jcrNamespace));
result = result.replace(jcrNamespace, jcrNamespacesToRDFNamespaces.get(jcrNamespace));
}
if (result.endsWith(JCR_CONTENT)) {
result = result.replace(JCR_CONTENT, FCR_CONTENT);
}
return result;
}

/*
* (non-Javadoc)
* @see
* org.fcrepo.kernel.identifiers.InternalIdentifierTranslator#doBackward
* (java.lang.String)
*/
@Override
protected String doBackward(final String b) {
log.debug("Converting identifier from external to internal...");
String result = b;
for (final String rdfNamespace : rdfNamespacesToJcrNamespaces.keySet()) {
log.debug("Replacing namespace: {} with: {}", rdfNamespace, rdfNamespacesToJcrNamespaces.get(rdfNamespace));
result = result.replace(rdfNamespace, rdfNamespacesToJcrNamespaces.get(rdfNamespace));
}
if (result.endsWith(FCR_CONTENT)) {
result = result.replace(FCR_CONTENT, JCR_CONTENT);
}
return result;
}
}
@@ -0,0 +1,70 @@
/**
* Copyright 2013 DuraSpace, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
*
*/

package org.fcrepo.kernel.identifiers;

/**
* A simple {@link InternalIdentifierTranslator} that adds or subtracts a
* prefix.
*
* @author ajs6f
* @date Apr 1, 2014
*/
public class PrefixingTranslator extends InternalIdentifierTranslator {

private String prefix;

/*
* (non-Javadoc)
* @see
* org.fcrepo.kernel.identifiers.InternalIdentifierTranslator#doForward(
* java.lang.String)
*/
@Override
protected String doForward(final String a) {
return getPrefix() + a;
}

/*
* (non-Javadoc)
* @see
* org.fcrepo.kernel.identifiers.InternalIdentifierTranslator#doBackward
* (java.lang.String)
*/
@Override
protected String doBackward(final String b) {
return b.substring(getPrefix().length());
}

/**
* @param p the prefix to use
*/
public void setPrefix(final String p) {
this.prefix = p;
}


/**
* @return the prefix
*/
public String getPrefix() {
return prefix;
}

}
@@ -0,0 +1,60 @@
/**
* Copyright 2013 DuraSpace, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.fcrepo.kernel.identifiers;

import static org.fcrepo.kernel.RdfLexicon.JCR_NAMESPACE;
import static org.junit.Assert.assertEquals;
import static org.modeshape.jcr.api.JcrConstants.JCR_CONTENT;
import static org.slf4j.LoggerFactory.getLogger;

import org.junit.Before;
import org.junit.Test;
import org.slf4j.Logger;

/**
* @author ajs6f
* @date Apr 2, 2014
*/
public class NamespaceTranslatorTest {

private NamespaceTranslator testTranslator;

private static final String testId1 = JCR_NAMESPACE + "test1";

private static final String testId2 = JCR_NAMESPACE + "test2/" + JCR_CONTENT;

private static final Logger log = getLogger(NamespaceTranslatorTest.class);

@Before
public void setUp() {
testTranslator = new NamespaceTranslator();
}

@Test
public void testRoundTrip() {
final String result = testTranslator.convert(testId1);
log.debug("Received translated identifier: {}", result);
assertEquals("Didn't get our original identifier back!", testId1, testTranslator.reverse().convert(result));
}

@Test
public void testRoundTrip2() {
final String result = testTranslator.convert(testId2);
log.debug("Received translated identifier: {}", result);
assertEquals("Didn't get our original identifier back!", testId2, testTranslator.reverse().convert(result));
}
}
@@ -0,0 +1,38 @@
/**
* Copyright 2013 DuraSpace, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.fcrepo.kernel.identifiers;

import static org.junit.Assert.assertEquals;

import org.junit.Test;

public class PrefixingTranslatorTest {

private static final String testPrefix = "info:";

private static final String testId = "test1";

private PrefixingTranslator testTranslator = new PrefixingTranslator();

@Test
public void testRoundTrip() {
testTranslator.setPrefix(testPrefix);
assertEquals("Didn't recover our original identifier!", testId, testTranslator.reverse().convert(
testTranslator.convert(testId)));
}

}

0 comments on commit bca6ac2

Please sign in to comment.