Parser/Relationships.pm untested

filter: Statementstmt 0.0% 0 / 47 Branchbran 0.0% 0 / 12 Conditioncond 0.0% 0 / 10 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 0.0% 0 / 11 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 90 CC 7 SCAR 17.3
CC
7
Cov
0%
_start
12.0
new
6.0
relation_target
6.0
CRAP 5.6
1 package Data::XLSX::Parser::Relationships;
2 use strict;
3 use warnings;
4  
5 use XML::Parser::Expat;
6 use Archive::Zip ();
7 use File::Temp;
8 use Carp;
9  
10 sub new {
11 my ($class, $archive) = @_;
12  
13 my $self = bless {
14 _relationships => {}, # { <rid> => {Target => "...", Type => "..."}, ... }
15 }, $class;
16  
17 my $fh = File::Temp->new( SUFFIX => '.xml.rels') or confess "couldn't create tempfile $!";
18  
19 my $handle = $archive->relationships or confess "couldn't get handle to relationships archive $!";
20 confess 'Failed to write temporary file: ', $fh->filename
21 unless $handle->extractToFileNamed($fh->filename) == Archive::Zip::AZ_OK;
22  
23 my $parser = XML::Parser::Expat->new(Namespaces=>1);
24 $parser->setHandlers(
25 Start => sub { $self->_start(@_) },
26 End => sub { $self->_end(@_) },
27 );
28 $parser->parse($fh);
29  
30 $self;
31 }
32  
33 sub relation_target {
34 my ($self, $rid) = @_;
35  
36 unless (exists $self->{_relationships}->{$rid}) {
37 confess "relation ID $rid not found in relationships!";
38 return;
39 }
40  
41 my $relation = $self->{_relationships}->{$rid};
42  
43 return $relation->{Target};
44 }
45  
46 sub relation {
47 my ($self, $rid) = @_;
48  
49 unless (exists $self->{_relationships}->{$rid}) {
50 confess "relation ID $rid not found in relationships!";
51 return;
52 }
53  
54 return $self->{_relationships}->{$rid};
55 }
56  
57 sub _start {
58 my ($self, $parser, $name, %attrs) = @_;
59  
60 $self->{_in_relationships} = 1 if $name eq "Relationships";
61  
62 if ($self->{_in_relationships} && $name eq "Relationship" && $attrs{Id}) {
63 $self->{_relationships}->{$attrs{Id}} = {
64 Target => $attrs{Target},
65 Type => $attrs{Type},
66 };
67 }
68 }
69  
70 sub _end {
71 my ($self, $parser, $name) = @_;
72  
73 $self->{_in_relationships} = 0 if $name eq "Relationships";
74 }
75  
76 1;
77 __END__