Record keeps track of its parent IRSpy object, and uses $irspy->log()
[irspy-moved-to-github.git] / lib / ZOOM / IRSpy / Record.pm
1 # $Id: Record.pm,v 1.13 2006-09-26 09:08:09 mike Exp $
2
3 package ZOOM::IRSpy::Record;
4
5 use 5.008;
6 use strict;
7 use warnings;
8
9 use Exporter 'import';
10 our @EXPORT_OK = qw(xml_encode);
11
12 use XML::LibXML;
13 use XML::LibXML::XPathContext;
14
15
16 =head1 NAME
17
18 ZOOM::IRSpy::Record - record describing a target for IRSpy
19
20 =head1 SYNOPSIS
21
22  ## To follow
23
24 =head1 DESCRIPTION
25
26 I<## To follow>
27
28 =cut
29
30 sub new {
31     my $class = shift();
32     my($irspy, $target, $zeerex) = @_;
33
34     if (!defined $zeerex) {
35         $zeerex = _empty_zeerex_record($target);
36     }
37
38     my $parser = new XML::LibXML();
39     return bless {
40         irspy => $irspy,
41         target => $target,
42         parser => $parser,
43         zeerex => $parser->parse_string($zeerex)->documentElement(),
44     }, $class;
45 }
46
47
48 sub _empty_zeerex_record {
49     my($target) = @_;
50
51     ### Doesn't recognise SRU/SRW URLs
52     my($host, $port, $db) = ZOOM::IRSpy::_parse_target_string($target);
53
54     my $xhost = xml_encode($host);
55     my $xport = xml_encode($port);
56     my $xdb = xml_encode($db);
57     return <<__EOT__;
58 <explain xmlns="http://explain.z3950.org/dtd/2.0/">
59  <serverInfo protocol="Z39.50" version="1995">
60   <host>$xhost</host>
61   <port>$xport</port>
62   <database>$xdb</database>
63  </serverInfo>
64 </explain>
65 __EOT__
66 }
67
68
69 # I can't -- just can't, can't, can't -- believe that this function
70 # isn't provided by one of the core XML modules.  But the evidence all
71 # says that it's not: among other things, XML::Generator and
72 # Template::Plugin both roll their own.  So I will do likewise.  D'oh!
73 #
74 sub xml_encode {
75     my ($text) = @_;
76     $text =~ s/&/&amp;/g;
77     $text =~ s/</&lt;/g;
78     $text =~ s/>/&gt;/g;
79     $text =~ s/['']/&apos;/g;
80     $text =~ s/[""]/&quot;/g;
81     return $text;
82 }
83
84
85 sub append_entry {
86     my $this = shift();
87     my($xpath, $frag) = @_;
88
89     #print STDERR "this=$this, xpath='$xpath', frag='$frag'\n";
90     my $root = $this->{zeerex}; # XML::LibXML::Element ISA XML::LibXML::Node
91     my $xc = XML::LibXML::XPathContext->new($root);
92     $xc->registerNs(zeerex => "http://explain.z3950.org/dtd/2.0/");
93     $xc->registerNs(irspy => "http://indexdata.com/irspy/1.0");
94
95     my @nodes = $xc->findnodes($xpath);
96     if (@nodes == 0) {
97         # Make the node that we're inserting into, if possible.  A
98         # fully general version would work its way through each
99         # component of the XPath, but for now we just treat it as a
100         # single chunk to go inside the top-level node.
101         $this->_half_decent_appendWellBalancedChunk($root,
102                                                     "<$xpath></$xpath>");
103         @nodes = $xc->findnodes($xpath);
104         die("still no matches for '$xpath' after creating: can't append")
105             if @nodes == 0;
106     }
107
108     $this->{irspy}->log("irspy",
109                         scalar(@nodes), " matches for '$xpath': using first")
110         if @nodes > 1;
111
112     $this->_half_decent_appendWellBalancedChunk($nodes[0], $frag);
113 }
114
115
116 # *sigh*
117 #
118 # _Clearly_ the right way to append a well-balanced chunk of XML to
119 # a node's children is to call appendWellBalancedChunk() from the
120 # XML::LibXML::Element class.  However, this fails in the common case
121 # where the ZeeRex record we're working with doesn't declare the
122 # "irspy" namespace that the inserted fragments use.
123 #
124 # To my utter astonishment it seems that XML::LibXML (as of version
125 # 1.58, 31st March 2004) doesn't provide ANY way to register a
126 # namespace for parsing, which makes the parse_balanced_chunk()
127 # function that appendWellBalancedChunk() uses effectively useless.
128 # It _is_ possible to use setNamespace() on a node, to register a new
129 # namespace mapping for that node -- but that only affects pre-parsed
130 # trees, and is no use for parsing.  Hence the following pair of lines
131 # DOES NOT WORK:
132 #       $node->setNamespace("http://indexdata.com/irspy/1.0", "irspy", 0);
133 #       $node->appendWellBalancedChunk($frag);
134 #
135 # Instead I have to go the long way round, hence this method.  I have
136 # two candidate re-implementations, of which the former is marginally
137 # less loathsome, but does require that the excess namespace
138 # declarations be factored out later -- as least, if you want neat
139 # output.
140 #
141 sub _half_decent_appendWellBalancedChunk {
142     my $this = shift();
143     my($node, $frag) = @_;
144
145     if (1) {
146         $frag =~ s,>, xmlns:irspy="http://indexdata.com/irspy/1.0">,;
147         $node->appendWellBalancedChunk($frag);
148         return;
149     }
150
151     # Instead -- and to call this brain-damaged would be an insult
152     # to all those fine people out there with actual brain damage
153     # -- I have to "parse" the XML fragment myself and insert the
154     # resulting hand-build DOM tree.  Someone shoot me now.
155     my($open, $content, $close) = $frag =~ /^<(.*?)>(.*)<\/(.*?)>$/;
156     die "can't 'parse' XML fragment '$frag'"
157         if !defined $open;
158     my($tag, $attrs) = $open =~ /(.*?)\s(.*)/;
159     $tag = $open if !defined $tag;
160     die "mismatched XML start/end <$open>...<$close>"
161         if $close ne $tag;
162     print STDERR "tag='$tag', attrs=[$attrs], content='$content'\n";
163     die "## no code yet to make DOM node";
164 }
165
166
167 =head1 SEE ALSO
168
169 ZOOM::IRSpy
170
171 =head1 AUTHOR
172
173 Mike Taylor, E<lt>mike@indexdata.comE<gt>
174
175 =head1 COPYRIGHT AND LICENSE
176
177 Copyright (C) 2006 by Index Data ApS.
178
179 This library is free software; you can redistribute it and/or modify
180 it under the same terms as Perl itself, either Perl version 5.8.7 or,
181 at your option, any later version of Perl 5 you may have available.
182
183 =cut
184
185 1;