532603a218d6008594bb5592149b5100662d7b82
[irspy-moved-to-github.git] / lib / ZOOM / IRSpy / Record.pm
1 # $Id: Record.pm,v 1.16 2006-10-20 14:51:01 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($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 = ZOOM::IRSpy::xml_encode($host);
52     my $xport = ZOOM::IRSpy::xml_encode($port);
53     my $xdb = ZOOM::IRSpy::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::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
97 # *sigh*
98 #
99 # _Clearly_ the right way to append a well-balanced chunk of XML to
100 # a node's children is to call appendWellBalancedChunk() from the
101 # XML::LibXML::Element class.  However, this fails in the common case
102 # where the ZeeRex record we're working with doesn't declare the
103 # "irspy" namespace that the inserted fragments use.
104 #
105 # To my utter astonishment it seems that XML::LibXML (as of version
106 # 1.58, 31st March 2004) doesn't provide ANY way to register a
107 # namespace for parsing, which makes the parse_balanced_chunk()
108 # function that appendWellBalancedChunk() uses effectively useless.
109 # It _is_ possible to use setNamespace() on a node, to register a new
110 # namespace mapping for that node -- but that only affects pre-parsed
111 # trees, and is no use for parsing.  Hence the following pair of lines
112 # DOES NOT WORK:
113 #       $node->setNamespace($ZOOM::IRSpy::irspy_ns, "irspy", 0);
114 #       $node->appendWellBalancedChunk($frag);
115 #
116 # Instead I have to go the long way round, hence this method.  I have
117 # two candidate re-implementations, of which the former is marginally
118 # less loathsome, but does require that the excess namespace
119 # declarations be factored out later -- as least, if you want neat
120 # output.
121 #
122 sub _half_decent_appendWellBalancedChunk {
123     my $this = shift();
124     my($node, $frag) = @_;
125
126     if (1) {
127         $frag =~ s,>, xmlns:irspy="$ZOOM::IRSpy::irspy_ns">,;
128         $node->appendWellBalancedChunk($frag);
129         return;
130     }
131
132     # Instead -- and to call this brain-damaged would be an insult
133     # to all those fine people out there with actual brain damage
134     # -- I have to "parse" the XML fragment myself and insert the
135     # resulting hand-build DOM tree.  Someone shoot me now.
136     my($open, $content, $close) = $frag =~ /^<(.*?)>(.*)<\/(.*?)>$/;
137     die "can't 'parse' XML fragment '$frag'"
138         if !defined $open;
139     my($tag, $attrs) = $open =~ /(.*?)\s(.*)/;
140     $tag = $open if !defined $tag;
141     die "mismatched XML start/end <$open>...<$close>"
142         if $close ne $tag;
143     print STDERR "tag='$tag', attrs=[$attrs], content='$content'\n";
144     die "## no code yet to make DOM node";
145 }
146
147
148 =head1 SEE ALSO
149
150 ZOOM::IRSpy
151
152 =head1 AUTHOR
153
154 Mike Taylor, E<lt>mike@indexdata.comE<gt>
155
156 =head1 COPYRIGHT AND LICENSE
157
158 Copyright (C) 2006 by Index Data ApS.
159
160 This library is free software; you can redistribute it and/or modify
161 it under the same terms as Perl itself, either Perl version 5.8.7 or,
162 at your option, any later version of Perl 5 you may have available.
163
164 =cut
165
166 1;