Field.pm

filter: Statementstmt 91.4% 299 / 327 Branchbran 88.1% 127 / 144 Conditioncond 55.5% 15 / 27 MC/DCmcdc 27.7% 5 / 18 Subroutinesub 93.7% 30 / 32 Podpod 100% 21 / 21 totaltotal 87.3% 497 / 569 CC 89 SCAR 19.7
CC
89
Cov
89%
html
20.0
parse
13.2
string
12.0
CRAP 7.2
« Store.pm ↑ Summary Parser.pm »
1 package PICA::Field;
2 {
3 13 $PICA::Field::VERSION = '0.585';
4 }
5 #ABSTRACT: Perl extension for handling PICA+ fields
6 13 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 13 ▶use base qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 13 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 13 ▶use XML::Writer;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 13 ▶use PICA::Record;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 13 ▶use PICA::Writer;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 13 our @EXPORT = qw(parse_pp_tag);
16  
17 13 our $SUBFIELD_INDICATOR = "\x1F"; # 31
18 13 our $START_OF_FIELD = "\x1E"; # 30
19 13 our $END_OF_FIELD = "\x0A"; # 10
20  
21 13 our $FIELD_TAG_REGEXP = qr/[012][0-9][0-9][A-Z@]$/;
22 13 our $FIELD_OCCURRENCE_REGEXP = qr/[0-9][0-9]$/;
23 13 our $SUBFIELD_CODE_REGEXP = qr/^[0-9a-zA-Z]$/;
24  
25 use overload
26 84 ▶ 'bool' => sub { ! $_[0]->empty; },
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
27 13 ▶ '""' => sub { $_[0]->string; };
Subroutine __ANON__: called CC=1 SCAR=0.0
28  
29 13 ▶use sort 'stable';
Subroutine BEGIN: called CC=1 SCAR=0.0
30  
31  
32 4563 ▶sub new($) {
Subroutine new: called CC=5 SCAR=16.1
Pod documented
33 4563 my $class = shift;
34 4563 ▶ $class = ref($class) || $class;
Condition66%2/3
ref $class || $class
l!l&&r!l&&!r
4495680
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table66%2/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
35  
36 4563 my $tag = shift;
37 4563 ▶ $tag or croak( "No tag provided." );
Branch: 1 branch
BranchTrueFalseTotal
$tag or croak("No tag provided.") 4555 8 4563
38  
39 4555 ▶ if (not @_) { # empty field
Branch: 1 branch
BranchTrueFalseTotal
unless (@_) 12 4543 4555
40 12 return PICA::Field->parse($tag);
41 }
42  
43 4543 my ($occurrence, $tagno) = parse_pp_tag($tag);
44  
45 4543 ▶ defined $tagno or croak( "\"$tag\" is not a valid tag." );
Branch: 1 branch
BranchTrueFalseTotal
defined $tagno or croak(qq["$tag" is not a valid tag.]) 4536 7 4543
46  
47 4536 my $self = bless {
48 _tag => $tagno,
49 _occurrence => $occurrence,
50 _subfields => [],
51 }, $class;
52  
53 4536 $self->add(@_);
54  
55 4536 return $self;
56 }
57  
58  
59 23 ▶sub copy {
Subroutine copy: called CC=1 SCAR=0.0
Pod documented
60 23 my $self = shift;
61  
62 23 my $tagno = $self->{_tag};
63 23 my $occurrence = $self->{_occurrence};
64  
65 23 my $copy = bless {
66 _tag => $tagno,
67 _occurrence => $occurrence,
68 }, ref($self);
69  
70 23 $copy->add( @{$self->{_subfields}} );
71  
72 23 return $copy;
73 }
74  
75  
76 4498 ▶sub parse {
Subroutine parse: called CC=13 SCAR=25.8
Pod documented
77 4498 my $class = shift;
78 4498 ▶ $class = ref($class) || $class;
Condition33%1/3
ref $class || $class
l!l&&r!l&&!r
044980
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table33%1/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
79  
80 4498 my $data = shift;
81 4498 my $tag_filter_func = shift;
82  
83 # TODO: better manage different parsing modes (normalized, plain, WinIBW...)
84 4498 my $END_OF_FIELD = qr/[\x0A\x0D]+/; # local
85  
86 4498 $data =~ s/^$START_OF_FIELD//;
87 4498 $data =~ s/$END_OF_FIELD$//;
88  
89 4498 my $self = bless {}, $class;
90  
91 4498 my ($tagno, $subfields) = ($data =~ /([^\$\x1F\x83\s]+)\s?(.*)/);
92  
93 4498 ▶ return if $tag_filter_func and !$tag_filter_func->($tagno);
Branch: 1 branch
BranchTrueFalseTotal
if $tag_filter_func and not &$tag_filter_func($tagno) 0 4498 4498
Condition33%1/3
$tag_filter_func and not &$tag_filter_func($tagno)
!ll&&!rl&&r
449800
MC/DC0%0/2
$tag_filter_func and not &$tag_filter_func($tagno)
$tag_filter_func not &$tag_filter_func($tagno)
Truth table33%1/3
A and not(B)
A= $tag_filter_funcB= not &$tag_filter_func($tagno)
ABresult
0X0
100
111
94  
95 # TODO: better manage different parsing modes (normalized, plain, WinIBW...)
96 4498 my $sfreg;
97 4498 ▶ my $sf = defined $subfields ? substr($subfields, 0, 1) : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $subfields ? : 4490 8 4498
98 4498 ▶ if ($sf eq "\x1F") { $sfreg = '\x1F'; }
Branch: 5 branches
BranchTrueFalseTotal
if ($sf eq "\037") { } 1097 3401 4498
elsif ($sf eq '$') { } 3379 22 3401
elsif ($sf eq "\203") { } 1 21 22
elsif ($sf eq "\237") { } 1 20 21
elsif ($sf eq '') { } 17 3 20
99 3379 elsif ( $sf eq '$' ) { $sfreg = '\$'; }
100 1 elsif( $sf eq "\x83" ) { $sfreg = '\x83'; }
101 1 elsif( $sf eq "\x9f" ) { $sfreg = '\x9f'; }
102 elsif( $sf eq '') {
103 17 return $self->new($tagno,'');
104 } else {
105 3 croak("not allowed subfield indicator (ord: " . ord($sf) . ") specified");
106 }
107 4478 $sfreg = '('.$sfreg.'[0-9a-zA-Z])';
108  
109 4478 my @sfields = split($sfreg, $subfields);
110 4478 shift @sfields;
111  
112 4478 my @subfields = ();
113 4478 my ($value, $code);
114 4478 ▶ while (@sfields) {
Branch: 1 branch
BranchTrueFalseTotal
if (@sfields) 8957 4478 13435
115 8957 $code = shift @sfields;
116 8957 $code = substr($code, 1);
117 8957 $value = shift @sfields;
118 8957 ▶ next unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 8957 8957
119 8957 ▶ $value =~ s/\$\$/\$/g if $sf eq '$';
Branch: 1 branch
BranchTrueFalseTotal
if $sf eq "\$" 7267 1690 8957
120 8957 $value =~ s/\s+/ /gm;
121 8957 push(@subfields, ($code, $value));
122 }
123  
124 4478 return $self->new($tagno, @subfields);
125 }
126  
127  
128 57220 ▶sub tag {
Subroutine tag: called CC=4 SCAR=13.9
Pod documented
129 57220 my $self = shift;
130 57220 my $tag = shift;
131  
132 57220 ▶ if (defined $tag) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $tag) 2 57218 57220
133 2 my ($occurrence, $tagno) = parse_pp_tag($tag);
134 2 ▶ defined $tagno or croak( "\"$tag\" is not a valid tag." );
Branch: 1 branch
BranchTrueFalseTotal
defined $tagno or croak(qq["$tag" is not a valid tag.]) 2 0 2
135  
136 2 $self->{_tag} = $tagno;
137 2 $self->{_occurrence} = $occurrence;
138 }
139  
140 57220 ▶ return $self->{_tag} . ($self->{_occurrence} ? ("/" . $self->{_occurrence}) : "");
Branch: 1 branch
BranchTrueFalseTotal
$self->{'_occurrence'} ? : 51670 5550 57220
141 }
142  
143  
144 4 ▶sub occurrence {
Subroutine occurrence: called CC=4 SCAR=14.3
Pod documented
145 4 my $self = shift;
146 4 my $occurrence = shift;
147  
148 4 ▶ if (defined $occurrence) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $occurrence) 1 3 4
149 1 ▶ croak unless $occurrence >= 0 and $occurrence <= 99;
Branch: 1 branch
BranchTrueFalseTotal
unless $occurrence >= 0 and $occurrence <= 99 0 1 1
Condition33%1/3
$occurrence >= 0 and $occurrence <= 99
!ll&&!rl&&r
001
MC/DC0%0/2
$occurrence >= 0 and $occurrence <= 99
$occurrence >= 0 $occurrence <= 99
Truth table33%1/3
A and B
A= $occurrence >= 0B= $occurrence <= 99
ABresult
0X0
100
111
150 1 $self->{_occurrence} = sprintf("%02d", $occurrence);
151 }
152  
153 4 return $self->{_occurrence};
154 }
155  
156 # Shortcut
157 13 *occ = \&occurrence;
158  
159  
160 0 ▶sub level {
Subroutine level: not called CC=1 SCAR=6.9
Pod documented
161 0 my $self = shift;
162 0 return substr($self->{_tag},0,1);
163 }
164  
165  
166 1067 ▶sub subfield {
Subroutine subfield: called CC=7 SCAR=19.5
Pod documented
167 1067 my $self = shift;
168 1067 my $codes = $_[0];
169 1067 ▶ if (ref($codes) ne 'Regexp') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $codes ne "Regexp") 1065 2 1067
170 1065 $codes = join('',@_);
171 1065 ▶ if ($codes eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($codes eq '') { } 2 1063 1065
172 2 $codes = qr/./;
173 } else {
174 1063 $codes = qr/[$codes]/;
175 }
176 }
177  
178 1067 my @list;
179 1067 my @data = @{$self->{_subfields}};
180  
181 1067 ▶ for ( my $i=0; $i < @data; $i+=2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @data) 2218 623 2841
182 2218 ▶ next unless $data[$i] =~ $codes;
Branch: 1 branch
BranchTrueFalseTotal
unless $data[$i] =~ /$codes/ 1134 1084 2218
183 1084 my $value = $data[$i+1];
184 1084 $value =~ s/\s+/ /gm;
185 1084 ▶ if ( wantarray ) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 640 444 1084
186 640 push( @list, $value );
187 } else {
188 444 return $value;
189 }
190 }
191  
192 623 ▶ return $list[0] unless wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless wantarray 8 615 623
193 615 return @list;
194 }
195  
196 # Shortcut
197 13 *sf = \&subfield;
198  
199  
200 94 ▶sub content {
Subroutine content: called CC=4 SCAR=13.9
Pod documented
201 94 my $self = shift;
202 94 my $codes = join('',@_);
203 94 ▶ $codes = $codes eq '' ? '.' : "[$codes]";
Branch: 1 branch
BranchTrueFalseTotal
$codes eq '' ? : 94 0 94
204 94 $codes = qr/$codes/;
205  
206 94 my @list;
207 94 my @data = @{$self->{_subfields}};
208  
209 94 ▶ for ( my $i=0; $i < @data; $i+=2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @data) 140 94 234
210 140 ▶ next unless $data[$i] =~ $codes;
Branch: 1 branch
BranchTrueFalseTotal
unless $data[$i] =~ /$codes/ 0 140 140
211 140 push( @list, [ $data[$i], $data[$i+1] ] );
212 }
213  
214 94 return @list;
215 }
216  
217  
218 4562 ▶sub add {
Subroutine add: called CC=5 SCAR=16.1
Pod documented
219 4562 my $self = shift;
220 4562 my $nfields = @_ / 2;
221  
222 4562 ▶ ($nfields >= 1) or return 0;
Branch: 1 branch
BranchTrueFalseTotal
$nfields >= 1 or return 0 4560 2 4562
223  
224 4560 for my $i ( 1..$nfields ) {
225 9065 my $offset = ($i-1)*2;
226 9065 my $code = $_[$offset];
227 9065 my $value = $_[$offset+1];
228 9065 ▶ $value = defined $value ? "$value" : "";
Branch: 1 branch
BranchTrueFalseTotal
defined $value ? : 9063 2 9065
229 9065 $value =~ s/\s+/ /gm;
230  
231 9065 ▶ croak( "Subfield code \"$code\" is not a valid subfield code" )
Branch: 1 branch
BranchTrueFalseTotal
unless $code =~ /$PICA::Field::SUBFIELD_CODE_REGEXP/ 0 9065 9065
232 if !($code =~ $SUBFIELD_CODE_REGEXP);
233  
234 9065 push( @{$self->{_subfields}}, $code, $value );
235 }
236  
237 4560 return $nfields;
238 }
239  
240  
241 12 ▶sub update {
Subroutine update: called CC=11 SCAR=24.0
Pod documented
242 12 my $self = shift;
243 12 my %values;
244 my @order;
245  
246 # collect values into a hash of array references
247 12 ▶ while( @_ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 17 12 29
248 17 my $c = shift;
249 17 ▶ croak( "Subfield code \"$c\" is not a valid subfield code" )
Branch: 1 branch
BranchTrueFalseTotal
unless $c =~ /$PICA::Field::SUBFIELD_CODE_REGEXP/ 0 17 17
250 unless $c =~ $SUBFIELD_CODE_REGEXP;
251 17 my $v = shift;
252 17 ▶ if ( exists $values{$c} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $values{$c}) { } 1 16 17
253 0 ▶ push @{$values{$c}}, (UNIVERSAL::isa($v,'ARRAY') ? @{$v} : $v);
Branch: 1 branch
BranchTrueFalseTotal
UNIVERSAL::isa($v, 'ARRAY') ? : 0 1 1
254 } else {
255 16 push @order, $c;
256 16 ▶ $values{$c} = UNIVERSAL::isa($v,'ARRAY') ? $v : [ $v ];
Branch: 1 branch
BranchTrueFalseTotal
UNIVERSAL::isa($v, 'ARRAY') ? : 2 14 16
257 }
258 }
259  
260 12 my @data;
261 12 my $changes = 0;
262  
263 12 ▶ while ( @{$self->{_subfields}} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->{"_subfields"};}) 25 12 37
264 25 my $code = shift @{$self->{_subfields}};
265 25 my $value = shift @{$self->{_subfields}};
266  
267 25 ▶ if ( exists $values{$code} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $values{$code}) { } 15 10 25
268 15 ▶ if ( defined $values{$code} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $values{$code}) 13 2 15
269 13 my @vals = grep { defined $_ } @{$values{$code}};
270 13 push @data, map { $code => "$_" } @vals;
271 13 $changes += scalar @vals;
272 13 $values{$code} = undef;
273 }
274 # TODO: better count
275 } else {
276 # keep subfield unchanged
277 10 push @data, $code => $value;
278 }
279 }
280  
281 ## append new subfields in their order
282 12 foreach my $code ( @order ) {
283 16 ▶ next unless defined $values{$code};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $values{$code} 13 3 16
284 3 my @vals = grep { defined $_ } @{$values{$code}};
285 3 $changes += scalar @vals;
286 3 push @data, map { $code => "$_" } @vals;
287 }
288  
289 ## synchronize our subfields
290 12 $self->{_subfields} = \@data;
291  
292 12 return $changes;
293 }
294  
295  
296 7 ▶sub replace {
Subroutine replace: called CC=3 SCAR=12.3
Pod documented
297 7 my $self = shift;
298 7 my $new;
299  
300 7 ▶ if (@_ and UNIVERSAL::isa($self,'PICA::Field')) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ and UNIVERSAL::isa($self, 'PICA::Field')) { } 7 0 7
Condition33%1/3
@_ and UNIVERSAL::isa($self, "PICA::Field")
!ll&&!rl&&r
007
MC/DC0%0/2
@_ and UNIVERSAL::isa($self, "PICA::Field")
@_ UNIVERSAL::isa($self, "PICA::Field")
Truth table33%1/3
A and B
A= @_B= UNIVERSAL::isa($self, "PICA::Field")
ABresult
0X0
100
111
301 7 $new = shift;
302 } else {
303 0 $new = PICA::Field->new(@_);
304 }
305  
306 7 %$self = %$new;
307 }
308  
309  
310 1 ▶sub empty_subfields {
Subroutine empty_subfields: called CC=3 SCAR=11.0
Pod documented
311 1 my $self = shift;
312  
313 1 my @list;
314 1 my @data = @{$self->{_subfields}};
315  
316 1 ▶ while ( defined( my $code = shift @data ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $code = shift @data)) 2 1 3
317 2 ▶ push (@list, $code) if shift @data eq "";
Branch: 1 branch
BranchTrueFalseTotal
if shift @data eq "" 2 0 2
318 }
319  
320 1 return @list;
321 }
322  
323  
324 11668 ▶sub empty {
Subroutine empty: called CC=4 SCAR=13.9
Pod documented
325 11668 my $self = shift;
326  
327 11668 ▶ return 1 unless @{$self->{_subfields}};
Branch: 1 branch
BranchTrueFalseTotal
unless @{$self->{"_subfields"};} 3 11665 11668
328  
329 11665 my @data = @{$self->{_subfields}};
330  
331 11665 ▶ while ( defined( my $code = shift @data ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $code = shift @data)) 11668 2 11670
332 11668 ▶ return 0 if shift @data ne "";
Branch: 1 branch
BranchTrueFalseTotal
if shift @data ne "" 11663 5 11668
333 }
334  
335 2 return 1;
336 }
337  
338  
339 5 ▶sub purged {
Subroutine purged: called CC=6 SCAR=17.9
Pod documented
340 5 my $self = shift;
341  
342 5 my @subfields;
343 my $code;
344 5 foreach (@{$self->{_subfields}}) {
345 14 ▶ if (defined $code) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $code) { } 7 7 14
346 7 ▶ push @subfields, ($code, $_) if defined $_ and $_ ne "";
Branch: 1 branch
BranchTrueFalseTotal
if defined $_ and $_ ne "" 3 4 7
Condition66%2/3
defined $_ and $_ ne ""
!ll&&!rl&&r
043
MC/DC50%1/2
defined $_ and $_ ne ""
defined $_ $_ ne ""
Truth table66%2/3
A and B
A= defined $_B= $_ ne ""
ABresult
0X0
100
111
347 7 undef $code;
348 } else {
349 7 $code = $_;
350 }
351 }
352  
353 5 ▶ return unless @subfields;
Branch: 1 branch
BranchTrueFalseTotal
unless @subfields 2 3 5
354  
355 my $copy = bless {
356 _tag => $self->{_tag},
357 _occurrence => $self->{_occurrence},
358 3 _subfields => \@subfields
359 }, ref($self);
360  
361 3 return $copy;
362 }
363  
364  
365 15 ▶sub normalized {
Subroutine normalized: called CC=1 SCAR=0.0
Pod documented
366 15 my $self = shift;
367 15 my $subfields = shift;
368  
369 15 return $self->string(
370 subfields => $subfields,
371 startfield => $START_OF_FIELD,
372 endfield => $END_OF_FIELD,
373 startsubfield => $SUBFIELD_INDICATOR
374 );
375 }
376  
377  
378 2 ▶sub sort {
Subroutine sort: called CC=5 SCAR=16.1
Pod documented
379 2 my ($self, $order) = @_;
380 2 ▶ return unless @{$self->{_subfields}};
Branch: 1 branch
BranchTrueFalseTotal
unless @{$self->{"_subfields"};} 0 2 2
381 2 ▶ $order = "" unless defined $order;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $order 1 1 2
382  
383 2 my (%pos,$i);
384 2 for (split('',$order.'0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ')) {
385 127 ▶ $pos{$_} = $i++ unless defined $pos{$_};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pos{$_} 124 3 127
386 }
387  
388 2 my @sf = @{$self->{_subfields}};
389 2 my $n = @sf / 2 - 1;
390 2 my @sorted = ();
391  
392 @sorted = sort {
393 2 $pos{$sf[2*$a]} <=> $pos{$sf[2*$b]}
394 } (0..$n);
395  
396 2 $self->{_subfields} = [ map { $sf[2*$_] => $sf[2*$_+1] } @sorted ];
397 }
398  
399  
400 2 ▶sub size {
Subroutine size: called CC=1 SCAR=0.0
Pod documented
401 2 my $self = shift;
402 2 return @{$self->{_subfields}} / 2;
403 }
404  
405  
406 861 ▶sub string {
Subroutine string: called CC=12 SCAR=24.9
Pod documented
407 861 my $self = shift;
408 861 ▶ my (%args) = @_ ? @_ : ();
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 814 47 861
409  
410 861 ▶ my $subfields = defined($args{subfields}) ? $args{subfields} : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $args{'subfields'} ? : 1 860 861
411 861 ▶ my $startfield = defined($args{startfield}) ? $args{startfield} : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $args{'startfield'} ? : 15 846 861
412 861 ▶ my $endfield = defined($args{endfield}) ? $args{endfield} : "\n";
Branch: 1 branch
BranchTrueFalseTotal
defined $args{'endfield'} ? : 813 48 861
413 861 ▶ my $startsubfield = defined($args{startsubfield}) ? $args{startsubfield} : '$';
Branch: 1 branch
BranchTrueFalseTotal
defined $args{'startsubfield'} ? : 15 846 861
414  
415 861 my @subs;
416  
417 861 my $subs = $self->{_subfields};
418 861 my $nfields = @$subs / 2;
419  
420 861 for my $i ( 1..$nfields ) {
421 1482 my $offset = ($i-1)*2;
422 1482 my $code = $subs->[$offset];
423 1482 my $value = $subs->[$offset+1];
424 1482 ▶ if (!$subfields || $code =~ /^[$subfields]$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $subfields or $code =~ /^[$subfields]$/) 1480 2 1482
Condition66%2/3
not $subfields or $code =~ /^[$subfields]$/
l!l&&r!l&&!r
148002
MC/DC50%1/2
not $subfields or $code =~ /^[$subfields]$/
not $subfields $code =~ /^[$subfields]$/
Truth table66%2/3
not(A) or B
A= not $subfieldsB= $code =~ /^[$subfields]$/
ABresult
1X1
011
000
425 1480 ▶ $value =~ s/\$/\$\$/g if $startsubfield eq '$';
Branch: 1 branch
BranchTrueFalseTotal
if $startsubfield eq "\$" 1426 54 1480
426 1480 push( @subs, $code.$value )
427 }
428 } # for
429  
430 861 ▶ return "" unless @subs; # no subfields => no field
Branch: 1 branch
BranchTrueFalseTotal
unless @subs 3 858 861
431  
432 858 my $occ = '';
433 858 ▶ $occ = "/" . $self->{_occurrence} if defined $self->{_occurrence};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"_occurrence"} 230 628 858
434  
435 return $startfield .
436 858 $self->{_tag} . $occ . ' ' .
437 $startsubfield . join( $startsubfield, @subs ) .
438 $endfield;
439 }
440  
441 # Write the field to a L<XML::Writer> object
442 21 ▶my $write_xml = sub {
Subroutine __ANON__: called CC=5 SCAR=16.1
443 21 my ($self, $writer) = @_;
444  
445 21 my ($datafield, $subfield);
446  
447 21 ▶ if (UNIVERSAL::isa( $writer, 'XML::Writer::Namespaces' )) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::isa($writer, 'XML::Writer::Namespaces')) { } 18 3 21
448 18 $datafield = [$PICA::Record::XMLNAMESPACE, 'datafield'];
449 18 $subfield = [$PICA::Record::XMLNAMESPACE, 'subfield'];
450 } else {
451 3 $datafield = 'datafield';
452 3 $subfield = 'subfield';
453 }
454  
455 21 my %attr = ('tag' => $self->{_tag});
456 21 ▶ $attr{occurrence} = $self->{_occurrence} if defined $self->{_occurrence};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"_occurrence"} 0 21 21
457  
458 21 $writer->startTag( $datafield, %attr );
459  
460 21 my $subs = $self->{_subfields};
461 21 my $nfields = @$subs / 2;
462  
463 21 ▶ if ($nfields) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nfields) 20 1 21
464 20 for my $i ( 1..$nfields ) {
465 47 my $offset = ($i-1)*2;
466 47 $writer->startTag( $subfield, code => $subs->[$offset] );
467 47 $writer->characters( $subs->[$offset+1] );
468 47 $writer->endTag(); # subfield
469 }
470 }
471  
472 21 $writer->endTag(); # datafield
473  
474 21 $writer;
475 13 };
476  
477  
478 21 ▶sub xml {
Subroutine xml: called CC=6 SCAR=17.9
Pod documented
479 21 my $self = shift;
480  
481 21 my %param;
482 21 ▶ if ( UNIVERSAL::isa( $_[0], 'XML::Writer' ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (UNIVERSAL::isa($_[0], 'XML::Writer')) { } 15 6 21
elsif (ref $_[0]) { } 1 5 6
483 15 (%param) = ( writer => @_ );
484 } elsif ( ref($_[0]) ) {
485 1 (%param) = ( OUTPUT => @_ );
486 } else {
487 5 (%param) = @_;
488 }
489  
490 21 ▶ if ( defined $param{writer} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $param{'writer'}) { } 15 6 21
491 15 $write_xml->( $self, $param{writer} );
492 15 return $param{writer};
493 } else {
494 6 my ($string, $sref);
495 6 ▶ if (not defined $param{OUTPUT}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $param{"OUTPUT"}) 5 1 6
496 5 $sref = \$string;
497 5 $param{OUTPUT} = $sref;
498 }
499  
500 6 my $writer = PICA::Writer::xmlwriter( %param );
501  
502 6 $write_xml->( $self, $writer );
503  
504 6 ▶ return defined $sref ? "$string" : $writer;
Branch: 1 branch
BranchTrueFalseTotal
defined $sref ? : 5 1 6
505 }
506 }
507  
508  
509 0 ▶sub html {
Subroutine html: not called CC=4 SCAR=30.0
Pod documented
510 0 my $self = shift;
511 0 my %options = @_;
512  
513 # CSS classes (TODO: customize)
514 0 my $field = 'field';
515 0 my $tag = 'tag';
516 0 my $tagcode = 'tagcode';
517 0 my $occurrence = 'occurrence';
518 0 my $sfcode = 'sfcode';
519 0 my $sfindicator = 'sfindicator';
520  
521 my $html = "<div class='$field'><span class='$tag'>"
522 0 . "<span class='$tagcode'>" . $self->{_tag} . "</span>";
523 0 ▶ if (defined $self->{_occurrence}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'_occurrence'}) { } 0 0 0
524 $html .= "/<span class='$occurrence'>"
525 0 . $self->{_occurrence} . "</span>";
526 } else {
527 # TODO: in monospaced mode only
528 # $html .= "&#xA0;&#xA0;&#xA0;";
529 }
530 0 $html .= "</span> "; # tag
531  
532 0 my $subs = $self->{_subfields};
533 0 my $nfields = @$subs / 2;
534 0 ▶ if ($nfields) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nfields) 0 0 0
535 0 for my $i ( 1..$nfields ) {
536 0 my $offset = ($i-1)*2;
537 0 my $code = $subs->[$offset];
538 0 my $text = $subs->[$offset+1];
539 0 $html .= "<span class='$sfindicator'>\$</span>"
540 . "<span class='$sfcode'>$code</span>";
541 0 $text =~ s/&/&amp;/g;
542 0 $text =~ s/</&lt;/g;
543 0 $html .= $text; # TODO: character encoding (?)
544 }
545 }
546 0 return $html . "</div>\n";
547 }
548  
549  
550 4975 ▶sub parse_pp_tag {
Subroutine parse_pp_tag: called CC=5 SCAR=16.1
Pod documented
551 4975 my $tag = shift;
552  
553 4975 my ($tagno, $occurrence) = split ('/', $tag);
554 4975 ▶ undef $tagno unless defined $tagno and $tagno =~ $FIELD_TAG_REGEXP;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $tagno and $tagno =~ /$PICA::Field::FIELD_TAG_REGEXP/ 33 4942 4975
Condition100%3/3
defined $tagno and $tagno =~ /$PICA::Field::FIELD_TAG_REGEXP/
!ll&&!rl&&r
1324942
MC/DC100%2/2
defined $tagno and $tagno =~ /$PICA::Field::FIELD_TAG_REGEXP/
defined $tagno $tagno =~ /$PICA::Field::FIELD_TAG_REGEXP/
Truth table100%3/3
A and B
A= defined $tagnoB= $tagno =~ /$PICA::Field::FIELD_TAG_REGEXP/
ABresult
0X0
100
111
555 4975 ▶ undef $occurrence unless defined $occurrence and $occurrence =~ $FIELD_OCCURRENCE_REGEXP;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $occurrence and $occurrence =~ /$PICA::Field::FIELD_OCCURRENCE_REGEXP/ 1740 3235 4975
Condition66%2/3
defined $occurrence and $occurrence =~ /$PICA::Field::FIELD_OCCURRENCE_REGEXP/
!ll&&!rl&&r
174003235
MC/DC50%1/2
defined $occurrence and $occurrence =~ /$PICA::Field::FIELD_OCCURRENCE_REGEXP/
defined $occurrence $occurrence =~ /$PICA::Field::FIELD_OCCURRENCE_REGEXP/
Truth table66%2/3
A and B
A= defined $occurrenceB= $occurrence =~ /$PICA::Field::FIELD_OCCURRENCE_REGEXP/
ABresult
0X0
100
111
556  
557 4975 return ($occurrence, $tagno);
558 }
559  
560 13 1;
561  
562 __END__
« Store.pm ↑ Summary Parser.pm »