/// <summary>Parses RDF in the form of N-Quads.</summary> /// <remarks>Parses RDF in the form of N-Quads.</remarks> /// <param name="input">the N-Quads input to parse.</param> /// <returns>an RDF dataset.</returns> /// <exception cref="JsonLD.Core.JsonLdError"></exception> public static RDFDataset ParseNQuads(string input) { // build RDF dataset RDFDataset dataset = new RDFDataset(); // split N-Quad input into lines string[] lines = RDFDatasetUtils.Regex.Eoln.Split(input); int lineNumber = 0; foreach (string line in lines) { lineNumber++; // skip empty lines if (RDFDatasetUtils.Regex.EmptyOrComment.Matcher(line).Matches()) { continue; } // parse quad Matcher match = RDFDatasetUtils.Regex.Quad.Matcher(line); if (!match.Matches()) { throw new JsonLdError(JsonLdError.Error.SyntaxError, "Error while parsing N-Quads; invalid quad. line:" + lineNumber); } // get subject RDFDataset.Node subject; if (match.Group(1) != null) { var subjectIri = Unescape(match.Group(1)); AssertAbsoluteIri(subjectIri); subject = new RDFDataset.IRI(subjectIri); } else { subject = new RDFDataset.BlankNode(Unescape(match.Group(2))); } // get predicate var predicateIri = Unescape(match.Group(3)); AssertAbsoluteIri(predicateIri); RDFDataset.Node predicate = new RDFDataset.IRI(predicateIri); // get object RDFDataset.Node @object; if (match.Group(4) != null) { var objectIri = Unescape(match.Group(4)); AssertAbsoluteIri(objectIri); @object = new RDFDataset.IRI(objectIri); } else { if (match.Group(5) != null) { @object = new RDFDataset.BlankNode(Unescape(match.Group(5))); } else { string language = Unescape(match.Group(8)); string datatype = match.Group(7) != null?Unescape(match.Group(7)) : match.Group (8) != null ? JSONLDConsts.RdfLangstring : JSONLDConsts.XsdString; AssertAbsoluteIri(datatype); string unescaped = Unescape(match.Group(6)); @object = new RDFDataset.Literal(unescaped, datatype, language); } } // get graph name ('@default' is used for the default graph) string name = "@default"; if (match.Group(9) != null) { name = Unescape(match.Group(9)); AssertAbsoluteIri(name); } else { if (match.Group(10) != null) { name = Unescape(match.Group(10)); } } RDFDataset.Quad triple = new RDFDataset.Quad(subject, predicate, @object, name); // initialise graph in dataset if (!dataset.ContainsKey(name)) { IList <RDFDataset.Quad> tmp = new List <RDFDataset.Quad>(); tmp.Add(triple); dataset[name] = tmp; } else { // add triple if unique to its graph IList <RDFDataset.Quad> triples = (IList <RDFDataset.Quad>)dataset[name]; if (!triples.Contains(triple)) { triples.Add(triple); } } } return(dataset); }