RDFStore::Parser::NTriples - This module implements a streaming N-Triples parser
use 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(),
store => {
persistent => 1,
seevalues => 1,
options => { name => '/tmp/test' }
}
);
$pstore->parsefile('http://www.gils.net/bsr-gils.nt');
This module implements a N-Triples streaming parser.
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).
This method returns a list of type, handler pairs corresponding to the input. The handlers returned are the ones that were in effect prior to the call.
See a description of the handler types in HANDLERS.
A die call is thrown if a parse error occurs. Otherwise it will return 1 or whatever is returned from the Final handler, if one is installed. In other words, what parse may return depends on the style.
e.g. the RDFStore::Parser::NTriples::Style::RDFStore::Model Style module returns an instance of RDFStore::Model
getReificationCounter()
The 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 the XML::Parser::Expat manpage 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.
This is called just before the parsing of the document starts.
This is called just after parsing has finished, but only if no errors occurred during the parse. Parse returns what this returns.
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.
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.
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.
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.
You 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.
use 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"; } );
A 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;
RDFStore::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/
Alberto Reggiori <areggiori@webweaving.org>