8c9282637a4051d8c91320a4dddddc72d66398d1
[irspy-moved-to-github.git] / lib / ZOOM / IRSpy / Record.pm
1 # $Id: Record.pm,v 1.22 2007-03-01 13:21:05 mike Exp $
2
3 package ZOOM::IRSpy::Record;
4
5 use 5.008;
6 use strict;
7 use warnings;
8
9 use XML::LibXML;
10 use XML::LibXML::XPathContext;
11 use ZOOM::IRSpy::Utils qw(xml_encode isodate);
12
13 =head1 NAME
14
15 ZOOM::IRSpy::Record - record describing a target for IRSpy
16
17 =head1 SYNOPSIS
18
19  ## To follow
20
21 =head1 DESCRIPTION
22
23 I<## To follow>
24
25 =cut
26
27 sub new {
28     my $class = shift();
29     my($irspy, $target, $zeerex) = @_;
30
31     if (!defined $zeerex) {
32         $zeerex = _empty_zeerex_record($target);
33     }
34
35     ### Parser should be in the IRSpy object
36     my $parser = new XML::LibXML();
37     return bless {
38         irspy => $irspy,
39         target => $target,
40         parser => $parser,
41         zeerex => $parser->parse_string($zeerex)->documentElement(),
42     }, $class;
43 }
44
45
46 sub _empty_zeerex_record {
47     my($target) = @_;
48
49     ### Doesn't recognise SRU/SRW URLs
50     my($host, $port, $db) = ZOOM::IRSpy::_parse_target_string($target);
51
52     my $xhost = xml_encode($host);
53     my $xport = xml_encode($port);
54     my $xdb = xml_encode($db);
55     return <<__EOT__;
56 <explain xmlns="http://explain.z3950.org/dtd/2.0/">
57  <serverInfo protocol="Z39.50" version="1995">
58   <host>$xhost</host>
59   <port>$xport</port>
60   <database>$xdb</database>
61  </serverInfo>
62 </explain>
63 __EOT__
64 }
65
66
67 sub append_entry {
68     my $this = shift();
69     my($xpath, $frag) = @_;
70
71     #print STDERR "this=$this, xpath='$xpath', frag='$frag'\n";
72     my $root = $this->{zeerex}; # XML::LibXML::Element ISA XML::LibXML::Node
73     my $xc = XML::LibXML::XPathContext->new($root);
74     $xc->registerNs(zeerex => "http://explain.z3950.org/dtd/2.0/");
75     $xc->registerNs(irspy => $ZOOM::IRSpy::Utils::IRSPY_NS);
76
77     my @nodes = $xc->findnodes($xpath);
78     if (@nodes == 0) {
79         # Make the node that we're inserting into, if possible.  A
80         # fully general version would work its way through each
81         # component of the XPath, but for now we just treat it as a
82         # single chunk to go inside the top-level node.
83         $this->_half_decent_appendWellBalancedChunk($root,
84                                                     "<$xpath></$xpath>");
85         @nodes = $xc->findnodes($xpath);
86         die("still no matches for '$xpath' after creating: can't append")
87             if @nodes == 0;
88     }
89
90     $this->{irspy}->log("warn",
91                         scalar(@nodes), " matches for '$xpath': using first")
92         if @nodes > 1;
93
94     $this->_half_decent_appendWellBalancedChunk($nodes[0], $frag);
95 }
96
97 sub store_result {
98     my ($this, $type, %info) = @_;
99     my $xml = "<irspy:$type";
100
101     foreach my $key (keys %info) {
102         $xml .= " $key=\"" . xml_encode($info{$key}) . "\"";
103     }
104
105     $xml .= ">" . isodate(time()) . "</irspy:$type>\n";
106
107     $this->append_entry('irspy:status', $xml);
108 }
109
110
111 # *sigh*
112 #
113 # _Clearly_ the right way to append a well-balanced chunk of XML to
114 # a node's children is to call appendWellBalancedChunk() from the
115 # XML::LibXML::Element class.  However, this fails in the common case
116 # where the ZeeRex record we're working with doesn't declare the
117 # "irspy" namespace that the inserted fragments use.
118 #
119 # To my utter astonishment it seems that XML::LibXML (as of version
120 # 1.58, 31st March 2004) doesn't provide ANY way to register a
121 # namespace for parsing, which makes the parse_balanced_chunk()
122 # function that appendWellBalancedChunk() uses effectively useless.
123 # It _is_ possible to use setNamespace() on a node, to register a new
124 # namespace mapping for that node -- but that only affects pre-parsed
125 # trees, and is no use for parsing.  Hence the following pair of lines
126 # DOES NOT WORK:
127 #       $node->setNamespace($ZOOM::IRSpy::Utils::IRSPY_NS, "irspy", 0);
128 #       $node->appendWellBalancedChunk($frag);
129 #
130 # Instead I have to go the long way round, hence this method.  I have
131 # two candidate re-implementations, of which the former is marginally
132 # less loathsome, but does require that the excess namespace
133 # declarations be factored out later -- as least, if you want neat
134 # output.
135 #
136 sub _half_decent_appendWellBalancedChunk {
137     my $this = shift();
138     my($node, $frag) = @_;
139
140     if (1) {
141         $frag =~ s,>, xmlns:irspy="$ZOOM::IRSpy::Utils::IRSPY_NS">,;
142         $node->appendWellBalancedChunk($frag);
143         return;
144     }
145
146     # Instead -- and to call this brain-damaged would be an insult
147     # to all those fine people out there with actual brain damage
148     # -- I have to "parse" the XML fragment myself and insert the
149     # resulting hand-build DOM tree.  Someone shoot me now.
150     my($open, $content, $close) = $frag =~ /^<(.*?)>(.*)<\/(.*?)>$/;
151     die "can't 'parse' XML fragment '$frag'"
152         if !defined $open;
153     my($tag, $attrs) = $open =~ /(.*?)\s(.*)/;
154     $tag = $open if !defined $tag;
155     die "mismatched XML start/end <$open>...<$close>"
156         if $close ne $tag;
157     print STDERR "tag='$tag', attrs=[$attrs], content='$content'\n";
158     die "## no code yet to make DOM node";
159 }
160
161
162 =head1 SEE ALSO
163
164 ZOOM::IRSpy
165
166 =head1 AUTHOR
167
168 Mike Taylor, E<lt>mike@indexdata.comE<gt>
169
170 =head1 COPYRIGHT AND LICENSE
171
172 Copyright (C) 2006 by Index Data ApS.
173
174 This library is free software; you can redistribute it and/or modify
175 it under the same terms as Perl itself, either Perl version 5.8.7 or,
176 at your option, any later version of Perl 5 you may have available.
177
178 =cut
179
180 1;