private static void appendNamespace(StringBuilder builder, Namespace namespace) { // PREFIX pref: <pref_name> builder .append(" PREFIX ") .append(namespace.getPrefix()) .append(": <") .append(namespace.getName()) .append("> "); }
@Override public Map<String, String> getNsPrefixMap() { Map<String, String> map = new HashMap<String, String>(); try { RepositoryResult<Namespace> result = getGraph().getConnection().getNamespaces(); while (result.hasNext()) { Namespace ns = result.next(); map.put(ns.getPrefix(), ns.getName()); } } catch (RepositoryException e) { throw new RuntimeException(e); } return map; }
public Map<String, String> getNamespaces() { Map<String, String> map = new HashMap<String, String>(); try { RepositoryResult<Namespace> spaces = con.getNamespaces(); try { while (spaces.hasNext()) { Namespace ns = spaces.next(); map.put(ns.getPrefix(), ns.getName()); } } finally { spaces.close(); } return map; } catch (RepositoryException e) { throw new ModelException(e); } }
@Override public String getNsURIPrefix(String uri) { // TODO speed this up! String prefix = null; try { RepositoryResult<Namespace> result = getGraph().getConnection().getNamespaces(); while (prefix == null && result.hasNext()) { Namespace ns = result.next(); if (uri.equalsIgnoreCase(ns.getName())) { prefix = ns.getPrefix(); } } } catch (RepositoryException e) { throw new RuntimeException(e); } return prefix; }
@Test public void testGetNamespaces() throws Exception { con.begin(); con.setNamespace("rdf", RDF.NAMESPACE); con.commit(); CloseableIteration<? extends Namespace, SailException> namespaces = con.getNamespaces(); try { assertTrue(namespaces.hasNext()); Namespace rdf = namespaces.next(); assertEquals("rdf", rdf.getPrefix()); assertEquals(RDF.NAMESPACE, rdf.getName()); assertTrue(!namespaces.hasNext()); } finally { namespaces.close(); } }
/** * Returns format of given uri that can be used in a SPARQL query. If the uri is prefixed with a * prefix which is known in the given repo than the return value of this function is the given * uri. <br> * If the uri is not prefixed with a known prefix it is assumed that it has no prefix and the * value returned by this function is <uri> * * @param uri * @param repo * @return given uri if it has prefix known by repo <br> * <uri> otherwise */ public static String getUriForSparqlQuery(String uri, Repository repo) { // check if the instanceUri starts with one of known prefixes RepositoryConnection con = null; try { con = repo.getConnection(); RepositoryResult<Namespace> namespaces = con.getNamespaces(); while (namespaces.hasNext()) { Namespace space = namespaces.next(); if (uri.startsWith(space.getPrefix())) { return uri; } } } catch (RepositoryException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { try { if (con != null) { con.close(); } } catch (RepositoryException e) { // TODO Auto-generated catch block e.printStackTrace(); } } // instanceUri is not prefixed => it has to be put into <> symbols for // SPARQL query if (!uri.startsWith("<")) { uri = "<" + uri; } if (!uri.endsWith(">")) { uri += ">"; } return uri; }
private static void addPrefixes(Map<String, Object> ctx, Set<Namespace> namespaces) { for (final Namespace ns : namespaces) { ctx.put(ns.getPrefix(), ns.getName()); } }