Example #1
0
 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;
 }
Example #3
0
 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;
 }
Example #5
0
  @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();
    }
  }
Example #6
0
  /**
   * 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();
  }
Example #7
0
  /**
   * 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 &lt;uri&gt;
   *
   * @param uri
   * @param repo
   * @return given uri if it has prefix known by repo <br>
   *     &lt;uri&gt 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());
   }
 }
 @Override
 protected String getNamespaceInternal(String prefix) throws SailException {
   Namespace ns = namespaces.findByPrefix(prefix);
   if (ns == null) return null;
   return ns.getName();
 }