XLSX/Backend/Regex.pm

filter: Statementstmt 93.8% 107 / 114 Branchbran 86.0% 43 / 50 Conditioncond 85.0% 34 / 40 MC/DCmcdc 66.6% 16 / 24 Subroutinesub 100% 15 / 15 Podpod n/a totaltotal 88.4% 215 / 243 CC 43 SCAR 30.2
CC
43
Cov
90%
__ANON__
35.4
_parse_table_xml
5.6
_workbook_data
4.0
CRAP 20.6
« XLSX/Backend/LibXML.pm ↑ Summary XLSX.pm »
1 package Excel::ValueReader::XLSX::Backend::Regex;
2 2 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use 5.12.1;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use Scalar::Util qw/looks_like_number/;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use Iterator::Simple qw/iter/;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 2 extends 'Excel::ValueReader::XLSX::Backend';
9  
10  
11 #======================================================================
12 # LAZY ATTRIBUTE CONSTRUCTORS
13 #======================================================================
14  
15 8 ▶sub _strings {
Subroutine _strings: called CC=2 SCAR=6.9
16 8 my $self = shift;
17 8 my @strings;
18  
19 # read from the sharedStrings zip member
20 8 my $contents = $self->_zip_member_contents('xl/sharedStrings.xml');
21  
22 # iterate on <si> nodes
23 8 ▶ while ($contents =~ m[<si>(.*?)</si>]sg) {
Branch: 1 branch
BranchTrueFalseTotal
if ($contents =~ m[<si>(.*?)</si>]gsu) 530 8 538
24 530 my $innerXML = $1;
25  
26 # concatenate contents from all <t> nodes (usually there is only 1) and decode XML entities
27 530 my $string = join "", ($innerXML =~ m[<t[^>]*>(.+?)</t>]sg);
28 530 _decode_xml_entities($string);
29  
30 530 push @strings, $string;
31 }
32  
33 8 return \@strings;
34 }
35  
36  
37 15 ▶sub _workbook_data {
Subroutine _workbook_data: called CC=4 SCAR=13.9
38 15 my $self = shift;
39  
40 15 my %workbook_data;
41  
42 # read from the workbook.xml zip member
43 15 my $workbook = $self->_zip_member_contents('xl/workbook.xml');
44  
45 # extract sheet names
46 15 my @sheet_names = ($workbook =~ m[<sheet name="(.+?)"]g);
47 15 $workbook_data{sheets} = {map {$sheet_names[$_] => $_+1} 0 .. $#sheet_names};
48  
49 # does this workbook use the 1904 calendar ?
50 15 my ($date1904) = $workbook =~ m[date1904="(.+?)"];
51 15 ▶ $workbook_data{base_year} = $date1904 && $date1904 =~ /^(1|true)$/ ? 1904 : 1900;
Branch: 1 branch
BranchTrueFalseTotal
$date1904 && $date1904 =~ /^(1|true)$/u ? : 2 13 15
Condition100%3/3
$date1904 and $date1904 =~ /^(1|true)$/u
!ll&&!rl&&r
1122
MC/DC100%2/2
$date1904 and $date1904 =~ /^(1|true)$/u
$date1904 $date1904 =~ /^(1|true)$/u
Truth table100%3/3
A and B
A= $date1904B= $date1904 =~ /^(1|true)$/u
ABresult
0X0
100
111
52  
53 # active sheet
54 15 my ($active_tab) = $workbook =~ m[<workbookView[^>]+activeTab="(\d+)"];
55 15 ▶ $workbook_data{active_sheet} = $active_tab + 1 if defined $active_tab;
Branch: 1 branch
BranchTrueFalseTotal
if defined $active_tab 7 8 15
56  
57 15 return \%workbook_data;
58 }
59  
60  
61  
62 10 ▶sub _date_styles {
Subroutine _date_styles: called CC=3 SCAR=11.0
63 10 my $self = shift;
64  
65 10 state $date_style_regex = qr{[dy]|\bmm\b};
66  
67 # read from the styles.xml zip member
68 10 my $styles = $self->_zip_member_contents('xl/styles.xml');
69  
70 # start with Excel builtin number formats for dates and times
71 10 my @numFmt = $self->Excel_builtin_date_formats;
72  
73 # add other date formats explicitly specified in this workbook
74 10 ▶ while ($styles =~ m[<numFmt numFmtId="(\d+)" formatCode="([^"]+)"/>]g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($styles =~ m[<numFmt numFmtId="(\d+)" formatCode="([^"]+)"/>]gu) 76 10 86
75 76 my ($id, $code) = ($1, $2);
76 76 ▶ $numFmt[$id] = $code if $code =~ $date_style_regex;
Branch: 1 branch
BranchTrueFalseTotal
if $code =~ /$date_style_regex/u 74 2 76
77 }
78  
79 # read all cell formats, just rembember those that involve a date number format
80 10 my ($cellXfs) = ($styles =~ m[<cellXfs count="\d+">(.+?)</cellXfs>]);
81 10 my @cell_formats = $self->_extract_xf($cellXfs);
82 10 my @date_styles = map {$numFmt[$_->{numFmtId}]} @cell_formats;
83  
84 10 return \@date_styles; # array of shape (xf_index => numFmt_code)
85 }
86  
87  
88 10 ▶sub _extract_xf {
Subroutine _extract_xf: called CC=2 SCAR=6.9
89 10 my ($self, $xml) = @_;
90  
91 10 state $xf_node_regex = qr{
92 <xf # initial format tag
93 \s
94 ([^>/]*+) # attributes (captured in $1)
95 (?: # non-capturing group for an alternation :
96 /> # .. either an xml closing without content
97 | # or
98 > # .. closing for the xf tag
99 .*? # .. then some formatting content
100 </xf> # .. then the ending tag for the xf node
101 )
102 }x;
103  
104 10 my @xf_nodes;
105 10 ▶ while ($xml =~ /$xf_node_regex/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($xml =~ /$xf_node_regex/gu) 180 10 190
106 180 push @xf_nodes, _xml_attrs($1);
107 }
108 10 return @xf_nodes;
109 }
110  
111  
112 #======================================================================
113 # METHODS
114 #======================================================================
115  
116 44 ▶sub _values {
Subroutine _values: called CC=2 SCAR=7.0
117 44 my ($self, $sheet, $want_iterator) = @_;
118  
119 # regex for the initial preamble
120 44 state $preamble_regex = qr(
121 <dimension\s+ref="([A-Z]+\d+(?::[A-Z]+\d+)?)"/> # node specifying the range of defined cells
122 .*?
123 <sheetData> # start container node for actual rows and cells content
124 )xs;
125  
126 # regex for extracting information from cell nodes
127 44 state $row_or_cell_regex = qr(
128 <(row) # row tag ($1)
129 (?:\s+r="(\d+)")? # optional row number ($2)
130 [^>/]*? # unused attrs
131 > # end of tag
132  
133 | # .. or ..
134  
135 <(c) # cell tag ($3)
136 (?: \s+ | (?=>) ) # either a space before attrs, or end of tag
137 (?:r="([A-Z]+)(\d+)")? # capture col ($4) and row ($5)
138 [^>/]*? # unused attrs
139 (?:s="(\d+)"\s*)? # style attribute ($6)
140 (?:t="(\w+)"\s*)? # type attribute ($7)
141 (?: # non-capturing group for an alternation :
142 /> # .. either an xml closing without content
143 | # or
144 > # .. closing xml tag, followed by ..
145 (?:
146 <v>(.+?)</v> # .. a value ($8)
147 | # or
148 (.+?) # .. some node content ($9)
149 )
150 </c> # followed by a closing cell tag
151 )
152 )xs;
153 # NOTE : this regex uses positional capturing groups; it would be more readable with named
154 # captures instead, but this would double the execution time on big Excel files, so I
155 # stick to plain old capturing groups.
156  
157 # does this instance want date formatting ?
158 44 my $has_date_formatter = $self->frontend->date_formatter;
159  
160 # get worksheet XML
161 44 my $contents = $self->_zip_member_contents($self->_zip_member_name_for_sheet($sheet));
162  
163 # parse the preamble
164 44 my ($ref) = $contents =~ /$preamble_regex/g; # /g to leave the pos() cursor before the 1st cell
165  
166 # variables for the closure below
167 44 my ($row_num, $col_num, @rows) = (0, 0);
168  
169 # dual closure : may be used as an iterator or as a regular sub, depending on $want_iterator. Of course
170 # it would have been simpler to just write an iterator, and call it in a loop if the client wants all rows
171 # at once ... but thousands of additional sub calls would slow down the process. So this more complex implementation
172 # is for the sake of processing speed.
173 2100066 ▶ my $get_values = sub {
Subroutine __ANON__: called CC=31 SCAR=35.7
174  
175 # in iterator mode, if we have a row ready, just return it
176 2100066 ▶ return shift @rows if $want_iterator and @rows > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $want_iterator and @rows > 1 2099210 856 2100066
Condition100%3/3
$want_iterator and @rows > 1
!ll&&!rl&&r
228342099210
MC/DC100%2/2
$want_iterator and @rows > 1
$want_iterator @rows > 1
Truth table100%3/3
A and B
A= $want_iteratorB= @rows > 1
ABresult
0X0
100
111
177  
178 # otherwise loop on matching nodes
179 856 ▶ while ($contents =~ /$row_or_cell_regex/cg) { # /g allows the iterator to remember where the last cell left off
Branch: 1 branch
BranchTrueFalseTotal
if ($contents =~ /$row_or_cell_regex/cgu) 5626 48 5674
180 5626 ▶ if ($1) { # this is a 'row' tag
Branch: 2 branches
BranchTrueFalseTotal
if ($1) { } 1746 3880 5626
elsif ($3) { } 3880 0 3880
181 1746 my $prev_row = $row_num;
182 1746 ▶ $row_num = $2 // $row_num+1; # if present, capture group $2 is the row number
Condition100%2/2
$2 // $row_num + 1
l!l
17442
MC/DC100%1/1
$2 // $row_num + 1
$2
Truth table100%2/2
A
A= $2
Aresult
11
00
183 1746 $col_num = 0;
184 1746 push @rows, [] for 1 .. $row_num-$prev_row;
185  
186 # in iterator mode, if we have a closed empty row, just return it
187 1746 ▶ return shift @rows if $want_iterator and @rows > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $want_iterator and @rows > 1 808 938 1746
Condition100%3/3
$want_iterator and @rows > 1
!ll&&!rl&&r
91820808
MC/DC100%2/2
$want_iterator and @rows > 1
$want_iterator @rows > 1
Truth table100%3/3
A and B
A= $want_iteratorB= @rows > 1
ABresult
0X0
100
111
188 }
189 elsif ($3) { # this is a 'c' tag
190 3880 my ($col_A1, $given_row, $style, $cell_type, $val, $inner) = ($4, $5, $6, $7, $8, $9);
191  
192 # deal with the row number given in the 'r' attribute, if present
193 3880 ▶ $given_row //= $row_num;
Condition66%2/3
$given_row //= $row_num
l!l&&r!l&&!r
3856240
MC/DC0%0/2
$given_row //= $row_num
$given_row $row_num
Truth table66%2/3
A //= B
A= $given_rowB= $row_num
ABresult
1X1
011
000
194 0 ▶ if ($given_row < $row_num) {die "cell claims to be in row $given_row while current row is $row_num"}
Branch: 2 branches
BranchTrueFalseTotal
if ($given_row < $row_num) { } 0 3880 3880
elsif ($given_row > $row_num) { } 0 3880 3880
195 0 elsif ($given_row > $row_num) {push @rows, [] for 1 .. $given_row-$row_num;
196 0 $col_num = 0;
197 0 $row_num = $given_row;}
198  
199  
200 # deal with the col number given in the 'r' attribute, if present
201 3880 ▶ if ($col_A1) {$col_num = $Excel::ValueReader::XLSX::A1_to_num_memoized{$col_A1}
Branch: 1 branch
BranchTrueFalseTotal
if ($col_A1) { } 3856 24 3880
Condition66%2/3
$Excel::ValueReader::XLSX::A1_to_num_memoized{$col_A1} //= "Excel::ValueReader::XLSX"->A1_to_num($col_A1)
l!l&&r!l&&!r
3841150
MC/DC0%0/2
$Excel::ValueReader::XLSX::A1_to_num_memoized{$col_A1} //= "Excel::ValueReader::XLSX"->A1_to_num($col_A1)
$Excel::ValueReader::XLSX::A1_to_num_memoized{$col_A1} "Excel::ValueReader::XLSX"->A1_to_num($col_A1)
Truth table66%2/3
A //= B
A= $Excel::ValueReader::XLSX::A1_to_num_memoized{$col_A1}B= "Excel::ValueReader::XLSX"->A1_to_num($col_A1)
ABresult
1X1
011
000
202 //= Excel::ValueReader::XLSX->A1_to_num($col_A1)}
203 24 else {$col_num++}
204  
205 # handle the cell value according to cell type
206 3880 ▶ $cell_type //= '';
Condition100%2/2
$cell_type //= ""
l!l
24591421
MC/DC100%1/1
$cell_type //= ""
$cell_type
Truth table100%2/2
A
A= $cell_type
Aresult
11
00
207 3880 ▶ if ($cell_type eq 'inlineStr') {
Branch: 2 branches
BranchTrueFalseTotal
if ($cell_type eq 'inlineStr') { } 0 3880 3880
elsif ($cell_type eq 's') { } 2433 1447 3880
208 # this is an inline string; gather all <t> nodes within the cell node
209 0 $val = join "", ($inner =~ m[<t>(.+?)</t>]g);
210 0 ▶ _decode_xml_entities($val) if $val;
Branch: 1 branch
BranchTrueFalseTotal
if $val 0 0 0
211 }
212 elsif ($cell_type eq 's') {
213 # this is a string cell; $val is a pointer into the global array of shared strings
214 2433 $val = $self->strings->[$val];
215 }
216 else {
217 # this is a plain value
218 1447 ▶ ($val) = ($inner =~ m[<v>(.*?)</v>]) if !defined $val && $inner;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $val and $inner 30 1417 1447
Condition100%3/3
not defined $val and $inner
!ll&&!rl&&r
111530230
MC/DC100%2/2
not defined $val and $inner
not defined $val $inner
Truth table100%3/3
not(A) and B
A= not defined $valB= $inner
ABresult
0X0
100
111
219 1447 ▶ _decode_xml_entities($val) if $val && $cell_type eq 'str';
Branch: 1 branch
BranchTrueFalseTotal
if $val and $cell_type eq "str" 4 1443 1447
Condition100%3/3
$val and $cell_type eq "str"
!ll&&!rl&&r
30211414
MC/DC100%2/2
$val and $cell_type eq "str"
$val $cell_type eq "str"
Truth table100%3/3
A and B
A= $valB= $cell_type eq "str"
ABresult
0X0
100
111
220  
221 # if necessary, transform the numeric value into a formatted date
222 1447 ▶ if ($has_date_formatter && $style && looks_like_number($val) && $val >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($has_date_formatter and $style and &looks_like_number($val) and $val >= 0) 486 961 1447
Condition88%8/9
$has_date_formatter and $style and &looks_like_number($val) and $val >= 0
!ll&&!rl&&r
9610486
$has_date_formatter and $style and &looks_like_number($val)
!ll&&!rl&&r
659302486
$has_date_formatter and $style
!ll&&!rl&&r
54605788
MC/DC75%3/4
$has_date_formatter and $style and &looks_like_number($val) and $val >= 0
$has_date_formatter $style &looks_like_number($val) $val >= 0
Truth table80%4/5
A and B and C and D
A= $has_date_formatterB= $styleC= &looks_like_number($val)D= $val >= 0
ABCDresult
0XXX0
10XX0
110X0
11100
11111
223 486 my $date_style = $self->date_styles->[$style];
224 486 ▶ $val = $self->formatted_date($val, $date_style) if $date_style;
Branch: 1 branch
BranchTrueFalseTotal
if $date_style 152 334 486
225 }
226 }
227  
228 # insert this value into the last row
229 3880 $rows[-1][$col_num-1] = $val;
230 }
231 0 else {die "found a node which is neither a <row> nor a <c> (cell)"}
232 }
233  
234 # end of regex matches. In iterator mode, return a row if we have one
235 48 ▶ return @rows ? shift @rows : undef if $want_iterator;
Branch: 2 branches
BranchTrueFalseTotal
if $want_iterator 26 22 48
@rows ? : 14 12 26
236 44 };
237  
238 # decide what to return depending on the dual mode
239 my $retval = $want_iterator ? iter($get_values)
240 44 ▶ : do {$get_values->(); \@rows}; # run the closure and return the rows
Branch: 1 branch
BranchTrueFalseTotal
$want_iterator ? : 22 22 44
241  
242 44 return ($ref, $retval);
243 }
244  
245  
246 10 ▶sub _table_targets {
Subroutine _table_targets: called CC=1 SCAR=0.0
247 10 my ($self, $rel_xml) = @_;
248  
249 10 my @table_targets = $rel_xml =~ m[<Relationship .*? Target="../tables/table(\d+)\.xml"]g;
250 10 return @table_targets; # a list of positive integers corresponding to table ids
251 }
252  
253  
254 12 ▶sub _parse_table_xml {
Subroutine _parse_table_xml: called CC=5 SCAR=17.2
255 12 my ($self, $xml) = @_;
256  
257 12 ▶ $xml =~ m[<table (.*?)>]g and my $table_attrs = _xml_attrs($1)
Branch: 1 branch
BranchTrueFalseTotal
$xml =~ /<table (.*?)>/gu and my $table_attrs = _xml_attrs($1) or die "invalid table XML: $xml" 12 0 12
Condition33%1/3
$xml =~ /<table (.*?)>/gu and my $table_attrs = _xml_attrs($1)
!ll&&!rl&&r
0012
MC/DC0%0/2
$xml =~ /<table (.*?)>/gu and my $table_attrs = _xml_attrs($1)
$xml =~ /<table (.*?)>/gu my $table_attrs = _xml_attrs($1)
Truth table33%1/3
A and B
A= $xml =~ /<table (.*?)>/guB= my $table_attrs = _xml_attrs($1)
ABresult
0X0
100
111
258 or die "invalid table XML: $xml";
259  
260 # extract relevant attributes
261 my %table_info = (
262 name => $table_attrs->{displayName},
263 ref => $table_attrs->{ref},
264 no_headers => exists $table_attrs->{headerRowCount} && !$table_attrs->{headerRowCount},
265 has_totals => $table_attrs->{totalsRowCount},
266 12 ▶ columns => [$xml =~ m{<tableColumn [^>]+? name="([^"]+)"}gx],
Condition66%2/3
exists $table_attrs->{'headerRowCount'} && !$table_attrs->{'headerRowCount'}
!ll&&!rl&&r
1002
MC/DC50%1/2
exists $table_attrs->{'headerRowCount'} && !$table_attrs->{'headerRowCount'}
exists $table_attrs->{'headerRowCount'} !$table_attrs->{'headerRowCount'}
Truth table66%2/3
A && not(B)
A= exists $table_attrs->{'headerRowCount'}B= !$table_attrs->{'headerRowCount'}
ABresult
0X0
100
111
267 );
268  
269  
270 # decode entites for all string values
271 12 _decode_xml_entities($_) for $table_info{name}, @{$table_info{columns}};
272  
273 12 return \%table_info;
274 }
275  
276  
277 #======================================================================
278 # AUXILIARY FUNCTIONS
279 #======================================================================
280  
281  
282 584 ▶sub _decode_xml_entities {
Subroutine _decode_xml_entities: called CC=1 SCAR=0.0
283 584 state $xml_entities = { amp => '&',
284 lt => '<',
285 gt => '>',
286 quot => '"',
287 apos => "'",
288 };
289 584 state $entity_names = join '|', keys %$xml_entities;
290 584 state $regex_entities = qr/&($entity_names);/;
291  
292 # substitute in-place
293 584 $_[0] =~ s/$regex_entities/$xml_entities->{$1}/eg;
294 }
295  
296  
297 192 ▶sub _xml_attrs {
Subroutine _xml_attrs: called CC=1 SCAR=0.0
298 192 my $attrs_list = shift;
299 192 my %attr = $attrs_list =~ m[(\w+)="(.+?)"]g;
300 192 return \%attr;
301 }
302  
303  
304  
305  
306 2 1;
307  
308 __END__
« XLSX/Backend/LibXML.pm ↑ Summary XLSX.pm »