tasks/jsonld/js/jsonld.js
function _parseNQuads(input) {
  // define partial regexes
  var iri = '(?:<([^:]+:[^>]*)>)';
  var bnode = '(_:(?:[A-Za-z][A-Za-z0-9]*))';
  var plain = '"([^"\\\\]*(?:\\\\.[^"\\\\]*)*)"';
  var datatype = '(?:\\^\\^' + iri + ')';
  var language = '(?:@([a-z]+(?:-[a-z0-9]+)*))';
  var literal = '(?:' + plain + '(?:' + datatype + '|' + language + ')?)';
  var ws = '[ \\t]+';
  var wso = '[ \\t]*';
  var eoln = /(?:\r\n)|(?:\n)|(?:\r)/g;
  var empty = new RegExp('^' + wso + '$');

  // define quad part regexes
  var subject = '(?:' + iri + '|' + bnode + ')' + ws;
  var property = iri + ws;
  var object = '(?:' + iri + '|' + bnode + '|' + literal + ')' + wso;
  var graphName = '(?:\\.|(?:(?:' + iri + '|' + bnode + ')' + wso + '\\.))';

  // full quad regex
  var quad = new RegExp(
    '^' + wso + subject + property + object + graphName + wso + '$');

  // build RDF dataset
  var dataset = {};

  // split N-Quad input into lines
  var lines = input.split(eoln);
  var lineNumber = 0;
  for(var li = 0; li < lines.length; ++li) {
    var line = lines[li];
    lineNumber++;

    // skip empty lines
    if(empty.test(line)) {
      continue;
    }

    // parse quad
    var match = line.match(quad);
    if(match === null) {
      throw new JsonLdError(
        'Error while parsing N-Quads; invalid quad.',
        'jsonld.ParseError', {line: lineNumber});
    }

    // create RDF triple
    var triple = {};

    // get subject
    if(!_isUndefined(match[1])) {
      triple.subject = {type: 'IRI', value: match[1]};
    }
    else {
      triple.subject = {type: 'blank node', value: match[2]};
    }

    // get predicate
    triple.predicate = {type: 'IRI', value: match[3]};

    // get object
    if(!_isUndefined(match[4])) {
      triple.object = {type: 'IRI', value: match[4]};
    }
    else if(!_isUndefined(match[5])) {
      triple.object = {type: 'blank node', value: match[5]};
    }
    else {
      triple.object = {type: 'literal'};
      if(!_isUndefined(match[7])) {
        triple.object.datatype = match[7];
      }
      else if(!_isUndefined(match[8])) {
        triple.object.datatype = RDF_LANGSTRING;
        triple.object.language = match[8];
      }
      else {
        triple.object.datatype = XSD_STRING;
      }
      var unescaped = match[6]
        .replace(/\\"/g, '"')
        .replace(/\\t/g, '\t')
        .replace(/\\n/g, '\n')
        .replace(/\\r/g, '\r')
        .replace(/\\\\/g, '\\');
      triple.object.value = unescaped;
    }

    // get graph name ('@default' is used for the default graph)
    var name = '@default';
    if(!_isUndefined(match[9])) {
      name = match[9];
    }
    else if(!_isUndefined(match[10])) {
      name = match[10];
    }

    // initialize graph in dataset
    if(!(name in dataset)) {
      dataset[name] = [triple];
    }
    // add triple if unique to its graph
    else {
      var unique = true;
      var triples = dataset[name];
      for(var ti = 0; unique && ti < triples.length; ++ti) {
        if(_compareRDFTriples(triples[ti], triple)) {
          unique = false;
        }
      }
      if(unique) {
        triples.push(triple);
      }
    }
  }

  return dataset;
}
function _parseNQuads(input) {
  // define partial regexes
  var iri = '(?:<([^:]+:[^>]*)>)';
  var bnode = '(_:(?:[A-Za-z][A-Za-z0-9]*))';
  var plain = '"([^"\\\\]*(?:\\\\.[^"\\\\]*)*)"';
  var datatype = '(?:\\^\\^' + iri + ')';
  var language = '(?:@([a-z]+(?:-[a-z0-9]+)*))';
  var literal = '(?:' + plain + '(?:' + datatype + '|' + language + ')?)';
  var ws = '[ \\t]+';
  var wso = '[ \\t]*';
  var eoln = /(?:\r\n)|(?:\n)|(?:\r)/g;
  var empty = new RegExp('^' + wso + '$');

  // define quad part regexes
  var subject = '(?:' + iri + '|' + bnode + ')' + ws;
  var property = iri + ws;
  var object = '(?:' + iri + '|' + bnode + '|' + literal + ')' + wso;
  var graphName = '(?:\\.|(?:(?:' + iri + '|' + bnode + ')' + wso + '\\.))';

  // full quad regex
  var quad = new RegExp(
    '^' + wso + subject + property + object + graphName + wso + '$');

  // build RDF dataset
  var dataset = {};

  // split N-Quad input into lines
  var lines = input.split(eoln);
  var lineNumber = 0;
  for(var li = 0; li < lines.length; ++li) {
    var line = lines[li];
    lineNumber++;

    // skip empty lines
    if(empty.test(line)) {
      continue;
    }

    // parse quad
    var match = line.match(quad);
    if(match === null) {
      throw new JsonLdError(
        'Error while parsing N-Quads; invalid quad.',
        'jsonld.ParseError', {line: lineNumber});
    }

    // create RDF triple
    var triple = {};

    // get subject
    if(!_isUndefined(match[1])) {
      triple.subject = {type: 'IRI', value: match[1]};
    }
    else {
      triple.subject = {type: 'blank node', value: match[2]};
    }

    // get predicate
    triple.predicate = {type: 'IRI', value: match[3]};

    // get object
    if(!_isUndefined(match[4])) {
      triple.object = {type: 'IRI', value: match[4]};
    }
    else if(!_isUndefined(match[5])) {
      triple.object = {type: 'blank node', value: match[5]};
    }
    else {
      triple.object = {type: 'literal'};
      if(!_isUndefined(match[7])) {
        triple.object.datatype = match[7];
      }
      else if(!_isUndefined(match[8])) {
        triple.object.datatype = RDF_LANGSTRING;
        triple.object.language = match[8];
      }
      else {
        triple.object.datatype = XSD_STRING;
      }
      var unescaped = match[6]
        .replace(/\\"/g, '"')
        .replace(/\\t/g, '\t')
        .replace(/\\n/g, '\n')
        .replace(/\\r/g, '\r')
        .replace(/\\\\/g, '\\');
      triple.object.value = unescaped;
    }

    // get graph name ('@default' is used for the default graph)
    var name = '@default';
    if(!_isUndefined(match[9])) {
      name = match[9];
    }
    else if(!_isUndefined(match[10])) {
      name = match[10];
    }

    // initialize graph in dataset
    if(!(name in dataset)) {
      dataset[name] = [triple];
    }
    // add triple if unique to its graph
    else {
      var unique = true;
      var triples = dataset[name];
      for(var ti = 0; unique && ti < triples.length; ++ti) {
        if(_compareRDFTriples(triples[ti], triple)) {
          unique = false;
        }
      }
      if(unique) {
        triples.push(triple);
      }
    }
  }

  return dataset;
}
function _parseNQuads ( input ) { // define partial regexes var iri = '(?:<([^:]+:[^>]*)>)' ; var bnode = '(_:(?:[A-Za-z][A-Za-z0-9]*))' ; var plain = '"([^"\\\\]*(?:\\\\.[^"\\\\]*)*)"' ; var datatype = '(?:\\^\\^' + iri + ')' ; var language = '(?:@([a-z]+(?:-[a-z0-9]+)*))' ; var literal = '(?:' + plain + '(?:' + datatype + '|' + language + ')?)' ; var ws = '[ \\t]+' ; var wso = '[ \\t]*' ; var eoln = / (?:\r\n)|(?:\n)|(?:\r) / g ; var empty = new RegExp ( '^' + wso + '$' ) ; // define quad part regexes var subject = '(?:' + iri + '|' + bnode + ')' + ws ; var property = iri + ws ; var object = '(?:' + iri + '|' + bnode + '|' + literal + ')' + wso ; var graphName = '(?:\\.|(?:(?:' + iri + '|' + bnode + ')' + wso + '\\.))' ; // full quad regex var quad = new RegExp ( '^' + wso + subject + property + object + graphName + wso + '$' ) ; // build RDF dataset var dataset = { } ; // split N-Quad input into lines var lines = input . split ( eoln ) ; var lineNumber = 0 ; for ( var li = 0 ; li < lines . length ; ++ li ) { var line = lines [ li ] ; lineNumber ++ ; // skip empty lines if ( empty . test ( line ) ) { continue ; } // parse quad var match = line . match ( quad ) ; if ( match === null ) { throw new JsonLdError ( 'Error while parsing N-Quads; invalid quad.' , 'jsonld.ParseError' , { line : lineNumber } ) ; } // create RDF triple var triple = { } ; // get subject if ( ! _isUndefined ( match [ 1 ] ) ) { triple . subject = { type : 'IRI' , value : match [ 1 ] } ; } else { triple . subject = { type : 'blank node' , value : match [ 2 ] } ; } // get predicate triple . predicate = { type : 'IRI' , value : match [ 3 ] } ; // get object if ( ! _isUndefined ( match [ 4 ] ) ) { triple . object = { type : 'IRI' , value : match [ 4 ] } ; } else if ( ! _isUndefined ( match [ 5 ] ) ) { triple . object = { type : 'blank node' , value : match [ 5 ] } ; } else { triple . object = { type : 'literal' } ; if ( ! _isUndefined ( match [ 7 ] ) ) { triple . object . datatype = match [ 7 ] ; } else if ( ! _isUndefined ( match [ 8 ] ) ) { triple . object . datatype = RDF_LANGSTRING ; triple . object . language = match [ 8 ] ; } else { triple . object . datatype = XSD_STRING ; } var unescaped = match [ 6 ] . replace ( / \\" / g , '"' ) . replace ( / \\t / g , '\t' ) . replace ( / \\n / g , '\n' ) . replace ( / \\r / g , '\r' ) . replace ( / \\\\ / g , '\\' ) ; triple . object . value = unescaped ; } // get graph name ('@default' is used for the default graph) var name = '@default' ; if ( ! _isUndefined ( match [ 9 ] ) ) { name = match [ 9 ] ; } else if ( ! _isUndefined ( match [ 10 ] ) ) { name = match [ 10 ] ; } // initialize graph in dataset if ( ! ( name in dataset ) ) { dataset [ name ] = [ triple ] ; } // add triple if unique to its graph else { var unique = true ; var triples = dataset [ name ] ; for ( var ti = 0 ; unique && ti < triples . length ; ++ ti ) { if ( _compareRDFTriples ( triples [ ti ] , triple ) ) { unique = false ; } } if ( unique ) { triples . push ( triple ) ; } } } return dataset ; }
Parses RDF in the form of N-Quads.

@param input the N-Quads input to parse.

@return an RDF dataset.
Parses RDF in the form of N - Quads .