2 package ZOOM::IRSpy::Record;
3 ### I don't think there's any reason for this to be separate from
4 # 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(),
46 #Scalar::Util::weaken($this->{irspy});
47 #Scalar::Util::weaken($this->{parser});
53 sub _empty_zeerex_record {
56 my($protocol, $host, $port, $db) =
57 ZOOM::IRSpy::_parse_target_string($target);
59 my $xprotocol = xml_encode($protocol);
60 my $xhost = xml_encode($host);
61 my $xport = xml_encode($port);
62 my $xdb = xml_encode($db);
64 <explain xmlns="http://explain.z3950.org/dtd/2.0/">
65 <serverInfo protocol="$xprotocol">
68 <database>$xdb</database>
77 my($xpath, $frag) = @_;
79 #print STDERR "this=$this, xpath='$xpath', frag='$frag'\n";
80 my $xc = $this->xpath_context();
81 $xc->registerNs(zeerex => "http://explain.z3950.org/dtd/2.0/");
82 $xc->registerNs(irspy => $ZOOM::IRSpy::Utils::IRSPY_NS);
84 my @nodes = $xc->findnodes($xpath);
86 # Make the node that we're inserting into, if possible. A
87 # fully general version would work its way through each
88 # component of the XPath, but for now we just treat it as a
89 # single chunk to go inside the top-level node.
90 $this->_half_decent_appendWellBalancedChunk($xc->getContextNode(),
92 @nodes = $xc->findnodes($xpath);
93 die("still no matches for '$xpath' after creating: can't append")
97 $this->{irspy}->log("warn",
98 scalar(@nodes), " matches for '$xpath': using first")
101 $this->_half_decent_appendWellBalancedChunk($nodes[0], $frag);
107 return irspy_xpath_context($this->{zeerex});
111 my ($this, $type, %info) = @_;
112 my $xml = "<irspy:$type";
114 foreach my $key (keys %info) {
115 $xml .= " $key=\"" . xml_encode($info{$key}) . "\"";
118 $xml .= ">" . isodate(time()) . "</irspy:$type>\n";
120 $this->append_entry('irspy:status', $xml);
126 # _Clearly_ the right way to append a well-balanced chunk of XML to
127 # a node's children is to call appendWellBalancedChunk() from the
128 # XML::LibXML::Element class. However, this fails in the common case
129 # where the ZeeRex record we're working with doesn't declare the
130 # "irspy" namespace that the inserted fragments use.
132 # To my utter astonishment it seems that XML::LibXML (as of version
133 # 1.58, 31st March 2004) doesn't provide ANY way to register a
134 # namespace for parsing, which makes the parse_balanced_chunk()
135 # function that appendWellBalancedChunk() uses effectively useless.
136 # It _is_ possible to use setNamespace() on a node, to register a new
137 # namespace mapping for that node -- but that only affects pre-parsed
138 # trees, and is no use for parsing. Hence the following pair of lines
140 # $node->setNamespace($ZOOM::IRSpy::Utils::IRSPY_NS, "irspy", 0);
141 # $node->appendWellBalancedChunk($frag);
143 # Instead I have to go the long way round, hence this method. I have
144 # two candidate re-implementations, of which the former is marginally
145 # less loathsome, but does require that the excess namespace
146 # declarations be factored out later -- as least, if you want neat
149 sub _half_decent_appendWellBalancedChunk {
151 my($node, $frag) = @_;
154 $frag =~ s,>, xmlns:irspy="$ZOOM::IRSpy::Utils::IRSPY_NS">,;
156 $node->appendWellBalancedChunk($frag);
158 print STDERR "died while trying to appendWellBalancedChunk(), probably due to bad XML:\n$frag";
164 # Instead -- and to call this brain-damaged would be an insult
165 # to all those fine people out there with actual brain damage
166 # -- I have to "parse" the XML fragment myself and insert the
167 # resulting hand-build DOM tree. Someone shoot me now.
168 my($open, $content, $close) = $frag =~ /^<(.*?)>(.*)<\/(.*?)>$/;
169 die "can't 'parse' XML fragment '$frag'"
171 my($tag, $attrs) = $open =~ /(.*?)\s(.*)/;
172 $tag = $open if !defined $tag;
173 die "mismatched XML start/end <$open>...<$close>"
175 print STDERR "tag='$tag', attrs=[$attrs], content='$content'\n";
176 die "## no code yet to make DOM node";
186 Mike Taylor, E<lt>mike@indexdata.comE<gt>
188 =head1 COPYRIGHT AND LICENSE
190 Copyright (C) 2006 by Index Data ApS.
192 This library is free software; you can redistribute it and/or modify
193 it under the same terms as Perl itself, either Perl version 5.8.7 or,
194 at your option, any later version of Perl 5 you may have available.