lib/Spreadsheet/Reader/ExcelXML/XMLReader/PositionSharedStrings.pm

filter: Statementstmt 84.1% 101 / 120 Branchbran 84.3% 54 / 64 Conditioncond 85.3% 35 / 41 MC/DCmcdc 66.6% 16 / 24 Subroutinesub 100% 13 / 13 Podpod 100% 2 / 2 totaltotal 83.7% 221 / 264 CC 48 SCAR 35.9
CC
48
Cov
84%
get_shared_string
50.6
load_unique_bits
6.1
__ANON__
2.0
CRAP 36.2
« lib/Spreadsheet/Reader/ExcelXML/Worksheet.pm ↑ Summary lib/Spreadsheet/Reader/ExcelXML/WorksheetToRow.pm »
1 package Spreadsheet::Reader::ExcelXML::XMLReader::PositionSharedStrings;
2 20 our $AUTHORITY = 'cpan:JANDREW';
3 20 ▶use version; our $VERSION = version->declare('v0.16.8');
Subroutine BEGIN: called CC=1 SCAR=0.0
4 ###LogSD warn "You uncovered internal logging statements for Spreadsheet::Reader::ExcelXML::XMLReader::PositionSharedStrings-$VERSION";
5  
6 20 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 20 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 20 requires qw(
9 set_error where_am_i has_position
10 advance_element_position start_the_file_over i_am_here
11 parse_element get_group_return_type squash_node
12 current_named_node current_node_parsed close_the_file
13 good_load
14 );#grep_node
15 20 ▶use Types::Standard qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
16 Int Bool HashRef is_HashRef ArrayRef Enum is_Int
17 20 );
18 20 ▶use Carp qw( confess );
Subroutine BEGIN: called CC=1 SCAR=0.0
19 20 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 20 ▶use lib '../../../../../../lib';
Subroutine BEGIN: called CC=1 SCAR=0.0
21 ###LogSD use Log::Shiras::Telephone;
22  
23 #########1 Public Attributes 3#########4#########5#########6#########7#########8#########9
24  
25 20 has cache_positions =>(
26 isa => Bool,
27 reader => 'should_cache_positions',
28 default => 1,
29 );
30  
31 #########1 Public Methods 3#########4#########5#########6#########7#########8#########9
32  
33 267 ▶sub get_shared_string{
Subroutine get_shared_string: called CC=42 SCAR=39.2
Pod documented
34 267 my( $self, $position ) = @_;
35 ###LogSD my $phone = Log::Shiras::Telephone->new( name_space =>
36 ###LogSD $self->get_all_space . '::get_shared_string', );
37 267 ▶ if( !defined $position ){
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $position) { } 0 267 267
elsif (not &is_Int($position)) { } 0 267 267
38 0 $self->set_error( "Requested shared string position required - none passed" );
39 0 return undef;
40 }elsif( !is_Int( $position ) ){
41 0 confess "The passed position -$position- is not an integer";
42 }
43 ###LogSD $phone->talk( level => 'debug', message => [
44 ###LogSD "Getting the sharedStrings position: $position",
45 ###LogSD "From current position: " . ($self->has_position ? $self->where_am_i : '(none yet)'), ] );
46  
47 #checking if the reqested position is too far
48 267 ▶ if( $position > $self->_get_unique_count - 1 ){
Branch: 1 branch
BranchTrueFalseTotal
if ($position > $self->_get_unique_count - 1) 15 252 267
49 15 $self->set_error( "Asking for position -$position- (from 0) but the shared string " .
50 "max cell position is: " . ($self->_get_unique_count - 1) );
51 15 return undef;# fail
52 }
53  
54 252 my ( $return, $success );
55 # handle cache retrieval
56 252 ▶ if( $self->should_cache_positions and $self->_last_cache_position >= $position ){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->should_cache_positions and $self->_last_cache_position >= $position) 142 110 252
Condition100%3/3
$self->should_cache_positions and $self->_last_cache_position >= $position
!ll&&!rl&&r
2882142
MC/DC100%2/2
$self->should_cache_positions and $self->_last_cache_position >= $position
$self->should_cache_positions $self->_last_cache_position >= $position
Truth table100%3/3
A and B
A= $self->should_cache_positionsB= $self->_last_cache_position >= $position
ABresult
0X0
100
111
57 ###LogSD $phone->talk( level => 'debug', message => [
58 ###LogSD "Retreiving position -$position- from cache" ] );
59 142 $return = $self->_get_ss_position( $position );
60 142 $success = 1;
61 }
62  
63 # checking if the reqested (last) position is stored (no caching)
64 252 ▶ if( !$success and $self->_has_last_position and $position == $self->_get_last_position ){
Branch: 1 branch
BranchTrueFalseTotal
if (not $success and $self->_has_last_position and $position == $self->_get_last_position) 0 252 252
Condition83%5/6
not $success and $self->_has_last_position and $position == $self->_get_last_position
!ll&&!rl&&r
227250
not $success and $self->_has_last_position
!ll&&!rl&&r
1428525
MC/DC0%0/3
not $success and $self->_has_last_position and $position == $self->_get_last_position
not $success $self->_has_last_position $position == $self->_get_last_position
Truth table75%3/4
not(A) and B and C
A= not $successB= $self->_has_last_positionC= $position == $self->_get_last_position
ABCresult
0XX0
10X0
1100
1111
65 ###LogSD $phone->talk( level => 'debug', message => [
66 ###LogSD "Already built the answer for position: $position",
67 ###LogSD $self->_get_last_position_ref ] );
68 0 $return = $self->_get_last_position_ref;
69 0 $success = 1;
70 }
71  
72 ###LogSD $phone->talk( level => 'debug', message => [
73 ###LogSD "Success: " . ($success//'not yet'), "position state: " . $self->has_position ] );
74 # reset the file if needed
75 252 ▶ if( !$success and $self->has_position and $self->where_am_i > $position ){
Branch: 1 branch
BranchTrueFalseTotal
if (not $success and $self->has_position and $self->where_am_i > $position) 4 248 252
Condition100%6/6
not $success and $self->has_position and $self->where_am_i > $position
!ll&&!rl&&r
168804
not $success and $self->has_position
!ll&&!rl&&r
1422684
MC/DC100%3/3
not $success and $self->has_position and $self->where_am_i > $position
not $success $self->has_position $self->where_am_i > $position
Truth table100%4/4
not(A) and B and C
A= not $successB= $self->has_positionC= $self->where_am_i > $position
ABCresult
0XX0
10X0
1100
1111
76 ###LogSD $phone->talk( level => 'debug', message => [
77 ###LogSD "Current position: " . $self->where_am_i, "..against desired position: $position" ] );
78 4 $self->start_the_file_over;
79 ###LogSD $phone->talk( level => 'debug', message => [
80 ###LogSD "Finished resetting the file" ] );
81 }
82 ###LogSD $phone->talk( level => 'debug', message => [
83 ###LogSD "Reset tests complete" ] );
84  
85 # Kick start position counting for the first go-round
86 252 ▶ if( !$success and !$self->has_position ){
Branch: 1 branch
BranchTrueFalseTotal
unless ($success or $self->has_position) 30 222 252
Condition100%3/3
$success or $self->has_position
l!l&&r!l&&!r
1428030
MC/DC100%2/2
$success or $self->has_position
$success $self->has_position
Truth table100%3/3
A or B
A= $successB= $self->has_position
ABresult
1X1
011
000
87 ###LogSD $phone->talk( level => 'debug', message => [
88 ###LogSD "Kickstart position counting - getting first si cell" ] );
89  
90 30 my( $result, $node_name, $node_level, $result_ref );
91 30 my $current_node = $self->current_node_parsed;
92 ###LogSD $phone->talk( level => 'trace', message =>[
93 ###LogSD "The current node is:", $current_node ] );
94 30 ▶ if( (keys %$current_node)[0] eq 'ai' ){
Branch: 1 branch
BranchTrueFalseTotal
if ((keys %$current_node)[0] eq 'ai') { } 0 30 30
95 ###LogSD $phone->talk( level => 'trace', message =>[
96 ###LogSD "Found the core properties node" ] );
97 0 $result = 2;
98 0 $node_name = 'si';
99 }else{
100 30 ( $result, $node_name, $node_level, $result_ref ) =
101 $self->advance_element_position( 'si' );
102 }
103 30 ▶ if( $result ){
Branch: 1 branch
BranchTrueFalseTotal
if ($result) { } 30 0 30
104 ###LogSD $phone->talk( level => 'debug', message => [
105 ###LogSD "Successfully advanced one share string position" ] );
106 30 $self->i_am_here( 0 );
107 }else{
108 0 $self->set_error( "No sharedStrings elements available" );
109 0 $self->_set_unique_count( 0 );
110 0 return undef;
111 }
112 }
113 ###LogSD $phone->talk( level => 'debug', message => [
114 ###LogSD "Any needed kickstarting complete" ] );
115  
116 # Advance to the proper position - storing along the way as needed
117 252 ▶ while( !$success ){
Branch: 1 branch
BranchTrueFalseTotal
unless ($success) 365 249 614
118 ###LogSD $phone->talk( level => 'debug', message => [
119 ###LogSD "Reading the position: " . $self->where_am_i ] );
120  
121 # Build a perl ref
122 365 my $inital_parse = $self->parse_element;
123 365 my $provisional_output;
124 ###LogSD $phone->talk( level => 'debug', message => [
125 ###LogSD "Collected:", $inital_parse ] );
126  
127 # Handle unexpected end of file here
128 365 ▶ if( !$inital_parse ){# Potential chopped off end of file here 20-empty_shared_strings_bug.t
Branch: 2 branches
BranchTrueFalseTotal
if (not $inital_parse) { } 0 365 365
elsif ($inital_parse and $inital_parse eq 'EOF') { } 3 362 365
129 ###LogSD $phone->talk( level => 'debug', message => [
130 ###LogSD "Handling the (empty) end of the file", ] );
131 0 $self->set_error( "The shared strings file ended (poorly?) before expected" );
132 0 $self->_set_unique_count( $self->where_am_i );
133 0 return undef;
134 ▶ }elsif( $inital_parse and $inital_parse eq 'EOF' ){
Condition66%2/3
$inital_parse and $inital_parse eq "EOF"
!ll&&!rl&&r
03623
MC/DC50%1/2
$inital_parse and $inital_parse eq "EOF"
$inital_parse $inital_parse eq "EOF"
Truth table66%2/3
A and B
A= $inital_parseB= $inital_parse eq "EOF"
ABresult
0X0
100
111
135 ###LogSD $phone->talk( level => 'debug', message => [
136 ###LogSD "Handling the end of the file", ] );
137 3 $return = $inital_parse;
138 3 $self->_set_unique_count( $self->where_am_i + 1 );
139 3 last;
140 }
141  
142 # Convert the perl ref to a styles ref
143 362 $inital_parse = $self->squash_node( $inital_parse );
144 ###LogSD $phone->talk( level => 'debug', message => [
145 ###LogSD "Squashed to:", $inital_parse ] );
146 362 ▶ if( is_HashRef( $inital_parse ) ){
Branch: 1 branch
BranchTrueFalseTotal
if (&is_HashRef($inital_parse)) { } 362 0 362
147 ###LogSD $phone->talk( level => 'debug', message => [
148 ###LogSD "The initial parse is a hash ref" ] );
149 362 ▶ if( exists $inital_parse->{t} ){
Branch: 2 branches
BranchTrueFalseTotal
if (exists $inital_parse->{'t'}) { } 347 15 362
elsif (exists $inital_parse->{'list'}) { } 15 0 15
150 347 $provisional_output = $inital_parse->{t};
151 }elsif( exists $inital_parse->{list} ){
152 ###LogSD $phone->talk( level => 'debug', message => [
153 ###LogSD "The initial parse is broken up into list elements", $inital_parse->{list} ] );
154 15 my ( $raw_text, $rich_text );
155 15 for my $element( @{$inital_parse->{list}} ){
156 ###LogSD $phone->talk( level => 'debug', message => [
157 ###LogSD "processing element:", $element ] );
158 45 ▶ push( @$rich_text, length( $raw_text ), $element->{rPr} ) if exists $element->{rPr};
Branch: 1 branch
BranchTrueFalseTotal
if exists $element->{"rPr"} 30 15 45
159 45 $raw_text .= $element->{t};
160 }
161 15 @$provisional_output{qw( raw_text rich_text )} = ( $raw_text, $rich_text );
162 }else{
163 0 confess "Couldn't find 't' or 'list' keys in: " . Dumper( $inital_parse );
164 }
165 }else{
166 0 confess "Found unknown parse return: " . Dumper( $inital_parse );
167 }
168 ###LogSD $phone->talk( level => 'debug', message => [
169 ###LogSD "Built position " . $self->where_am_i . " => ", $provisional_output ] );
170  
171 # Cache the position as needed
172 362 ▶ if( $self->should_cache_positions ){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->should_cache_positions) 277 85 362
173 my $cache_value =
174 !$provisional_output ? undef :
175 !is_HashRef( $provisional_output ) ? $provisional_output :
176 (scalar( keys %$provisional_output ) == 1 or $self->_should_block_formats) ?
177 277 ▶ $provisional_output->{raw_text} : $provisional_output;
Branch: 3 branches
BranchTrueFalseTotal
!$provisional_output ? : 2 275 277
!&is_HashRef($provisional_output) ? : 262 13 275
scalar keys %$provisional_output == 1 || $self->_should_block_formats ? : 7 6 13
Condition66%2/3
scalar keys %$provisional_output == 1 or $self->_should_block_formats
l!l&&r!l&&!r
076
MC/DC50%1/2
scalar keys %$provisional_output == 1 or $self->_should_block_formats
scalar keys %$provisional_output == 1 $self->_should_block_formats
Truth table66%2/3
A or B
A= scalar keys %$provisional_output == 1B= $self->_should_block_formats
ABresult
1X1
011
000
178 ###LogSD $phone->talk( level => 'debug', message =>[ "Caching position: " . $self->where_am_i, $cache_value ] );
179 277 $self->_set_ss_position( $self->where_am_i => $cache_value );
180 277 $self->_set_last_cache_position( $self->where_am_i );
181 ###LogSD $phone->talk( level => 'trace', message =>[ "Updated cache:", $self->_get_all_cache ] );
182 }
183  
184 # Determine if we have arrived
185 362 ▶ if( $self->where_am_i == $position ){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->where_am_i == $position) 107 255 362
186 107 $success = 1;
187 107 $return = $provisional_output;
188 107 ▶ if( !$self->should_cache_positions ){
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->should_cache_positions) 27 80 107
189 #~ my $cache_value = scalar( keys %$provisional_output ) == 1 ? $provisional_output->{raw_text} : $provisional_output;
190 ###LogSD $phone->talk( level => 'debug', message =>[ "Saving the last postion" ] );
191 27 $self->_set_last_position( $self->where_am_i );
192 27 $self->_set_last_position_ref( $return );
193 }
194 }
195 362 $self->i_am_here( $self->where_am_i + 1 );
196 ###LogSD $phone->talk( level => 'debug', message => [
197 ###LogSD "The next position to collect is: " . $self->where_am_i ] );
198 362 $self->advance_element_position( 'si' )
199 }
200  
201 # Manage the output
202 $return =
203 !defined $return ? $return :
204 ( $return and $return eq 'EOF' ) ? undef :
205 ( $self->_should_block_formats and is_HashRef( $return ) ) ? $return->{raw_text} :
206 252 ▶ $self->_should_block_formats ? $return :
Branch: 5 branches
BranchTrueFalseTotal
!defined($return) ? : 3 249 252
$return && $return eq 'EOF' ? : 3 246 249
$self->_should_block_formats && &is_HashRef($return) ? : 7 239 246
$self->_should_block_formats ? : 105 134 239
&is_HashRef($return) ? : 8 126 134
Condition83%5/6
$return and $return eq "EOF"
!ll&&!rl&&r
02463
$self->_should_block_formats and &is_HashRef($return)
!ll&&!rl&&r
1341057
MC/DC50%1/2
$return and $return eq "EOF"
$return $return eq "EOF"
MC/DC100%2/2
$self->_should_block_formats and &is_HashRef($return)
$self->_should_block_formats &is_HashRef($return)
Truth table66%2/3
A and B
A= $returnB= $return eq "EOF"
ABresult
0X0
100
111
Truth table100%3/3
A and B
A= $self->_should_block_formatsB= &is_HashRef($return)
ABresult
0X0
100
111
207 is_HashRef( $return ) ? $return : { raw_text => $return } ;
208 ###LogSD $phone->talk( level => 'debug', message => [
209 ###LogSD "After possible format stripping: " . $self->_should_block_formats, $return ] );
210  
211 # Close the file if caching complete
212 252 ▶ if( $self->should_cache_positions and $self->has_file and $self->where_am_i > $self->_get_unique_count - 1 ){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->should_cache_positions and $self->has_file and $self->where_am_i > $self->_get_unique_count - 1) 20 232 252
Condition100%6/6
$self->should_cache_positions and $self->has_file and $self->where_am_i > $self->_get_unique_count - 1
!ll&&!rl&&r
12410820
$self->should_cache_positions and $self->has_file
!ll&&!rl&&r
2896128
MC/DC100%3/3
$self->should_cache_positions and $self->has_file and $self->where_am_i > $self->_get_unique_count - 1
$self->should_cache_positions $self->has_file $self->where_am_i > $self->_get_unique_count - 1
Truth table100%4/4
A and B and C
A= $self->should_cache_positionsB= $self->has_fileC= $self->where_am_i > $self->_get_unique_count - 1
ABCresult
0XX0
10X0
1100
1111
213 ###LogSD $phone->talk( level => 'debug', message => [
214 ###LogSD "Closing the file - all positions have been stored in cache" ] );
215 20 $self->close_the_file;
216 }
217 252 return $return;
218 }
219  
220 28 ▶sub load_unique_bits{
Subroutine load_unique_bits: called CC=5 SCAR=18.0
Pod documented
221 28 my( $self, ) = @_;
222 ###LogSD my $phone = Log::Shiras::Telephone->new( name_space =>
223 ###LogSD $self->get_all_space . '::load_unique_bits', );
224 ###LogSD $phone->talk( level => 'debug', message => [
225 ###LogSD "Setting the sharedStrings unique bits" ] );
226 28 my( $result, $node_name, $node_level, $result_ref );
227 28 my $current_node = $self->current_node_parsed;
228 ###LogSD $phone->talk( level => 'trace', message =>[
229 ###LogSD "The current node is:", $current_node ] );
230 28 ▶ if( (keys %$current_node)[0] eq 'sst' ){
Branch: 1 branch
BranchTrueFalseTotal
if ((keys %$current_node)[0] eq 'sst') { } 28 0 28
231 ###LogSD $phone->talk( level => 'trace', message =>[
232 ###LogSD "Found the core properties node" ] );
233 28 $result = 2;
234 28 $node_name = 'sst';
235 }else{
236 0 ( $result, $node_name, $node_level, $result_ref ) =
237 $self->advance_element_position( 'sst' );
238 0 $current_node = $self->current_node_parsed;
239 }
240 28 ▶ if( $result and $node_name eq 'sst' ){
Branch: 1 branch
BranchTrueFalseTotal
if ($result and $node_name eq 'sst') { } 28 0 28
Condition33%1/3
$result and $node_name eq "sst"
!ll&&!rl&&r
0028
MC/DC0%0/2
$result and $node_name eq "sst"
$result $node_name eq "sst"
Truth table33%1/3
A and B
A= $resultB= $node_name eq "sst"
ABresult
0X0
100
111
241 28 ▶ my $unique_count = $current_node->{sst}->{uniqueCount} // 0;
Condition100%2/2
$current_node->{"sst"}{"uniqueCount"} // 0
l!l
271
MC/DC100%1/1
$current_node->{"sst"}{"uniqueCount"} // 0
$current_node->{"sst"}{"uniqueCount"}
Truth table100%2/2
A
A= $current_node->{"sst"}{"uniqueCount"}
Aresult
11
00
242 ###LogSD $phone->talk( level => 'debug', message => [
243 ###LogSD "Loading unique count: $unique_count" ] );
244 28 $self->_set_unique_count( $unique_count );
245 28 $self->good_load( 1 );
246 }else{
247 0 $self->set_error( "No 'sst' element found - can't parse this as a shared strings file" );
248 0 $self->_clear_unique_count;
249 }
250 }
251  
252 #########1 Private Attributes 3#########4#########5#########6#########7#########8#########9
253  
254 20 has _unique_count =>(
255 isa => Int,
256 writer => '_set_unique_count',
257 reader => '_get_unique_count',
258 clearer => '_clear_unique_count',
259 predicate => '_has_unique_count'
260 );
261  
262 has _last_position =>(
263 isa => Int,
264 writer => '_set_last_position',
265 reader => '_get_last_position',
266 predicate => '_has_last_position',
267 27 ▶ trigger => sub{
Subroutine __ANON__: called CC=2 SCAR=6.9
268 27 my ( $self ) = @_;
269 27 ▶ if( $self->_has_last_position_ref ){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_has_last_position_ref) 24 3 27
270 24 $self->_clear_last_position_ref;
271 }
272 },
273 20 );
274  
275 20 has _last_position_ref =>(
276 writer => '_set_last_position_ref',
277 reader => '_get_last_position_ref',
278 clearer => '_clear_last_position_ref',
279 predicate => '_has_last_position_ref',
280 );
281  
282 has _shared_strings_positions =>(
283 isa => ArrayRef,
284 traits => ['Array'],
285 28 ▶ default => sub{ [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
286 20 handles =>{
287 _get_ss_position => 'get',
288 _set_ss_position => 'set',
289 },
290 reader => '_get_all_cache',
291 );
292  
293 20 has _cache_completed =>(
294 isa => Int,
295 default => -1,
296 reader => '_last_cache_position',
297 writer => '_set_last_cache_position',
298 );
299  
300 #########1 Private Methods 3#########4#########5#########6#########7#########8#########9
301  
302 498 ▶sub _should_block_formats{
Subroutine _should_block_formats: called CC=2 SCAR=6.9
303 498 my( $self, ) = @_;
304 ###LogSD my $phone = Log::Shiras::Telephone->new( name_space =>
305 ###LogSD $self->get_all_space . '::_should_block_formats', );
306 ###LogSD $phone->talk( level => 'debug', message => [
307 ###LogSD "determining if formats should be blocked: " . $self->get_group_return_type ] );
308 498 ▶ return ( $self->get_group_return_type =~ /(unformatted|value|xml_value)/) ? 1 : 0 ;
Branch: 1 branch
BranchTrueFalseTotal
$self->get_group_return_type =~ /(unformatted|value|xml_value)/ ? : 224 274 498
309 }
310  
311 #########1 Phinish 3#########4#########5#########6#########7#########8#########9
312  
313 20 ▶no Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
314  
315 20 1;
316  
317 #########1 Documentation 3#########4#########5#########6#########7#########8#########9
318 __END__
« lib/Spreadsheet/Reader/ExcelXML/Worksheet.pm ↑ Summary lib/Spreadsheet/Reader/ExcelXML/WorksheetToRow.pm »