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