RDFStore

 view release on metacpan or  search on metacpan

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

		$ret = $class->parse($arg, $namespace,@_);
		};
	my $err = $@;

	croak $err
		if $err;

	return $ret;
        };

sub parsefile {
	my $class = shift;

	$class->SUPER::parsefile( @_ );

	my $file = shift;

	if( (defined $file) && ($file ne '') ) {
		my $ret;
		my $file_uri;
		my $scheme;
		$scheme='file:'
			if( (-e $file) || (!($file =~ /^\w+:/)) );
                $file_uri= URI->new(((defined $scheme) ? $scheme : '' ).$file);
		if (	(defined $file_uri) && (defined $file_uri->scheme)	&&
			($file_uri->scheme ne 'file') ) {
  			my $content = $class->wget($file_uri);
			if(defined $content) {
				eval {
					$ret = $class->parsestring($content, $file_uri,@_);
    				};
    				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:



( run in 0.788 second using v1.01-cache-2.11-cpan-cdf2f3d4e48 )