Comment.
[irspy-moved-to-github.git] / lib / ZOOM / IRSpy / Record.pm
1 # $Id: Record.pm,v 1.23 2007-03-05 19:42:13 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     ### Doesn't recognise SRU/SRW URLs
52     my($host, $port, $db) = ZOOM::IRSpy::_parse_target_string($target);
53
54     my $xhost = xml_encode($host);
55     my $xport = xml_encode($port);
56     my $xdb = xml_encode($db);
57     return <<__EOT__;
58 <explain xmlns="http://explain.z3950.org/dtd/2.0/">
59  <serverInfo protocol="Z39.50" version="1995">
60   <host>$xhost</host>
61   <port>$xport</port>
62   <database>$xdb</database>
63  </serverInfo>
64 </explain>
65 __EOT__
66 }
67
68
69 sub append_entry {
70     my $this = shift();
71     my($xpath, $frag) = @_;
72
73     #print STDERR "this=$this, xpath='$xpath', frag='$frag'\n";
74     my $root = $this->{zeerex}; # XML::LibXML::Element ISA XML::LibXML::Node
75     my $xc = XML::LibXML::XPathContext->new($root);
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($root,
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 store_result {
100     my ($this, $type, %info) = @_;
101     my $xml = "<irspy:$type";
102
103     foreach my $key (keys %info) {
104         $xml .= " $key=\"" . xml_encode($info{$key}) . "\"";
105     }
106
107     $xml .= ">" . isodate(time()) . "</irspy:$type>\n";
108
109     $this->append_entry('irspy:status', $xml);
110 }
111
112
113 # *sigh*
114 #
115 # _Clearly_ the right way to append a well-balanced chunk of XML to
116 # a node's children is to call appendWellBalancedChunk() from the
117 # XML::LibXML::Element class.  However, this fails in the common case
118 # where the ZeeRex record we're working with doesn't declare the
119 # "irspy" namespace that the inserted fragments use.
120 #
121 # To my utter astonishment it seems that XML::LibXML (as of version
122 # 1.58, 31st March 2004) doesn't provide ANY way to register a
123 # namespace for parsing, which makes the parse_balanced_chunk()
124 # function that appendWellBalancedChunk() uses effectively useless.
125 # It _is_ possible to use setNamespace() on a node, to register a new
126 # namespace mapping for that node -- but that only affects pre-parsed
127 # trees, and is no use for parsing.  Hence the following pair of lines
128 # DOES NOT WORK:
129 #       $node->setNamespace($ZOOM::IRSpy::Utils::IRSPY_NS, "irspy", 0);
130 #       $node->appendWellBalancedChunk($frag);
131 #
132 # Instead I have to go the long way round, hence this method.  I have
133 # two candidate re-implementations, of which the former is marginally
134 # less loathsome, but does require that the excess namespace
135 # declarations be factored out later -- as least, if you want neat
136 # output.
137 #
138 sub _half_decent_appendWellBalancedChunk {
139     my $this = shift();
140     my($node, $frag) = @_;
141
142     if (1) {
143         $frag =~ s,>, xmlns:irspy="$ZOOM::IRSpy::Utils::IRSPY_NS">,;
144         $node->appendWellBalancedChunk($frag);
145         return;
146     }
147
148     # Instead -- and to call this brain-damaged would be an insult
149     # to all those fine people out there with actual brain damage
150     # -- I have to "parse" the XML fragment myself and insert the
151     # resulting hand-build DOM tree.  Someone shoot me now.
152     my($open, $content, $close) = $frag =~ /^<(.*?)>(.*)<\/(.*?)>$/;
153     die "can't 'parse' XML fragment '$frag'"
154         if !defined $open;
155     my($tag, $attrs) = $open =~ /(.*?)\s(.*)/;
156     $tag = $open if !defined $tag;
157     die "mismatched XML start/end <$open>...<$close>"
158         if $close ne $tag;
159     print STDERR "tag='$tag', attrs=[$attrs], content='$content'\n";
160     die "## no code yet to make DOM node";
161 }
162
163
164 =head1 SEE ALSO
165
166 ZOOM::IRSpy
167
168 =head1 AUTHOR
169
170 Mike Taylor, E<lt>mike@indexdata.comE<gt>
171
172 =head1 COPYRIGHT AND LICENSE
173
174 Copyright (C) 2006 by Index Data ApS.
175
176 This library is free software; you can redistribute it and/or modify
177 it under the same terms as Perl itself, either Perl version 5.8.7 or,
178 at your option, any later version of Perl 5 you may have available.
179
180 =cut
181
182 1;