RDFStore

 view release on metacpan or  search on metacpan

lib/RDFStore/Parser/NTriples.pm  view on Meta::CPAN

    				my $err = $@;
    				croak $err 	
					if $err;
                        } else {
				croak "Cannot fetch '$file_uri'";
				};
    		} else {
			my $filename= $file_uri->file;

			# FIXME: it might be wrong in some cases
			local(*FILE);
			open(FILE, $filename) 
				or  croak "Couldn't open $filename:\n$!";
			binmode(FILE);
			eval {
				$ret = $class->parse(*FILE,$file_uri,@_);
    				};
    			my $err = $@;
    			close(FILE);
    			croak $err 	
				if $err;
			};
		return $ret;
  		};
	};

sub addTriple {
        my ($class,$subject,$predicate,$object,$context) = @_;

#print STDERR "addTriple('".$subject->toString."','".$predicate->toString."','".$object->toString."'".( ($context) ? ",'".$context->toString."'" : '' ).")",((caller)[2]),"\n";

        # If there is no subject (about=""), then use the URI/filename where the RDF description came from
	$subject = $class->{nodeFactory}->createResource($class->{sSource})
		unless( (defined $subject) && ($subject->toString()) && (length($subject->toString())>0) );

	#Trigger 'Assert' event
        my $assert = $class->{Handlers}->{Assert}
		if(ref($class->{Handlers}) =~ /HASH/);
        if (defined($assert)) {
        	return &$assert($class, $class->{nodeFactory}->createStatement($subject,$predicate,$object,$context) );
	} else {
		return;
		};
	};

sub newReificationID {
	my ($class) = @_;

#print STDERR "newReificationID($class): ",((caller)[2]),"\n";

	return 'genid' . $class->{iReificationCounter}++;
	};

1;
};

__END__

=head1 NAME

RDFStore::Parser::NTriples - This module implements a streaming N-Triples parser 

=head1 SYNOPSIS

	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(),
                style_options   =>      {
                                        persistent      =>      1,
                                        seevalues       =>      1,
                                        store_options         =>      { Name => '/tmp/test' }
                                }
        );
	$pstore->parsefile('http://www.gils.net/bsr-gils.nt');


=head1 DESCRIPTION

This module implements a N-Triples I<streaming> parser.

=head1 METHODS

=over 4

=item new

This is a class method, the constructor for RDFStore::Parser::NTriples. B<Options> are passed as keyword value
pairs. Recognized options are:

=over 4

=item * NodeFactory

This option is B<mandatory> to run the RDFStore::Parser::NTriples parser correctly and must contain a reference to an object of type RDFStore::NodeFactory(3). Such a reference is used during the RDF parsing to create resources, literal and statements...
with the RDFStore package.

=item * Source

This option can be specified by the user to set a base URI to use for the generation of resource URIs during parsing. If this option is omitted the parser will try to generate a prefix for generated resources using the input filename or URL actually ...

=item * GenidNumber

Seed the genid numbers with the given value

=item * Style

This option provides an easy way to set a given style of parser. There is one sample Sylte module provided with the RDFStore::Parser::NTriples distribution called RDFStore::Parser::Styles::RDFStore::Model. Such a module uses the RDFStore::Model(3) to...
Custom styles can be provided by giving a full package name containing
at least one '::'. This package should then have subs defined for each
handler it wishes to have installed. See L<"WRITE YOUR OWN PARSER"> below
for a discussion on how to build one.

=item * Handlers

When provided, this option should be an anonymous hash containing as
keys the type of handler and as values a sub reference to handle that
type of event. All the handlers get passed as their 1st parameter the
instance of Expat that is parsing the document. Further details on
handlers can be found in L<"HANDLERS">. Any handler set here
overrides the corresponding handler set with the Style option.

=item * ErrorContext

This is an XML::Parser option. When this option is defined, errors are reported
in context. The value should be the number of lines to show on either side
of the line in which the error occurred.

=back

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).

=item  setHandlers(TYPE, HANDLER [, TYPE, HANDLER [...]])

This method registers handlers for various parser events. It overrides any
previous handlers registered through the Style or Handler options or through
earlier calls to setHandlers. By providing a false or undefined value as
the handler, the existing handler can be unset.

This method returns a list of type, handler pairs corresponding to the



( run in 2.364 seconds using v1.01-cache-2.11-cpan-437f7b0c052 )