Esempio n. 1
0
            public RDFDataset Parse(JToken input)
            {
                // by public decree, references to example.org are normalized to https going forward...
                var converted = ((string)input).Replace("http://example.org/", "https://example.org/");

                return(RDFDatasetUtils.ParseNQuads(converted));
            }
Esempio n. 2
0
 /// <exception cref="JsonLD.Core.JsonLdError"></exception>
 public virtual RDFDataset Parse(JToken input)
 {
     if (input.Type == JTokenType.String)
     {
         return(RDFDatasetUtils.ParseNQuads((string)input));
     }
     else
     {
         throw new JsonLdError(JsonLdError.Error.InvalidInput, "NQuad Parser expected string input."
                               );
     }
 }
        internal static string Run()
        {
            var doc  = JObject.Parse(_docJson);
            var opts = new JsonLdOptions();
            var rdf  = (RDFDataset)JsonLdProcessor.ToRDF(doc, opts);

            var serialized = RDFDatasetUtils.ToNQuads(rdf); // serialize RDF to string

            Console.WriteLine(serialized);

            /*
             *
             * Output:
             * <http://example.org/ld-experts> <http://schema.org/member> _:b0 .
             * <http://example.org/ld-experts> <http://schema.org/name> "LD Experts" .
             * _:b0 <http://schema.org/image> <http://manu.sporny.org/images/manu.png> .
             * _:b0 <http://schema.org/name> "Manu Sporny" .
             * _:b0 <http://schema.org/url> <http://manu.sporny.org/> .
             * _:b0 <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://schema.org/Person> .
             *
             */

            return(serialized);
        }
Esempio n. 4
0
 public virtual object Call(RDFDataset dataset)
 {
     return(RDFDatasetUtils.ToNQuads(dataset));
 }
