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