I get a SPARQL query string from a service which looks like the following query:
SELECT * FROM { ?s owl:sameAs ?p }
If I try to parse this with the QueryFactory
class I get the Unresolved Prefixed Name Exception
which totally makes sense.
I have a properties file which maps the predefined namespace prefixes .
For now I have a workaround which looks like the following:
Iterator<String> it = prefixConfig.getKeys();
String prefixes = "";
while(it.hasNext()) {
String p = it.next();
prefixes += "PREFIX " + p + ":<" + prefixConfig.getString(p) + "> ";
}
Query q = QueryFactory.create(prefixes + query);
it = prefixConfig.getKeys();
while(it.hasNext()) {
String p = it.next();
q.getPrefixMapping().removeNsPrefix(p);
}
query = q.toString();
This works but I am not happy at all with this solution , so my question is:
Is there a good way to either replace the prefixes in the SPARQL query string or to only add the relevant prefixes?
Thanks a lot in advance!
public static String replacePrefixes(String query, PrefixMapping pmap){
/* With Prologue and the parse method,
* the queryString gets parsed without an error
*/
Prologue prog = new Prologue();
prog.setPrefixMapping(pmap);
Query q = QueryFactory.parse(new Query(prog), query, null, null);
//Set Prefix Mapping
q.setPrefixMapping(pmap);
//remove PrefixMapping so the prefixes will get replaced by the full uris
q.setPrefixMapping(null);
return q.serialize();
}
This works for me :)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.