blib/lib/PICA/Data.pm

filter: Statementstmt 89.1% 180 / 202 Branchbran 79.2% 65 / 82 Conditioncond 72.2% 13 / 18 MC/DCmcdc 44.4% 4 / 9 Subroutinesub 94.5% 35 / 37 Podpod 92.8% 13 / 14 totaltotal 85.6% 310 / 362 CC 57 SCAR 19.4
CC
57
Cov
85%
pica_xml_struct
20.0
pica_items
13.0
pica_holdings
12.0
CRAP 7.0
« blib/lib/PICA/Parser/XML.pm ↑ Summary blib/lib/PICA/Parser/Base.pm »
1 package PICA::Data;
2 8 ▶use v5.14.1;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 8 our $VERSION = '1.18';
5  
6 8 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 8 our @EXPORT_OK = qw(pica_parser pica_writer pica_path pica_xml_struct
8 pica_match pica_values pica_value pica_fields pica_holdings pica_items
9 pica_guess clean_pica pica_string);
10 8 our %EXPORT_TAGS = (all => [@EXPORT_OK]);
11  
12 8 our $ILN_PATH = PICA::Path->new('101@a');
13 8 our $EPN_PATH = PICA::Path->new('203@/..0');
14  
15 8 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
16 8 ▶use Scalar::Util qw(reftype blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
17 8 ▶use Encode qw(decode);
Subroutine BEGIN: called CC=1 SCAR=0.0
18 8 ▶use List::Util qw(first any);
Subroutine BEGIN: called CC=1 SCAR=0.0
19 8 ▶use IO::Handle;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 8 ▶use PICA::Path;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 sub pica_match {
23 58 ▶ my ($record, $path, %args) = @_;
Subroutine pica_match: called CC=3 SCAR=11.0
Pod documented
24  
25 58 ▶ $path = eval {PICA::Path->new($path)} unless ref $path;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $path 58 0 58
26 58 ▶ return unless ref $path;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $path 4 54 58
27  
28 54 return $path->match_record($record, %args);
29 }
30  
31 sub pica_values {
32 10 ▶ my ($record, $path) = @_;
Subroutine pica_values: called CC=3 SCAR=11.0
Pod documented
33  
34 10 ▶ $path = eval {PICA::Path->new($path)} unless ref $path;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $path 9 1 10
35 10 ▶ return unless ref $path;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $path 1 9 10
36  
37 9 return $path->record_subfields($record);
38 }
39  
40 sub pica_fields {
41 4 ▶ my $record = shift;
Subroutine pica_fields: called CC=3 SCAR=11.1
Pod documented
42 4 ▶ $record = $record->{record} if reftype $record eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if builtin::reftype($record) eq "HASH" 4 0 4
43  
44 my @pathes = map {
45 4 ▶ ref $_ ? $_ : eval {PICA::Path->new($_)}
Branch: 1 branch
BranchTrueFalseTotal
ref $_ ? : 0 5 5
46 } @_;
47  
48 return [
49 grep {
50 4 my $cur = $_;
51 71 ▶ any {$_->match_field($cur)} @pathes
Subroutine __ANON__: called CC=1 SCAR=0.0
52 72 } @$record
53 ];
54 }
55  
56 sub pica_value {
57 8 ▶ my ($record, $path) = @_;
Subroutine pica_value: called CC=7 SCAR=19.7
Pod documented
58  
59 8 ▶ $record = $record->{record} if reftype $record eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if builtin::reftype($record) eq "HASH" 8 0 8
60 8 ▶ $path = eval {PICA::Path->new($path)} unless ref $path;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $path 8 0 8
61 8 ▶ return unless defined $path;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $path 0 8 8
62  
63 8 foreach my $field (@$record) {
64 70 ▶ next unless $path->match_field($field);
Branch: 1 branch
BranchTrueFalseTotal
unless $path->match_field($field) 61 9 70
65 9 my @values = $path->match_subfields($field);
66 9 ▶ return $values[0] if @values;
Branch: 1 branch
BranchTrueFalseTotal
if @values 7 2 9
67 }
68  
69 1 return;
70 }
71  
72 sub pica_items {
73 6 ▶ my ($record) = @_;
Subroutine pica_items: called CC=13 SCAR=25.6
Pod documented
74  
75 6 my $blessed = blessed($record);
76 6 ▶ $record = $record->{record} if reftype $record eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if builtin::reftype($record) eq "HASH" 5 1 6
77 6 my (@items, $current, $occurrence);
78  
79 6 foreach my $field (@$record) {
80 6110 ▶ if ($field->[0] =~ /^2/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($field->[0] =~ /^2/u) { } 5726 384 6110
elsif ($current) { } 110 274 384
81  
82 5726 ▶ if (($occurrence // '') ne $field->[1]) {
Branch: 1 branch
BranchTrueFalseTotal
if (($occurrence // "") ne $field->[1]) 710 5016 5726
Condition100%2/2
$occurrence // ""
l!l
5611115
MC/DC100%1/1
$occurrence // ""
$occurrence
Truth table100%2/2
A
A= $occurrence
Aresult
11
00
83 710 ▶ if ($current) {
Branch: 1 branch
BranchTrueFalseTotal
if ($current) 595 115 710
84 595 push @items, $current;
85 595 $current = undef;
86 }
87 710 $occurrence = $field->[1];
88 }
89  
90 5726 ▶ $current //= {record => []};
Condition100%2/2
$current //= {"record", []}
l!l
5016710
MC/DC100%1/1
$current //= {"record", []}
$current
Truth table100%2/2
A
A= $current
Aresult
11
00
91  
92 5726 push @{$current->{record}}, [@$field];
93 5726 ▶ if ($field->[0] eq '203@') {
Branch: 1 branch
BranchTrueFalseTotal
if ($field->[0] eq "203\@") 710 5016 5726
94 710 ($current->{_id}) = $EPN_PATH->match_subfields($field);
95 }
96 }
97 elsif ($current) {
98 110 push @items, $current;
99 110 $current = undef;
100 110 $occurrence = undef;
101 }
102 }
103  
104 6 ▶ push @items, $current if $current;
Branch: 1 branch
BranchTrueFalseTotal
if $current 5 1 6
105  
106 6 ▶ if ($blessed) {
Branch: 1 branch
BranchTrueFalseTotal
if ($blessed) 4 2 6
107 4 bless $_, $blessed for @items;
108 }
109  
110 6 return \@items;
111 }
112  
113 sub pica_holdings {
114 3 ▶ my ($record) = @_;
Subroutine pica_holdings: called CC=12 SCAR=24.9
Pod documented
115  
116 3 my $blessed = blessed($record);
117 3 ▶ $record = $record->{record} if reftype $record eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if builtin::reftype($record) eq "HASH" 2 1 3
118 3 my (@holdings, $iln);
119 3 my $field_buffer = [];
120  
121 3 foreach my $field (@$record) {
122 3047 my $tag = substr $field->[0], 0, 1;
123 3047 ▶ if ($tag eq '0') {
Branch: 2 branches
BranchTrueFalseTotal
if ($tag eq '0') { } 42 3005 3047
elsif ($tag eq '1') { } 150 2855 3005
124 42 next;
125 }
126 elsif ($tag eq '1') {
127 150 ▶ if ($field->[0] eq '101@') {
Branch: 1 branch
BranchTrueFalseTotal
if ($field->[0] eq "101\@") 57 93 150
128 57 my ($id) = $ILN_PATH->match_subfields($field);
129 57 ▶ if (defined $iln && ($id // '') ne $iln) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $iln and ($id // "") ne $iln) 55 2 57
Condition60%3/5
defined $iln and ($id // "") ne $iln
!ll&&!rl&&r
2055
$id // ""
l!l
550
MC/DC50%1/2
defined $iln and ($id // "") ne $iln
defined $iln $id
Truth table66%2/3
A and B
A= defined $ilnB= $id
ABresult
0X0
100
111
130 55 push @holdings, {record => $field_buffer, _id => $iln};
131 }
132 57 $field_buffer = [[@$field]];
133 57 $iln = $id;
134 57 next;
135 }
136 }
137 2948 push @$field_buffer, [@$field];
138 }
139  
140 3 ▶ if (@$field_buffer) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$field_buffer) 2 1 3
141 2 push @holdings, {record => $field_buffer, _id => $iln};
142 }
143  
144 3 ▶ if ($blessed) {
Branch: 1 branch
BranchTrueFalseTotal
if ($blessed) 2 1 3
145 2 bless $_, $blessed for @holdings;
146 }
147  
148 3 return \@holdings;
149 }
150  
151 sub pica_string {
152 9 ▶ my ($pica, $type, %options) = @_;
Subroutine pica_string: called CC=3 SCAR=11.0
Pod documented
153 9 my $string = "";
154 9 ▶ $type ||= 'plain';
Condition100%2/2
$type ||= "plain"
l!l
36
MC/DC100%1/1
$type ||= "plain"
$type
Truth table100%2/2
A
A= $type
Aresult
11
00
155 9 $options{fh} = \$string;
156 9 ▶ $options{start} //= 0;
Condition50%1/2
$options{"start"} //= 0
l!l
09
MC/DC0%0/1
$options{"start"} //= 0
$options{"start"}
Truth table50%1/2
A
A= $options{"start"}
Aresult
11
00
157 9 pica_writer($type => %options)->write($pica);
158 9 return decode('UTF-8', $string);
159 }
160  
161 8 *fields = *pica_fields;
162 8 *holdings = *pica_holdings;
163 8 *items = *pica_items;
164 8 *match = *pica_match;
165 8 *value = *pica_value;
166 8 *values = *pica_values;
167 8 *string = *pica_string;
168  
169 8 ▶use PICA::Parser::XML;
Subroutine BEGIN: called CC=1 SCAR=0.0
170 8 ▶use PICA::Parser::Plus;
Subroutine BEGIN: called CC=1 SCAR=0.0
171 8 ▶use PICA::Parser::Plain;
Subroutine BEGIN: called CC=1 SCAR=0.0
172 8 ▶use PICA::Parser::Binary;
Subroutine BEGIN: called CC=1 SCAR=0.0
173 8 ▶use PICA::Parser::PPXML;
Subroutine BEGIN: called CC=1 SCAR=0.0
174 8 ▶use PICA::Parser::JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
175 8 ▶use PICA::Writer::XML;
Subroutine BEGIN: called CC=1 SCAR=0.0
176 8 ▶use PICA::Writer::Plus;
Subroutine BEGIN: called CC=1 SCAR=0.0
177 8 ▶use PICA::Writer::Plain;
Subroutine BEGIN: called CC=1 SCAR=0.0
178 8 ▶use PICA::Writer::Binary;
Subroutine BEGIN: called CC=1 SCAR=0.0
179 8 ▶use PICA::Writer::PPXML;
Subroutine BEGIN: called CC=1 SCAR=0.0
180 8 ▶use PICA::Writer::JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
181 8 ▶use PICA::Writer::Fields;
Subroutine BEGIN: called CC=1 SCAR=0.0
182  
183 sub pica_parser {
184 54 ▶ _pica_module('PICA::Parser', @_);
Subroutine pica_parser: called CC=1 SCAR=0.0
Pod documented
185 }
186  
187 sub pica_writer {
188 9 ▶ if (lc $_[0] eq 'generic') {
Subroutine pica_writer: called CC=2 SCAR=9.2
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
if (lc $_[0] eq "generic") 0 9 9
189 0 shift;
190 0 return PICA::Writer::Generic->new(@_);
191 }
192 9 _pica_module('PICA::Writer', @_);
193 }
194  
195 sub pica_path {
196 1 ▶ PICA::Path->new(@_);
Subroutine pica_path: called CC=1 SCAR=0.0
Pod documented
197 }
198  
199 sub pica_guess {
200 7 ▶ my ($pica) = @_;
Subroutine pica_guess: called CC=6 SCAR=18.0
Pod documented
201  
202 7 my $format = '';
203 7 my %count = (
204 '' => 0,
205 'Plain' => ($pica =~ tr/$//),
206 'Plus' => ($pica =~ tr/\x{0A}//),
207 'Binary' => ($pica =~ tr/\x{1D}//),
208 'XML' => ($pica =~ tr/<//),
209 'JSON' => ($pica =~ tr/[{[]//),
210 );
211 7 ▶ $count{$_} > $count{$format} and $format = $_ for grep {$_} keys %count;
Branch: 1 branch
BranchTrueFalseTotal
$count{$_} > $count{$format} and $format = $_ 6 29 35
212  
213 7 ▶ $format = 'PPXML' if $format eq 'XML' and $pica =~ qr{xmlns/ppxml-1\.0};
Branch: 1 branch
BranchTrueFalseTotal
if $format eq "XML" and $pica =~ qr"xmlns/ppxml-1\.0"u 0 7 7
Condition66%2/3
$format eq "XML" and $pica =~ qr"xmlns/ppxml-1\.0"u
!ll&&!rl&&r
610
MC/DC0%0/2
$format eq "XML" and $pica =~ qr"xmlns/ppxml-1\.0"u
$format eq "XML" $pica =~ qr"xmlns/ppxml-1\.0"u
Truth table66%2/3
A and B
A= $format eq "XML"B= $pica =~ qr"xmlns/ppxml-1\.0"u
ABresult
0X0
100
111
214  
215 7 ▶ $format ? "PICA::Parser::$format" : undef;
Branch: 1 branch
BranchTrueFalseTotal
$format ? : 6 1 7
216 }
217  
218 sub _pica_module {
219 63 ▶ my $base = shift;
Subroutine _pica_module: called CC=8 SCAR=20.8
220 63 ▶ my $type = lc(shift) // '';
Condition50%1/2
lc shift() // ""
l!l
630
MC/DC0%0/1
lc shift() // ""
lc shift()
Truth table50%1/2
A
A= lc shift()
Aresult
11
00
221  
222 63 ▶ if ($type =~ /^(pica)?plus$/) {
Branch: 6 branches
BranchTrueFalseTotal
if ($type =~ /^(pica)?plus$/u) { } 4 59 63
elsif ($type eq 'binary') { } 3 56 59
elsif ($type =~ /^(pica)?plain$/u) { } 37 19 56
elsif ($type =~ /^(pica)?xml$/u) { } 9 10 19
elsif ($type =~ /^(pica)?ppxml$/u) { } 3 7 10
elsif ($type =~ /^(nd)?json$/u) { } 6 1 7
223 4 "${base}::Plus"->new(@_);
224 }
225 elsif ($type eq 'binary') {
226 3 "${base}::Binary"->new(@_);
227 }
228 elsif ($type =~ /^(pica)?plain$/) {
229 37 "${base}::Plain"->new(@_);
230 }
231 elsif ($type =~ /^(pica)?xml$/) {
232 9 "${base}::XML"->new(@_);
233 }
234 elsif ($type =~ /^(pica)?ppxml$/) {
235 3 "${base}::PPXML"->new(@_);
236 }
237 elsif ($type =~ /^(nd)?json$/) {
238 6 "${base}::JSON"->new(@_);
239 }
240 else {
241 1 croak "unknown PICA parser type: $type";
242 }
243 }
244  
245 sub write {
246 0 ▶ my $pica = shift;
Subroutine write: not called CC=3 SCAR=24.8
Pod documented
247 0 my $writer = $_[0];
248 0 ▶ unless (blessed $writer) {
Branch: 1 branch
BranchTrueFalseTotal
unless (builtin::blessed($writer)) 0 0 0
249 0 ▶ $writer = pica_writer(@_ ? @_ : 'plain');
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 0 0
250 }
251 0 $writer->write($pica);
252 }
253  
254 sub TO_JSON {
255 1 ▶ my $record = shift;
Subroutine TO_JSON: called CC=2 SCAR=7.1
Pod undocumented
256 1 ▶ $record = $record->{record} if reftype $record eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if builtin::reftype($record) eq "HASH" 1 0 1
257 1 return [@$record];
258 }
259  
260 sub pica_xml_struct {
261 0 ▶ my ($xml, %options) = @_;
Subroutine pica_xml_struct: not called CC=4 SCAR=30.0
Pod documented
262 0 my $record;
263  
264 0 foreach my $f (@{$xml->[2]}) {
265 0 ▶ next unless $f->[0] eq 'datafield';
Branch: 1 branch
BranchTrueFalseTotal
unless $f->[0] eq "datafield" 0 0 0
266 push @$record,
267 [
268 0 map ({$f->[1]->{$_}} qw(tag occurrence)),
269 0 map ({$_->[1]->{code} => $_->[2]->[0]} @{$f->[2]})
270 ];
271 }
272  
273 0 my ($id) = map {$_->[-1]} grep {$_->[0] =~ '003@'} @$record;
274 0 $record = {_id => $id, record => $record};
275 0 ▶ bless $record, 'PICA::Data' if !!$options{bless};
Branch: 1 branch
BranchTrueFalseTotal
unless not $options{"bless"} 0 0 0
276 0 return $record;
277 }
278  
279 8 1;
280 __END__
« blib/lib/PICA/Parser/XML.pm ↑ Summary blib/lib/PICA/Parser/Base.pm »