Attean

 view release on metacpan or  search on metacpan

lib/AtteanX/Parser/RDFXML.pm  view on Meta::CPAN


=item C<< bnode_prefix >>

=item C<< blank_nodes >>

A string prefix for identifiers generated for blank nodes.

=back

=head1 METHODS

=over 4

=cut

use v5.14;
use warnings;

package AtteanX::Parser::RDFXML 0.038 {
	use Moo;
	use Types::Standard qw(Bool HashRef ArrayRef HashRef Str Object Maybe InstanceOf ConsumerOf);
	use Attean;
	use Attean::RDF;
	
	use Carp;
	use Encode;
	use XML::SAX;
	use Data::Dumper;
	use Scalar::Util qw(blessed);
	use Module::Load::Conditional qw[can_load];

=item C<< canonical_media_type >>

Returns the canonical media type for SPARQL XML: application/sparql-results+json.

=cut

	sub canonical_media_type { return "application/rdf+xml" }

=item C<< media_types >>

Returns a list of media types that may be parsed with the SPARQL XML parser:
application/sparql-results+json.

=cut

	sub media_types { return [qw(application/rdf+xml application/octet-stream)]; }
	
=item C<< file_extensions >>

Returns a list of file extensions that may be parsed with the parser.

=cut

	sub file_extensions { return [qw(rdf xrdf)] }

	with 'Attean::API::TripleParser', 'Attean::API::AbbreviatingParser', 'Attean::API::Parser';
	with 'Attean::API::PushParser';

	has 'bnode_prefix'	=> (is => 'ro', isa => Str, default => '');
	has 'blank_nodes'	=> (is => 'ro', isa => HashRef[ConsumerOf['Attean::API::Blank']], default => sub { +{} });
	
=item C<< parse_cb_from_io( $fh ) >>

Calls the C<< $parser->handler >> function once for each
L<Attean::API::Binding> object that result from parsing
the data read from the L<IO::Handle> object C<< $fh >>.

=cut

	sub parse_cb_from_io {
		my $self	= shift;
		$self->_parse(@_);
	}
	
=item C<< parse_cb_from_bytes( $data ) >>

Calls the C<< $parser->handler >> function once for each
L<Attean::API::Binding> object that result from parsing
the data read from the UTF-8 encoded byte string C<< $data >>.

=cut

	sub parse_cb_from_bytes {
		my $self	= shift;
		$self->_parse(@_);
	}

	sub _parse {
		my $self	= shift;
		my $data	= shift;

		my @args;
		if (my $map = $self->namespaces) {
			push(@args, namespaces => $map);
		}
		
		if ($self->has_base) {
			push(@args, base => $self->base);
		}
		my $new_iri		= sub { $self->new_iri(@_) };
		my $saxhandler	= AtteanX::Parser::RDFXML::SAXHandler->new( bnode_prefix => $self->bnode_prefix, blank_nodes => $self->blank_nodes, handler => $self->handler, new_iri => $new_iri, @args );
		my $p			= XML::SAX::ParserFactory->parser(Handler => $saxhandler);
		$saxhandler->push_base( $self->base ) if ($self->has_base);
		eval {
			if (ref($data)) {
				$p->parse_file($data);
			} else {
				if (length($data) > 0) {
					$p->parse_string($data);
				}
			}
		};
		
		if ($@) {
			if ($@ =~ /no element found at line 1, column 0, byte/) {
				# silence XML::Parser output on empty input
			} else {
				die $@;
			}
		}



( run in 0.410 second using v1.01-cache-2.11-cpan-8f98c5d2c55 )