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