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