PlainParser.pm

filter: Statementstmt 95.9% 141 / 147 Branchbran 87.0% 87 / 100 Conditioncond 73.3% 22 / 30 MC/DCmcdc 50.0% 9 / 18 Subroutinesub 100% 15 / 15 Podpod 100% 9 / 9 totaltotal 88.7% 283 / 319 CC 64 SCAR 23.7
CC
64
Cov
90%
parsefile
19.3
_parseline
11.0
handle_record
11.0
CRAP 10.7
« XMLParser.pm ↑ Summary Record.pm »
1 package PICA::PlainParser;
2 {
3 13 $PICA::PlainParser::VERSION = '0.585';
4 }
5 #ABSTRACT: Parse normalized PICA+
6 13 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8  
9 13 ▶use PICA::Field;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 13 ▶use PICA::Record;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 13 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13  
14 65 ▶sub new {
Subroutine new: called CC=10 SCAR=23.2
Pod documented
15 65 my ($class, %params) = @_;
16 65 ▶ $class = ref $class || $class;
Condition33%1/3
ref $class || $class
l!l&&r!l&&!r
0650
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table33%1/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
17  
18 my $self = bless {
19 field_handler => defined $params{Field} ? $params{Field} : undef,
20 record_handler => defined $params{Record} ? $params{Record} : undef,
21  
22 broken_field_handler => defined $params{FieldError} ? $params{FieldError} : undef,
23 broken_record_handler => defined $params{RecordError} ? $params{RecordError} : undef,
24  
25 proceed => $params{Proceed} ? $params{Proceed} : 0,
26 limit => ($params{Limit} || 0) * 1,
27 offset => ($params{Offset} || 0) * 1,
28  
29 record => undef,
30 broken => undef, # broken record
31  
32 read_records => [],
33 65 ▶ 'strict' => $params{strict} || 0,
Branch: 5 branches
BranchTrueFalseTotal
defined $params{'Field'} ? : 9 56 65
defined $params{'Record'} ? : 19 46 65
defined $params{'FieldError'} ? : 9 56 65
defined $params{'RecordError'} ? : 3 62 65
$params{'Proceed'} ? : 1 64 65
Condition83%5/6
$params{'Limit'} || 0
l!l
1649
$params{'Offset'} || 0
l!l
065
$params{'strict'} || 0
l!l
659
MC/DC100%1/1
$params{'Limit'} || 0
$params{'Limit'}
MC/DC0%0/1
$params{'Offset'} || 0
$params{'Offset'}
MC/DC100%1/1
$params{'strict'} || 0
$params{'strict'}
Truth table100%2/2
A
A= $params{'Limit'}
Aresult
11
00
Truth table50%1/2
A
A= $params{'Offset'}
Aresult
11
00
Truth table100%2/2
A
A= $params{'strict'}
Aresult
11
00
34 filename => "",
35 fields => [],
36 read_counter => 0,
37 active => 0,
38 }, $class;
39  
40 65 return $self;
41 }
42  
43  
44 32 ▶sub parsefile {
Subroutine parsefile: called CC=19 SCAR=29.6
Pod documented
45 32 my ($self, $file) = @_;
46  
47 32 ▶ if ( ref($file) eq 'GLOB' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $file eq 'GLOB') { } 5 27 32
elsif (UNIVERSAL::isa($file, 'IO::Handle')) { } 8 19 27
48 5 $self->{filehandle} = $file;
49 5 $self->{filename} = "";
50 } elsif ( UNIVERSAL::isa( $file, 'IO::Handle' ) ) {
51 8 $self->{filehandle} = $file;
52 8 $self->{filename} = "";
53 } else {
54 19 $self->{filename} = $file;
55  
56 19 my $fh = $file;
57 19 ▶ $fh = "zcat $fh |" if $fh =~ /\.gz$/;
Branch: 1 branch
BranchTrueFalseTotal
if $fh =~ /\.gz$/ 0 19 19
58 19 ▶ $fh = "unzip -p $fh |" if $fh =~ /\.zip$/;
Branch: 1 branch
BranchTrueFalseTotal
if $fh =~ /\.zip$/ 0 19 19
59  
60 19 ▶ $self->{filehandle} = IO::File->new($file, '<:utf8')
Branch: 1 branch
BranchTrueFalseTotal
$self->{"filehandle"} = "IO::File"->new($file, "<:utf8") or croak("failed to open file $file") 19 0 19
61 or croak("failed to open file $file");
62 }
63  
64 32 PICA::Parser::enable_binmode_encoding( $self->{filehandle} );
65  
66 32 ▶ if ( not $self->{proceed} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"proceed"}) 32 0 32
67 32 $self->{read_counter} = 0;
68 32 $self->{read_records} = [];
69 }
70  
71 32 $self->{active} = 0;
72 32 $self->{record} = undef;
73  
74 32 my $dumpformat = 0;
75 32 my $line = readline( $self->{filehandle} );
76 32 ▶ if ($line =~ /\x1E/) { # dumpformat useds \x1E instead of newlines
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /\x1E/) { } 5 27 32
77  
78 5 my $EOL = $/;
79 5 $/ = chr(0x1E);
80 5 my $id = "";
81  
82 5 my @linebuf = split( /\x1E/, $line );
83
84 5 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
defined $line and do { do { last if $self->finished; if (@linebuf) { $line = shift @linebuf; if (defined $line and not @linebuf) { $line .= readline $self->{"filehandle"}; } ; } else { $line = readline $self->{"filehandle"}; } ; if (defined $line) { $line =~ /^\x1D?([^\s]*)/; if (PICA::Field::parse_pp_tag($1)) { $self->_parseline($line); } else { if ("$id" ne "$1") { $self->_parseline(""); } ; $id = $1; } ; } } } 417 5 422
85 422 ▶ last if ($self->finished());
Branch: 1 branch
BranchTrueFalseTotal
if $self->finished 0 422 422
86 422 ▶ if (@linebuf) {
Branch: 1 branch
BranchTrueFalseTotal
if (@linebuf) { } 417 5 422
87 417 $line = shift @linebuf;
88 417 ▶ if (defined $line and not @linebuf) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $line and not @linebuf) 5 412 417
Condition66%2/3
defined $line and not @linebuf
!ll&&!rl&&r
04125
MC/DC50%1/2
defined $line and not @linebuf
defined $line not @linebuf
Truth table66%2/3
A and not(B)
A= defined $lineB= not @linebuf
ABresult
0X0
100
111
89 5 $line .= readline( $self->{filehandle} );
90 }
91 } else {
92 5 $line = readline( $self->{filehandle} );
93 }
94 422 ▶ if ( defined $line ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $line) 417 5 422
95 417 $line =~ /^\x1D?([^\s]*)/;
96 417 ▶ if (PICA::Field::parse_pp_tag($1)) {
Branch: 1 branch
BranchTrueFalseTotal
if (PICA::Field::parse_pp_tag($1)) { } 392 25 417
97 392 $self->_parseline($line);
98 } else {
99 25 ▶ if ( "$id" ne "$1" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ("$id" ne "$1") 12 13 25
100 12 $self->_parseline(""); # next record
101 }
102 25 $id = $1;
103 }
104 }
105 } while(defined $line);
106  
107 5 $/ = $EOL;
108  
109 } else {
110 27 ▶ while ( defined $line and not $self->finished ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $line and not $self->finished) 3593 27 3620
Condition66%2/3
defined $line and not $self->finished
!ll&&!rl&&r
2703593
MC/DC50%1/2
defined $line and not $self->finished
defined $line not $self->finished
Truth table66%2/3
A and not(B)
A= defined $lineB= not $self->finished
ABresult
0X0
100
111
111 3593 $self->_parseline($line);
112 3593 $line = readline( $self->{filehandle} );
113 };
114 }
115  
116 32 ▶ $self->handle_record() unless $self->finished(); # handle last record
Branch: 1 branch
BranchTrueFalseTotal
unless $self->finished 32 0 32
117  
118 32 $self;
119 }
120  
121  
122 39 ▶sub parsedata {
Subroutine parsedata: called CC=6 SCAR=17.9
Pod documented
123 39 my ($self, $data, $additional) = @_;
124  
125 39 $self->{active} = 0;
126 39 $self->{record} = undef;
127  
128 39 ▶ if ( ! $self->{proceed} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"proceed"}) 37 2 39
129 37 $self->{read_counter} = 0;
130 37 $self->{read_records} = [];
131 }
132  
133 39 ▶ if ( ref($data) eq 'CODE' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $data eq 'CODE') { } 1 38 39
elsif (UNIVERSAL::isa($data, 'PICA::Record')) { } 1 37 38
134 1 my $chunk = &$data();
135 1 ▶ while(defined $chunk) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $chunk) 27 1 28
136 27 $self->_parsedata($chunk);
137 27 $chunk = &$data();
138 }
139 } elsif( UNIVERSAL::isa( $data, "PICA::Record" ) ) {
140 # re-parse the record (could obviously be speed up by dropping tests)
141 1 foreach ( $data->fields ) {
142 26 $self->_parseline( $_->string );
143 }
144 } else {
145 37 $self->_parsedata($data);
146 }
147  
148 39 $self->handle_record(); # handle last record
149  
150 39 $self;
151 }
152  
153  
154  
155 17 ▶sub records {
Subroutine records: called CC=1 SCAR=0.0
Pod documented
156 17 my $self = shift;
157 17 return @{ $self->{read_records} };
158 }
159  
160  
161 3354 ▶sub counter {
Subroutine counter: called CC=1 SCAR=0.0
Pod documented
162 3354 my $self = shift;
163 3354 return $self->{read_counter};
164 }
165  
166  
167 4047 ▶sub finished {
Subroutine finished: called CC=2 SCAR=7.1
Pod documented
168 4047 my $self = shift;
169 4047 ▶ return $self->{limit} && $self->counter() >= $self->{limit};
Condition66%2/3
$self->{'limit'} && $self->counter >= $self->{'limit'}
!ll&&!rl&&r
71333340
MC/DC0%0/2
$self->{'limit'} && $self->counter >= $self->{'limit'}
$self->{'limit'} $self->counter >= $self->{'limit'}
Truth table66%2/3
A && B
A= $self->{'limit'}B= $self->counter >= $self->{'limit'}
ABresult
0X0
100
111
170 }
171  
172  
173 64 ▶sub _parsedata {
Subroutine _parsedata: called CC=5 SCAR=16.2
174 64 my ($self, $data) = @_;
175  
176 64 my @lines;
177  
178 64 ▶ if (ref(\$data) eq 'SCALAR') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref \$data eq 'SCALAR') { } 63 1 64
elsif (ref $data eq 'ARRAY') { } 1 0 1
179 63 ▶ @lines = $data eq "\n" ? ('') : split "\n", $data;
Branch: 1 branch
BranchTrueFalseTotal
$data eq "\n" ? : 2 61 63
180 } elsif (ref($data) eq 'ARRAY') {
181 1 @lines = @{$data};
182 } else {
183 0 croak("Got " . ref(\$data) . " when parsing PICA+ while expecting SCALAR or ARRAY");
184 }
185  
186 64 foreach my $line (@lines) {
187 458 $self->_parseline($line);
188 }
189 }
190  
191  
192 4481 ▶sub _parseline {
Subroutine _parseline: called CC=11 SCAR=24.0
193 4481 my ($self, $line) = @_;
194 4481 chomp $line; # remove newline if present
195  
196 # start of record marker
197 4481 ▶ if ( $line eq "\x1D" or (not $self->{strict} and $line =~ /^\s*$|^#|^SET/) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line eq "\035" or not $self->{'strict'} and $line =~ /^\s*$|^#|^SET/) { } 61 4420 4481
Condition100%6/6
$line eq "\35" or not $self->{"strict"} and $line =~ /^\s*$|^#|^SET/
l!l&&r!l&&!r
26354420
not $self->{"strict"} and $line =~ /^\s*$|^#|^SET/
!ll&&!rl&&r
22439835
MC/DC100%3/3
$line eq "\35" or not $self->{"strict"} and $line =~ /^\s*$|^#|^SET/
$line eq "\35" not $self->{"strict"} $line =~ /^\s*$|^#|^SET/
Truth table100%4/4
A or not(B) and C
A= $line eq "\35"B= not $self->{"strict"}C= $line =~ /^\s*$|^#|^SET/
ABCresult
1XX1
0111
00X0
0100
198 61 ▶ $self->handle_record() if $self->{active} and @{$self->{fields}};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"active"} and @{$self->{"fields"};} 21 40 61
Condition100%3/3
$self->{"active"} and @{$self->{"fields"};}
!ll&&!rl&&r
32821
MC/DC100%2/2
$self->{"active"} and @{$self->{"fields"};}
$self->{"active"} @{$self->{"fields"};}
Truth table100%3/3
A and B
A= $self->{"active"}B= @{$self->{"fields"};}
ABresult
0X0
100
111
199 } else {
200 4420 $line =~ s/^\x1D//;
201 4420 my $field = eval { PICA::Field->parse($line); };
202 4420 ▶ if ($@) {
Branch: 2 branches
BranchTrueFalseTotal
if ($@) { } 17 4403 4420
elsif ($self->{'field_handler'}) { } 3111 1292 4403
203 17 $@ =~ s/ at .*\n//; # remove line number
204 17 $field = $self->broken_field( $@, $line );
205 } elsif ($self->{field_handler}) {
206 3111 $field = $self->{field_handler}( $field );
207 }
208 4420 ▶ if ( UNIVERSAL::isa( $field, 'PICA::Field' ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (UNIVERSAL::isa($field, 'PICA::Field')) { } 1293 3127 4420
elsif (defined $field) { } 4 3123 3127
209 1293 push (@{$self->{fields}}, $field);
210 } elsif ( defined $field ) {
211 4 ▶ $self->{broken} = $field unless defined $self->{broken};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"broken"} 2 2 4
212 }
213 }
214 4481 $self->{active} = 1;
215 }
216  
217  
218 17 ▶sub broken_field {
Subroutine broken_field: called CC=3 SCAR=16.0
Pod documented
219 17 my ($self, $msg, $line) = @_;
220 17 ▶ if ($self->{broken_field_handler}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"broken_field_handler"}) 17 0 17
221 17 return $self->{broken_field_handler}( $msg, $line );
222 }
223 0 ▶ $msg = "$msg in line \"$line\"" if defined $line;
Branch: 1 branch
BranchTrueFalseTotal
if defined $line 0 0 0
224 0 print STDERR "$msg\n";
225 # TODO: count/collect errors
226 0 return;
227 }
228  
229  
230 13 ▶sub broken_record {
Subroutine broken_record: called CC=5 SCAR=20.2
Pod documented
231 13 my ($self, $msg, $record) = @_;
232 13 ▶ if ($self->{broken_record_handler}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"broken_record_handler"}) 4 9 13
233 4 return $self->{broken_record_handler}( $msg, $record );
234 }
235 9 ▶ return if UNIVERSAL::isa( $record, 'PICA::Record' ) && $record->empty;
Branch: 1 branch
BranchTrueFalseTotal
if UNIVERSAL::isa($record, "PICA::Record") and $record->empty 9 0 9
Condition33%1/3
UNIVERSAL::isa($record, "PICA::Record") and $record->empty
!ll&&!rl&&r
009
MC/DC0%0/2
UNIVERSAL::isa($record, "PICA::Record") and $record->empty
UNIVERSAL::isa($record, "PICA::Record") $record->empty
Truth table33%1/3
A and B
A= UNIVERSAL::isa($record, "PICA::Record")B= $record->empty
ABresult
0X0
100
111
236 0 ▶ print STDERR "$msg\n" if defined $msg;
Branch: 1 branch
BranchTrueFalseTotal
if defined $msg 0 0 0
237 0 return;
238 }
239  
240  
241 92 ▶sub handle_record {
Subroutine handle_record: called CC=11 SCAR=24.0
Pod documented
242 92 my $self = shift;
243  
244 92 $self->{read_counter}++;
245  
246 92 my ($record, $broken);
247  
248 # $self->{broken} = "empty record"
249 # unless defined $self->{broken} or @{$self->{fields}} > 0;
250  
251 92 ▶ if ( $self->{broken} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'broken'}) { } 2 90 92
252 2 $broken = $self->{broken};
253 } else {
254 90 $record = PICA::Record->new( @{$self->{fields}} );
255 }
256 92 $self->{fields} = [];
257 92 $self->{broken} = undef;
258  
259 # TODO: fix this
260 # return if ($self->{offset} && $self->{read_counter} < $self->{offset});
261  
262 92 ▶ if (not defined $broken) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $broken) 90 2 92
263 90 ▶ if ($self->{record_handler}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"record_handler"}) 28 62 90
264 28 ▶ if (UNIVERSAL::isa( $self->{record_handler}, 'PICA::Writer') ) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::isa($self->{'record_handler'}, 'PICA::Writer')) { } 7 21 28
265 7 $self->{record_handler}->write( $record );
266 #$record = TODO allow here!
267 } else {
268 21 $record = $self->{record_handler}( $record );
269 21 ▶ $record = undef if $record =~ /^-?\d+$/;
Branch: 1 branch
BranchTrueFalseTotal
if $record =~ /^-?\d+$/ 8 13 21
270 }
271 }
272 90 ▶ if (defined $record) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $record) 82 8 90
273 82 ▶ if ( UNIVERSAL::isa( $record, 'PICA::Record' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::isa($record, 'PICA::Record')) { } 81 1 82
274 81 ▶ $broken = "empty record" if $record->empty;
Branch: 1 branch
BranchTrueFalseTotal
if $record->empty 10 71 81
275 } else {
276 1 $broken = $record;
277 }
278 }
279 }
280  
281 92 ▶ if ( defined $broken ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $broken) { } 13 79 92
elsif (defined $record) { } 71 8 79
282 13 $self->broken_record( $broken, $record );
283 } elsif ( defined $record ) {
284 71 push @{ $self->{read_records} }, $record;
285 }
286 }
287  
288 13 1;
289  
290 __END__
« XMLParser.pm ↑ Summary Record.pm »