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.16';
4              
5 1     1   15442 use Catmandu::Sane;
  1         72119  
  1         7  
6 1     1   219 use Moo;
  1         1  
  1         4  
7 1     1   1273 use XML::Struct::Reader;
  0            
  0            
8             use XML::LibXML::Reader;
9             use Catmandu::Fix::Has;
10              
11             with 'Catmandu::Fix::Base';
12              
13             has field => (fix_arg => 1);
14             has attributes => (fix_opt => 1);
15             has ns => (fix_opt => 1);
16             has content => (fix_opt => 1);
17             has simple => (fix_opt => 1);
18             has root => (fix_opt => 1);
19             has depth => (fix_opt => 1);
20             has path => (fix_opt => 1);
21             has whitespace => (fix_opt => 1);
22              
23             has _reader => (
24             is => 'ro',
25             lazy => 1,
26             builder => sub {
27             XML::Struct::Reader->new(
28             map { $_ => $_[0]->$_ } grep { defined $_[0]->$_ }
29             qw(attributes ns simple root depth content whitespace)
30             );
31             }
32             );
33              
34             sub emit {
35             my ($self,$fixer) = @_;
36              
37             my $path = $fixer->split_path($self->field);
38             my $key = pop @$path;
39            
40             my $reader = $fixer->capture($self->_reader);
41             my $xpath = $fixer->capture($self->path);
42              
43             return $fixer->emit_walk_path($fixer->var,$path,sub{
44             my $var = $_[0];
45             $fixer->emit_get_key($var,$key,sub{
46             my $var = $_[0];
47             return "my \$stream = XML::LibXML::Reader->new( string => ${var} );".
48             "${var} = ${xpath} ? [ ${reader}->readDocument(\$stream, ${xpath}) ] " .
49             ": ${reader}->readDocument(\$stream);";
50             });
51             });
52             }
53              
54             1;
55             __END__