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; }
/** * Returns a string containing prefixes defined in the given repository. The returned string is in * format that can be used for a SPARQL query. * * @param repository * @return string containing prefixes defined in the given repository for a usage in SPARQL query */ public static String getSparqlQueryPrefixesHumanReadable(Repository repository) { StringBuilder query = new StringBuilder(); RepositoryResult<Namespace> namespaces; RepositoryConnection con = null; try { con = repository.getConnection(); namespaces = con.getNamespaces(); while (namespaces.hasNext()) { Namespace space = namespaces.next(); appendHumanReadableNamespace(query, space); } // add well known rdf and rdfs prefixes if they are missing // they are used by basic queries (these might be issued on empty // repositories and the missing prefixes are causing troubles) if (query.indexOf(RDF_NAMESPACE.getName()) == -1) { appendNamespace(query, RDF_NAMESPACE); } if (query.indexOf(RDFS_NAMESPACE.getName()) == -1) { appendNamespace(query, RDFS_NAMESPACE); } } 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(); } } return query.toString(); }
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(); } }
private static void addPrefixes(Map<String, Object> ctx, Set<Namespace> namespaces) { for (final Namespace ns : namespaces) { ctx.put(ns.getPrefix(), ns.getName()); } }
@Override protected String getNamespaceInternal(String prefix) throws SailException { Namespace ns = namespaces.findByPrefix(prefix); if (ns == null) return null; return ns.getName(); }