1 # $Id: Record.pm,v 1.13 2006-09-26 09:08:09 mike Exp $
3 package ZOOM::IRSpy::Record;
10 our @EXPORT_OK = qw(xml_encode);
13 use XML::LibXML::XPathContext;
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 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>
69 # I can't -- just can't, can't, can't -- believe that this function
70 # isn't provided by one of the core XML modules. But the evidence all
71 # says that it's not: among other things, XML::Generator and
72 # Template::Plugin both roll their own. So I will do likewise. D'oh!
79 $text =~ s/['']/'/g;
80 $text =~ s/[""]/"/g;
87 my($xpath, $frag) = @_;
89 #print STDERR "this=$this, xpath='$xpath', frag='$frag'\n";
90 my $root = $this->{zeerex}; # XML::LibXML::Element ISA XML::LibXML::Node
91 my $xc = XML::LibXML::XPathContext->new($root);
92 $xc->registerNs(zeerex => "http://explain.z3950.org/dtd/2.0/");
93 $xc->registerNs(irspy => "http://indexdata.com/irspy/1.0");
95 my @nodes = $xc->findnodes($xpath);
97 # Make the node that we're inserting into, if possible. A
98 # fully general version would work its way through each
99 # component of the XPath, but for now we just treat it as a
100 # single chunk to go inside the top-level node.
101 $this->_half_decent_appendWellBalancedChunk($root,
102 "<$xpath></$xpath>");
103 @nodes = $xc->findnodes($xpath);
104 die("still no matches for '$xpath' after creating: can't append")
108 $this->{irspy}->log("irspy",
109 scalar(@nodes), " matches for '$xpath': using first")
112 $this->_half_decent_appendWellBalancedChunk($nodes[0], $frag);
118 # _Clearly_ the right way to append a well-balanced chunk of XML to
119 # a node's children is to call appendWellBalancedChunk() from the
120 # XML::LibXML::Element class. However, this fails in the common case
121 # where the ZeeRex record we're working with doesn't declare the
122 # "irspy" namespace that the inserted fragments use.
124 # To my utter astonishment it seems that XML::LibXML (as of version
125 # 1.58, 31st March 2004) doesn't provide ANY way to register a
126 # namespace for parsing, which makes the parse_balanced_chunk()
127 # function that appendWellBalancedChunk() uses effectively useless.
128 # It _is_ possible to use setNamespace() on a node, to register a new
129 # namespace mapping for that node -- but that only affects pre-parsed
130 # trees, and is no use for parsing. Hence the following pair of lines
132 # $node->setNamespace("http://indexdata.com/irspy/1.0", "irspy", 0);
133 # $node->appendWellBalancedChunk($frag);
135 # Instead I have to go the long way round, hence this method. I have
136 # two candidate re-implementations, of which the former is marginally
137 # less loathsome, but does require that the excess namespace
138 # declarations be factored out later -- as least, if you want neat
141 sub _half_decent_appendWellBalancedChunk {
143 my($node, $frag) = @_;
146 $frag =~ s,>, xmlns:irspy="http://indexdata.com/irspy/1.0">,;
147 $node->appendWellBalancedChunk($frag);
151 # Instead -- and to call this brain-damaged would be an insult
152 # to all those fine people out there with actual brain damage
153 # -- I have to "parse" the XML fragment myself and insert the
154 # resulting hand-build DOM tree. Someone shoot me now.
155 my($open, $content, $close) = $frag =~ /^<(.*?)>(.*)<\/(.*?)>$/;
156 die "can't 'parse' XML fragment '$frag'"
158 my($tag, $attrs) = $open =~ /(.*?)\s(.*)/;
159 $tag = $open if !defined $tag;
160 die "mismatched XML start/end <$open>...<$close>"
162 print STDERR "tag='$tag', attrs=[$attrs], content='$content'\n";
163 die "## no code yet to make DOM node";
173 Mike Taylor, E<lt>mike@indexdata.comE<gt>
175 =head1 COPYRIGHT AND LICENSE
177 Copyright (C) 2006 by Index Data ApS.
179 This library is free software; you can redistribute it and/or modify
180 it under the same terms as Perl itself, either Perl version 5.8.7 or,
181 at your option, any later version of Perl 5 you may have available.