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