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