|
NAMERDFStore::Parser::NTriples - This module implements a streaming N-Triples parserSYNOPSISuse RDFStore::Parser::NTriples; use RDFStore::NodeFactory; my $p=new RDFStore::Parser::NTriples( ErrorContext => 2, Handlers => { Init => sub { print "INIT\n"; }, Final => sub { print "FINAL\n"; }, Assert => sub { print "STATEMENT - @_\n"; } }, NodeFactory => new RDFStore::NodeFactory() ); $p->parsefile('http://www.gils.net/bsr-gils.nt'); $p->parsefile('http://www.gils.net/rdf/bsr-gils.nt'); $p->parsefile('/some/where/my.nt'); $p->parsefile('file:/some/where/my.nt'); $p->parse(*STDIN); use RDFStore::Parser::NTriples; use RDFStore::NodeFactory; my $pstore=new RDFStore::Parser::NTriples( ErrorContext => 2, Style => 'RDFStore::Parser::Styles::RDFStore::Model', NodeFactory => new RDFStore::NodeFactory(), style_options => { persistent => 1, seevalues => 1, store_options => { Name => '/tmp/test' } } ); $pstore->parsefile('http://www.gils.net/bsr-gils.nt'); DESCRIPTIONThis module implements a N-Triples streaming parser.METHODS
All the other XML::Parser and XML::Parser::Expat options should work freely with RDFStore::Parser::NTriples see XML::Parser(3) and XML::Parser::Expat(3).
HANDLERSThe parser is an event based parser. As the parser recognizes N-Triples then any handlers registered for that type of an event are called with suitable parameters.All handlers receive an instance of XML::Parser::Expat as their first argument. See "METHODS" in XML::Parser::Expat for a discussion of the methods that can be called on this object. Expat is needed to further process thing like rdf:parseType="Literal" as XML. Init (Expat)This is called just before the parsing of the document starts.Final (Expat)This is called just after parsing has finished, but only if no errors occurred during the parse. Parse returns what this returns.Assert (Expat, Statement)This event is generated when a new RDF statement has been generated by the parseer.start tag is recognized. Statement is of type RDFStore::Statement(3) as generated by the RDFStore::NodeFactory(3) passed as argument to the RDFStore::Parser::NTriples constructor.Start_XML_Literal (Expat, Element [, Attr, Val [,...]])This event is generated when an XML start tag is recognized within an RDF property with parseType="Literal". Element is the name of the XML element type that is opened with the start tag. The Attr & Val pairs are generated for each attribute in the start tag.This handler should return a string containing either the original XML chunck or one f its transformations, perhaps using XSLT. Stop_XML_Literal (Expat, Element)This event is generated when an XML end tag is recognized within an RDF property with parseType="Literal". Note that an XML empty tag (<foo/>) generates both a Start_XML_Literal and an Stop_XML_Literal event.Char_XML_Literal (Expat, String)This event is generated when non-markup is recognized within an RDF property with parseType="Literal". The non-markup sequence of characters is in String. A single non-markup sequence of encoding of the string in the original document, this is given to the handler in UTF-8.This handler should return the processed text as a string. WRITE YOUR OWN PARSERYou can either make you Perl script a parser self by embedding the needed function hooks or write a custom Style module for RDFStore::Parser::NTriples.*.pl scriptsuse RDFStore::Parser::NTriples; use RDFStore::NodeFactory; my $p=new RDFStore::Parser::NTriples( Handlers => { Init => sub { print "INIT\n"; }, Final => sub { print "FINAL\n"; }, Assert => sub { print "STATEMENT - @_\n"; } }, NodeFactory => new RDFStore::NodeFactory() ); or something like: use RDFStore::Parser::NTriples; use RDFStore::NodeFactory; my $p=new RDFStore::Parser::NTriples( NodeFactory => new RDFStore::NodeFactory() ); $p->setHandlers( Init => sub { print "INIT\n"; }, Final => sub { print "FINAL\n"; }, Assert => sub { print join(",",@_),"\n"; } ); Style modulesA more sophisticated solution is to write a complete Perl5 Sytle module for RDFStore::Parser::NTriples that can be easily reused in your code. E.g. a perl script could use this piece of code:use RDFStore::Parser::NTriples; use RDFStore::Parser::NTriples::MyStyle; use RDFStore::NodeFactory; my $p=new RDFStore::Parser::NTriples( Style => 'RDFStore::Parser::NTriples::MyStyle', NodeFactory => new RDFStore::NodeFactory() ); $p->parsefile('http://www.gils.net/bsr-gils.rdfs'); The Style module self could stored into a file like MyStyle.pm like this: package RDFStore::Parser::NTriples::MyStyle; sub Init { print "INIT\n"; }; sub Final { print "FINAL\n"; }; sub Assert { print "ASSERT: ", $_[1]->subject()->toString(), $_[1]->predicate()->toString(), $_[1]->object()->toString(), "\n"; }; sub Start_XML_Literal { print "STARTAG: ",$_[1],"\n"; }; sub Stop_XML_Literal { print "ENDTAG: ",$_[1],"\n"; }; sub Char_XML_Literal { print "UTF8 chrs: ",$_[1],"\n"; }; 1; SEE ALSORDFStore::Parser::SiRPAC(3), DBMS(3) and XML::Parser(3) XML::Parser::Expat(3) RDFStore::Model(3) RDFStore::NodeFactory(3) N-Triples - http://www.w3.org/TR/rdf-testcases/#ntriples RDF Model and Syntax Specification - http://www.w3.org/TR/rdf-syntax-grammar/ RDF Schema Specification 1.0 - http://www.w3.org/TR/rdf-schema/ AUTHORAlberto Reggiori <areggiori@webweaving.org>
Visit the GSP FreeBSD Man Page Interface. |