Attean
view release on metacpan or search on metacpan
lib/AtteanX/Parser/SPARQLXML/SAXHandler.pm view on Meta::CPAN
AtteanX::Parser::SPARQLXML::SAXHandler - XML parser for SPARQL XML Results format
=head1 VERSION
This document describes AtteanX::Parser::SPARQLXML::SAXHandler version 0.038
=head1 STATUS
This module's API and functionality should be considered unstable.
In the future, this module may change in backwards-incompatible ways,
or be removed entirely.
=head1 SYNOPSIS
use AtteanX::Parser::SPARQLXML::SAXHandler;
=head1 METHODS
=over 4
=cut
package AtteanX::Parser::SPARQLXML::SAXHandler 0.038;
use v5.14;
use warnings;
use Attean;
use Scalar::Util qw(refaddr);
use base qw(XML::SAX::Base);
use Attean;
use namespace::clean;
my %strings;
my %tagstack;
my %results;
my %values;
my %bindings;
my %booleans;
my %variables;
my %has_head;
my %has_end;
my %result_count;
my %result_handlers;
my %config;
my %triples;
my %expecting_string = map { $_ => 1 } qw(boolean bnode uri literal);
=item C<< new ( [ \&handler ] ) >>
Returns a new XML::SAX handler object. If C<< &handler >> is supplied, it will
be called with a variable bindings object as each is parsed, bypassing the
normal process of collecting the results for retrieval via an iterator object.
=cut
sub new {
my $class = shift;
my $self = $class->SUPER::new();
if (@_) {
my $addr = refaddr( $self );
my $code = shift;
my $args = shift || {};
$result_handlers{ $addr } = $code;
$config{ $addr } = { %$args };
}
return $self;
}
=begin private
=item C<< start_element >>
=cut
sub start_element {
my $self = shift;
my $el = shift;
my $tag = $el->{LocalName};
my $addr = refaddr( $self );
unshift( @{ $tagstack{ $addr } }, [$tag, $el] );
if ($expecting_string{ $tag }) {
$strings{ $addr } = '';
}
if ($tag eq 'triple') {
push(@{ $triples{ $addr } }, {});
}
}
=item C<< end_element >>
=cut
sub end_element {
my $self = shift;
my $class = ref($self);
my $eel = shift;
my $addr = refaddr( $self );
my $string = $strings{ $addr };
my $taginfo = shift( @{ $tagstack{ $addr } } );
my ($tag, $el) = @$taginfo;
if ($tag eq 'head') {
$has_head{ $addr } = 1;
if (my $code = $result_handlers{ $addr }) {
if ($config{ $addr }{ variables }) {
$code->( $variables{ $addr } );
}
}
} elsif ($tag eq 'sparql') {
$has_end{ $addr } = 1;
} elsif ($tag eq 'variable') {
push( @{ $variables{ $addr } }, $el->{Attributes}{'{}name'}{Value});
} elsif ($tag eq 'boolean') {
$booleans{ $addr } = ($string eq 'true') ? 1 : 0;
if ($string =~ /^(?:true|false)$/ and my $code = $result_handlers{ $addr }) {
$code->( Attean::Literal->$string() );
( run in 0.715 second using v1.01-cache-2.11-cpan-8f98c5d2c55 )