From 97e8355cde94a213459a6c35dcd4cd886128360a Mon Sep 17 00:00:00 2001 From: David Fuchs Date: Wed, 6 Nov 2019 14:08:32 +0100 Subject: [PATCH] Started handing out well-known prefix names. --- security/auth.json | 1 + .../com/github/fuchsdavid/annotator/Main.java | 52 ++++++++++++++----- src/main/resources/config/known_prefixes.conf | 11 ++++ src/main/resources/sparql/construct.sparql | 2 + src/main/resources/www/annotator.js | 2 +- 5 files changed, 53 insertions(+), 15 deletions(-) create mode 100644 security/auth.json create mode 100644 src/main/resources/config/known_prefixes.conf diff --git a/security/auth.json b/security/auth.json new file mode 100644 index 0000000..59e1daf --- /dev/null +++ b/security/auth.json @@ -0,0 +1 @@ +{"users":[{"email":"fucd00@vse.cz","salt":"-358365752","passwordHash":"QKiqdyVfGLrqzVRedri+qVKfL/XrVIzu037Mfcb7UIA="}]} \ No newline at end of file diff --git a/src/main/java/com/github/fuchsdavid/annotator/Main.java b/src/main/java/com/github/fuchsdavid/annotator/Main.java index 1d2499a..ef3032d 100644 --- a/src/main/java/com/github/fuchsdavid/annotator/Main.java +++ b/src/main/java/com/github/fuchsdavid/annotator/Main.java @@ -36,6 +36,7 @@ import java.util.HashMap; import java.util.Map; import java.util.Random; +import java.util.Scanner; import java.util.logging.Level; import java.util.logging.Logger; import javax.json.Json; @@ -66,6 +67,8 @@ import org.apache.jena.query.Query; import org.apache.jena.query.QueryExecutionFactory; import org.apache.jena.rdf.model.Model; +import org.apache.jena.rdf.model.Resource; +import org.apache.jena.shared.PrefixMapping; import org.apache.jena.update.UpdateExecutionFactory; import org.apache.jena.update.UpdateRequest; import org.w3c.dom.DOMException; @@ -84,6 +87,7 @@ public class Main { private static final String AUTH = "/www/auth.xhtml"; private static final String PASSWD = "./security/auth.json"; private static final String CONSTRUCT = "/sparql/construct.sparql"; + private static final String PREFIXES = "/config/known_prefixes.conf"; private static final Random RNG = new Random(); private static final Map> ID2MODEL_LIST = new HashMap<>(); @@ -93,6 +97,8 @@ public class Main { private static final Map EMAIL2USER = new HashMap<>(); private static final Map CACHED_FILES = new HashMap<>(); + private static final PrefixMapping PM = PrefixMapping.Factory.create(); + private static final TransformerFactory TF = TransformerFactory.newInstance(); private static JsonBuilderFactory JF = Json.createBuilderFactory(null); @@ -139,6 +145,13 @@ public class Main { Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex); } })); + Scanner scanner = new Scanner(Main.class.getResourceAsStream(PREFIXES)); + while(scanner.hasNextLine()){ + String[] line = scanner.nextLine().split("\\t"); + if(line.length != 2) + throw new Exception("Failed to parse prefix list"); + PM.setNsPrefix(line[0], new URL(line[1]).toExternalForm()); + } } catch(MalformedURLException | NoSuchAlgorithmException | ParserConfigurationException | SAXException ex){ Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex); @@ -147,6 +160,9 @@ public class Main { catch (IOException ex) { Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex); System.exit(1); + } catch (Exception ex) { + Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex); + System.exit(1); } } @@ -183,7 +199,7 @@ public static HttpServer runHTTPServer() { server = null; } finally{ - return server; + return Main.server = server; } } @@ -295,14 +311,14 @@ private static void fillTable(String session_id, Document document) throws DOMEx final Node c = document.createElement("strong"); c.appendChild(document.createTextNode("You are currently annotating resource:")); caption.appendChild(c); - caption.appendChild(document.createTextNode(" <" + m.listSubjects().next().getURI() + ">")); + caption.appendChild(document.createTextNode(" " + getPrefixedName(m.listSubjects().next()))); m.listStatements().forEachRemaining(statement -> { Node tr = tbody.appendChild(document.createElement("tr")); Node p = tr.appendChild(document.createElement("td")); - p.setTextContent(statement.getPredicate().getURI()); + p.setTextContent(getPrefixedName(statement.getPredicate())); Node o = tr.appendChild(document.createElement("td")); if(statement.getObject().isResource()) - o.setTextContent(statement.getObject().toString()); + o.setTextContent(getPrefixedName(statement.getObject().asResource())); else o.setTextContent(statement.getObject().asLiteral().getLexicalForm()); }); @@ -369,18 +385,14 @@ private static void doGetData(HttpExchange exchange, String session_id) throws I JsonObjectBuilder object = JF.createObjectBuilder(); JsonArrayBuilder array = JF.createArrayBuilder(); m.listStatements().forEachRemaining(statement -> { + JsonObjectBuilder createTriple = JF.createObjectBuilder() + .add("subject",getPrefixedName(statement.getSubject())); + createTriple.add("predicate", getPrefixedName(statement.getPredicate())); if(statement.getObject().isResource()) - array.add(JF.createObjectBuilder() - .add("subject", statement.getSubject().toString()) - .add("predicate",statement.getPredicate().toString()) - .add("object", statement.getObject().toString()) - .build()); + createTriple.add("object", getPrefixedName(statement.getObject().asResource())); else - array.add(JF.createObjectBuilder() - .add("subject", statement.getSubject().toString()) - .add("predicate",statement.getPredicate().toString()) - .add("object", statement.getObject().asLiteral().getLexicalForm()) - .build()); + createTriple.add("object",statement.getObject().asLiteral().getLexicalForm()); + array.add(createTriple.build()); }); object.add("triples", array); json = object.build().toString(); @@ -617,6 +629,7 @@ private static Model retrieveTriples(HttpExchange exchange,InputStream file) thr pss.setLiteral("limit", 1); Query query = pss.asQuery(); m = QueryExecutionFactory.sparqlService(SPARQLendpoint,query).execConstruct(); + m.setNsPrefixes(PM); } catch (FileNotFoundException ex) { Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex); exchange.sendResponseHeaders(500, 0); @@ -628,6 +641,17 @@ private static Model retrieveTriples(HttpExchange exchange,InputStream file) thr } return m; } + + /** + * Get prefixed name of a URI resource. + * + * @param resource + * @return + */ + private static String getPrefixedName(Resource resource){ + String prefix = PM.getNsURIPrefix(resource.getNameSpace()); + return (prefix == null ? resource.getURI() : prefix + ":" + resource.getLocalName()); + } /** * Retrieves session ID from HTTP request header. diff --git a/src/main/resources/config/known_prefixes.conf b/src/main/resources/config/known_prefixes.conf new file mode 100644 index 0000000..2c0cad1 --- /dev/null +++ b/src/main/resources/config/known_prefixes.conf @@ -0,0 +1,11 @@ +dbo http://dbpedia.org/ontology/ +dbp http://dbpedia.org/property/ +dbr http://dbpedia.org/resource/ +dcterms http://purl.org/dc/terms/ +foaf http://xmlns.com/foaf/0.1/ +rdf http://www.w3.org/1999/02/22-rdf-syntax-ns# +rdfs http://www.w3.org/2000/01/rdf-schema# +owl http://www.w3.org/2002/07/owl# +foaf http://xmlns.com/foaf/0.1/ +dcterms http://purl.org/dc/terms/ +skos http://www.w3.org/2004/02/skos/core# \ No newline at end of file diff --git a/src/main/resources/sparql/construct.sparql b/src/main/resources/sparql/construct.sparql index 97b4dcd..52f8481 100644 --- a/src/main/resources/sparql/construct.sparql +++ b/src/main/resources/sparql/construct.sparql @@ -4,6 +4,7 @@ CONSTRUCT { } WHERE { ?dbr owl:sameAs ?wdr. + FILTER (!isBlank(?dbr)). { SELECT ?dbr ?wdr WHERE { @@ -17,5 +18,6 @@ WHERE { SERVICE { ?dbr ?dbp ?dbo. FILTER(!strstarts(str(?dbo),"http://www.wikidata.org/")&&!strstarts(str(?dbo),"http://wikidata.dbpedia.org/")). + FILTER (!isBlank(?dbo)). } } \ No newline at end of file diff --git a/src/main/resources/www/annotator.js b/src/main/resources/www/annotator.js index 99b513c..035b229 100644 --- a/src/main/resources/www/annotator.js +++ b/src/main/resources/www/annotator.js @@ -43,7 +43,7 @@ getData.onreadystatechange = function(){ let tbody = data.getElementsByTagName("tbody").item(0); data.removeChild(thead); data.removeChild(tbody); - data.getElementsByTagName("caption").item(0).innerHTML="You are currently annotating resource: <" + triples[0].subject + ">"; + data.getElementsByTagName("caption").item(0).innerHTML="You are currently annotating resource: " + triples[0].subject; try{ for(var i=0;i