1 # $Id: Record.pm,v 1.11 2006-09-25 12:39:34 mike Exp $
3 package ZOOM::IRSpy::Record;
10 use XML::LibXML::XPathContext;
15 ZOOM::IRSpy::Record - record describing a target for IRSpy
29 my($target, $zeerex) = @_;
31 if (!defined $zeerex) {
32 $zeerex = _empty_zeerex_record($target);
35 my $parser = new XML::LibXML();
39 zeerex => $parser->parse_string($zeerex)->documentElement(),
44 sub _empty_zeerex_record {
47 ### Doesn't recognise SRU/SRW URLs
48 my($host, $port, $db) = ZOOM::IRSpy::_parse_target_string($target);
50 my $xhost = xml_encode($host);
51 my $xport = xml_encode($port);
52 my $xdb = xml_encode($db);
54 <explain xmlns="http://explain.z3950.org/dtd/2.0/">
55 <serverInfo protocol="Z39.50" version="1995">
58 <database>$xdb</database>
65 # I can't -- just can't, can't, can't -- believe that this function
66 # isn't provided by one of the core XML modules. But the evidence all
67 # says that it's not: among other things, XML::Generator and
68 # Template::Plugin both roll their own. So I will do likewise. D'oh!
75 $text =~ s/['']/'/g;
76 $text =~ s/[""]/"/g;
83 my($xpath, $frag) = @_;
85 #print STDERR "this=$this, xpath='$xpath', frag='$frag'\n";
86 my $root = $this->{zeerex}; # XML::LibXML::Element ISA XML::LibXML::Node
87 my $xc = XML::LibXML::XPathContext->new($root);
88 $xc->registerNs(zeerex => "http://explain.z3950.org/dtd/2.0/");
89 $xc->registerNs(irspy => "http://indexdata.com/irspy/1.0");
91 my @nodes = $xc->findnodes($xpath);
93 # Make the node that we're inserting into, if possible. A
94 # fully general version would work its way through each
95 # component of the XPath, but for now we just treat it as a
96 # single chunk to go inside the top-level node.
97 $this->_half_decent_appendWellBalancedChunk($root,
99 @nodes = $xc->findnodes($xpath);
100 die("still no matches for '$xpath' after creating: can't append")
104 ZOOM::Log::log("irspy",
105 scalar(@nodes), " matches for '$xpath': using first")
108 $this->_half_decent_appendWellBalancedChunk($nodes[0], $frag);
114 # _Clearly_ the right way to append a well-balanced chunk of XML to
115 # a node's children is to call appendWellBalancedChunk() from the
116 # XML::LibXML::Element class. However, this fails in the common case
117 # where the ZeeRex record we're working with doesn't declare the
118 # "irspy" namespace that the inserted fragments use.
120 # To my utter astonishment it seems that XML::LibXML (as of version
121 # 1.58, 31st March 2004) doesn't provide ANY way to register a
122 # namespace for parsing, which makes the parse_balanced_chunk()
123 # function that appendWellBalancedChunk() uses effectively useless.
124 # It _is_ possible to use setNamespace() on a node, to register a new
125 # namespace mapping for that node -- but that only affects pre-parsed
126 # trees, and is no use for parsing. Hence the following pair of lines
128 # $node->setNamespace("http://indexdata.com/irspy/1.0", "irspy", 0);
129 # $node->appendWellBalancedChunk($frag);
131 # Instead I have to go the long way round, hence this method. I have
132 # two candidate re-implementations, of which the former is marginally
133 # less loathsome, but does require that the excess namespace
134 # declarations be factored out later -- as least, if you want neat
137 sub _half_decent_appendWellBalancedChunk {
139 my($node, $frag) = @_;
142 $frag =~ s,>, xmlns:irspy="http://indexdata.com/irspy/1.0">,;
143 $node->appendWellBalancedChunk($frag);
147 # Instead -- and to call this brain-damaged would be an insult
148 # to all those fine people out there with actual brain damage
149 # -- I have to "parse" the XML fragment myself and insert the
150 # resulting hand-build DOM tree. Someone shoot me now.
151 my($open, $content, $close) = $frag =~ /^<(.*?)>(.*)<\/(.*?)>$/;
152 die "can't 'parse' XML fragment '$frag'"
154 my($tag, $attrs) = $open =~ /(.*?)\s(.*)/;
155 $tag = $open if !defined $tag;
156 die "mismatched XML start/end <$open>...<$close>"
158 print STDERR "tag='$tag', attrs=[$attrs], content='$content'\n";
159 die "## no code yet to make DOM node";
169 Mike Taylor, E<lt>mike@indexdata.comE<gt>
171 =head1 COPYRIGHT AND LICENSE
173 Copyright (C) 2006 by Index Data ApS.
175 This library is free software; you can redistribute it and/or modify
176 it under the same terms as Perl itself, either Perl version 5.8.7 or,
177 at your option, any later version of Perl 5 you may have available.