Add and use xpath_context()
[irspy-moved-to-github.git] / lib / ZOOM / IRSpy / Record.pm
1 # $Id: Record.pm,v 1.25 2007-05-04 12:09:24 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, "<$xpath></$xpath>");
86         @nodes = $xc->findnodes($xpath);
87         die("still no matches for '$xpath' after creating: can't append")
88             if @nodes == 0;
89     }
90
91     $this->{irspy}->log("warn",
92                         scalar(@nodes), " matches for '$xpath': using first")
93         if @nodes > 1;
94
95     $this->_half_decent_appendWellBalancedChunk($nodes[0], $frag);
96 }
97
98 sub xpath_context {
99     my $this = shift();
100
101     return irspy_xpath_context($this->{zeerex});
102 }
103
104 sub store_result {
105     my ($this, $type, %info) = @_;
106     my $xml = "<irspy:$type";
107
108     foreach my $key (keys %info) {
109         $xml .= " $key=\"" . xml_encode($info{$key}) . "\"";
110     }
111
112     $xml .= ">" . isodate(time()) . "</irspy:$type>\n";
113
114     $this->append_entry('irspy:status', $xml);
115 }
116
117
118 # *sigh*
119 #
120 # _Clearly_ the right way to append a well-balanced chunk of XML to
121 # a node's children is to call appendWellBalancedChunk() from the
122 # XML::LibXML::Element class.  However, this fails in the common case
123 # where the ZeeRex record we're working with doesn't declare the
124 # "irspy" namespace that the inserted fragments use.
125 #
126 # To my utter astonishment it seems that XML::LibXML (as of version
127 # 1.58, 31st March 2004) doesn't provide ANY way to register a
128 # namespace for parsing, which makes the parse_balanced_chunk()
129 # function that appendWellBalancedChunk() uses effectively useless.
130 # It _is_ possible to use setNamespace() on a node, to register a new
131 # namespace mapping for that node -- but that only affects pre-parsed
132 # trees, and is no use for parsing.  Hence the following pair of lines
133 # DOES NOT WORK:
134 #       $node->setNamespace($ZOOM::IRSpy::Utils::IRSPY_NS, "irspy", 0);
135 #       $node->appendWellBalancedChunk($frag);
136 #
137 # Instead I have to go the long way round, hence this method.  I have
138 # two candidate re-implementations, of which the former is marginally
139 # less loathsome, but does require that the excess namespace
140 # declarations be factored out later -- as least, if you want neat
141 # output.
142 #
143 sub _half_decent_appendWellBalancedChunk {
144     my $this = shift();
145     my($node, $frag) = @_;
146
147     if (1) {
148         $frag =~ s,>, xmlns:irspy="$ZOOM::IRSpy::Utils::IRSPY_NS">,;
149         $node->appendWellBalancedChunk($frag);
150         return;
151     }
152
153     # Instead -- and to call this brain-damaged would be an insult
154     # to all those fine people out there with actual brain damage
155     # -- I have to "parse" the XML fragment myself and insert the
156     # resulting hand-build DOM tree.  Someone shoot me now.
157     my($open, $content, $close) = $frag =~ /^<(.*?)>(.*)<\/(.*?)>$/;
158     die "can't 'parse' XML fragment '$frag'"
159         if !defined $open;
160     my($tag, $attrs) = $open =~ /(.*?)\s(.*)/;
161     $tag = $open if !defined $tag;
162     die "mismatched XML start/end <$open>...<$close>"
163         if $close ne $tag;
164     print STDERR "tag='$tag', attrs=[$attrs], content='$content'\n";
165     die "## no code yet to make DOM node";
166 }
167
168
169 =head1 SEE ALSO
170
171 ZOOM::IRSpy
172
173 =head1 AUTHOR
174
175 Mike Taylor, E<lt>mike@indexdata.comE<gt>
176
177 =head1 COPYRIGHT AND LICENSE
178
179 Copyright (C) 2006 by Index Data ApS.
180
181 This library is free software; you can redistribute it and/or modify
182 it under the same terms as Perl itself, either Perl version 5.8.7 or,
183 at your option, any later version of Perl 5 you may have available.
184
185 =cut
186
187 1;