File Coverage

blib/lib/Excel/Writer/XLSX/Package/ContentTypes.pm
Criterion Covered Total %
statement 104 104 100.0
branch 2 2 100.0
condition n/a
subroutine 26 26 100.0
pod 0 1 0.0
total 132 133 99.2


line stmt bran cond sub pod time code
1             package Excel::Writer::XLSX::Package::ContentTypes;
2              
3             ###############################################################################
4             #
5             # Excel::Writer::XLSX::Package::ContentTypes - A class for writing the Excel
6             # XLS [Content_Types] file.
7             #
8             # Used in conjunction with Excel::Writer::XLSX
9             #
10             # Copyright 2000-2020, John McNamara, jmcnamara@cpan.org
11             #
12             # Documentation after __END__
13             #
14              
15             # perltidy with the following options: -mbl=2 -pt=0 -nola
16              
17 1081     1081   17731 use 5.008002;
  1081         4812  
18 1081     1081   7752 use strict;
  1081         3622  
  1081         21365  
19 1081     1081   7276 use warnings;
  1081         3233  
  1081         25017  
20 1081     1081   4944 use Carp;
  1081         3591  
  1081         55515  
21 1081     1081   7716 use Excel::Writer::XLSX::Package::XMLwriter;
  1081         3644  
  1081         1212658  
