blib/lib/MARC/Parser/XML.pm

filter: Statementstmt 78.7% 37 / 47 Branchbran 45.8% 11 / 24 Conditioncond 27.7% 5 / 18 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 100% 8 / 8 Podpod 100% 2 / 2 totaltotal 57.8% 63 / 109 SCAR 39.2
CC
21
Cov
60%
new
66.6
_decode
7.2
next
3.2
CRAP 50.2
↑ Summary Build »
1 package MARC::Parser::XML;
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use 5.008_005;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 our $VERSION = '0.02';
7  
8 2 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use XML::LibXML::Reader;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 sub new {
12 2 ▶ my ( $class, $input ) = @_;
Subroutine new: called CC=13 SCAR=42.0
Pod documented
13  
14 2 my $self = bless { input => $input, rec_number => 0, }, $class;
15  
16 # check for file or filehandle
17 2 my $ishandle = eval { fileno($input); };
18 2 ▶ if ( !$@ && defined $ishandle ) {
Branch: 3 branches
BranchTrueFalseTotal
if (not $@ and defined $ishandle) { } 0 2 2
elsif (defined $input and not $input =~ /\n/ and -e $input) { } 2 0 2
elsif (defined $input and length $input > 0) { } 0 0 0
Condition33%1/3
not $@ and defined $ishandle
!ll&&!rl&&r
020
MC/DC0%0/2
not $@ and defined $ishandle
not $@ defined $ishandle
Truth table33%1/3
not(A) and B
A= not $@B= defined $ishandle
ABresult
0X0
100
111
19 0 binmode $input; # drop all PerlIO layers, as required by libxml2
20 0 ▶ my $reader = XML::LibXML::Reader->new( IO => $input )
Branch: 1 branch
BranchTrueFalseTotal
my $reader = "XML::LibXML::Reader"->new("IO", $input) or croak("cannot read from filehandle $input\n") 0 0 0
21 or croak "cannot read from filehandle $input\n";
22 0 $self->{xml_reader} = $reader;
23 }
24 ▶ elsif ( defined $input && $input !~ /\n/ && -e $input ) {
Condition33%2/6
defined $input and not $input =~ /\n/ and -e $input
!ll&&!rl&&r
002
defined $input and not $input =~ /\n/
!ll&&!rl&&r
002
MC/DC0%0/3
defined $input and not $input =~ /\n/ and -e $input
defined $input not $input =~ /\n/ -e $input
Truth table25%1/4
A and not(B) and C
A= defined $inputB= not $input =~ /\n/C= -e $input
ABCresult
0XX0
10X0
1100
1111
25 2 ▶ my $reader = XML::LibXML::Reader->new( location => $input )
Branch: 1 branch
BranchTrueFalseTotal
my $reader = "XML::LibXML::Reader"->new("location", $input) or croak("cannot read from file $input\n") 0 2 2
26 or croak "cannot read from file $input\n";
27 2 $self->{xml_reader} = $reader;
28 }
29 ▶ elsif ( defined $input && length $input > 0 ) {
Condition0%0/3
defined $input and length $input > 0
!ll&&!rl&&r
000
MC/DC0%0/2
defined $input and length $input > 0
defined $input length $input > 0
Truth table0%0/3
A and B
A= defined $inputB= length $input > 0
ABresult
0X0
100
111
30 0 ▶ $input = ${$input} if ( ref($input) // '' eq 'SCALAR' );
Branch: 1 branch
BranchTrueFalseTotal
if 0 0 0
Condition0%0/2
ref $input //
l!l
00
MC/DC0%0/1
ref $input //
ref $input
Truth table0%0/2
A
A= ref $input
Aresult
11
00
31 0 ▶ my $reader = XML::LibXML::Reader->new( string => $input )
Branch: 1 branch
BranchTrueFalseTotal
my $reader = "XML::LibXML::Reader"->new("string", $input) or croak("cannot read XML string $input\n") 0 0 0
32 or croak "cannot read XML string $input\n";
33 0 $self->{xml_reader} = $reader;
34 }
35 else {
36 0 croak "file, filehande or string $input does not exists";
37 }
38 2 return $self;
39 }
40  
41 sub next {
42 22 ▶ my ($self) = @_;
Subroutine next: called CC=3 SCAR=11.8
Pod documented
43  
44 return
45 unless $self->{xml_reader}
46 22 ▶ ->nextElement( 'record', 'http://www.loc.gov/MARC21/slim' );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"xml_reader"}->nextElement("record", "http://www.loc.gov/MARC21/slim") 2 20 22
47  
48 20 ▶ if ( my $record = $self->_decode() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $record = $self->_decode) { } 20 0 20
49 20 return $record;
50 }
51 else {
52 0 return $self->next;
53 }
54  
55 0 return;
56 }
57  
58 sub _decode {
59 20 ▶ my ($self) = @_;
Subroutine _decode: called CC=7 SCAR=19.7
60 20 my @record;
61  
62 20 foreach my $field_node (
63 $self->{xml_reader}->copyCurrentNode(1)->getChildrenByTagName('*') )
64 {
65  
66 366 ▶ if ( $field_node->localName =~ m/leader/ ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($field_node->localName =~ /leader/) { } 20 346 366
elsif ($field_node->localName =~ /controlfield/) { } 80 266 346
elsif ($field_node->localName eq 'datafield') { } 266 0 266
67 20 push @record,
68 [ 'ldr', undef, undef, '_', $field_node->textContent ];
69 }
70 elsif ( $field_node->localName =~ m/controlfield/ ) {
71 80 push @record,
72 [
73 $field_node->getAttribute('tag'), undef,
74 undef, '_',
75 $field_node->textContent
76 ];
77 }
78 elsif ( $field_node->localName eq 'datafield' ) {
79 push @record,
80 [
81 $field_node->getAttribute('tag'),
82 $field_node->getAttribute('ind1') // '',
83 $field_node->getAttribute('ind2') // '',
84 266 ▶ map { $_->getAttribute('code'), $_->textContent }
Condition50%2/4
$field_node->getAttribute("ind1") // ""
l!l
2660
$field_node->getAttribute("ind2") // ""
l!l
2660
MC/DC0%0/1
$field_node->getAttribute("ind1") // ""
$field_node->getAttribute("ind1")
MC/DC0%0/1
$field_node->getAttribute("ind2") // ""
$field_node->getAttribute("ind2")
Truth table50%1/2
A
A= $field_node->getAttribute("ind1")
Aresult
11
00
Truth table50%1/2
A
A= $field_node->getAttribute("ind2")
Aresult
11
00
85 $field_node->getChildrenByTagName('*')
86 ];
87 }
88 }
89 20 return \@record;
90 }
91  
92 2 1;
93 __END__
↑ Summary Build »