Esempio n. 5
0
        public IEnumerator <object[]> GetEnumerator()
        {
            foreach (string manifest in manifests)
            {
                JToken manifestJson;

                manifestJson = GetJson(manifest);

                foreach (JObject testcase in manifestJson["sequence"])
                {
                    Func <JToken>   run;
                    ConformanceCase newCase = new ConformanceCase();

                    newCase.input   = GetJson(testcase["input"]);
                    newCase.context = GetJson(testcase["context"]);
                    newCase.frame   = GetJson(testcase["frame"]);

                    var options = new JsonLdOptions("http://json-ld.org/test-suite/tests/" + (string)testcase["input"]);

                    var testType = (JArray)testcase["@type"];

                    if (testType.Any((s) => (string)s == "jld:NegativeEvaluationTest"))
                    {
                        newCase.error = testcase["expect"];
                    }
                    else if (testType.Any((s) => (string)s == "jld:PositiveEvaluationTest"))
                    {
                        if (testType.Any((s) => new List <string> {
                            "jld:ToRDFTest", "jld:NormalizeTest"
                        }.Contains((string)s)))
                        {
                            newCase.output = File.ReadAllText(Path.Combine("W3C", (string)testcase["expect"]));
                        }
                        else if (testType.Any((s) => (string)s == "jld:FromRDFTest"))
                        {
                            newCase.input  = File.ReadAllText(Path.Combine("W3C", (string)testcase["input"]));
                            newCase.output = GetJson(testcase["expect"]);
                        }
                        else
                        {
                            newCase.output = GetJson(testcase["expect"]);
                        }
                    }
                    else
                    {
                        throw new Exception("Expecting either positive or negative evaluation test.");
                    }

                    JToken optionToken;
                    JToken value;

                    if (testcase.TryGetValue("option", out optionToken))
                    {
                        JObject optionDescription = (JObject)optionToken;

                        if (optionDescription.TryGetValue("compactArrays", out value))
                        {
                            options.SetCompactArrays((bool)value);
                        }
                        if (optionDescription.TryGetValue("base", out value))
                        {
                            options.SetBase((string)value);
                        }
                        if (optionDescription.TryGetValue("expandContext", out value))
                        {
                            newCase.context = GetJson(testcase["option"]["expandContext"]);
                            options.SetExpandContext((JObject)newCase.context);
                        }
                        if (optionDescription.TryGetValue("produceGeneralizedRdf", out value))
                        {
                            options.SetProduceGeneralizedRdf((bool)value);
                        }
                        if (optionDescription.TryGetValue("useNativeTypes", out value))
                        {
                            options.SetUseNativeTypes((bool)value);
                        }
                        if (optionDescription.TryGetValue("useRdfType", out value))
                        {
                            options.SetUseRdfType((bool)value);
                        }
                    }

                    if (testType.Any((s) => (string)s == "jld:CompactTest"))
                    {
                        run = () => JsonLdProcessor.Compact(newCase.input, newCase.context, options);
                    }
                    else if (testType.Any((s) => (string)s == "jld:ExpandTest"))
                    {
                        run = () => JsonLdProcessor.Expand(newCase.input, options);
                    }
                    else if (testType.Any((s) => (string)s == "jld:FlattenTest"))
                    {
                        run = () => JsonLdProcessor.Flatten(newCase.input, newCase.context, options);
                    }
                    else if (testType.Any((s) => (string)s == "jld:FrameTest"))
                    {
                        run = () => JsonLdProcessor.Frame(newCase.input, newCase.frame, options);
                    }
                    else if (testType.Any((s) => (string)s == "jld:NormalizeTest"))
                    {
                        run = () => new JValue(
                            RDFDatasetUtils.ToNQuads((RDFDataset)JsonLdProcessor.Normalize(newCase.input, options)).Replace("\n", "\r\n")
                            );
                    }
                    else if (testType.Any((s) => (string)s == "jld:ToRDFTest"))
                    {
                        options.format = "application/nquads";
                        run            = () => new JValue(
                            ((string)JsonLdProcessor.ToRDF(newCase.input, options)).Replace("\n", "\r\n")
                            );
                    }
                    else if (testType.Any((s) => (string)s == "jld:FromRDFTest"))
                    {
                        options.format = "application/nquads";
                        run            = () => JsonLdProcessor.FromRDF(newCase.input, options);
                    }
                    else
                    {
                        run = () => { throw new Exception("Couldn't find a test type, apparently."); };
                    }

                    if ((string)manifestJson["name"] == "Remote document")
                    {
                        Func <JToken> innerRun = run;
                        run = () =>
                        {
                            var remoteDoc = options.documentLoader.LoadDocument("https://json-ld.org/test-suite/tests/" + (string)testcase["input"]);
                            newCase.input = remoteDoc.Document;
                            options.SetBase(remoteDoc.DocumentUrl);
                            options.SetExpandContext((JObject)remoteDoc.Context);
                            return(innerRun());
                        };
                    }

                    if (testType.Any((s) => (string)s == "jld:NegativeEvaluationTest"))
                    {
                        Func <JToken> innerRun = run;
                        run = () =>
                        {
                            try
                            {
                                return(innerRun());
                            }
                            catch (JsonLdError err)
                            {
                                JObject result = new JObject();
                                result["error"] = err.Message;
                                return(result);
                            }
                        };
                    }

                    newCase.run = run;

                    yield return(new object[] { manifest + (string)testcase["@id"], (string)testcase["name"], newCase });
                }
            }
        }
