public void applyRule(Model model, Map<String,URI> namespaceMap) {
searchAndReplace(model, this.searchURIPrefix, this.replaceURIPrefix);
}
public static void searchAndReplace(Model model, String searchURIPrefix, String replaceURIPrefix) {
Model add = RDF2Go.getModelFactory().createModel();
add.open();
Model remove = RDF2Go.getModelFactory().createModel();
remove.open();
ClosableIterator<Statement> it = model.iterator();
while(it.hasNext()) {
Statement stmt = it.next();
Resource s = stmt.getSubject();
URI p = stmt.getPredicate();
Node o = stmt.getObject();
boolean match = false;
if(s instanceof URI && s.asURI().toString().startsWith(searchURIPrefix)) {
match = true;
String sURI = s.asURI().toString().replace(searchURIPrefix, replaceURIPrefix);
s = new URIImpl(sURI);
}
if(p.toString().startsWith(searchURIPrefix)) {
match = true;
String pURI = p.toString().replace(searchURIPrefix, replaceURIPrefix);
p = new URIImpl(pURI);
}
if(o instanceof URI && o.asURI().toString().startsWith(searchURIPrefix)) {
match = true;
String oURI = o.asURI().toString().replace(searchURIPrefix, replaceURIPrefix);
o = new URIImpl(oURI);
}
if(match) {
remove.addStatement(stmt);
add.addStatement(s, p, o);
}
}
it.close();
ClosableIterator<Statement> addIt = add.iterator();
ClosableIterator<Statement> removeIt = remove.iterator();
model.update(new DiffImpl(addIt, removeIt));
addIt.close();
removeIt.close();
add.close();
remove.close();
}