File Coverage

blib/lib/Parse/CPAN/Whois.pm
Criterion Covered Total %
statement 45 48 93.7
branch 14 16 87.5
condition n/a
subroutine 10 11 90.9
pod 6 6 100.0
total 75 81 92.5


line stmt bran cond sub pod time code
1             # vim: sts=3 sw=3 et
2             package Parse::CPAN::Whois;
3 1     1   2120 use strict;
  1         3  
  1         172  
4 1     1   6 use warnings;
  1         2  
  1         60  
5              
6             our $VERSION='0.02';
7              
8             =head1 NAME
9              
10             Parse::CPAN::Whois - Parse CPAN's authors/00whois.xml file
11              
12             =head1 DESCRIPTION
13              
14             CPAN has two author indices, "01mailrc.txt.gz", which L parses for you, and "00whois.xml", which is handled by this module.
15              
16             It tries to be API-compatible with L, while providing
17             access to the extra information "00whois.xml" has over "01mailrc.txt.gz".
18              
19             =cut
20              
21 1     1   1237 use XML::SAX::ParserFactory;
  1         7590  
  1         34  
22 1     1   1411 use Parse::CPAN::Whois::Author;
  1         3  
  1         38  
23              
24 1     1   6 use base qw(XML::SAX::Base);
  1         2  
  1         2364  
25              
26             =head1 METHODS
27              
28             =head2 new FILENAME|DATA
29              
30             new() takes either a path or a scalar containing the data to parse
31             as an argument. It parses the data, and then returns an object you
32             can query for PAUSE ids.
33              
34             =cut
35              
36             sub new {
37 1     1 1 550 my $class = shift;
38 1         7 my $file = shift;
39              
40 1 50       5 $file = '00whois.xml' unless (defined $file);
41              
42 1         9 my $handler = $class->SUPER::new;
43 1         99 my $p = XML::SAX::ParserFactory->parser(Handler => $handler);
44 1 50       135487 if (substr ($file, 0, 1) eq '<') {
45 0         0 $p->parse_string ($file);
46             } else {
47 1         25 $p->parse_file($file);
48             }
49              
50 1         548 return delete $handler->{list};
51             }
52              
53             =head2 author PAUSEID
54              
55             returns the L object that corresponds to
56             the PAUSE id.
57              
58             =cut
59              
60             sub author {
61 2     2 1 631 my $self = shift;
62 2         4 my $cpanid = shift;
63              
64 2         7 return $self->{uc($cpanid)}
65             }
66              
67             =head2 authors
68              
69             returns a list of L objects.
70              
71             =cut
72              
73             sub authors {
74 0     0 1 0 my $self = shift;
75              
76 0         0 return values %$self;
77             }
78              
79              
80             # below are the SAX2 methods used.
81              
82              
83             sub start_element {
84 38     38 1 16344 my $self = shift;
85 38         51 my $elem = shift;
86              
87 38 100       118 if ($elem->{LocalName} eq 'cpan-whois') {
    100          
88 1         10 $self->{list} = bless {}, 'Parse::CPAN::Whois';
89             } elsif ($elem->{LocalName} eq 'cpanid') {
90 5         44 $self->{tmp} = bless {}, 'Parse::CPAN::Whois::Author';
91             } else {
92 32         76 $self->{key} = $elem->{LocalName};
93 32         99 $self->{value} = '';
94             }
95             }
96              
97             sub characters {
98 75     75 1 5625 my $self = shift;
99 75         93 my $data = shift;
100              
101 75 100       347 if (defined $self->{value}) {
102 32         121 $self->{value} .= $data->{Data};
103             }
104             }
105              
106             sub end_element {
107 38     38 1 6126 my $self = shift;
108 38         52 my $elem = shift;
109              
110 38 100       116 if ($elem->{LocalName} eq 'cpan-whois') {
    100          
111             } elsif ($elem->{LocalName} eq 'cpanid') {
112 5         12 my $id = $self->{tmp}->{id};
113 5         10 my $foo = delete $self->{tmp};
114 5 100       36 if ($foo->{type} eq 'author') {
115 4         78 $self->{list}->{$id} = $foo;
116             }
117             } else {
118 32         225 $self->{tmp}->{delete $self->{key}} = delete $self->{value};
119             }
120             }
121              
122             1;
123              
124             __END__