PICA/Parser/XML.pm

filter: Statementstmt 100% 53 / 53 Branchbran 80.0% 16 / 20 Conditioncond 76.1% 16 / 21 MC/DCmcdc 50.0% 7 / 14 Subroutinesub 100% 8 / 8 Podpod 100% 1 / 1 totaltotal 86.3% 101 / 117 CC 20 SCAR 22.0
CC
20
Cov
90%
new
13.4
_next_record
8.1
BEGIN
1.0
CRAP 9.1
« PICA/Parser/Plain.pm ↑ Summary PICA/Writer/Import.pm »
1 package PICA::Parser::XML;
2 16 ▶use v5.14.1;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 16 our $VERSION = '2.12';
5  
6 16 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 16 ▶use Scalar::Util qw(reftype);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 16 ▶use Encode qw(encode);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 16 ▶use XML::LibXML::Reader;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 16 ▶use parent 'PICA::Parser::Base';
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 14 ▶sub new {
Subroutine new: called CC=13 SCAR=26.0
Pod documented
14 14 my $self = PICA::Parser::Base::_new(@_);
15 14 my $input = $self->{fh};
16  
17 # check for file or filehandle
18 14 my $ishandle = eval {fileno($input);};
19 14 ▶ if (!$@ && defined $ishandle) {
Branch: 4 branches
BranchTrueFalseTotal
if (not $@ and defined $ishandle) { } 3 11 14
elsif (not $input =~ /\n/u and -e $input) { } 3 8 11
elsif (ref $input and builtin::reftype($input) eq 'SCALAR') { } 4 4 8
elsif (defined $input and length $input > 0) { } 3 1 4
Condition100%3/3
not $@ and defined $ishandle
!ll&&!rl&&r
473
MC/DC100%2/2
not $@ and defined $ishandle
not $@ defined $ishandle
Truth table100%3/3
not(A) and B
A= not $@B= defined $ishandle
ABresult
0X0
100
111
20 3 binmode $input; # drop all PerlIO layers, as required by libxml2
21 3 ▶ 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") 3 0 3
22 or croak "cannot read from filehandle $input\n";
23 3 $self->{xml_reader} = $reader;
24 }
25 ▶ elsif ($input !~ /\n/ && -e $input) {
Condition100%3/3
not $input =~ /\n/u and -e $input
!ll&&!rl&&r
263
MC/DC100%2/2
not $input =~ /\n/u and -e $input
not $input =~ /\n/u -e $input
Truth table100%3/3
not(A) and B
A= not $input =~ /\n/uB= -e $input
ABresult
0X0
100
111
26 3 ▶ 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") 3 0 3
27 or croak "cannot read from file $input\n";
28 3 $self->{xml_reader} = $reader;
29 }
30 ▶ elsif ((ref $input and reftype $input eq 'SCALAR')) {
Condition66%2/3
ref $input and builtin::reftype($input) eq "SCALAR"
!ll&&!rl&&r
404
MC/DC50%1/2
ref $input and builtin::reftype($input) eq "SCALAR"
ref $input builtin::reftype($input) eq "SCALAR"
Truth table66%2/3
A and B
A= ref $inputB= builtin::reftype($input) eq "SCALAR"
ABresult
0X0
100
111
31 4 $input = encode('UTF-8', $$input); # Unicode to raw bytes
32 4 ▶ 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 reference\n") 4 0 4
33 or croak "cannot read XML string reference\n";
34 4 $self->{xml_reader} = $reader;
35 }
36 ▶ elsif (defined $input && length $input > 0) {
Condition66%2/3
defined $input and length $input > 0
!ll&&!rl&&r
013
MC/DC50%1/2
defined $input and length $input > 0
defined $input length $input > 0
Truth table66%2/3
A and B
A= defined $inputB= length $input > 0
ABresult
0X0
100
111
37 3 ▶ 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\n") 3 0 3
38 or croak "cannot read XML string\n";
39 3 $self->{xml_reader} = $reader;
40 }
41 else {
42 1 croak "file, filehande or string $input does not exists";
43 }
44  
45 13 $self;
46 }
47  
48 16 my $namespaceURI = 'info:srw/schema/5/picaXML-v1.0';
49 16 my $recordPattern
50 = XML::LibXML::Pattern->new('record|p:record', {p => $namespaceURI});
51  
52 11 ▶sub _next_record {
Subroutine _next_record: called CC=8 SCAR=20.9
53 11 my ($self) = @_;
54  
55 11 my $reader = $self->{xml_reader};
56 11 ▶ return unless $reader->nextPatternMatch($recordPattern);
Branch: 1 branch
BranchTrueFalseTotal
unless $reader->nextPatternMatch($recordPattern) 1 10 11
57  
58 10 my @record;
59  
60 # get all field nodes from PICA record;
61 10 foreach my $field_node (
62 $reader->copyCurrentNode(1)->getChildrenByLocalName('datafield'))
63 {
64 59 my @fields;
65  
66 # get field tag number
67 59 ▶ my $tag = $field_node->getAttribute('tag')
Condition66%2/3
$field_node->getAttribute("tag") // $field_node->getAttributeNS($namespaceURI, "tag")
l!l&&r!l&&!r
5810
MC/DC0%0/2
$field_node->getAttribute("tag") // $field_node->getAttributeNS($namespaceURI, "tag")
$field_node->getAttribute("tag") $field_node->getAttributeNS($namespaceURI, "tag")
Truth table66%2/3
A // B
A= $field_node->getAttribute("tag")B= $field_node->getAttributeNS($namespaceURI, "tag")
ABresult
1X1
011
000
68 // $field_node->getAttributeNS($namespaceURI, 'tag');
69 59 ▶ my $occurrence = $field_node->getAttribute('occurrence')
Condition66%2/3
$field_node->getAttribute("occurrence") // $field_node->getAttributeNS($namespaceURI, "occurrence")
l!l&&r!l&&!r
21038
MC/DC50%1/2
$field_node->getAttribute("occurrence") // $field_node->getAttributeNS($namespaceURI, "occurrence")
$field_node->getAttribute("occurrence") $field_node->getAttributeNS($namespaceURI, "occurrence")
Truth table66%2/3
A // B
A= $field_node->getAttribute("occurrence")B= $field_node->getAttributeNS($namespaceURI, "occurrence")
ABresult
1X1
011
000
70 // $field_node->getAttributeNS($namespaceURI, 'occurrence');
71 59 ▶ push(@fields, ($tag, $occurrence > 0 ? $occurrence : ''));
Branch: 1 branch
BranchTrueFalseTotal
$occurrence > 0 ? : 21 38 59
72  
73 # get all subfield nodes
74 59 foreach my $subfield_node (
75 $field_node->getChildrenByLocalName('subfield'))
76 {
77 122 ▶ my $subfield_code = $subfield_node->getAttribute('code')
Condition66%2/3
$subfield_node->getAttribute("code") // $subfield_node->getAttributeNS($namespaceURI, "code")
l!l&&r!l&&!r
12110
MC/DC0%0/2
$subfield_node->getAttribute("code") // $subfield_node->getAttributeNS($namespaceURI, "code")
$subfield_node->getAttribute("code") $subfield_node->getAttributeNS($namespaceURI, "code")
Truth table66%2/3
A // B
A= $subfield_node->getAttribute("code")B= $subfield_node->getAttributeNS($namespaceURI, "code")
ABresult
1X1
011
000
78 // $subfield_node->getAttributeNS($namespaceURI, 'code');
79 122 my $subfield_data = $subfield_node->textContent;
80 122 push(@fields, ($subfield_code, $subfield_data));
81 }
82 59 push(@record, [@fields]);
83 }
84 10 return \@record;
85 }
86  
87 16 1;
88 __END__
« PICA/Parser/Plain.pm ↑ Summary PICA/Writer/Import.pm »