| 1 | package PICA::SRUSearchParser; | ||||||||||||||||||||||||||||
| 2 | { | ||||||||||||||||||||||||||||
| 3 | 1 | $PICA::SRUSearchParser::VERSION = '0.585'; | |||||||||||||||||||||||||||
| 4 | } | ||||||||||||||||||||||||||||
| 5 | #ABSTRACT: Parse a SRU response in XML and extract PICA+ records. | ||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 1 | ▶ | use Carp qw(croak); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 1 | ▶ | use PICA::XMLParser; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 1 | ▶ | use XML::SAX::ParserFactory; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 1 | ▶ | use base qw(XML::SAX::Base); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 3 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=3 SCAR=11.2
Pod
documented
| |||||||||||||||||||||||||||||
| 16 | 3 | my ($class, $xmlparser) = @_; | |||||||||||||||||||||||||||
| 17 | 3 | ▶ | $class = ref $class || $class; | ||||||||||||||||||||||||||
|
Condition33%1/3
ref $class || $class
MC/DC0%0/2
ref $class || $class
ref $class
$class
Truth table33%1/3
A || B
A= ref $classB= $class
| |||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 3 | ▶ | $xmlparser = PICA::XMLParser->new() | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 20 | unless UNIVERSAL::isa($xmlparser, "PICA::XMLParser"); | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 3 | my $self = { | |||||||||||||||||||||||||||
| 23 | xmlparser => $xmlparser, | ||||||||||||||||||||||||||||
| 24 | char_data => "", | ||||||||||||||||||||||||||||
| 25 | in_record => 0, | ||||||||||||||||||||||||||||
| 26 | numberOfRecords => undef, | ||||||||||||||||||||||||||||
| 27 | currentNumber => 0, | ||||||||||||||||||||||||||||
| 28 | resultSetId => undef, | ||||||||||||||||||||||||||||
| 29 | }; | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 3 | return bless $self, $class; | |||||||||||||||||||||||||||
| 32 | } | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 3 | ▶ | sub parse { | ||||||||||||||||||||||||||
|
Subroutine
parse: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 36 | 3 | my ($self, $document) = @_; | |||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 3 | my $parser = XML::SAX::ParserFactory->new( | |||||||||||||||||||||||||||
| 39 | RequiredFeatures => { 'http://xml.org/sax/features/namespaces' => 1 } | ||||||||||||||||||||||||||||
| 40 | )->parser( Handler => $self ); | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | 3 | $self->{currentNumber} = 0; | |||||||||||||||||||||||||||
| 43 | 3 | $parser->parse_string($document); | |||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 3 | return $self->{xmlparser}; | |||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 2 | ▶ | sub numberOfRecords { | ||||||||||||||||||||||||||
|
Subroutine
numberOfRecords: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 50 | 2 | my $self = shift; | |||||||||||||||||||||||||||
| 51 | 2 | return $self->{numberOfRecords}; | |||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | 4 | ▶ | sub currentNumber { | ||||||||||||||||||||||||||
|
Subroutine
currentNumber: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 56 | 4 | my $self = shift; | |||||||||||||||||||||||||||
| 57 | 4 | return $self->{currentNumber}; | |||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | 1 | ▶ | sub resultSetId { | ||||||||||||||||||||||||||
|
Subroutine
resultSetId: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 62 | 1 | my $self = shift; | |||||||||||||||||||||||||||
| 63 | 1 | return $self->{resultSetId}; | |||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | 1419 | ▶ | sub start_element { | ||||||||||||||||||||||||||
|
Subroutine
start_element: called CC=5 SCAR=16.3
Pod
documented
| |||||||||||||||||||||||||||||
| 68 | 1419 | my ($self, $el) = @_; | |||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 1419 | ▶ | if ($self->{in_record}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | # TODO: nasty hack because sru.gbv.de is broken: | ||||||||||||||||||||||||||||
| 73 | 1293 | my ($tag) = grep { $_->{LocalName} eq 'tag' } values %{ $el->{Attributes} }; | |||||||||||||||||||||||||||
| 74 | 1293 | ▶ | if (defined $tag and $tag->{Value} eq '') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $tag and $tag->{"Value"} eq ""
MC/DC0%0/2
defined $tag and $tag->{"Value"} eq ""
defined $tag
$tag->{"Value"} eq ""
Truth table66%2/3
A and B
A= defined $tagB= $tag->{"Value"} eq ""
| |||||||||||||||||||||||||||||
| 75 | 0 | $self->{skip_field} = 1; | |||||||||||||||||||||||||||
| 76 | } else { | ||||||||||||||||||||||||||||
| 77 | 1293 | $self->{xmlparser}->start_element($el); | |||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | } else { | ||||||||||||||||||||||||||||
| 81 | 126 | $self->{char_data} = ""; | |||||||||||||||||||||||||||
| 82 | 126 | ▶ | if ( _sru_element($el,"recordData") ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 83 | #print "$name\n"; | ||||||||||||||||||||||||||||
| 84 | 6 | $self->{in_record} = 1; | |||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 1662 | ▶ | sub _sru_element { | ||||||||||||||||||||||||||
|
Subroutine
_sru_element: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 90 | 1662 | my ($el, $name) = @_; | |||||||||||||||||||||||||||
| 91 | 1662 | return $el->{LocalName} eq $name and $el->{NamespaceURI} eq 'http://www.loc.gov/zing/srw/'; | |||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | 1419 | ▶ | sub end_element { | ||||||||||||||||||||||||||
|
Subroutine
end_element: called CC=7 SCAR=19.8
Pod
documented
| |||||||||||||||||||||||||||||
| 96 | 1419 | my ($self, $el) = @_; | |||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | 1419 | ▶ | if ($self->{in_record}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 99 | 1299 | ▶ | if ( _sru_element($el,"recordData") ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 100 | 6 | $self->{currentNumber}++; | |||||||||||||||||||||||||||
| 101 | 6 | $self->{in_record} = 0; | |||||||||||||||||||||||||||
| 102 | } else { | ||||||||||||||||||||||||||||
| 103 | 1293 | ▶ | if ( $self->{skip_field} ) { # nasty hack because sru.gbv.de is broken | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 104 | 0 | ▶ | $self->{skip_field} = 0 if $el->{LocalName} eq 'datafield'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 105 | } else { | ||||||||||||||||||||||||||||
| 106 | 1293 | $self->{xmlparser}->end_element($el); | |||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||
| 109 | } else { | ||||||||||||||||||||||||||||
| 110 | 120 | ▶ | if ( _sru_element($el,"numberOfRecords") ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 111 | 3 | $self->{numberOfRecords} = $self->{char_data}; | |||||||||||||||||||||||||||
| 112 | } elsif ( _sru_element($el,"resultSetId") ) { | ||||||||||||||||||||||||||||
| 113 | 3 | $self->{resultSetId} = $self->{char_data}; | |||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | 1929 | ▶ | sub characters { | ||||||||||||||||||||||||||
|
Subroutine
characters: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 120 | 1929 | my ($self, $data) = @_; | |||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | 1929 | ▶ | if ($self->{in_record}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 123 | 1746 | $self->{xmlparser}->characters($data); | |||||||||||||||||||||||||||
| 124 | } else { | ||||||||||||||||||||||||||||
| 125 | 183 | ($data) = values %$data; | |||||||||||||||||||||||||||
| 126 | 183 | $self->{char_data} .= $data; | |||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | 1 | 1; | |||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | __END__ | ||||||||||||||||||||||||||||