| 1 | package MAB2::Parser::XML; | ||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||
| 3 | 7 | our $VERSION = '0.24'; | |||||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||||||
| 5 | 7 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 6 | 7 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 7 | 7 | ▶ | use Carp qw<croak>; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 8 | 7 | ▶ | use XML::LibXML::Reader; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||
| 11 | 10 | ▶ | sub new { | ||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=11 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||||||||||
| 12 | 10 | my $class = shift; | |||||||||||||||||||||||||||||||||||
| 13 | 10 | my $input = shift; | |||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||
| 15 | 10 | my $self = { | |||||||||||||||||||||||||||||||||||
| 16 | filename => undef, | ||||||||||||||||||||||||||||||||||||
| 17 | rec_number => 0, | ||||||||||||||||||||||||||||||||||||
| 18 | xml_reader => undef, | ||||||||||||||||||||||||||||||||||||
| 19 | }; | ||||||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||||||
| 21 | # check for file or filehandle | ||||||||||||||||||||||||||||||||||||
| 22 | 10 | my $ishandle = eval { fileno($input); }; | |||||||||||||||||||||||||||||||||||
| 23 | 10 | ▶ | if ( !$@ && defined $ishandle ) { | ||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
Condition66%2/3
not $@ and defined $ishandle
MC/DC50%1/2
not $@ and defined $ishandle
not $@
defined $ishandle
Truth table66%2/3
not(A) and B
A= not $@B= defined $ishandle
| |||||||||||||||||||||||||||||||||||||
| 24 | 3 | binmode $input; # drop all PerlIO layers, as required by libxml2 | |||||||||||||||||||||||||||||||||||
| 25 | 3 | ▶ | my $reader = XML::LibXML::Reader->new( IO => $input ) | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 26 | or croak "cannot read from filehandle $input\n"; | ||||||||||||||||||||||||||||||||||||
| 27 | 3 | $self->{filename} = scalar $input; | |||||||||||||||||||||||||||||||||||
| 28 | 3 | $self->{xml_reader} = $reader; | |||||||||||||||||||||||||||||||||||
| 29 | } | ||||||||||||||||||||||||||||||||||||
| 30 | ▶ | elsif ( defined $input && $input !~ /\n/ && -e $input ) { | |||||||||||||||||||||||||||||||||||
|
Condition50%3/6
defined $input and not $input =~ /\n/ and -e $input
defined $input and not $input =~ /\n/
MC/DC33%1/3
defined $input and not $input =~ /\n/ and -e $input
defined $input
not $input =~ /\n/
-e $input
Truth table50%2/4
A and not(B) and C
A= defined $inputB= not $input =~ /\n/C= -e $input
| |||||||||||||||||||||||||||||||||||||
| 31 | 1 | ▶ | my $reader = XML::LibXML::Reader->new( location => $input ) | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 32 | or croak "cannot read from file $input\n"; | ||||||||||||||||||||||||||||||||||||
| 33 | 1 | $self->{filename} = $input; | |||||||||||||||||||||||||||||||||||
| 34 | 1 | $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 | 5 | ▶ | my $reader = XML::LibXML::Reader->new( string => $input ) | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 38 | or croak "cannot read XML string $input\n"; | ||||||||||||||||||||||||||||||||||||
| 39 | 5 | $self->{xml_reader} = $reader; | |||||||||||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||||||||||
| 41 | else { | ||||||||||||||||||||||||||||||||||||
| 42 | 1 | croak "file, filehande or string $input does not exists"; | |||||||||||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||||||||||
| 44 | 9 | return ( bless $self, $class ); | |||||||||||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||||||
| 48 | 30 | ▶ | sub next { | ||||||||||||||||||||||||||||||||||
|
Subroutine
next: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||
| 49 | 30 | my $self = shift; | |||||||||||||||||||||||||||||||||||
| 50 | 30 | ▶ | if ( $self->{xml_reader}->nextElement('datensatz') ) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 51 | 29 | $self->{rec_number}++; | |||||||||||||||||||||||||||||||||||
| 52 | 29 | my $record = _decode( $self->{xml_reader} ); | |||||||||||||||||||||||||||||||||||
| 53 | 29 | my ($id) = map { $_->[-1] } grep { $_->[0] =~ '001' } @{$record}; | |||||||||||||||||||||||||||||||||||
| 54 | 29 | return { _id => $id, record => $record }; | |||||||||||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||||||||||
| 56 | 1 | return; | |||||||||||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||||||
| 60 | 29 | ▶ | sub _decode { | ||||||||||||||||||||||||||||||||||
|
Subroutine
_decode: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||||||||||
| 61 | 29 | my $reader = shift; | |||||||||||||||||||||||||||||||||||
| 62 | 29 | my @record; | |||||||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||||||
| 64 | # get all field nodes from MAB2 XML record; | ||||||||||||||||||||||||||||||||||||
| 65 | 29 | foreach my $field_node ( | |||||||||||||||||||||||||||||||||||
| 66 | $reader->copyCurrentNode(1)->getChildrenByTagName('feld') ) | ||||||||||||||||||||||||||||||||||||
| 67 | { | ||||||||||||||||||||||||||||||||||||
| 68 | 1408 | my @field; | |||||||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||||||
| 70 | # get field tag number | ||||||||||||||||||||||||||||||||||||
| 71 | 1408 | my $tag = $field_node->getAttribute('nr'); | |||||||||||||||||||||||||||||||||||
| 72 | 1408 | ▶ | my $ind = $field_node->getAttribute('ind') // ''; | ||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$field_node->getAttribute("ind") // ""
MC/DC0%0/1
$field_node->getAttribute("ind") // ""
$field_node->getAttribute("ind")
Truth table50%1/2
A
A= $field_node->getAttribute("ind")
| |||||||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||||||
| 74 | # ToDo: textContent ignores </tf> and <ns> | ||||||||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||||||||
| 76 | # Check for data or subfields | ||||||||||||||||||||||||||||||||||||
| 77 | 1408 | ▶ | if ( my @subfields = $field_node->getChildrenByTagName('uf') ) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 78 | 71 | push( @field, ( $tag, $ind ) ); | |||||||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||||||
| 80 | # get all subfield nodes | ||||||||||||||||||||||||||||||||||||
| 81 | 71 | foreach my $subfield_node (@subfields) { | |||||||||||||||||||||||||||||||||||
| 82 | 115 | my $subfield_code = $subfield_node->getAttribute('code'); | |||||||||||||||||||||||||||||||||||
| 83 | 115 | my $subfield_data = $subfield_node->textContent; | |||||||||||||||||||||||||||||||||||
| 84 | 115 | push( @field, ( $subfield_code, $subfield_data ) ); | |||||||||||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||||||||||
| 87 | else { | ||||||||||||||||||||||||||||||||||||
| 88 | 1337 | my $data = $field_node->textContent(); | |||||||||||||||||||||||||||||||||||
| 89 | 1337 | push( @field, ( $tag, $ind, '_', $data ) ); | |||||||||||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||||||
| 92 | 1408 | push( @record, [@field] ); | |||||||||||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||||||||||
| 94 | 29 | return \@record; | |||||||||||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||||||
| 98 | 7 | 1; | |||||||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||||||
| 100 | __END__ | ||||||||||||||||||||||||||||||||||||