1 # $Id: Record.pm,v 1.23 2007-03-05 19:42:13 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 ### Doesn't recognise SRU/SRW URLs
52 my($host, $port, $db) = ZOOM::IRSpy::_parse_target_string($target);
54 my $xhost = xml_encode($host);
55 my $xport = xml_encode($port);
56 my $xdb = xml_encode($db);
58 <explain xmlns="http://explain.z3950.org/dtd/2.0/">
59 <serverInfo protocol="Z39.50" version="1995">
62 <database>$xdb</database>
71 my($xpath, $frag) = @_;
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);
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($root,
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);
100 my ($this, $type, %info) = @_;
101 my $xml = "<irspy:$type";
103 foreach my $key (keys %info) {
104 $xml .= " $key=\"" . xml_encode($info{$key}) . "\"";
107 $xml .= ">" . isodate(time()) . "</irspy:$type>\n";
109 $this->append_entry('irspy:status', $xml);
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.
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
129 # $node->setNamespace($ZOOM::IRSpy::Utils::IRSPY_NS, "irspy", 0);
130 # $node->appendWellBalancedChunk($frag);
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
138 sub _half_decent_appendWellBalancedChunk {
140 my($node, $frag) = @_;
143 $frag =~ s,>, xmlns:irspy="$ZOOM::IRSpy::Utils::IRSPY_NS">,;
144 $node->appendWellBalancedChunk($frag);
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'"
155 my($tag, $attrs) = $open =~ /(.*?)\s(.*)/;
156 $tag = $open if !defined $tag;
157 die "mismatched XML start/end <$open>...<$close>"
159 print STDERR "tag='$tag', attrs=[$attrs], content='$content'\n";
160 die "## no code yet to make DOM node";
170 Mike Taylor, E<lt>mike@indexdata.comE<gt>
172 =head1 COPYRIGHT AND LICENSE
174 Copyright (C) 2006 by Index Data ApS.
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.