blib/lib/XML/Saxtract.pm

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