@Path("/complete")
@Produces(Namespaces.MIME_TYPE_JSON)
public List<String> complete(@QueryParam("prefix") String prefix, @QueryParam("uri") String uri,
@QueryParam("mode") @DefaultValue("path") String mode, @Context UriInfo info) {
final int limit = 20;
final PrefixService prefixService = createLocalPrefixService(info);
if (uri != null) {
// Complete <URI>
final List<String> suggestions = new ArrayList<String>();
for (String sug : getCompletions(uri, limit, mode)) {
final String curie = prefixService.getCurie(sug);
suggestions.add(curie != null ? curie : sug);
}
return suggestions;
} else if (prefix != null) {
Matcher m = CURIE_PATTERN.matcher(prefix);
if (m.matches()) {
String px = m.group(1);
String local = m.group(2);
if (px.equals(FUNCTION_PREFIX)) {
try {
final RepositoryConnection conn = sesameService.getConnection();
try {
conn.begin();
SesameConnectionBackend backend = SesameConnectionBackend.withConnection(conn);
final Set<SelectorFunction<Value>> functions = ldPathService.getFunctions();
List<String> suggestions = new ArrayList<String>();
for (SelectorFunction<Value> fn : functions) {
final String fName = fn.getPathExpression(backend);
if (fName.startsWith(local)) {
suggestions.add(FUNCTION_PREFIX + ":" + fName + "()");
}
}
return suggestions;
} finally {
conn.commit();
conn.close();
}
} catch (RepositoryException e) {
return Collections.emptyList();
}
} else if (prefixService.containsPrefix(px)) {
String resolved = prefixService.getNamespace(px) + (local != null ? local : "");
List<String> suggestions = new ArrayList<String>();
for (String c : getCompletions(resolved, limit, mode)) {
// CURIE urs MUST have a local part
if (c.length() <= resolved.length()) {
continue;
}
final String curie = prefixService.getCurie(c);
suggestions.add(curie != null ? curie : c);
}
return suggestions;
}
} else {
List<String> suggestions = new ArrayList<String>();
if (mode.equals(MODE_TRANSFORM)) {
for (String s : ldPathService.getTransformableTypes()) {
String px = prefixService.getPrefix(UriUtil.getNamespace(s));
if (px != null && px.startsWith(prefix) && !suggestions.contains(px)) {
suggestions.add(px);
}
}
} else {
if (FUNCTION_PREFIX.startsWith(prefix)) {
suggestions.add(FUNCTION_PREFIX);
}
for (String px : prefixService.getMappings().keySet()) {
if (px.startsWith(prefix)) {
suggestions.add(px);
}
}
}