1 # $Id: Record.pm,v 1.24 2007-04-27 14:04:40 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
12 use XML::LibXML::XPathContext;
13 use ZOOM::IRSpy::Utils qw(xml_encode isodate);
17 ZOOM::IRSpy::Record - record describing a target for IRSpy
31 my($irspy, $target, $zeerex) = @_;
33 if (!defined $zeerex) {
34 $zeerex = _empty_zeerex_record($target);
37 ### Parser should be in the IRSpy object
38 my $parser = new XML::LibXML();
43 zeerex => $parser->parse_string($zeerex)->documentElement(),
48 sub _empty_zeerex_record {
51 my($protocol, $host, $port, $db) =
52 ZOOM::IRSpy::_parse_target_string($target);
54 my $xprotocol = xml_encode($protocol);
55 my $xhost = xml_encode($host);
56 my $xport = xml_encode($port);
57 my $xdb = xml_encode($db);
59 <explain xmlns="http://explain.z3950.org/dtd/2.0/">
60 <serverInfo protocol="$xprotocol">
63 <database>$xdb</database>
72 my($xpath, $frag) = @_;
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);
80 my @nodes = $xc->findnodes($xpath);
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,
88 @nodes = $xc->findnodes($xpath);
89 die("still no matches for '$xpath' after creating: can't append")
93 $this->{irspy}->log("warn",
94 scalar(@nodes), " matches for '$xpath': using first")
97 $this->_half_decent_appendWellBalancedChunk($nodes[0], $frag);
101 my ($this, $type, %info) = @_;
102 my $xml = "<irspy:$type";
104 foreach my $key (keys %info) {
105 $xml .= " $key=\"" . xml_encode($info{$key}) . "\"";
108 $xml .= ">" . isodate(time()) . "</irspy:$type>\n";
110 $this->append_entry('irspy:status', $xml);
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.
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
130 # $node->setNamespace($ZOOM::IRSpy::Utils::IRSPY_NS, "irspy", 0);
131 # $node->appendWellBalancedChunk($frag);
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
139 sub _half_decent_appendWellBalancedChunk {
141 my($node, $frag) = @_;
144 $frag =~ s,>, xmlns:irspy="$ZOOM::IRSpy::Utils::IRSPY_NS">,;
145 $node->appendWellBalancedChunk($frag);
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'"
156 my($tag, $attrs) = $open =~ /(.*?)\s(.*)/;
157 $tag = $open if !defined $tag;
158 die "mismatched XML start/end <$open>...<$close>"
160 print STDERR "tag='$tag', attrs=[$attrs], content='$content'\n";
161 die "## no code yet to make DOM node";
171 Mike Taylor, E<lt>mike@indexdata.comE<gt>
173 =head1 COPYRIGHT AND LICENSE
175 Copyright (C) 2006 by Index Data ApS.
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.