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