use of org.eclipse.rdf4j.model.Namespace in project rdf4j by eclipse.
the class ModelBuilder method convertPrefixedName.
/**
* Convert the given prefixed name string to an IRI if possible.
*
* @param prefixedName
* a prefixed name string, e.g. "rdf:type"
* @return the IRI corresponding to the prefixed name, or {@code null} if the supplied string couldn't be
* converted.
*/
private IRI convertPrefixedName(String prefixedName) {
if (prefixedName.indexOf(':') < 0) {
return null;
}
final String prefix = prefixedName.substring(0, prefixedName.indexOf(':'));
final ValueFactory vf = SimpleValueFactory.getInstance();
for (Namespace ns : model.getNamespaces()) {
if (prefix.equals(ns.getPrefix())) {
return vf.createIRI(ns.getName(), prefixedName.substring(prefixedName.indexOf(':') + 1));
}
}
// try mapping using some of the default / well-known namespaces
for (Namespace ns : getDefaultNamespaces()) {
if (prefix.equals(ns.getPrefix())) {
model.setNamespace(ns);
return vf.createIRI(ns.getName(), prefixedName.substring(prefixedName.indexOf(':') + 1));
}
}
return null;
}
use of org.eclipse.rdf4j.model.Namespace in project rdf4j by eclipse.
the class RepositoryConnectionWrapper method exportStatements.
/**
* Exports all statements contained in the supplied statement iterator and all relevant namespace
* information to the supplied RDFHandler.
*/
protected void exportStatements(RepositoryResult<Statement> stIter, RDFHandler handler) throws RepositoryException, RDFHandlerException {
try {
handler.startRDF();
// Export namespace information
RepositoryResult<Namespace> nsIter = getNamespaces();
try {
while (nsIter.hasNext()) {
Namespace ns = nsIter.next();
handler.handleNamespace(ns.getPrefix(), ns.getName());
}
} finally {
nsIter.close();
}
// Export statemnts
while (stIter.hasNext()) {
handler.handleStatement(stIter.next());
}
handler.endRDF();
} finally {
stIter.close();
}
}
use of org.eclipse.rdf4j.model.Namespace in project rdf4j by eclipse.
the class NamespacesTest method testWrapKeySet.
/**
* Test method for {@link org.eclipse.rdf4j.model.util.Namespaces#wrap(java.util.Set)}.
*/
@Test
public final void testWrapKeySet() throws Exception {
Set<Namespace> testSet = new LinkedHashSet<Namespace>();
Map<String, String> testMap = Namespaces.wrap(testSet);
Set<String> keySet1 = testMap.keySet();
assertNotNull(keySet1);
assertTrue(keySet1.isEmpty());
testSet.add(new SimpleNamespace(testPrefix1, testName1));
Set<String> keySet2 = testMap.keySet();
assertNotNull(keySet2);
assertFalse(keySet2.isEmpty());
assertEquals(1, keySet2.size());
String nextKey = keySet2.iterator().next();
assertEquals(testPrefix1, nextKey);
testSet.clear();
Set<String> keySet3 = testMap.keySet();
assertNotNull(keySet3);
assertTrue(keySet3.isEmpty());
}
use of org.eclipse.rdf4j.model.Namespace in project rdf4j by eclipse.
the class NamespacesTest method testWrapRemove.
/**
* Test method for {@link org.eclipse.rdf4j.model.util.Namespaces#wrap(java.util.Set)}.
*/
@Test
public final void testWrapRemove() throws Exception {
Set<Namespace> testSet = new LinkedHashSet<Namespace>();
Map<String, String> testMap = Namespaces.wrap(testSet);
assertTrue(testMap.isEmpty());
assertEquals(0, testMap.size());
assertTrue(testSet.isEmpty());
assertEquals(0, testSet.size());
assertFalse(testMap.containsKey(testPrefix1));
assertFalse(testMap.containsValue(testName1));
// Directly add to Set, and then try to remove it using the Map
testSet.add(new SimpleNamespace(testPrefix1, testName1));
assertFalse(testMap.isEmpty());
assertEquals(1, testMap.size());
assertFalse(testSet.isEmpty());
assertEquals(1, testSet.size());
assertTrue(testSet.contains(new SimpleNamespace(testPrefix1, testName1)));
assertTrue(testMap.containsKey(testPrefix1));
assertTrue(testMap.containsValue(testName1));
testSet.remove(new SimpleNamespace(testPrefix1, testName1));
assertTrue(testMap.isEmpty());
assertEquals(0, testMap.size());
assertTrue(testSet.isEmpty());
assertEquals(0, testSet.size());
assertFalse(testMap.containsKey(testPrefix1));
assertFalse(testMap.containsValue(testName1));
testSet.clear();
// Try again after clear
testSet.add(new SimpleNamespace(testPrefix1, testName1));
assertFalse(testMap.isEmpty());
assertEquals(1, testMap.size());
assertFalse(testSet.isEmpty());
assertEquals(1, testSet.size());
assertTrue(testSet.contains(new SimpleNamespace(testPrefix1, testName1)));
assertTrue(testMap.containsKey(testPrefix1));
assertTrue(testMap.containsValue(testName1));
testSet.remove(new SimpleNamespace(testPrefix1, testName1));
assertTrue(testMap.isEmpty());
assertEquals(0, testMap.size());
assertTrue(testSet.isEmpty());
assertEquals(0, testSet.size());
assertFalse(testMap.containsKey(testPrefix1));
assertFalse(testMap.containsValue(testName1));
}
use of org.eclipse.rdf4j.model.Namespace in project rdf4j by eclipse.
the class NamespacesTest method testAsMapOne.
/**
* Test method for {@link org.eclipse.rdf4j.model.util.Namespaces#asMap(java.util.Set)}.
*/
@Test
public final void testAsMapOne() {
Set<Namespace> input = new HashSet<Namespace>();
input.add(new SimpleNamespace(RDF.PREFIX, RDF.NAMESPACE));
Map<String, String> map = Namespaces.asMap(input);
assertFalse(map.isEmpty());
assertEquals(1, map.size());
assertTrue(map.containsKey(RDF.PREFIX));
assertEquals(RDF.NAMESPACE, map.get(RDF.PREFIX));
}
Aggregations