1 # $Id: Record.pm,v 1.28 2007-12-12 08:49:58 mike Exp $
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
13 use XML::LibXML::XPathContext;
14 use ZOOM::IRSpy::Utils qw(xml_encode isodate irspy_xpath_context);
18 ZOOM::IRSpy::Record - record describing a target for IRSpy
32 my($irspy, $target, $zeerex) = @_;
34 if (!defined $zeerex) {
35 $zeerex = _empty_zeerex_record($target);
38 ### Parser should be in the IRSpy object
39 my $parser = new XML::LibXML();
44 zeerex => $parser->parse_string($zeerex)->documentElement(),
47 #Scalar::Util::weaken($this->{irspy});
48 #Scalar::Util::weaken($this->{parser});
54 sub _empty_zeerex_record {
57 my($protocol, $host, $port, $db) =
58 ZOOM::IRSpy::_parse_target_string($target);
60 my $xprotocol = xml_encode($protocol);
61 my $xhost = xml_encode($host);
62 my $xport = xml_encode($port);
63 my $xdb = xml_encode($db);
65 <explain xmlns="http://explain.z3950.org/dtd/2.0/">
66 <serverInfo protocol="$xprotocol">
69 <database>$xdb</database>
78 my($xpath, $frag) = @_;
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);
85 my @nodes = $xc->findnodes($xpath);
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(),
93 @nodes = $xc->findnodes($xpath);
94 die("still no matches for '$xpath' after creating: can't append")
98 $this->{irspy}->log("warn",
99 scalar(@nodes), " matches for '$xpath': using first")
102 $this->_half_decent_appendWellBalancedChunk($nodes[0], $frag);
108 return irspy_xpath_context($this->{zeerex});
112 my ($this, $type, %info) = @_;
113 my $xml = "<irspy:$type";
115 foreach my $key (keys %info) {
116 $xml .= " $key=\"" . xml_encode($info{$key}) . "\"";
119 $xml .= ">" . isodate(time()) . "</irspy:$type>\n";
121 $this->append_entry('irspy:status', $xml);
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.
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
141 # $node->setNamespace($ZOOM::IRSpy::Utils::IRSPY_NS, "irspy", 0);
142 # $node->appendWellBalancedChunk($frag);
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
150 sub _half_decent_appendWellBalancedChunk {
152 my($node, $frag) = @_;
155 $frag =~ s,>, xmlns:irspy="$ZOOM::IRSpy::Utils::IRSPY_NS">,;
157 $node->appendWellBalancedChunk($frag);
159 print STDERR "died while trying to appendWellBalancedChunk(), probably due to bad XML:\n$frag";
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'"
172 my($tag, $attrs) = $open =~ /(.*?)\s(.*)/;
173 $tag = $open if !defined $tag;
174 die "mismatched XML start/end <$open>...<$close>"
176 print STDERR "tag='$tag', attrs=[$attrs], content='$content'\n";
177 die "## no code yet to make DOM node";
187 Mike Taylor, E<lt>mike@indexdata.comE<gt>
189 =head1 COPYRIGHT AND LICENSE
191 Copyright (C) 2006 by Index Data ApS.
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.