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