UDDI/HalfDecent/ResultSet.pm untested

filter: Statementstmt 0.0% 0 / 32 Branchbran 0.0% 0 / 8 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 0.0% 0 / 6 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 54 CC 5 SCAR 23.0
CC
5
Cov
0%
record
20.0
new
6.0
count
2.0
CRAP 10.0
1 package UDDI::HalfDecent::ResultSet;
2 use strict;
3 use warnings;
4  
5 use UDDI::HalfDecent::Record;
6  
7 ### At present, this class only represents sets of businesses. When
8 # we implement more methods, it will need generalising to represent
9 # sets of other kinds of record.
10  
11 =head1 NAME
12  
13 UDDI::HalfDecent::ResultSet - a set of results from a UDDI query
14  
15 =head1 SYNOPSIS
16  
17 $rs = $uddi->find_business(name => 'frog');
18 $n = $rs->count();
19 foreach $i (0 .. $n-1) {
20 $bi = $rs->record($i);
21 }
22  
23 =head1 DESCRIPTION
24  
25 This is a container class which allows access to the multiple records
26 found by various interrogation methods on a UDDI object. Depending on
27 what method gave rise to the result-set it will contain different
28 kinds of objects, e.g. C<find_business()> will yield a result-set of
29 Business objects. In a parallel universe, there are ResultSet
30 subclasses corresponding to each of the possible contained object
31 types, but that's not how we do things here.
32  
33 =head1 METHODS
34  
35 =head2 new()
36  
37 $uddi = new UDDI::HalfDecent::ResultSet($uddi, $xpc, $node, $class);
38  
39 Creates and returns a new UDDI result-set object representing the
40 result of a search. Note that B<client code need never call this>: it
41 is invoked by the UDDI library itself, in methods such as
42 C<find_business()>.
43  
44 This constructor takes four arguments: C<$uddi> is the UDDI object
45 for which it is created, and which will be used for logging, etc;
46 C<$xpc> is an XPath content with all relevant namespaces registered;
47 and C<$node> is the context node of the list-containing element, which
48 in general will be different from the context-node registered as part
49 of the XPath context. (If you didn't understand that last bit, don't
50 worry about it -- you won't be calling this constructor yourself,
51 remember?) C<$class> is the class to which the individual result
52 records must belong.
53  
54 =cut
55  
56 # Actually, I don't think we need this.
57 #
58 #our %_class2xpath = (
59 # business => "uddi:businessInfos/uddi:businessInfo"
60 # service => "uddi:serviceInfos/uddi:serviceInfo"
61 # binding => "uddi:bindingTemplate",
62 # tmodel => "uddi:tModelInfos/uddi:tModelInfo"
63 #);
64  
65 sub new {
66 my $class = shift();
67 my($uddi, $xpc, $node, $recclass) = @_;
68  
69 my $this = bless {
70 uddi => $uddi,
71 xpc => $xpc,
72 node => $node,
73 recclass => $recclass,
74 count => undef,
75 dom => [], # The raw DOM nodes
76 records => [], # Corresponding wrapped objects
77 }, $class;
78  
79 my $xpath;
80 if ($recclass eq "binding") {
81 $xpath = "uddi:bindingTemplate";
82 } else {
83 $xpath = "uddi:${recclass}Infos/uddi:${recclass}Info";
84 }
85 $this->{dom} = $xpc->findnodes($xpath, $node);
86 $this->{count} = @{ $this->{dom} };
87  
88 return $this;
89 }
90  
91  
92 =head2 count()
93  
94 $n = $rs->count();
95  
96 Returns the number of records in the result-set, e.g. the number of
97 businesses found by a C<find_business()> call.
98  
99 =cut
100  
101 sub count {
102 my $this = shift();
103  
104 return $this->{count};
105 }
106  
107  
108 =head2 record()
109  
110 foreach $i (0 .. $n-1) {
111 $bi = $rs->record($i);
112 }
113  
114 Returns a single record from the result-set, indexed from zero.
115  
116 =cut
117  
118 sub record {
119 my $this = shift();
120 my($index) = @_;
121  
122 my $count = $this->count();
123 UDDI::HalfDecent::oops(error => "out of range",
124 detail => "record $index not in 0..$count")
125 if $index < 0 || $index >= $count;
126  
127 my $rec = $this->{records}->[$index];
128 if (!defined $rec) {
129 my $dom = $this->{dom}->[$index];
130 UDDI::HalfDecent::oops(error => "no DOM",
131 detail => "record $index in 0..$count")
132 if !defined $dom;
133  
134 $rec = $this->{records}->[$index] =
135 new UDDI::HalfDecent::Record($this, $dom);
136 }
137  
138 return $rec;
139 }
140  
141  
142 =head1 SEE ALSO
143  
144 C<UDDI::HalfDecent>
145 is the module that uses this. See also its SEE ALSOs.
146  
147 =head1 AUTHOR, COPYRIGHT AND LICENSE
148  
149 As for C<Net::Z3950::UDDI>.
150  
151 =cut
152  
153 1;