Beispiel #1
0
        public void ParsingRdfABadProfile()
        {
            RdfAParser parser = new RdfAParser(RdfASyntax.RDFa_1_1);
            parser.Warning += TestTools.WarningPrinter;

            Graph g = new Graph();
            parser.Load(g, "bad_profile.xhtml");

            TestTools.ShowGraph(g);

            Assert.AreEqual(1, g.Triples.Count, "Should only produce 1 Triple");
        }
Beispiel #2
0
        public void Load(IRdfHandler handler, TextReader input)
        {
            if (handler == null)
            {
                throw new RdfParseException("Cannot parse RDF using a null RDF Handler");
            }
            if (input == null)
            {
                throw new RdfParseException("Cannpt parse RDF from a null input");
            }
            try
            {
                RdfACoreParserContext context = this.GetParserContext(handler, input);

                // Before we start parsing check if a Version has been specified
                if (context.HostLanguage.Version != null)
                {
                    switch (context.HostLanguage.Version)
                    {
                    case RdfAParser.XHtmlPlusRdfA10Version:
                    case RdfAParser.HtmlPlusRdfA10Version:
                        // If using RDFa 1.0 then should use the old parser instead
                        RdfAParser parser = new RdfAParser();
                        parser.Load(context.Handler, input);
                        return;
                    }
                }

                // For any other RDFa Version use this parser
                this.Parse(context);
            }
            catch
            {
                try
                {
                    input.Close();
                }
                catch
                {
                    // No catch actions just trying to clean up
                }
                throw;
            }
        }
Beispiel #3
0
        public void ParsingRdfABadSyntax()
        {
            RdfAParser parser = new RdfAParser();
            Graph g = new Graph();
            Console.WriteLine("Tests parsing a file which has much invalid RDFa syntax in it, some triples will be produced (6-8) but most of the triples are wrongly encoded and will be ignored");
            g.BaseUri = new Uri("http://www.wurvoc.org/vocabularies/om-1.6/Kelvin_scale");
            FileLoader.Load(g, "bad_rdfa.html");

            Console.WriteLine(g.Triples.Count + " Triples");
            foreach (Triple t in g.Triples)
            {
                Console.WriteLine(t.ToString());
            }

            Console.WriteLine();
            CompressingTurtleWriter ttlwriter = new CompressingTurtleWriter(WriterCompressionLevel.High);
            ttlwriter.HighSpeedModePermitted = false;
            ttlwriter.Save(g, "test.ttl");
        }
