Skip to content

Registering a custom parser

Antonio Garrote edited this page Aug 15, 2015 · 1 revision

To add a parser for a particular media-type, you need to pass the information during the instantiation of the store.

An example can be found in the frontend directory, where a RDFa parser is registered.

This is an example of how to register the parser:

rdfstore.create({"communication": {
                     "parsers": {
                       "text/html" :           rdfstore_frontend.rdfaParser,
                       "application/rdf+xml":  rdfstore_frontend.rdfParser
                     },
                   "precedences": ["text/n3", "text/turtle", "application/rdf+xml", "text/html", "application/json"] }
                  }, callback);

The 'communication' argument receives an object with two properties:

  • 'parsers': a map of media types to parsers for each of this media types.
  • 'precedences': a list of media types indicating which format is preferred during content negotiation when loading a remote resource.

The other important thing to take into account is the interface for the parser. The parser object must implement a 'parse' function accepting the raw data of the serialised RDF graph, a graph object for the named graph where the RDF data will be inserted, an options argument that can be ignored and a callback function that must be invoked with the parsed data or indicating an error.

Again, an example from the frontend:

RDFaParser.parse = function(data, graph, options, callback) {
  // parsing logic here

  callback(null, quads);
}

The 'quads' objects returned in the parser callback must meet the format expected by the store:

// named node
{'uri': uri}

// blank node
{'blank' : '_:'+counter}

// literal
{'literal': literalLexicalForm}

Take a look at the files in the frontend for a working example:

Clone this wiki locally