NIHVIVO-3510 modified sparql query builder to manufacture prefixes for any namespaces that need them
This commit is contained in:
parent
4b8f9c613d
commit
6a8c434a84
2 changed files with 69 additions and 33 deletions
|
@ -4,8 +4,10 @@ package edu.cornell.mannlib.vitro.webapp.sparql;
|
|||
import java.io.IOException;
|
||||
import java.io.PrintWriter;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
@ -13,7 +15,6 @@ import javax.servlet.http.HttpServletResponse;
|
|||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.mindswap.pellet.jena.vocabulary.SWRL;
|
||||
import org.openrdf.model.vocabulary.OWL;
|
||||
import org.openrdf.model.vocabulary.RDF;
|
||||
|
||||
|
@ -23,10 +24,12 @@ import com.hp.hpl.jena.vocabulary.XSD;
|
|||
import edu.cornell.mannlib.vedit.controller.BaseEditController;
|
||||
import edu.cornell.mannlib.vitro.webapp.auth.requestedAction.Actions;
|
||||
import edu.cornell.mannlib.vitro.webapp.auth.requestedAction.usepages.UseMiscellaneousPages;
|
||||
import edu.cornell.mannlib.vitro.webapp.beans.BaseResourceBean;
|
||||
import edu.cornell.mannlib.vitro.webapp.beans.Ontology;
|
||||
import edu.cornell.mannlib.vitro.webapp.controller.VitroRequest;
|
||||
import edu.cornell.mannlib.vitro.webapp.dao.OntologyDao;
|
||||
import edu.cornell.mannlib.vitro.webapp.dao.VitroVocabulary;
|
||||
import edu.cornell.mannlib.vitro.webapp.dao.WebappDaoFactory;
|
||||
|
||||
/**
|
||||
* This servlet gets all the prefix for initizing the sparql query builder.
|
||||
|
@ -59,42 +62,74 @@ public class GetAllPrefix extends BaseEditController {
|
|||
}
|
||||
|
||||
VitroRequest vreq = new VitroRequest(request);
|
||||
|
||||
// EditProcessObject epo = super.createEpo(request);
|
||||
OntologyDao daoObj = vreq.getFullWebappDaoFactory().getOntologyDao();
|
||||
List ontologiesObj = daoObj.getAllOntologies();
|
||||
ArrayList prefixList = new ArrayList();
|
||||
Map<String, String> prefixMap = getPrefixMap(vreq.getFullWebappDaoFactory());
|
||||
|
||||
response.setContentType("text/xml");
|
||||
response.setCharacterEncoding("UTF-8");
|
||||
PrintWriter out = response.getWriter();
|
||||
String respo = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
|
||||
respo += "<options>";
|
||||
if (ontologiesObj != null && ontologiesObj.size() > 0) {
|
||||
|
||||
Iterator ontItr = ontologiesObj.iterator();
|
||||
while (ontItr.hasNext()) {
|
||||
Ontology ont = (Ontology) ontItr.next();
|
||||
if (ont.getPrefix() != null) {
|
||||
respo += makeOption(ont.getPrefix(), ont.getURI());
|
||||
}
|
||||
}
|
||||
|
||||
for (String namespace : prefixMap.keySet()) {
|
||||
respo += makeOption(prefixMap.get(namespace), namespace);
|
||||
}
|
||||
;
|
||||
respo += makeOption("owl", OWL.NAMESPACE);
|
||||
respo += makeOption("rdf", RDF.NAMESPACE);
|
||||
respo += makeOption("rdfs", RDFS.getURI());
|
||||
respo += makeOption("swrl", "http://www.w3.org/2003/11/swrl#");
|
||||
respo += makeOption("swrlb", "http://www.w3.org/2003/11/swrlb#");
|
||||
respo += makeOption("xsd", XSD.getURI());
|
||||
respo += makeOption("vitro", VitroVocabulary.vitroURI);
|
||||
respo += "</options>";
|
||||
out.println(respo);
|
||||
out.flush();
|
||||
out.close();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a map to prefixes for use in building queries. Will manufacture a
|
||||
* prefix for any namespace that doesn't have an associated owl:Ontology resource
|
||||
* with a prefix annotation
|
||||
* @param wadf
|
||||
* @return map of namespace URIs to prefix strings
|
||||
*/
|
||||
private Map<String, String> getPrefixMap(WebappDaoFactory wadf) {
|
||||
Map<String, String> prefixMap = new HashMap<String, String>();
|
||||
|
||||
OntologyDao oDao = wadf.getOntologyDao();
|
||||
for(Ontology o : oDao.getAllOntologies()) {
|
||||
if (o.getPrefix() != null) {
|
||||
prefixMap.put(o.getPrefix(), o.getURI());
|
||||
}
|
||||
}
|
||||
|
||||
// add standard namespaces
|
||||
addPrefixIfNecessary("owl", OWL.NAMESPACE, prefixMap);
|
||||
addPrefixIfNecessary("rdf", RDF.NAMESPACE, prefixMap);
|
||||
addPrefixIfNecessary("rdfs", RDFS.getURI(), prefixMap);
|
||||
addPrefixIfNecessary("swrl", "http://www.w3.org/2003/11/swrl#", prefixMap);
|
||||
addPrefixIfNecessary("swrlb", "http://www.w3.org/2003/11/swrlb#", prefixMap);
|
||||
addPrefixIfNecessary("xsd", XSD.getURI(), prefixMap);
|
||||
addPrefixIfNecessary("vitro", VitroVocabulary.vitroURI, prefixMap);
|
||||
|
||||
// we also need to manufacture prefixes for namespaces used by any class or
|
||||
// property, regardless of whether there's an associated owl:Ontology.
|
||||
int newPrefixCount = 0;
|
||||
List<BaseResourceBean> ontEntityList = new ArrayList<BaseResourceBean>();
|
||||
ontEntityList.addAll(wadf.getVClassDao().getAllVclasses());
|
||||
ontEntityList.addAll(wadf.getObjectPropertyDao().getAllObjectProperties());
|
||||
ontEntityList.addAll(wadf.getDataPropertyDao().getAllDataProperties());
|
||||
for (BaseResourceBean ontEntity : ontEntityList) {
|
||||
if (!ontEntity.isAnonymous()
|
||||
&& !prefixMap.containsKey(ontEntity.getNamespace())) {
|
||||
newPrefixCount++;
|
||||
prefixMap.put(ontEntity.getNamespace(), "p." + Integer.toString(
|
||||
newPrefixCount));
|
||||
}
|
||||
}
|
||||
|
||||
return prefixMap;
|
||||
}
|
||||
|
||||
private void addPrefixIfNecessary(String prefix, String namespace,
|
||||
Map<String, String> prefixMap) {
|
||||
if (!prefixMap.containsKey(namespace)) {
|
||||
prefixMap.put(namespace, prefix);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Makes the markup for a prefix option
|
||||
* @param prefix
|
||||
|
|
|
@ -1,12 +1,13 @@
|
|||
/* $This file is distributed under the terms of the license in /doc/license.txt$ */
|
||||
var namespaces = {
|
||||
rdf : "http://www.w3.org/1999/02/22-rdf-syntax-ns#",
|
||||
rdfs : "http://www.w3.org/2000/01/rdf-schema#",
|
||||
xsd : "http://www.w3.org/2001/XMLSchema#",
|
||||
owl : "http://www.w3.org/2002/07/owl#",
|
||||
swrl : "http://www.w3.org/2003/11/swrl#",
|
||||
swrlb : "http://www.w3.org/2003/11/swrlb#",
|
||||
vitro : "http://vitro.mannlib.cornell.edu/ns/vitro/0.7#"
|
||||
// now handled in GetAllPrefix.java
|
||||
// rdf : "http://www.w3.org/1999/02/22-rdf-syntax-ns#",
|
||||
// rdfs : "http://www.w3.org/2000/01/rdf-schema#",
|
||||
// xsd : "http://www.w3.org/2001/XMLSchema#",
|
||||
// owl : "http://www.w3.org/2002/07/owl#",
|
||||
// swrl : "http://www.w3.org/2003/11/swrl#",
|
||||
// swrlb : "http://www.w3.org/2003/11/swrlb#",
|
||||
// vitro : "http://vitro.mannlib.cornell.edu/ns/vitro/0.7#"
|
||||
};
|
||||
|
||||
var level = 0;
|
||||
|
|
Loading…
Add table
Reference in a new issue