use of org.openrdf.model.Namespace in project blueprints by tinkerpop.
the class SailGraph method prefixNamespace.
/**
* Given a URI, compress it to its prefixed URI.
*
* @param uri the expanded URI (e.g. http://tinkerpop.com#knows)
* @return the prefixed URI (e.g. tg:knows)
*/
public String prefixNamespace(String uri) {
try {
CloseableIteration<? extends Namespace, SailException> namespaces = this.sailConnection.get().getNamespaces();
while (namespaces.hasNext()) {
Namespace namespace = namespaces.next();
if (uri.contains(namespace.getName()))
uri = uri.replace(namespace.getName(), namespace.getPrefix() + SailTokens.NAMESPACE_SEPARATOR);
}
namespaces.close();
} catch (SailException e) {
throw new RuntimeException(e.getMessage(), e);
}
return uri;
}
use of org.openrdf.model.Namespace in project blueprints by tinkerpop.
the class SailGraph method getNamespaces.
/**
* Get all the prefix-to-namespace mappings of the graph.
*
* @return a map of the prefix-to-namespace mappings
*/
public Map<String, String> getNamespaces() {
Map<String, String> namespaces = new HashMap<String, String>();
try {
final CloseableIteration<? extends Namespace, SailException> results = this.sailConnection.get().getNamespaces();
while (results.hasNext()) {
Namespace namespace = results.next();
namespaces.put(namespace.getPrefix(), namespace.getName());
}
results.close();
} catch (SailException e) {
throw new RuntimeException(e.getMessage(), e);
}
return namespaces;
}
use of org.openrdf.model.Namespace in project blueprints by tinkerpop.
the class SailTest method testGetNamespaces.
@Test
public void testGetNamespaces() throws Exception {
SailConnection sc = sail.getConnection();
try {
sc.begin();
CloseableIteration<? extends Namespace, SailException> namespaces;
int before = 0, during = 0, after = 0;
// just iterate through all namespaces
namespaces = sc.getNamespaces();
while (namespaces.hasNext()) {
Namespace ns = namespaces.next();
before++;
// System.out.println("namespace: " + ns);
}
namespaces.close();
// Note: assumes that these namespace prefixes are unused.
int nTests = 10;
String prefixPrefix = "testns";
String namePrefix = "http://example.org/test";
for (int i = 0; i < nTests; i++) {
sc.setNamespace(prefixPrefix + i, namePrefix + i);
}
sc.commit();
sc.begin();
namespaces = sc.getNamespaces();
while (namespaces.hasNext()) {
Namespace ns = namespaces.next();
during++;
String prefix = ns.getPrefix();
String name = ns.getName();
if (prefix.startsWith(prefixPrefix)) {
assertEquals(name, namePrefix + prefix.substring(prefixPrefix.length()));
}
}
namespaces.close();
for (int i = 0; i < nTests; i++) {
sc.removeNamespace(prefixPrefix + i);
}
sc.commit();
sc.begin();
namespaces = sc.getNamespaces();
while (namespaces.hasNext()) {
namespaces.next();
after++;
}
namespaces.close();
assertEquals(during, before + nTests);
assertEquals(after, before);
} finally {
sc.rollback();
sc.close();
}
}
use of org.openrdf.model.Namespace in project blueprints by tinkerpop.
the class PropertyGraphSailConnection method addNamespace.
private static void addNamespace(final String prefix, final String uri) {
Namespace n = new NamespaceImpl(prefix, uri);
namespaces.put(prefix, n);
}
use of org.openrdf.model.Namespace in project blueprints by tinkerpop.
the class SailTest method showNamespaces.
private void showNamespaces(final SailConnection c) throws SailException {
System.out.println("namespaces:");
CloseableIteration<? extends Namespace, SailException> iter = c.getNamespaces();
try {
while (iter.hasNext()) {
Namespace n = iter.next();
System.out.println("\t" + n.getPrefix() + ":\t" + n.getName());
}
} finally {
iter.close();
}
}
Aggregations