22              
23             our @ISA = qw(Excel::Writer::XLSX::Package::XMLwriter);
24             our $VERSION = '1.07';
25              
26              
27             ###############################################################################
28             #
29             # Package data.
30             #
31             ###############################################################################
32              
33             my $app_package = 'application/vnd.openxmlformats-package.';
34             my $app_document = 'application/vnd.openxmlformats-officedocument.';
35              
36             our @defaults = (
37             [ 'rels', $app_package . 'relationships+xml' ],
38             [ 'xml', 'application/xml' ],
39             );
40              
41             our @overrides = (
42             [ '/docProps/app.xml', $app_document . 'extended-properties+xml' ],
43             [ '/docProps/core.xml', $app_package . 'core-properties+xml' ],
44             [ '/xl/styles.xml', $app_document . 'spreadsheetml.styles+xml' ],
45             [ '/xl/theme/theme1.xml', $app_document . 'theme+xml' ],
46             [ '/xl/workbook.xml', $app_document . 'spreadsheetml.sheet.main+xml' ],
47             );
48              
49              
50             ###############################################################################
51             #
52             # Public and private API methods.
53             #
54             ###############################################################################
55              
56             ###############################################################################
57             #
58             # new()
59             #
60             # Constructor.
61             #
62             sub new {
63              
64 853     853 0 5221 my $class = shift;
65 853         1923 my $fh = shift;
66 853         3558 my $self = Excel::Writer::XLSX::Package::XMLwriter->new( $fh );
67              
68 853         4003 $self->{_defaults} = [@defaults];
69 853         3790 $self->{_overrides} = [@overrides];
70              
71 853         1904 bless $self, $class;
72              
73 853         2428 return $self;
74             }
75              
76              
77             ###############################################################################
78             #
79             # _assemble_xml_file()
80             #
81             # Assemble and write the XML file.
82             #
83             sub _assemble_xml_file {
84              
85 850     850   2536 my $self = shift;
86              
87 850         9529 $self->xml_declaration;
88 850         5330 $self->_write_types();
89 850         4053 $self->_write_defaults();
90 850         3690 $self->_write_overrides();
91              
92 850         5222 $self->xml_end_tag( 'Types' );
93              
94             # Close the XML writer filehandle.
95 850         4582 $self->xml_get_fh()->close();
96             }
97              
98              
99             ###############################################################################
100             #
101             # _add_default()
102             #
103             # Add elements to the ContentTypes defaults.
104             #
105             sub _add_default {
106              
107 172     172   417 my $self = shift;
108 172         362 my $part_name = shift;
109 172         351 my $content_type = shift;
110              
111 172         315 push @{ $self->{_defaults} }, [ $part_name, $content_type ];
  172         1310  
112              
113             }
114              
115              
116             ###############################################################################
117             #
118             # _add_override()
119             #
120             # Add elements to the ContentTypes overrides.
121             #
122             sub _add_override {
123              
124 2239     2239   4330 my $self = shift;
125 2239         4240 my $part_name = shift;
126 2239         4258 my $content_type = shift;
127              
128 2239         3528 push @{ $self->{_overrides} }, [ $part_name, $content_type ];
  2239         14008  
129              
130             }
131              
132              
133             ###############################################################################
134             #
135             # _add_worksheet_name()
136             #
137             # Add the name of a worksheet to the ContentTypes overrides.
138             #
139             sub _add_worksheet_name {
140              
141 977     977   2381 my $self = shift;
142 977         3525 my $worksheet_name = shift;
143              
144 977         4241 $worksheet_name = "/xl/worksheets/$worksheet_name.xml";
145              
146 977         5598 $self->_add_override( $worksheet_name,
147             $app_document . 'spreadsheetml.worksheet+xml' );
148             }
149              
150              
151             ###############################################################################
152             #
153             # _add_chartsheet_name()
154             #
155             # Add the name of a chartsheet to the ContentTypes overrides.
156             #
157             sub _add_chartsheet_name {
158              
159 20     20   47 my $self = shift;
160 20         60 my $chartsheet_name = shift;
161              
162 20         73 $chartsheet_name = "/xl/chartsheets/$chartsheet_name.xml";
163              
164 20         76 $self->_add_override( $chartsheet_name,
165             $app_document . 'spreadsheetml.chartsheet+xml' );
166             }
167              
168              
169             ###############################################################################
170             #
171             # _add_chart_name()
172             #
173             # Add the name of a chart to the ContentTypes overrides.
174             #
175             sub _add_chart_name {
176              
177 418     418   1067 my $self = shift;
178 418         925 my $chart_name = shift;
179              
180 418         1395 $chart_name = "/xl/charts/$chart_name.xml";
181              
182 418         1817 $self->_add_override( $chart_name, $app_document . 'drawingml.chart+xml' );
183             }
184              
185              
186             ###############################################################################
187             #
188             # _add_drawing_name()
189             #
190             # Add the name of a drawing to the ContentTypes overrides.
191             #
192             sub _add_drawing_name {
193              
194 503     503   1497 my $self = shift;
195 503         1152 my $drawing_name = shift;
196              
197 503         1766 $drawing_name = "/xl/drawings/$drawing_name.xml";
198              
199 503         2262 $self->_add_override( $drawing_name, $app_document . 'drawing+xml' );
200             }
201              
202              
203             ###############################################################################
204             #
205             # _add_vml_name()
206             #
207             # Add the name of a VML drawing to the ContentTypes defaults.
208             #
209             sub _add_vml_name {
210              
211 61     61   132 my $self = shift;
212              
213 61         509 $self->_add_default( 'vml', $app_document . 'vmlDrawing' );
214             }
215              
216              
217             ###############################################################################
218             #
219             # _add_comment_name()
220             #
221             # Add the name of a comment to the ContentTypes overrides.
222             #
223             sub _add_comment_name {
224              
225 43     43   109 my $self = shift;
226 43         80 my $comment_name = shift;
227              
228 43         115 $comment_name = "/xl/$comment_name.xml";
229              
230 43         177 $self->_add_override( $comment_name,
231             $app_document . 'spreadsheetml.comments+xml' );
232             }
233              
234             ###############################################################################
235             #
236             # _Add_shared_strings()
237             #
238             # Add the sharedStrings link to the ContentTypes overrides.
239             #
240             sub _add_shared_strings {
241              
242 238     238   782 my $self = shift;
243              
244 238         1386 $self->_add_override( '/xl/sharedStrings.xml',
245             $app_document . 'spreadsheetml.sharedStrings+xml' );
246             }
247              
248              
249             ###############################################################################
250             #
251             # _add_calc_chain()
252             #
253             # Add the calcChain link to the ContentTypes overrides.
254             #
255             sub _add_calc_chain {
256              
257 1     1   4 my $self = shift;
258              
259 1         4 $self->_add_override( '/xl/calcChain.xml',
260             $app_document . 'spreadsheetml.calcChain+xml' );
261             }
262              
263              
264             ###############################################################################
265             #
266             # _add_image_types()
267             #
268             # Add the image default types.
269             #
270             sub _add_image_types {
271              
272 849     849   2028 my $self = shift;
273 849         2622 my %types = @_;
274              
275 849         4268 for my $type ( keys %types ) {
276 104         616 $self->_add_default( $type, 'image/' . $type );
277             }
278             }
279              
280              
281             ###############################################################################
282             #
283             # _add_table_name()
284             #
285             # Add the name of a table to the ContentTypes overrides.
286             #
287             sub _add_table_name {
288              
289 35     35   83 my $self = shift;
290 35         73 my $table_name = shift;
291              
292 35         108 $table_name = "/xl/tables/$table_name.xml";
293              
294 35         140 $self->_add_override( $table_name,
295             $app_document . 'spreadsheetml.table+xml' );
296             }
297              
298              
299             ###############################################################################
300             #
301             # _add_vba_project()
302             #
303             # Add a vbaProject to the ContentTypes defaults.
304             #
305             sub _add_vba_project {
306              
307 6     6   13 my $self = shift;
308              
309             # Change the workbook.xml content-type from xlsx to xlsm.
310 6         10 for my $aref ( @{ $self->{_overrides} } ) {
  6         14  
311 36 100       82 if ( $aref->[0] eq '/xl/workbook.xml' ) {
312 6         13 $aref->[1] = 'application/vnd.ms-excel.sheet.macroEnabled.main+xml';
313             }
314             }
315              
316 6         20 $self->_add_default( 'bin', 'application/vnd.ms-office.vbaProject' );
317             }
318              
319              
320             ###############################################################################
321             #
322             # _add_custom_properties()
323             #
324             # Add the custom properties to the ContentTypes overrides.
325             #
326             sub _add_custom_properties {
327              
328 4     4   8 my $self = shift;
329 4         7 my $custom = "/docProps/custom.xml";
330              
331 4         12 $self->_add_override( $custom, $app_document . 'custom-properties+xml' );
332             }
333              
334              
335             ###############################################################################
336             #
337             # Internal methods.
338             #
339             ###############################################################################
340              
341              
342             ###############################################################################
343             #
344             # _write_defaults()
345             #
346             # Write out all of the types.
347             #
348             sub _write_defaults {
349              
350 850     850   2202 my $self = shift;
351              
352 850         2177 for my $aref ( @{ $self->{_defaults} } ) {
  850         3099  
353             #<<<
354 1872         8156 $self->xml_empty_tag(
355             'Default',
356             'Extension', $aref->[0],
357             'ContentType', $aref->[1] );
358             #>>>
359             }
360             }
361              
362              
363             ###############################################################################
364             #
365             # _write_overrides()
366             #
367             # Write out all of the types.
368             #
369             sub _write_overrides {
370              
371 850     850   2099 my $self = shift;
372              
373 850         2021 for my $aref ( @{ $self->{_overrides} } ) {
  850         2826  
374             #<<<
375 6489         16030 $self->xml_empty_tag(
376             'Override',
377             'PartName', $aref->[0],
378             'ContentType', $aref->[1] );
379             #>>>
380             }
381             }
382              
383              
384             ###############################################################################
385             #
386             # XML writing methods.
387             #
388             ###############################################################################
389              
390              
391             ###############################################################################
392             #
393             # _write_types()
394             #
395             # Write the element.
396             #
397             sub _write_types {
398              
399 850     850   2492 my $self = shift;
400 850         2380 my $xmlns = 'http://schemas.openxmlformats.org/package/2006/content-types';
401              
402 850         3241 my @attributes = ( 'xmlns' => $xmlns, );
403              
404 850         6316 $self->xml_start_tag( 'Types', @attributes );
405             }
406              
407             ###############################################################################
408             #
409             # _write_default()
410             #
411             # Write the element.
412             #
413             sub _write_default {
414              
415 1     1   11 my $self = shift;
416 1         2 my $extension = shift;
417 1         2 my $content_type = shift;
418              
419 1         4 my @attributes = (
420             'Extension' => $extension,
421             'ContentType' => $content_type,
422             );
423              
424 1         19 $self->xml_empty_tag( 'Default', @attributes );
425             }
426              
427              
428             ###############################################################################
429             #
430             # _write_override()
431             #
432             # Write the element.
433             #
434             sub _write_override {
435              
436 1     1   9 my $self = shift;
437 1         2 my $part_name = shift;
438 1         2 my $content_type = shift;
439 1         2 my $writer = $self;
440              
441 1         5 my @attributes = (
442             'PartName' => $part_name,
443             'ContentType' => $content_type,
444             );
445              
446 1         10 $self->xml_empty_tag( 'Override', @attributes );
447             }
448              
449              
450             1;
451              
452              
453             __END__