Search in sources :

Example 1 with Namespace

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;
}
Also used : SailException(org.openrdf.sail.SailException) Namespace(org.openrdf.model.Namespace)

Example 2 with Namespace

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;
}
Also used : HashMap(java.util.HashMap) SailException(org.openrdf.sail.SailException) Namespace(org.openrdf.model.Namespace)

Example 3 with Namespace

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();
    }
}
Also used : NotifyingSailConnection(org.openrdf.sail.NotifyingSailConnection) SailConnection(org.openrdf.sail.SailConnection) SailException(org.openrdf.sail.SailException) Namespace(org.openrdf.model.Namespace) Test(org.junit.Test)

Example 4 with Namespace

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);
}
Also used : Namespace(org.openrdf.model.Namespace) NamespaceImpl(org.openrdf.model.impl.NamespaceImpl)

Example 5 with Namespace

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();
    }
}
Also used : SailException(org.openrdf.sail.SailException) Namespace(org.openrdf.model.Namespace)

Aggregations

Namespace (org.openrdf.model.Namespace)5 SailException (org.openrdf.sail.SailException)4 HashMap (java.util.HashMap)1 Test (org.junit.Test)1 NamespaceImpl (org.openrdf.model.impl.NamespaceImpl)1 NotifyingSailConnection (org.openrdf.sail.NotifyingSailConnection)1 SailConnection (org.openrdf.sail.SailConnection)1