File Coverage

blib/lib/Catmandu/Fix/xml_read.pm
Criterion Covered Total %
statement 7 9 77.7
branch n/a
condition n/a
subroutine 3 3 100.0
pod n/a
total 10 12 83.3


line stmt bran cond sub pod time code
1             package Catmandu::Fix::xml_read;
2              
3             our $VERSION = '0.15';
4              
5 1     1   21467 use Catmandu::Sane;
  1         112182  
  1         8  
6 1     1   281 use Moo;
  1         1  
  1         5  
7 1     1   1554 use XML::Struct::Reader;
  0            
  0            
8             use XML::LibXML::Reader;
9              
10             with 'Catmandu::Fix::Base';
11              
12             has field => (is => 'ro', required => 1);
13             has attributes => (is => 'ro');
14             has ns => (is => 'ro');
15             has content => (is => 'ro');
16             has simple => (is => 'ro');
17             has root => (is => 'ro');
18             has depth => (is => 'ro');
19             has path => (is => 'ro');
20             has whitespace => (is => 'ro');
21              
22             around BUILDARGS => sub {
23             my ($orig,$class,$field,%opts) = @_;
24             $orig->($class,
25             field => $field,
26             map { $_ => $opts{$_} }
27             qw(attributes ns simple root depth content path whitespace)
28             );
29             };
30              
31             has _reader => (
32             is => 'ro',
33             lazy => 1,
34             builder => sub {
35             XML::Struct::Reader->new(
36             map { $_ => $_[0]->$_ } grep { defined $_[0]->$_ }
37             qw(attributes ns simple root depth content whitespace)
38             );
39             }
40             );
41              
42             sub emit {
43             my ($self,$fixer) = @_;
44              
45             my $path = $fixer->split_path($self->field);
46             my $key = pop @$path;
47            
48             my $reader = $fixer->capture($self->_reader);
49             my $xpath = $fixer->capture($self->path);
50              
51             return $fixer->emit_walk_path($fixer->var,$path,sub{
52             my $var = $_[0];
53             $fixer->emit_get_key($var,$key,sub{
54             my $var = $_[0];
55             return "my \$stream = XML::LibXML::Reader->new( string => ${var} );".
56             "${var} = ${xpath} ? [ ${reader}->readDocument(\$stream, ${xpath}) ] " .
57             ": ${reader}->readDocument(\$stream);";
58             });
59             });
60             }
61              
62             1;
63             __END__