blib/lib/XML/Saxtract.pm

filter: Statementstmt 91.2% 125 / 137 Branchbran 78.0% 39 / 50 Conditioncond 77.7% 14 / 18 MC/DCmcdc 60.0% 6 / 10 Subroutinesub 100% 16 / 16 Podpod 100% 2 / 2 totaltotal 86.7% 202 / 233 SCAR 36.0
CC
34
Cov
87%
_add_value
10.1
end_element
10.0
start_element
8.3
CRAP 36.6
↑ Summary
1 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 package XML::Saxtract;
5 1 $XML::Saxtract::VERSION = '1.02';
6 # ABSTRACT: Streaming parse XML data into a result hash based upon a specification hash
7 # PODNAME: XML::Saxtract
8  
9 1 ▶use Exporter qw(import);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 our @EXPORT_OK = qw(saxtract_string saxtract_url);
11  
12 1 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use XML::SAX;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 sub saxtract_string {
16 9 ▶ my $xml_string = shift;
Subroutine saxtract_string: called CC=1 SCAR=0.0
Pod documented
17 9 my $spec = shift;
18 9 my %options = @_;
19  
20 9 my $handler = XML::Saxtract::ContentHandler->new( $spec, $options{object} );
21 9 my $parser = XML::SAX::ParserFactory->parser( Handler => $handler );
22 9 $parser->parse_string($xml_string);
23  
24 9 return $handler->get_result();
25 }
26  
27 sub saxtract_url {
28 1 ▶ my $uri = shift;
Subroutine saxtract_url: called CC=4 SCAR=17.3
Pod documented
29 1 my $spec = shift;
30 1 my %options = @_;
31  
32 1 ▶ my $agent = $options{agent} || LWP::UserAgent->new();
Condition33%1/3
$options{'agent'} || 'LWP::UserAgent'->new
l!l&&r!l&&!r
010
MC/DC0%0/2
$options{'agent'} || 'LWP::UserAgent'->new
$options{'agent'} 'LWP::UserAgent'->new
Truth table33%1/3
A || B
A= $options{'agent'}B= 'LWP::UserAgent'->new
ABresult
1X1
011
000
33  
34 1 my $response = $agent->get($uri);
35 1 ▶ if ( !$response->is_success() ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($response->is_success) 0 1 1
36 0 ▶ if ( $options{die_on_failure} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options{'die_on_failure'}) { } 0 0 0
37 0 die($response);
38 }
39 else {
40 0 return;
41 }
42 }
43  
44 1 return saxtract_string( $response->content(), $spec, %options );
45 }
46  
47 package XML::Saxtract::ContentHandler;
48 1 $XML::Saxtract::ContentHandler::VERSION = '1.02';
49 1 ▶use parent qw(Class::Accessor);
Subroutine BEGIN: called CC=1 SCAR=0.0
50 1 __PACKAGE__->follow_best_practice;
51 1 __PACKAGE__->mk_ro_accessors(qw(result));
52  
53 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
54  
55 sub new {
56 9 ▶ my ( $class, @args ) = @_;
Subroutine new: called CC=1 SCAR=0.0
57 9 my $self = bless( {}, $class );
58  
59 9 return $self->_init(@args);
60 }
61  
62 sub _add_value {
63 44 ▶ my $object = shift;
Subroutine _add_value: called CC=10 SCAR=23.1
64 44 my $spec = shift;
65 44 my $value = shift;
66  
67 44 my $type = ref($spec);
68 44 ▶ if ( !$type ) {
Branch: 3 branches
BranchTrueFalseTotal
if (not $type) { } 32 12 44
elsif ($type eq 'SCALAR') { } 0 12 12
elsif ($type eq 'CODE') { } 1 11 12
69 32 $object->{$spec} = $value;
70 }
71 elsif ( $type eq 'SCALAR' ) {
72 0 $object->{$$spec} = $value;
73 }
74 elsif ( $type eq 'CODE' ) {
75 1 &$spec( $object, $value );
76 }
77 else {
78 11 my $name = $spec->{name};
79 11 ▶ if ( $spec->{type} eq 'array' ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($spec->{'type'} eq 'array') { } 2 9 11
elsif ($spec->{'type'} eq 'map') { } 4 5 9
elsif ($spec->{'type'} eq 'first') { } 5 0 5
80 2 ▶ if ( !defined( $object->{$name} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $object->{$name}) 1 1 2
81 1 $object->{$name} = [];
82 }
83 2 push( @{ $object->{$name} }, $value );
84 }
85 elsif ( $spec->{type} eq 'map' ) {
86 4 ▶ if ( !defined( $object->{$name} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $object->{$name}) 2 2 4
87 2 $object->{$name} = {};
88 }
89 4 $object->{$name}{ $value->{ $spec->{key} } } = $value;
90 }
91 elsif ( $spec->{type} eq 'first' ) {
92 5 ▶ if ( !defined( $object->{$name} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $object->{$name}) 3 2 5
93 3 $object->{$name} = $value;
94 }
95 }
96 else {
97 # type 'last' or default
98 0 $object->{$name} = $value;
99 }
100 }
101 }
102  
103 sub characters {
104 29 ▶ my ( $self, $characters ) = @_;
Subroutine characters: called CC=3 SCAR=11.3
105 29 ▶ return if ( $self->{skip} > 0 );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"skip"} > 0 0 29 29
106  
107 29 ▶ if ( defined($characters) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $characters) 29 0 29
108 29 push( @{ $self->{buffer} }, $characters->{Data} );
109 }
110 }
111  
112 sub end_element {
113 23 ▶ my ( $self, $element ) = @_;
Subroutine end_element: called CC=10 SCAR=23.1
114  
115 23 ▶ if ( $self->{skip} > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"skip"} > 0) 0 23 23
116 0 $self->{skip}--;
117 0 return;
118 }
119  
120 23 my $stack_element = pop( @{ $self->{element_stack} } );
121 23 my $name = $stack_element->{name};
122 23 my $attrs = $stack_element->{attrs};
123 23 my $spec = $stack_element->{spec};
124 23 my $path = $stack_element->{spec_path};
125 23 my $result = $stack_element->{result};
126  
127 23 ▶ if ( defined( $spec->{$path} ) && scalar( @{ $self->{buffer} } ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $spec->{$path} and scalar @{$self->{"buffer"};}) 16 7 23
Condition100%2/2
defined $spec->{$path} and scalar @{$self->{"buffer"};}
!ll
716
MC/DC100%1/1
defined $spec->{$path} and scalar @{$self->{"buffer"};}
defined $spec->{$path}
Truth table100%2/2
A
A= defined $spec->{$path}
Aresult
00
11
128 16 my $buffer_data = join( '', @{ $self->{buffer} } );
129 16 $buffer_data =~ s/^\s*//;
130 16 $buffer_data =~ s/\s*$//;
131 16 _add_value( $result, $spec->{$path}, $buffer_data );
132 }
133  
134 23 foreach my $attr ( values(%$attrs) ) {
135 27 my $ns_uri = $attr->{NamespaceURI};
136 my $attr_path = join( '',
137 $path, '/@', ( $ns_uri && $spec->{$ns_uri} ? "$spec->{$ns_uri}:" : '' ),
138 27 ▶ $attr->{LocalName} );
Branch: 1 branch
BranchTrueFalseTotal
$ns_uri && $spec->{$ns_uri} ? : 5 22 27
Condition100%3/3
$ns_uri and $spec->{$ns_uri}
!ll&&!rl&&r
1755
MC/DC100%2/2
$ns_uri and $spec->{$ns_uri}
$ns_uri $spec->{$ns_uri}
Truth table100%3/3
A and B
A= $ns_uriB= $spec->{$ns_uri}
ABresult
0X0
100
111
139  
140 27 ▶ if ( $spec->{$attr_path} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($spec->{$attr_path}) 17 10 27
141 17 _add_value( $result, $spec->{$attr_path}, $attr->{Value} );
142 }
143 }
144  
145 23 ▶ if ( !$path && scalar( @{ $self->{element_stack} } ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $path and scalar @{$self->{"element_stack"};}) 11 12 23
Condition100%2/2
not $path and scalar @{$self->{"element_stack"};}
!ll
1211
MC/DC100%1/1
not $path and scalar @{$self->{"element_stack"};}
not $path
Truth table100%2/2
not(A)
A= not $path
Aresult
00
11
146 11 my $parent_element = $self->{element_stack}[-1];
147 11 my $path_in_parent = "$parent_element->{spec_path}/$name";
148 11 _add_value( $parent_element->{result}, $parent_element->{spec}{$path_in_parent},
149 $result );
150 }
151  
152 23 $self->{buffer} = [];
153 }
154  
155 sub _init {
156 9 ▶ my ( $self, $spec, $result ) = @_;
Subroutine _init: called CC=2 SCAR=7.0
157  
158 9 ▶ $self->{result} = $result || {};
Condition50%1/2
$result || {}
l!l
09
MC/DC0%0/1
$result || {}
$result
Truth table50%1/2
A
A= $result
Aresult
11
00
159 $self->{element_stack} = [
160 { spec => $spec,
161 spec_path => '',
162 result => $self->{result}
163 }
164 9 ];
165 9 $self->{buffer} = [];
166 9 $self->{skip} = 0;
167  
168 9 return $self;
169 }
170  
171 sub _spec_prefix {
172 19 ▶ my ( $self, $uri ) = @_;
Subroutine _spec_prefix: called CC=3 SCAR=11.2
173  
174 19 ▶ for ( my $i = scalar( @{ $self->{element_stack} } ) - 1; $i >= 0; $i-- ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i >= 0) 23 0 23
175 23 my $spec_prefix = $self->{element_stack}[$i]->{spec}{$uri};
176 23 ▶ return $spec_prefix if ( defined($spec_prefix) );
Branch: 1 branch
BranchTrueFalseTotal
if defined $spec_prefix 19 4 23
177 }
178  
179 0 return;
180 }
181  
182 sub start_element {
183 23 ▶ my ( $self, $element ) = @_;
Subroutine start_element: called CC=8 SCAR=21.2
184  
185 23 ▶ if ( $self->{skip} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"skip"}) 0 23 23
186 0 $self->{skip}++;
187 0 return;
188 }
189  
190 23 my $stack_top = $self->{element_stack}[-1];
191 23 my $spec = $stack_top->{spec};
192 23 my $result = $stack_top->{result};
193 23 my $uri = $element->{NamespaceURI};
194  
195 23 my $qname;
196 23 ▶ if ($uri) {
Branch: 1 branch
BranchTrueFalseTotal
if ($uri) { } 19 4 23
197 19 my $spec_prefix = $self->_spec_prefix($uri);
198 19 ▶ if ( !defined($spec_prefix) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $spec_prefix) { } 0 19 19
elsif ($spec_prefix eq '') { } 11 8 19
199  
200 # uri is not in spec, so nothing could possibly match
201 0 $self->{skip} = 1;
202 0 return;
203 }
204 elsif ( $spec_prefix eq '' ) {
205 11 $qname = $element->{LocalName};
206 }
207 else {
208 8 $qname = "$spec_prefix:$element->{LocalName}";
209 }
210 }
211 else {
212 4 $qname = $element->{LocalName};
213 }
214  
215 23 my $spec_path = "$stack_top->{spec_path}/$qname";
216 23 ▶ if ( defined( $spec->{$spec_path} )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $spec->{$spec_path} and ref $spec->{$spec_path} eq "HASH" and defined $spec->{$spec_path}{"spec"}) 11 12 23
Condition83%5/6
defined $spec->{$spec_path} and ref $spec->{$spec_path} eq "HASH" and defined $spec->{$spec_path}{"spec"}
!ll&&!rl&&r
12011
defined $spec->{$spec_path} and ref $spec->{$spec_path} eq "HASH"
!ll&&!rl&&r
4811
MC/DC66%2/3
defined $spec->{$spec_path} and ref $spec->{$spec_path} eq "HASH" and defined $spec->{$spec_path}{"spec"}
defined $spec->{$spec_path} ref $spec->{$spec_path} eq "HASH" defined $spec->{$spec_path}{"spec"}
Truth table75%3/4
A and B and C
A= defined $spec->{$spec_path}B= ref $spec->{$spec_path} eq "HASH"C= defined $spec->{$spec_path}{"spec"}
ABCresult
0XX0
10X0
1100
1111
217 && ref( $spec->{$spec_path} ) eq 'HASH'
218 && defined( $spec->{$spec_path}{spec} ) )
219 {
220 11 $spec = $spec->{$spec_path}{spec};
221 11 $spec_path = '';
222 11 $result = {};
223 }
224  
225 push(
226 23 @{ $self->{element_stack} },
227 { name => $qname,
228 attrs => $element->{Attributes},
229 23 spec => $spec,
230 spec_path => $spec_path,
231 result => $result
232 }
233 );
234 }
235  
236 1 1;
237  
238 __END__
↑ Summary