8a09c541cdad02919a526bf5035ab30acd19f3ca
[irspy-moved-to-github.git] / lib / ZOOM / IRSpy / Record.pm
1 # $Id: Record.pm,v 1.6 2006-07-21 16:50:20 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         zeerex => $parser->parse_string($zeerex)->documentElement(),
39     }, $class;
40 }
41
42
43 sub _empty_zeerex_record {
44     my($target) = @_;
45
46     ### Doesn't recognise SRU/SRW URLs
47     my($host, $port, $db) = ZOOM::IRSpy::_parse_target_string($target);
48
49     return <<__EOT__;
50 <explain xmlns="http://explain.z3950.org/dtd/2.0/">
51  <serverInfo protocol="Z39.50" version="1995">
52   <host>$host</host>
53   <port>$port</port>
54   <database>$db</database>
55  </serverInfo>
56 </explain>
57 __EOT__
58 }
59
60
61 sub append_entry {
62     my $this = shift();
63     my($xpath, $frag) = @_;
64
65     print STDERR "this=$this, xpath='$xpath', frag='$frag'\n";
66     my $root = $this->{zeerex}; # XML::LibXML::Element ISA XML::LibXML::Node
67     print "Record='", $root->toString(), "'\n";
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     print STDERR "zeerex='$root'\n";
82 }
83
84
85 =head1 SEE ALSO
86
87 ZOOM::IRSpy
88
89 =head1 AUTHOR
90
91 Mike Taylor, E<lt>mike@indexdata.comE<gt>
92
93 =head1 COPYRIGHT AND LICENSE
94
95 Copyright (C) 2006 by Index Data ApS.
96
97 This library is free software; you can redistribute it and/or modify
98 it under the same terms as Perl itself, either Perl version 5.8.7 or,
99 at your option, any later version of Perl 5 you may have available.
100
101 =cut
102
103 1;