1 # $Id: Record.pm,v 1.26 2007-08-02 11:27:32 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 irspy_xpath_context);
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 $xc = $this->xpath_context();
76 $xc->registerNs(zeerex => "http://explain.z3950.org/dtd/2.0/");
77 $xc->registerNs(irspy => $ZOOM::IRSpy::Utils::IRSPY_NS);
79 my @nodes = $xc->findnodes($xpath);
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->getContextNode(),
87 @nodes = $xc->findnodes($xpath);
88 die("still no matches for '$xpath' after creating: can't append")
92 $this->{irspy}->log("warn",
93 scalar(@nodes), " matches for '$xpath': using first")
96 $this->_half_decent_appendWellBalancedChunk($nodes[0], $frag);
102 return irspy_xpath_context($this->{zeerex});
106 my ($this, $type, %info) = @_;
107 my $xml = "<irspy:$type";
109 foreach my $key (keys %info) {
110 $xml .= " $key=\"" . xml_encode($info{$key}) . "\"";
113 $xml .= ">" . isodate(time()) . "</irspy:$type>\n";
115 $this->append_entry('irspy:status', $xml);
121 # _Clearly_ the right way to append a well-balanced chunk of XML to
122 # a node's children is to call appendWellBalancedChunk() from the
123 # XML::LibXML::Element class. However, this fails in the common case
124 # where the ZeeRex record we're working with doesn't declare the
125 # "irspy" namespace that the inserted fragments use.
127 # To my utter astonishment it seems that XML::LibXML (as of version
128 # 1.58, 31st March 2004) doesn't provide ANY way to register a
129 # namespace for parsing, which makes the parse_balanced_chunk()
130 # function that appendWellBalancedChunk() uses effectively useless.
131 # It _is_ possible to use setNamespace() on a node, to register a new
132 # namespace mapping for that node -- but that only affects pre-parsed
133 # trees, and is no use for parsing. Hence the following pair of lines
135 # $node->setNamespace($ZOOM::IRSpy::Utils::IRSPY_NS, "irspy", 0);
136 # $node->appendWellBalancedChunk($frag);
138 # Instead I have to go the long way round, hence this method. I have
139 # two candidate re-implementations, of which the former is marginally
140 # less loathsome, but does require that the excess namespace
141 # declarations be factored out later -- as least, if you want neat
144 sub _half_decent_appendWellBalancedChunk {
146 my($node, $frag) = @_;
149 $frag =~ s,>, xmlns:irspy="$ZOOM::IRSpy::Utils::IRSPY_NS">,;
150 warn "calling appendWellBalancedChunk($node)";
151 $node->appendWellBalancedChunk($frag);
155 # Instead -- and to call this brain-damaged would be an insult
156 # to all those fine people out there with actual brain damage
157 # -- I have to "parse" the XML fragment myself and insert the
158 # resulting hand-build DOM tree. Someone shoot me now.
159 my($open, $content, $close) = $frag =~ /^<(.*?)>(.*)<\/(.*?)>$/;
160 die "can't 'parse' XML fragment '$frag'"
162 my($tag, $attrs) = $open =~ /(.*?)\s(.*)/;
163 $tag = $open if !defined $tag;
164 die "mismatched XML start/end <$open>...<$close>"
166 print STDERR "tag='$tag', attrs=[$attrs], content='$content'\n";
167 die "## no code yet to make DOM node";
177 Mike Taylor, E<lt>mike@indexdata.comE<gt>
179 =head1 COPYRIGHT AND LICENSE
181 Copyright (C) 2006 by Index Data ApS.
183 This library is free software; you can redistribute it and/or modify
184 it under the same terms as Perl itself, either Perl version 5.8.7 or,
185 at your option, any later version of Perl 5 you may have available.