Esempio n. 6
0
        /// <exception cref="JsonLD.Core.JsonLdError"></exception>
        public virtual RDFDataset Parse(JToken input)
        {
            if (!(input.Type == JTokenType.String))
            {
                throw new JsonLdError(JsonLdError.Error.InvalidInput, "Invalid input; Triple RDF Parser requires a string input"
                                      );
            }
            RDFDataset result = new RDFDataset();

            TurtleRDFParser.State state = new TurtleRDFParser.State(this, (string)input);
            while (!string.Empty.Equals(state.line))
            {
                // check if line is a directive
                Matcher match = TurtleRDFParser.Regex.Directive.Matcher(state.line);
                if (match.Find())
                {
                    if (match.Group(1) != null || match.Group(4) != null)
                    {
                        string ns = match.Group(1) != null?match.Group(1) : match.Group(4);

                        string iri = match.Group(1) != null?match.Group(2) : match.Group(5);

                        if (!iri.Contains(":"))
                        {
                            iri = state.baseIri + iri;
                        }
                        iri = RDFDatasetUtils.Unescape(iri);
                        ValidateIRI(state, iri);
                        state.namespaces[ns] = iri;
                        result.SetNamespace(ns, iri);
                    }
                    else
                    {
                        string @base = match.Group(3) != null?match.Group(3) : match.Group(6);

                        @base = RDFDatasetUtils.Unescape(@base);
                        ValidateIRI(state, @base);
                        if ([email protected](":"))
                        {
                            state.baseIri = state.baseIri + @base;
                        }
                        else
                        {
                            state.baseIri = @base;
                        }
                    }
                    state.AdvanceLinePosition(match.Group(0).Length);
                    continue;
                }
                if (state.curSubject == null)
                {
                    // we need to match a subject
                    match = TurtleRDFParser.Regex.Subject.Matcher(state.line);
                    if (match.Find())
                    {
                        string iri;
                        if (match.Group(1) != null)
                        {
                            // matched IRI
                            iri = RDFDatasetUtils.Unescape(match.Group(1));
                            if (!iri.Contains(":"))
                            {
                                iri = state.baseIri + iri;
                            }
                        }
                        else
                        {
                            if (match.Group(2) != null)
                            {
                                // matched NS:NAME
                                string ns   = match.Group(2);
                                string name = UnescapeReserved(match.Group(3));
                                iri = state.ExpandIRI(ns, name);
                            }
                            else
                            {
                                if (match.Group(4) != null)
                                {
                                    // match ns: only
                                    iri = state.ExpandIRI(match.Group(4), string.Empty);
                                }
                                else
                                {
                                    if (match.Group(5) != null)
                                    {
                                        // matched BNODE
                                        iri = state.namer.GetName(match.Group(0).Trim());
                                    }
                                    else
                                    {
                                        // matched anon node
                                        iri = state.namer.GetName();
                                    }
                                }
                            }
                        }
                        // make sure IRI still matches an IRI after escaping
                        ValidateIRI(state, iri);
                        state.curSubject = iri;
                        state.AdvanceLinePosition(match.Group(0).Length);
                    }
                    else
                    {
                        // handle blank nodes
                        if (state.line.StartsWith("["))
                        {
                            string bnode = state.namer.GetName();
                            state.AdvanceLinePosition(1);
                            state.Push();
                            state.curSubject = bnode;
                        }
                        else
                        {
                            // handle collections
                            if (state.line.StartsWith("("))
                            {
                                string bnode = state.namer.GetName();
                                // so we know we want a predicate if the collection close
                                // isn't followed by a subject end
                                state.curSubject = bnode;
                                state.AdvanceLinePosition(1);
                                state.Push();
                                state.curSubject   = bnode;
                                state.curPredicate = JSONLDConsts.RdfFirst;
                            }
                            else
                            {
                                // make sure we have a subject already
                                throw new JsonLdError(JsonLdError.Error.ParseError, "Error while parsing Turtle; missing expected subject. {line: "
                                                      + state.lineNumber + "position: " + state.linePosition + "}");
                            }
                        }
                    }
                }
                if (state.curPredicate == null)
                {
                    // match predicate
                    match = TurtleRDFParser.Regex.Predicate.Matcher(state.line);
                    if (match.Find())
                    {
                        string iri = string.Empty;
                        if (match.Group(1) != null)
                        {
                            // matched IRI
                            iri = RDFDatasetUtils.Unescape(match.Group(1));
                            if (!iri.Contains(":"))
                            {
                                iri = state.baseIri + iri;
                            }
                        }
                        else
                        {
                            if (match.Group(2) != null)
                            {
                                // matched NS:NAME
                                string ns   = match.Group(2);
                                string name = UnescapeReserved(match.Group(3));
                                iri = state.ExpandIRI(ns, name);
                            }
                            else
                            {
                                if (match.Group(4) != null)
                                {
                                    // matched ns:
                                    iri = state.ExpandIRI(match.Group(4), string.Empty);
                                }
                                else
                                {
                                    // matched "a"
                                    iri = JSONLDConsts.RdfType;
                                }
                            }
                        }
                        ValidateIRI(state, iri);
                        state.curPredicate = iri;
                        state.AdvanceLinePosition(match.Group(0).Length);
                    }
                    else
                    {
                        throw new JsonLdError(JsonLdError.Error.ParseError, "Error while parsing Turtle; missing expected predicate. {line: "
                                              + state.lineNumber + "position: " + state.linePosition + "}");
                    }
                }
                // expecting bnode or object
                // match BNODE values
                if (state.line.StartsWith("["))
                {
                    string bnode = state.namer.GetName();
                    result.AddTriple(state.curSubject, state.curPredicate, bnode);
                    state.AdvanceLinePosition(1);
                    // check for anonymous objects
                    if (state.line.StartsWith("]"))
                    {
                        state.AdvanceLinePosition(1);
                    }
                    else
                    {
                        // next we expect a statement or object separator
                        // otherwise we're inside the blank node
                        state.Push();
                        state.curSubject = bnode;
                        // next we expect a predicate
                        continue;
                    }
                }
                else
                {
                    // match collections
                    if (state.line.StartsWith("("))
                    {
                        state.AdvanceLinePosition(1);
                        // check for empty collection
                        if (state.line.StartsWith(")"))
                        {
                            state.AdvanceLinePosition(1);
                            result.AddTriple(state.curSubject, state.curPredicate, JSONLDConsts.RdfNil);
                        }
                        else
                        {
                            // next we expect a statement or object separator
                            // otherwise we're inside the collection
                            string bnode = state.namer.GetName();
                            result.AddTriple(state.curSubject, state.curPredicate, bnode);
                            state.Push();
                            state.curSubject   = bnode;
                            state.curPredicate = JSONLDConsts.RdfFirst;
                            continue;
                        }
                    }
                    else
                    {
                        // match object
                        match = TurtleRDFParser.Regex.Object.Matcher(state.line);
                        if (match.Find())
                        {
                            string iri = null;
                            if (match.Group(1) != null)
                            {
                                // matched IRI
                                iri = RDFDatasetUtils.Unescape(match.Group(1));
                                if (!iri.Contains(":"))
                                {
                                    iri = state.baseIri + iri;
                                }
                            }
                            else
                            {
                                if (match.Group(2) != null)
                                {
                                    // matched NS:NAME
                                    string ns   = match.Group(2);
                                    string name = UnescapeReserved(match.Group(3));
                                    iri = state.ExpandIRI(ns, name);
                                }
                                else
                                {
                                    if (match.Group(4) != null)
                                    {
                                        // matched ns:
                                        iri = state.ExpandIRI(match.Group(4), string.Empty);
                                    }
                                    else
                                    {
                                        if (match.Group(5) != null)
                                        {
                                            // matched BNODE
                                            iri = state.namer.GetName(match.Group(0).Trim());
                                        }
                                    }
                                }
                            }
                            if (iri != null)
                            {
                                ValidateIRI(state, iri);
                                // we have a object
                                result.AddTriple(state.curSubject, state.curPredicate, iri);
                            }
                            else
                            {
                                // we have a literal
                                string value    = match.Group(6);
                                string lang     = null;
                                string datatype = null;
                                if (value != null)
                                {
                                    // we have a string literal
                                    value = UnquoteString(value);
                                    value = RDFDatasetUtils.Unescape(value);
                                    lang  = match.Group(7);
                                    if (lang == null)
                                    {
                                        if (match.Group(8) != null)
                                        {
                                            datatype = RDFDatasetUtils.Unescape(match.Group(8));
                                            if (!datatype.Contains(":"))
                                            {
                                                datatype = state.baseIri + datatype;
                                            }
                                            ValidateIRI(state, datatype);
                                        }
                                        else
                                        {
                                            if (match.Group(9) != null)
                                            {
                                                datatype = state.ExpandIRI(match.Group(9), UnescapeReserved(match.Group(10)));
                                            }
                                            else
                                            {
                                                if (match.Group(11) != null)
                                                {
                                                    datatype = state.ExpandIRI(match.Group(11), string.Empty);
                                                }
                                            }
                                        }
                                    }
                                    else
                                    {
                                        datatype = JSONLDConsts.RdfLangstring;
                                    }
                                }
                                else
                                {
                                    if (match.Group(12) != null)
                                    {
                                        // integer literal
                                        value    = match.Group(12);
                                        datatype = JSONLDConsts.XsdDouble;
                                    }
                                    else
                                    {
                                        if (match.Group(13) != null)
                                        {
                                            // decimal literal
                                            value    = match.Group(13);
                                            datatype = JSONLDConsts.XsdDecimal;
                                        }
                                        else
                                        {
                                            if (match.Group(14) != null)
                                            {
                                                // double literal
                                                value    = match.Group(14);
                                                datatype = JSONLDConsts.XsdInteger;
                                            }
                                            else
                                            {
                                                if (match.Group(15) != null)
                                                {
                                                    // boolean literal
                                                    value    = match.Group(15);
                                                    datatype = JSONLDConsts.XsdBoolean;
                                                }
                                            }
                                        }
                                    }
                                }
                                result.AddTriple(state.curSubject, state.curPredicate, value, datatype, lang);
                            }
                            state.AdvanceLinePosition(match.Group(0).Length);
                        }
                        else
                        {
                            throw new JsonLdError(JsonLdError.Error.ParseError, "Error while parsing Turtle; missing expected object or blank node. {line: "
                                                  + state.lineNumber + "position: " + state.linePosition + "}");
                        }
                    }
                }
                // close collection
                bool collectionClosed = false;
                while (state.line.StartsWith(")"))
                {
                    if (!JSONLDConsts.RdfFirst.Equals(state.curPredicate))
                    {
                        throw new JsonLdError(JsonLdError.Error.ParseError, "Error while parsing Turtle; unexpected ). {line: "
                                              + state.lineNumber + "position: " + state.linePosition + "}");
                    }
                    result.AddTriple(state.curSubject, JSONLDConsts.RdfRest, JSONLDConsts.RdfNil);
                    state.Pop();
                    state.AdvanceLinePosition(1);
                    collectionClosed = true;
                }
                bool expectDotOrPred = false;
                // match end of bnode
                if (state.line.StartsWith("]"))
                {
                    string bnode = state.curSubject;
                    state.Pop();
                    state.AdvanceLinePosition(1);
                    if (state.curSubject == null)
                    {
                        // this is a bnode as a subject and we
                        // expect either a . or a predicate
                        state.curSubject = bnode;
                        expectDotOrPred  = true;
                    }
                }
                // match list separator
                if (!expectDotOrPred && state.line.StartsWith(","))
                {
                    state.AdvanceLinePosition(1);
                    // now we expect another object/bnode
                    continue;
                }
                // match predicate end
                if (!expectDotOrPred)
                {
                    while (state.line.StartsWith(";"))
                    {
                        state.curPredicate = null;
                        state.AdvanceLinePosition(1);
                        // now we expect another predicate, or a dot
                        expectDotOrPred = true;
                    }
                }
                if (state.line.StartsWith("."))
                {
                    if (state.expectingBnodeClose)
                    {
                        throw new JsonLdError(JsonLdError.Error.ParseError, "Error while parsing Turtle; missing expected )\"]\". {line: "
                                              + state.lineNumber + "position: " + state.linePosition + "}");
                    }
                    state.curSubject   = null;
                    state.curPredicate = null;
                    state.AdvanceLinePosition(1);
                    // this can now be the end of the document.
                    continue;
                }
                else
                {
                    if (expectDotOrPred)
                    {
                        // we're expecting another predicate since we didn't find a dot
                        continue;
                    }
                }
                // if we're in a collection
                if (JSONLDConsts.RdfFirst.Equals(state.curPredicate))
                {
                    string bnode = state.namer.GetName();
                    result.AddTriple(state.curSubject, JSONLDConsts.RdfRest, bnode);
                    state.curSubject = bnode;
                    continue;
                }
                if (collectionClosed)
                {
                    // we expect another object
                    // TODO: it's not clear yet if this is valid
                    continue;
                }
                // if we get here, we're missing a close statement
                throw new JsonLdError(JsonLdError.Error.ParseError, "Error while parsing Turtle; missing expected \"]\" \",\" \";\" or \".\". {line: "
                                      + state.lineNumber + "position: " + state.linePosition + "}");
            }
            return(result);
        }
Esempio n. 7
0
 public virtual JToken Call(RDFDataset dataset)
 {
     return(RDFDatasetUtils.ToNQuads(dataset));
 }
Esempio n. 8
0
 public object Call(RDFDataset dataset) =>
 RDFDatasetUtils.ToNQuads(dataset);     // serialize the RDF dataset as NQuads