Beispiel #4
0
        public void ParsingRdfABadSyntax()
        {
            RdfAParser parser = new RdfAParser();
            Graph      g      = new Graph();

            Console.WriteLine("Tests parsing a file which has much invalid RDFa syntax in it, some triples will be produced (6-8) but most of the triples are wrongly encoded and will be ignored");
            g.BaseUri = new Uri("http://www.wurvoc.org/vocabularies/om-1.6/Kelvin_scale");
            FileLoader.Load(g, "resources\\bad_rdfa.html");

            Console.WriteLine(g.Triples.Count + " Triples");
            foreach (Triple t in g.Triples)
            {
                Console.WriteLine(t.ToString());
            }

            Console.WriteLine();
            CompressingTurtleWriter ttlwriter = new CompressingTurtleWriter(WriterCompressionLevel.High);

            ttlwriter.HighSpeedModePermitted = false;
            ttlwriter.Save(g, "test.ttl");
        }
        public static void Main(string[] args)
        {
            StreamWriter output = new StreamWriter("RdfATestSuite.txt");
            String[] wantTrace = {  };
            String[] wantOutput = {  };
            bool outputAll = true;
            bool traceAll = false;
            String[] skipTests = { 
                                   "0002.xhtml",
                                   "0003.xhtml", 
                                   "0004.xhtml",
                                   "0005.xhtml",
                                   "0016.xhtml",
                                   "0022.xhtml",
                                   "0024.xhtml",
                                   "0028.xhtml",
                                   "0043.xhtml",
                                   "0044.xhtml",
                                   "0045.xhtml",
                                   "0095.xhtml",
                                   "0096.xhtml",
                                   "0097.xhtml",
                                   "0098.xhtml",
                                   "0122.xhtml",
                                   "0123.xhtml",
                                   "0124.xhtml",
                                   "0125.xhtml",
                                   "0126.xhtml"
                                 };

            String[] skipCheck = {
                                     "0011.xhtml",
                                     "0092.xhtml",
                                     "0094.xhtml",
                                     "0100.xhtml",
                                     "0101.xhtml",
                                     "0102.xhtml",
                                     "0103.xhtml",
                                 };

            String[] falseTests = {
                                    "0042.xhtml",
                                    "0086.xhtml",
                                    "0095.xhtml",
                                    "0096.xhtml",
                                    "0097.xhtml",
                                    "0107.xhtml",
                                    "0116.xhtml",
                                    "0122.xhtml",
                                    "0125.xhtml"
                                  };

            Console.SetOut(output);

            try
            {
                int testsPassed = 0;
                int testsFailed = 0;
                String[] files = Directory.GetFiles("rdfa_tests");
                RdfAParser parser = new RdfAParser(RdfASyntax.AutoDetectLegacy);
                parser.Warning += new RdfReaderWarning(parser_Warning);
                SparqlQueryParser queryparser = new SparqlQueryParser();
                bool passed, passDesired;
                Graph g = new Graph();
                Stopwatch timer = new Stopwatch();
                long totalTime = 0;
                long totalTriples = 0;

                foreach (String file in files)
                {
                    timer.Reset();

                    if (skipTests.Contains(Path.GetFileName(file)))
                    {
                        output.WriteLine("## Skipping Test of File " + Path.GetFileName(file));
                        output.WriteLine();
                        continue;
                    }

                    if (Path.GetExtension(file) != ".html" && Path.GetExtension(file) != ".xhtml")
                    {
                        continue;
                    }

                    Debug.WriteLine("Testing File " + Path.GetFileName(file));
                    output.WriteLine("## Testing File " + Path.GetFileName(file));
                    output.WriteLine("# Test Started at " + DateTime.Now.ToString(TestSuite.TestSuiteTimeFormat));

                    passed = false;
                    passDesired = true;

                    try
                    {
                        g = new Graph();
                        g.BaseUri = new Uri("http://www.w3.org/2006/07/SWD/RDFa/testsuite/xhtml1-testcases/" + Path.GetFileName(file));
                        if (Path.GetFileNameWithoutExtension(file).StartsWith("bad"))
                        {
                            passDesired = false;
                            output.WriteLine("# Desired Result = Parsing Failed");
                        }
                        else
                        {
                            output.WriteLine("# Desired Result = Parses OK");
                        }

                        //if (traceAll || wantTrace.Contains(Path.GetFileName(file)))
                        //{
                        //    parser.TraceTokeniser = true;
                        //}
                        //else
                        //{
                        //    parser.TraceTokeniser = false;
                        //}

                        timer.Start();
                        parser.Load(g, file);
                        timer.Stop();

                        Console.WriteLine("Parsing took " + timer.ElapsedMilliseconds + "ms");

                        passed = true;
                        output.WriteLine("Parsed OK");

                        if (outputAll || wantOutput.Contains(Path.GetFileName(file)))
                        {
                            NTriplesWriter writer = new NTriplesWriter();
                            writer.Save(g, "rdfa_tests\\" + Path.GetFileNameWithoutExtension(file) + ".out");
                        }

                    }
                    catch (IOException ioEx)
                    {
                        reportError(output, "IO Exception", ioEx);
                    }
                    catch (RdfParseException parseEx)
                    {
                        reportError(output, "Parsing Exception", parseEx);
                    }
                    catch (RdfException rdfEx)
                    {
                        reportError(output, "RDF Exception", rdfEx);
                    }
                    catch (Exception ex)
                    {
                        reportError(output, "Other Exception", ex);
                    }
                    finally
                    {
                        timer.Stop();

                        //Write the Triples to the Output
                        foreach (Triple t in g.Triples)
                        {
                            Console.WriteLine(t.ToString());
                        }

                        //Now we run the Test SPARQL (if present)
                        if (File.Exists("rdfa_tests/" + Path.GetFileNameWithoutExtension(file) + ".sparql"))
                        {
                            if (skipCheck.Contains(Path.GetFileName(file)))
                            {
                                output.WriteLine("## Skipping Check of File " + Path.GetFileName(file));
                                output.WriteLine();
                            }
                            else
                            {
                                try
                                {
                                    SparqlQuery q = queryparser.ParseFromFile("rdfa_tests/" + Path.GetFileNameWithoutExtension(file) + ".sparql");
                                    Object results = g.ExecuteQuery(q);
                                    if (results is SparqlResultSet)
                                    {
                                        //The Result is the result of the ASK Query
                                        if (falseTests.Contains(Path.GetFileName(file)))
                                        {
                                            passed = !((SparqlResultSet)results).Result;
                                        }
                                        else
                                        {
                                            passed = ((SparqlResultSet)results).Result;
                                        }
                                    }
                                }
                                catch
                                {
                                    passed = false;
                                }
                            }
                        }

                        if (passed && passDesired)
                        {
                            //Passed and we wanted to Pass
                            testsPassed++;
                            output.WriteLine("# Result = Test Passed");
                            totalTime += timer.ElapsedMilliseconds;
                            totalTriples += g.Triples.Count;
                        }
                        else if (!passed && passDesired)
                        {
                            //Failed when we should have Passed
                            testsFailed++;
                            output.WriteLine("# Result = Test Failed");
                        }
                        else if (passed && !passDesired)
                        {
                            //Passed when we should have Failed
                            testsFailed++;
                            output.WriteLine("# Result = Test Failed");
                        }
                        else
                        {
                            //Failed and we wanted to Fail
                            testsPassed++;
                            output.WriteLine("# Result = Test Passed");
                        }

                        output.WriteLine("# Triples Generated = " + g.Triples.Count());
                        output.WriteLine("# Test Ended at " + DateTime.Now.ToString(TestSuite.TestSuiteTimeFormat));
                    }

                    output.WriteLine();
                }

                output.WriteLine(testsPassed + " Tests Passed");
                output.WriteLine(testsFailed + " Tests Failed");
                output.WriteLine();
                output.Write("Total Parsing Time was " + totalTime + " ms");
                if (totalTime > 1000)
                {
                    output.WriteLine(" (" + totalTime / 1000d + " seconds)");
                }
                output.WriteLine("Average Parsing Speed was " + totalTriples / (totalTime / 1000d) + " triples/second");

            }
            catch (Exception ex)
            {
                reportError(output, "Other Exception", ex);
            }

            Console.SetOut(Console.Out);
            Console.WriteLine("Done");
            Debug.WriteLine("Finished");

            output.Close();

        }
