| 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.01'; | |||||||||||||||||||||||||||||||||||
| 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
Condition33%1/3
not $@ and defined $ishandle
MC/DC0%0/2
not $@ and defined $ishandle
not $@
defined $ishandle
Truth table33%1/3
not(A) and B
A= not $@B= defined $ishandle
| |||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||
| 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
defined $input and not $input =~ /\n/
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
| |||||||||||||||||||||||||||||||||||||
| 25 | 2 | ▶ | my $reader = XML::LibXML::Reader->new( location => $input ) | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 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
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
| |||||||||||||||||||||||||||||||||||||
| 30 | 0 | ▶ | $input = ${$input} if ( ref($input) // '' eq 'SCALAR' ); | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/2
ref $input //
MC/DC0%0/1
ref $input //
ref $input
Truth table0%0/2
A
A= ref $input
| |||||||||||||||||||||||||||||||||||||
| 31 | 0 | ▶ | my $reader = XML::LibXML::Reader->new( string => $input ) | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||||||
| 48 | 20 | ▶ | if ( my $record = $self->_decode() ) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||
| 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") // ""
$field_node->getAttribute("ind2") // ""
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")
Truth table50%1/2
A
A= $field_node->getAttribute("ind2")
| |||||||||||||||||||||||||||||||||||||
| 85 | $field_node->getChildrenByTagName('*') | ||||||||||||||||||||||||||||||||||||
| 86 | ]; | ||||||||||||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||||||||||
| 89 | 20 | return \@record; | |||||||||||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||||||
| 92 | 2 | 1; | |||||||||||||||||||||||||||||||||||
| 93 | __END__ | ||||||||||||||||||||||||||||||||||||