Permalink
Browse files

bit further with bake, generalizing srx2map a bit

  • Loading branch information...
1 parent 12555f0 commit 375703cc37740b01665cb81395b5b8a16d6baa9f @danja committed Apr 9, 2012
Showing with 137 additions and 18 deletions.
  1. +12 −7 src/TurtleHandler.js
  2. +16 −10 src/admin/Admin.js
  3. +1 −0 src/seki.js
  4. +1 −1 src/sparqlTemplates.js
  5. +7 −0 src/srx2map.js
  6. +100 −0 src/srx2map_multi.js
View
@@ -18,7 +18,7 @@ TurtleHandler.prototype = {
// // this.value2 = argument + 100;
// console.log("JSONHandler.GET called");
// }
- getGraphs : function() {
+ getGraphs : function(callback) {
// the client that will talk to the SPARQL server
var client = http.createClient(config.sparqlPort, config.sparqlHost);
var queryPath = config.sparqlQueryEndpoint + "?query="
@@ -27,21 +27,26 @@ TurtleHandler.prototype = {
var clientRequest = client.request("GET", queryPath, headers.graph);
clientRequest.end();
+ var saxer = require('./srx2map_multi');
+ var stream = saxer.createStream();
+
// handle SPARQL server response
clientRequest.on('response', function(queryResponse) {
console.log("STATUS=" + queryResponse.statusCode);
var data = "";
// response body may come in chunks, whatever, just pass them on
+
queryResponse.on('data', function(chunk) {
- // verbosity("headers " +
- // JSON.stringify(queryResponse.headers));
- data += chunk;
+ // console.log(chunk);
+ stream.write(chunk);
});
- // the SPARQL server response has finished, so finish up this
- // response
+
queryResponse.on('end', function() {
- console.log("URIs = "+data);
+
+ stream.end();
+
+ callback(stream.bindings);
});
});
}
View
@@ -1,8 +1,7 @@
var TurtleHandler = require('../TurtleHandler');
var config = require('../ConfigDefault').config;
-
-//Constructor
+// Constructor
function Admin(sekiRequest, sekiResponse) {
this.sekiRequest = sekiRequest;
this.sekiResponse = sekiResponse;
@@ -14,19 +13,26 @@ Admin.prototype = {
value1 : "default_value",
"testCall" : function() {
- // this.value2 = argument + 100;
console.log("Admin.testCall called");
},
- "bake" : function() { // perhaps this should spawn a separate OS process?
- // this.value2 = argument + 100;
- console.log("Admin.bake called");
-
+ "bake" : function() { // perhaps this should spawn a separate OS process?
+
+ // console.log("Admin.bake called");
+
var handler = new TurtleHandler();
- handler.getGraphs();
-// var graphURIs = getTurtleForGraph()
+ handler.getGraphs(function(bindings) {
+ // console.log(JSON.stringify(bindings, null, '\t'));
+ for (x in bindings) {
+ console.log(bindings[x].graph);
+ // HERE HERE HERE
+ // do a query to get the named graph - as in seki.js
+ // map URI to fs
+ // save to fs
+ }
+ });
},
- "unbake" : function() { // perhaps this should spawn a separate OS process?
+ "unbake" : function() { // perhaps this should spawn a separate OS process?
// this.value2 = argument + 100;
console.log("Admin.unbake called");
}
View
@@ -296,6 +296,7 @@ function serveHTML(resource, sekiResponse, queryResponse) {
// set up HTML builder
var viewTemplater = templater(htmlTemplates.viewTemplate);
// verbosity("GOT RESPONSE ");
+
var saxer = require('./srx2map');
var stream = saxer.createStream();
View
@@ -36,7 +36,7 @@ var sparqlTemplates = {
dcterms:date \"%date%\" .\
}}",
- listGraphURIs : "SELECT ?graph WHERE { GRAPH ?graph {} }"
+ listGraphURIs : "SELECT DISTINCT ?graph WHERE { GRAPH ?graph {} }"
};
//make it visible to other scripts
View
@@ -1,11 +1,18 @@
/*
+ * REPLACE WITH SRX2MAP_MULTI when HTML bits figured out
+ *
+ * THIS VERSION ASSUMED ONLY ONE VALUE FOR EACH BINDING NAME
+ *
* Parses SPARQL XML results, converts bindings to simple JSON mappings
+
*
* see docs/hello.srx for example of XML format
*
* sax is https://github.com/isaacs/sax-js
* SAX-like XML parser
*
+ * REPLACE WITH SRC2MAP_MULTI when HTML bits figured out
+ *
*/
// module imports
View
@@ -0,0 +1,100 @@
+/*
+ * THIS WILL REPLACE SRX2MAP when HTML bits figured out
+ *
+ * Parses SPARQL XML results, converts bindings to simple JSON mappings
+* returns an array of binding name/values
+*
+ * (part of http://www.w3.org/TR/rdf-sparql-json-res/ )
+ * see docs/hello.srx for example of XML format
+ *
+ * sax is https://github.com/isaacs/sax-js
+ * SAX-like XML parser
+ *
+ */
+
+// module imports
+var fs = require("fs");
+var sax = require("sax");
+
+// sax settings
+var strict = true; // set to false for html-mode
+var options = {};
+
+// flag for position in XML <binding> ...here if true ... </binding>
+var inBinding = false;
+
+// values to extract from the XML
+var element = "";
+var bindingName = "";
+var bindingValue = "";
+var bindingType = "";
+
+
+
+// create a stream parser
+var saxStream = sax.createStream(strict, options);
+
+// make it available to other scripts
+exports.createStream = function() {
+ return saxStream;
+};
+
+// minimal parse error handling
+saxStream.on("error", function(e) {
+ console.error("error!", e);
+});
+
+// handle an opening tag
+// loosely corresponds to SAX startElement(...)
+saxStream.on("opentag", function(node) {
+
+ element = node.name;
+ // console.log("ELEMENT = "+element);
+
+ // if results block starting, initialize container for results
+ if (element == "results")
+ this.bindings = [];
+
+ // get data from an individual binding
+ if (element == "binding") {
+ inBinding = true;
+ bindingName = node.attributes.name;
+ }
+ if (inBinding && element != "binding")
+ bindingType = element;
+
+});
+
+// handle text data in element content
+// loosely corresponds to SAX characters(...)
+saxStream.ontext = function(text) {
+ bindingValue = text;
+};
+
+// handle an opening tag
+// loosely corresponds to SAX endElement(...)
+saxStream.on("closetag", function(nodename) {
+ if (inBinding && nodename != "binding") {
+
+ // build the data
+ // with types - not used here, but keep in case needed later
+ // this.bindings[bindingName] = {};
+ // this.bindings[bindingName].value = bindingValue;
+ // this.bindings[bindingName].type = bindingType;
+
+ // put the result data into the JSON object
+ var binding = {};
+ binding[bindingName] = bindingValue;
+ this.bindings.push(binding);
+ // this.bindings[bindingName] = bindingValue;
+ inBinding = false;
+ }
+
+ // end of results block, for debugging
+ // (sax-js hasn't got SAX startDocument/endDocument)
+ if (nodename == "results") {
+ // console.log("END RESULTS");
+ // console.log("VALS: "+JSON.stringify(this.bindings));
+ // console.log("TITLE: "+JSON.stringify(bindings.title.value));
+ }
+})

0 comments on commit 375703c

Please sign in to comment.