Beispiel #6
0
        public void Load(IRdfHandler handler, TextReader input)
        {
            if (handler == null) throw new RdfParseException("Cannot parse RDF using a null RDF Handler");
            if (input == null) throw new RdfParseException("Cannpt parse RDF from a null input");
            try
            {
                RdfACoreParserContext context = this.GetParserContext(handler, input);

                //Before we start parsing check if a Version has been specified
                if (context.HostLanguage.Version != null)
                {
                    switch (context.HostLanguage.Version)
                    {
                        case RdfAParser.XHtmlPlusRdfA10Version:
                        case RdfAParser.HtmlPlusRdfA10Version:
                            //If using RDFa 1.0 then should use the old parser instead
                            RdfAParser parser = new RdfAParser();
                            parser.Load(context.Handler, input);
                            return;
                    }
                }

                //For any other RDFa Version use this parser
                this.Parse(context);
            }
            catch
            {
                try
                {
                    input.Close();
                }
                catch
                {
                    //No catch actions just trying to clean up
                }
                throw;
            }
        }
        public void ParsingMalformedRdfA()
        {
            Console.WriteLine("Tests how the RDFa Parser handles RDFa from the web which is embedded in malformed HTML and is known to contain malformed RDFa");
            Console.WriteLine("For this we use MySpace RDFa");
            Console.WriteLine();

            RdfAParser parser = new RdfAParser();
            parser.Warning += new RdfReaderWarning(TestTools.WarningPrinter);

            List<Uri> testUris = new List<Uri>()
            {
                new Uri("http://www.myspace.com/coldplay"),
                new Uri("http://www.myspace.com/fashionismylife10")
            };

            foreach (Uri u in testUris) 
            {
                Console.WriteLine("Testing URI " + u.ToString());
                Graph g = new Graph();
                UriLoader.Load(g, u, parser);

                foreach (Triple t in g.Triples)
                {
                    Console.WriteLine(t.ToString());
                }
                Console.WriteLine();
            }
        }