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