File Coverage

blib/lib/Excel/Writer/XLSX/Package/Relationships.pm
Criterion Covered Total %
statement 66 66 100.0
branch 2 2 100.0
condition n/a
subroutine 13 13 100.0
pod 0 1 0.0
total 81 82 98.7


line stmt bran cond sub pod time code
1             package Excel::Writer::XLSX::Package::Relationships;
2              
3             ###############################################################################
4             #
5             # Relationships - A class for writing the Excel XLSX Rels file.
6             #
7             # Used in conjunction with Excel::Writer::XLSX
8             #
9             # Copyright 2000-2019, John McNamara, jmcnamara@cpan.org
10             #
11             # Documentation after __END__
12             #
13              
14             # perltidy with the following options: -mbl=2 -pt=0 -nola
15              
16 1040     1040   17692 use 5.008002;
  1040         5078  
17 1040     1040   6833 use strict;
  1040         3485  
  1040         27084  
18 1040     1040   5782 use warnings;
  1040         5048  
  1040         30685  
19 1040     1040   6082 use Carp;
  1040         5302  
  1040         61791  
20 1040     1040   9985 use Excel::Writer::XLSX::Package::XMLwriter;
  1040         3959  
  1040         640339  
21              
22             our @ISA = qw(Excel::Writer::XLSX::Package::XMLwriter);
23             our $VERSION = '1.03';
24              
25             our $schema_root = 'http://schemas.openxmlformats.org';
26             our $package_schema = $schema_root . '/package/2006/relationships';
27             our $document_schema = $schema_root . '/officeDocument/2006/relationships';
28              
29             ###############################################################################
30             #
31             # Public and private API methods.
32             #
33             ###############################################################################
34              
35              
36             ###############################################################################
37             #
38             # new()
39             #
40             # Constructor.
41             #
42             sub new {
43              
44 2702     2702 0 8712 my $class = shift;
45 2702         4745 my $fh = shift;
46 2702         12051 my $self = Excel::Writer::XLSX::Package::XMLwriter->new( $fh );
47              
48 2702         7214 $self->{_rels} = [];
49 2702         5981 $self->{_id} = 1;
50              
51 2702         5372 bless $self, $class;
52              
53 2702         7170 return $self;
54             }
55              
56              
57             ###############################################################################
58             #
59             # _assemble_xml_file()
60             #
61             # Assemble and write the XML file.
62             #
63             sub _assemble_xml_file {
64              
65 2701     2701   6139 my $self = shift;
66              
67 2701         14490 $self->xml_declaration;
68 2701         10941 $self->_write_relationships();
69             }
70              
71              
72             ###############################################################################
73             #
74             # _add_document_relationship()
75             #
76             # Add container relationship to XLSX .rels xml files.
77             #
78             sub _add_document_relationship {
79              
80 4984     4984   9028 my $self = shift;
81 4984         8410 my $type = shift;
82 4984         7858 my $target = shift;
83 4984         7359 my $target_mode = shift;
84              
85 4984         12563 $type = $document_schema . $type;
86              
87 4984         8691 push @{ $self->{_rels} }, [ $type, $target, $target_mode ];
  4984         22880  
88             }
89              
90              
91             ###############################################################################
92             #
93             # _add_package_relationship()
94             #
95             # Add container relationship to XLSX .rels xml files.
96             #
97             sub _add_package_relationship {
98              
99 809     809   1879 my $self = shift;
100 809         1906 my $type = shift;
101 809         1784 my $target = shift;
102              
103 809         2745 $type = $package_schema . $type;
104              
105 809         1923 push @{ $self->{_rels} }, [ $type, $target ];
  809         3249  
106             }
107              
108              
109             ###############################################################################
110             #
111             # _add_ms_package_relationship()
112             #
113             # Add container relationship to XLSX .rels xml files. Uses MS schema.
114             #
115             sub _add_ms_package_relationship {
116              
117 5     5   12 my $self = shift;
118 5         13 my $type = shift;
119 5         9 my $target = shift;
120 5         12 my $schema = 'http://schemas.microsoft.com/office/2006/relationships';
121              
122 5         16 $type = $schema . $type;
123              
124 5         11 push @{ $self->{_rels} }, [ $type, $target ];
  5         18  
125             }
126              
127              
128             ###############################################################################
129             #
130             # _add_worksheet_relationship()
131             #
132             # Add worksheet relationship to sheet.rels xml files.
133             #
134             sub _add_worksheet_relationship {
135              
136 693     693   1698 my $self = shift;
137 693         1556 my $type = shift;
138 693         1495 my $target = shift;
139 693         1422 my $target_mode = shift;
140              
141 693         2407 $type = $document_schema . $type;
142              
143 693         1667 push @{ $self->{_rels} }, [ $type, $target, $target_mode ];
  693         3542  
144             }
145              
146              
147             ###############################################################################
148             #
149             # Internal methods.
150             #
151             ###############################################################################
152              
153              
154             ###############################################################################
155             #
156             # XML writing methods.
157             #
158             ###############################################################################
159              
160              
161             ##############################################################################
162             #
163             # _write_relationships()
164             #
165             # Write the element.
166             #
167             sub _write_relationships {
168              
169 2701     2701   5641 my $self = shift;
170              
171 2701         8589 my @attributes = ( 'xmlns' => $package_schema, );
172              
173 2701         12837 $self->xml_start_tag( 'Relationships', @attributes );
174              
175 2701         5713 for my $rel ( @{ $self->{_rels} } ) {
  2701         7961  
176 6491         16233 $self->_write_relationship( @$rel );
177             }
178              
179 2701         11406 $self->xml_end_tag( 'Relationships' );
180              
181             # Close the XML writer filehandle.
182 2701         9045 $self->xml_get_fh()->close();
183             }
184              
185              
186             ##############################################################################
187             #
188             # _write_relationship()
189             #
190             # Write the element.
191             #
192             sub _write_relationship {
193              
194 6491     6491   11136 my $self = shift;
195 6491         11014 my $type = shift;
196 6491         10281 my $target = shift;
197 6491         9953 my $target_mode = shift;
198              
199             my @attributes = (
200 6491         21485 'Id' => 'rId' . $self->{_id}++,
201             'Type' => $type,
202             'Target' => $target,
203             );
204              
205 6491 100       15253 push @attributes, ( 'TargetMode' => $target_mode ) if $target_mode;
206              
207 6491         18560 $self->xml_empty_tag( 'Relationship', @attributes );
208             }
209              
210              
211             1;
212              
213              
214             __END__