1 # $Id: Record.pm,v 1.10 2006-07-25 16:53:28 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 # Make the node that we're inserting into, if possible. A
75 # fully general version would work its way through each
76 # component of the XPath, but for now we just treat it as a
77 # single chunk to go inside the top-level node.
78 $this->_half_decent_appendWellBalancedChunk($root,
80 @nodes = $xc->findnodes($xpath);
81 die("still no matches for '$xpath' after creating: can't append")
85 ZOOM::Log::log("irspy",
86 scalar(@nodes), " matches for '$xpath': using first")
89 $this->_half_decent_appendWellBalancedChunk($nodes[0], $frag);
95 # _Clearly_ the right way to append a well-balanced chunk of XML to
96 # a node's children is to call appendWellBalancedChunk() from the
97 # XML::LibXML::Element class. However, this fails in the common case
98 # where the ZeeRex record we're working with doesn't declare the
99 # "irspy" namespace that the inserted fragments use.
101 # To my utter astonishment it seems that XML::LibXML (as of version
102 # 1.58, 31st March 2004) doesn't provide ANY way to register a
103 # namespace for parsing, which makes the parse_balanced_chunk()
104 # function that appendWellBalancedChunk() uses effectively useless.
105 # It _is_ possible to use setNamespace() on a node, to register a new
106 # namespace mapping for that node -- but that only affects pre-parsed
107 # trees, and is no use for parsing. Hence the following pair of lines
109 # $node->setNamespace("http://indexdata.com/irspy/1.0", "irspy", 0);
110 # $node->appendWellBalancedChunk($frag);
112 # Instead I have to go the long way round, hence this method. I have
113 # two candidate re-implementations, of which the former is marginally
114 # less loathsome, but does require that the excess namespace
115 # declarations be factored out later -- as least, if you want neat
118 sub _half_decent_appendWellBalancedChunk {
120 my($node, $frag) = @_;
123 $frag =~ s,>, xmlns:irspy="http://indexdata.com/irspy/1.0">,;
124 $node->appendWellBalancedChunk($frag);
128 # Instead -- and to call this brain-damaged would be an insult
129 # to all those fine people out there with actual brain damage
130 # -- I have to "parse" the XML fragment myself and insert the
131 # resulting hand-build DOM tree. Someone shoot me now.
132 my($open, $content, $close) = $frag =~ /^<(.*?)>(.*)<\/(.*?)>$/;
133 die "can't 'parse' XML fragment '$frag'"
135 my($tag, $attrs) = $open =~ /(.*?)\s(.*)/;
136 $tag = $open if !defined $tag;
137 die "mismatched XML start/end <$open>...<$close>"
139 print STDERR "tag='$tag', attrs=[$attrs], content='$content'\n";
140 die "## no code yet to make DOM node";
150 Mike Taylor, E<lt>mike@indexdata.comE<gt>
152 =head1 COPYRIGHT AND LICENSE
154 Copyright (C) 2006 by Index Data ApS.
156 This library is free software; you can redistribute it and/or modify
157 it under the same terms as Perl itself, either Perl version 5.8.7 or,
158 at your option, any later version of Perl 5 you may have available.