Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
258 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
fcrepo-kernel/src/main/java/org/fcrepo/kernel/identifiers/NamespaceTranslator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
70 changes: 70 additions & 0 deletions
70
fcrepo-kernel/src/main/java/org/fcrepo/kernel/identifiers/PrefixingTranslator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
|
||
} |
60 changes: 60 additions & 0 deletions
60
fcrepo-kernel/src/test/java/org/fcrepo/kernel/identifiers/NamespaceTranslatorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)); | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
fcrepo-kernel/src/test/java/org/fcrepo/kernel/identifiers/PrefixingTranslatorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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))); | ||
} | ||
|
||
} |