1 # $Id: Record.pm,v 1.7 2006-07-24 15:25:51 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);
51 <explain xmlns="http://explain.z3950.org/dtd/2.0/">
52 <serverInfo protocol="Z39.50" version="1995">
55 <database>$db</database>
64 my($xpath, $frag) = @_;
66 print STDERR "this=$this, xpath='$xpath', frag='$frag'\n";
67 my $root = $this->{zeerex}; # XML::LibXML::Element ISA XML::LibXML::Node
68 my $xc = XML::LibXML::XPathContext->new($root);
69 $xc->registerNs(zeerex => "http://explain.z3950.org/dtd/2.0/");
70 $xc->registerNs(irspy => "http://indexdata.com/irspy/1.0");
72 my @nodes = $xc->findnodes($xpath);
74 ZOOM::Log::log("irspy", "no matches for '$xpath': can't append");
76 } elsif (@nodes > 1) {
77 ZOOM::Log::log("irspy", scalar(@nodes),
78 " matches for '$xpath': using first");
82 # $node ISA XML::LibXML::ElementXML::LibXML::Element
83 $this->_half_decent_appendWellBalancedChunk($node, $frag);
84 #print STDERR "POST: zeerex='$root' = \n", $root->toString(), "\n";
90 # _Clearly_ the right way to append a well-balanced chunk of XML to
91 # a node's children is to call appendWellBalancedChunk() from the
92 # XML::LibXML::Element class. However, this fails in the common case
93 # where the ZeeRex record we're working with doesn't declare the
94 # "irspy" namespace that the inserted fragments use.
96 # To my utter astonishment it seems that XML::LibXML (as of version
97 # 1.58, 31st March 2004) doesn't provide ANY way to register a
98 # namespace for parsing, which makes the parse_balanced_chunk()
99 # function that appendWellBalancedChunk() uses effectively useless.
100 # It _is_ possible to use setNamespace() on a node, to register a new
101 # namespace mapping for that node -- but that only affects pre-parsed
102 # trees, and is no use for parsing. Hence the following pair of lines
104 # $node->setNamespace("http://indexdata.com/irspy/1.0", "irspy", 0);
105 # $node->appendWellBalancedChunk($frag);
107 # Instead I have to go the long way round, hence this method. I have
108 # two candidate re-implementations, of which the former is marginally
109 # less loathsome, but does require that the excess namespace
110 # declarations be factored out later -- as least, if you want neat
113 sub _half_decent_appendWellBalancedChunk {
115 my($node, $frag) = @_;
118 $frag =~ s,>, xmlns:irspy="http://indexdata.com/irspy/1.0">,;
119 $node->appendWellBalancedChunk($frag);
123 # Instead -- and to call this brain-damaged would be an insult
124 # to all those fine people out there with actual brain damage
125 # -- I have to "parse" the XML fragment myself and insert the
126 # resulting hand-build DOM tree. Someone shoot me now.
127 my($open, $content, $close) = $frag =~ /^<(.*?)>(.*)<\/(.*?)>$/;
128 die "can't 'parse' XML fragment '$frag'"
130 my($tag, $attrs) = $open =~ /(.*?)\s(.*)/;
131 $tag = $open if !defined $tag;
132 die "mismatched XML start/end <$open>...<$close>"
134 print STDERR "tag='$tag', attrs=[$attrs], content='$content'\n";
135 die "### no code yet to make DOM node";
145 Mike Taylor, E<lt>mike@indexdata.comE<gt>
147 =head1 COPYRIGHT AND LICENSE
149 Copyright (C) 2006 by Index Data ApS.
151 This library is free software; you can redistribute it and/or modify
152 it under the same terms as Perl itself, either Perl version 5.8.7 or,
153 at your option, any later version of Perl 5 you may have available.