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