85d465327d104a251ca2a5e29d52a36dda7d2401
[irspy-moved-to-github.git] / lib / ZOOM / IRSpy / Record.pm
1 # $Id: Record.pm,v 1.9 2006-07-24 17:01:46 mike Exp $
2
3 package ZOOM::IRSpy::Record;
4
5 use 5.008;
6 use strict;
7 use warnings;
8
9 use XML::LibXML;
10 use XML::LibXML::XPathContext;
11
12
13 =head1 NAME
14
15 ZOOM::IRSpy::Record - record describing a target for IRSpy
16
17 =head1 SYNOPSIS
18
19  ## To follow
20
21 =head1 DESCRIPTION
22
23 I<## To follow>
24
25 =cut
26
27 sub new {
28     my $class = shift();
29     my($target, $zeerex) = @_;
30
31     if (!defined $zeerex) {
32         $zeerex = _empty_zeerex_record($target);
33     }
34
35     my $parser = new XML::LibXML();
36     return bless {
37         target => $target,
38         parser => $parser,
39         zeerex => $parser->parse_string($zeerex)->documentElement(),
40     }, $class;
41 }
42
43
44 sub _empty_zeerex_record {
45     my($target) = @_;
46
47     ### Doesn't recognise SRU/SRW URLs
48     my($host, $port, $db) = ZOOM::IRSpy::_parse_target_string($target);
49
50     return <<__EOT__;
51 <explain xmlns="http://explain.z3950.org/dtd/2.0/">
52  <serverInfo protocol="Z39.50" version="1995">
53   <host>$host</host>
54   <port>$port</port>
55   <database>$db</database>
56  </serverInfo>
57 </explain>
58 __EOT__
59 }
60
61
62 sub append_entry {
63     my $this = shift();
64     my($xpath, $frag) = @_;
65
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");
71
72     my @nodes = $xc->findnodes($xpath);
73     if (@nodes == 0) {
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,
79                                                     "<$xpath></$xpath>");
80         @nodes = $xc->findnodes($xpath);
81         die("still no matches for '$xpath' after creating: can't append")
82             if @nodes == 0;
83     }
84
85     ZOOM::Log::log("irspy",
86                    scalar(@nodes), " matches for '$xpath': using first")
87         if @nodes > 1;
88
89     $this->_half_decent_appendWellBalancedChunk($nodes[0], $frag);
90 }
91
92
93 # *sigh*
94 #
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.
100 #
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
108 # DOES NOT WORK:
109 #       $node->setNamespace("http://indexdata.com/irspy/1.0", "irspy", 0);
110 #       $node->appendWellBalancedChunk($frag);
111 #
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
116 # output.
117 #
118 sub _half_decent_appendWellBalancedChunk {
119     my $this = shift();
120     my($node, $frag) = @_;
121
122     if (1) {
123         $frag =~ s,>, xmlns:irspy="http://indexdata.com/irspy/1.0">,;
124         $node->appendWellBalancedChunk($frag);
125         return;
126     }
127
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'"
134         if !defined $open;
135     my($tag, $attrs) = $open =~ /(.*?)\s(.*)/;
136     $tag = $open if !defined $tag;
137     die "mismatched XML start/end <$open>...<$close>"
138         if $close ne $tag;
139     print STDERR "tag='$tag', attrs=[$attrs], content='$content'\n";
140     die "## no code yet to make DOM node";
141 }
142
143
144 =head1 SEE ALSO
145
146 ZOOM::IRSpy
147
148 =head1 AUTHOR
149
150 Mike Taylor, E<lt>mike@indexdata.comE<gt>
151
152 =head1 COPYRIGHT AND LICENSE
153
154 Copyright (C) 2006 by Index Data ApS.
155
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.
159
160 =cut
161
162 1;