blib/lib/PICA/Parser/XML.pm

filter: Statementstmt 100% 53 / 53 Branchbran 77.7% 14 / 18 Conditioncond 78.2% 18 / 23 MC/DCmcdc 57.1% 8 / 14 Subroutinesub 100% 8 / 8 Podpod 100% 1 / 1 totaltotal 87.1% 102 / 117 CC 20 SCAR 22.0
CC
20
Cov
90%
new
13.4
_next_record
8.1
BEGIN
1.0
CRAP 9.1
« blib/lib/PICA/Schema/Builder.pm ↑ Summary blib/lib/PICA/Data.pm »
1 package PICA::Parser::XML;
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 Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 8 ▶use Scalar::Util qw(reftype);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 8 ▶use Encode qw(encode);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 8 ▶use XML::LibXML::Reader;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 8 ▶use parent 'PICA::Parser::Base';
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 sub new {
14 11 ▶ my $self = PICA::Parser::Base::_new(@_);
Subroutine new: called CC=13 SCAR=26.0
Pod documented
15 11 my $input = $self->{fh};
16  
17 # check for file or filehandle
18 11 my $ishandle = eval {fileno($input);};
19 11 ▶ if (!$@ && defined $ishandle) {
Branch: 4 branches
BranchTrueFalseTotal
if (not $@ and defined $ishandle) { } 2 9 11
elsif (not $input =~ /\n/u and -e $input) { } 2 7 9
elsif (ref $input and builtin::reftype($input) eq 'SCALAR') { } 3 4 7
elsif (defined $input and length $input > 0) { } 3 1 4
Condition100%3/3
not $@ and defined $ishandle
!ll&&!rl&&r
362
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 2 binmode $input; # drop all PerlIO layers, as required by libxml2
21 2 ▶ 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 2 2
22 or croak "cannot read from filehandle $input\n";
23 2 $self->{xml_reader} = $reader;
24 }
25 ▶ elsif ($input !~ /\n/ && -e $input) {
Condition100%3/3
not $input =~ /\n/u and -e $input
!ll&&!rl&&r
252
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 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
27 or croak "cannot read from file $input\n";
28 2 $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
403
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 3 $input = encode('UTF-8', $$input); # Unicode to raw bytes
32 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 reference\n") 0 3 3
33 or croak "cannot read XML string reference\n";
34 3 $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") 0 3 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 10 $self;
46 }
47  
48 8 my $namespaceURI = 'info:srw/schema/5/picaXML-v1.0';
49 8 my $recordPattern
50 = XML::LibXML::Pattern->new('record|p:record', {p => $namespaceURI});
51  
52 sub _next_record {
53 11 ▶ my ($self) = @_;
Subroutine _next_record: called CC=8 SCAR=20.9
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')
Condition80%4/5
($field_node->getAttribute("occurrence") // $field_node->getAttributeNS($namespaceURI, "occurrence")) // ""
l!l
2138
$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));
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
12101
MC/DC50%1/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 8 1;
88 __END__
« blib/lib/PICA/Schema/Builder.pm ↑ Summary blib/lib/PICA/Data.pm »