| 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
Condition100%3/3
not $@ and defined $ishandle
MC/DC100%2/2
not $@ and defined $ishandle
not $@
defined $ishandle
Truth table100%3/3
not(A) and B
A= not $@B= defined $ishandle
| |||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||
| 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
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
| |||||||||||||||||||||||||||||||||||||||||
| 26 | 3 | ▶ | my $reader = XML::LibXML::Reader->new(location => $input) | ||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||
| 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"
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"
| |||||||||||||||||||||||||||||||||||||||||
| 31 | 4 | $input = encode('UTF-8', $$input); # Unicode to raw bytes | |||||||||||||||||||||||||||||||||||||||
| 32 | 4 | ▶ | my $reader = XML::LibXML::Reader->new(string => $input) | ||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||
| 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
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
| |||||||||||||||||||||||||||||||||||||||||
| 37 | 3 | ▶ | my $reader = XML::LibXML::Reader->new(string => $input) | ||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||
| 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")
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")
| |||||||||||||||||||||||||||||||||||||||||
| 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")
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")
| |||||||||||||||||||||||||||||||||||||||||
| 70 | // $field_node->getAttributeNS($namespaceURI, 'occurrence'); | ||||||||||||||||||||||||||||||||||||||||
| 71 | 59 | ▶ | push(@fields, ($tag, $occurrence > 0 ? $occurrence : '')); | ||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||
| 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")
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")
| |||||||||||||||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||||||||||||||