File Coverage

blib/lib/Excel/Writer/XLSX/Package/Core.pm
Criterion Covered Total %
statement 95 95 100.0
branch 12 12 100.0
condition 6 7 85.7
subroutine 20 20 100.0
pod 0 1 0.0
total 133 135 98.5


line stmt bran cond sub pod time code
1             package Excel::Writer::XLSX::Package::Core;
2              
3             ###############################################################################
4             #
5             # Core - A class for writing the Excel XLSX core.xml 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   18391 use 5.008002;
  1040         4979  
17 1040     1040   7273 use strict;
  1040         3584  
  1040         23148  
18 1040     1040   8075 use warnings;
  1040         3846  
  1040         31060  
19 1040     1040   7054 use Carp;
  1040         3773  
  1040         62117  
20 1040     1040   8920 use Excel::Writer::XLSX::Package::XMLwriter;
  1040         3568  
  1040         927428  
21              
22             our @ISA = qw(Excel::Writer::XLSX::Package::XMLwriter);
23             our $VERSION = '1.03';
24              
25              
26             ###############################################################################
27             #
28             # Public and private API methods.
29             #
30             ###############################################################################
31              
32              
33             ###############################################################################
34             #
35             # new()
36             #
37             # Constructor.
38             #
39             sub new {
40              
41 812     812 0 4770 my $class = shift;
42 812         1925 my $fh = shift;
43 812         3268 my $self = Excel::Writer::XLSX::Package::XMLwriter->new( $fh );
44              
45 812         2934 $self->{_properties} = {};
46 812         5684 $self->{_createtime} = [ gmtime() ];
47              
48 812         2649 bless $self, $class;
49              
50 812         2753 return $self;
51             }
52              
53              
54             ###############################################################################
55             #
56             # _assemble_xml_file()
57             #
58             # Assemble and write the XML file.
59             #
60             sub _assemble_xml_file {
61              
62 811     811   2267 my $self = shift;
63              
64 811         7442 $self->xml_declaration;
65 811         5266 $self->_write_cp_core_properties();
66 811         5171 $self->_write_dc_title();
67 811         4180 $self->_write_dc_subject();
68 811         3824 $self->_write_dc_creator();
69 811         4783 $self->_write_cp_keywords();
70 811         3525 $self->_write_dc_description();
71 811         3823 $self->_write_cp_last_modified_by();
72 811         4899 $self->_write_dcterms_created();
73 811         5723 $self->_write_dcterms_modified();
74 811         5244 $self->_write_cp_category();
75 811         3846 $self->_write_cp_content_status();
76              
77 811         6120 $self->xml_end_tag( 'cp:coreProperties' );
78              
79             # Close the XML writer filehandle.
80 811         6416 $self->xml_get_fh()->close();
81             }
82              
83              
84             ###############################################################################
85             #
86             # _set_properties()
87             #
88             # Set the document properties.
89             #
90             sub _set_properties {
91              
92 811     811   2437 my $self = shift;
93 811         1851 my $properties = shift;
94              
95 811         6478 $self->{_properties} = $properties;
96             }
97              
98              
99             ###############################################################################
100             #
101             # Internal methods.
102             #
103             ###############################################################################
104              
105              
106             ###############################################################################
107             #
108             # _datetime_to_iso8601_date()
109             #
110             # Convert a gmtime/localtime() date to a ISO 8601 style "2010-01-01T00:00:00Z"
111             # date. Excel always treats this as a utc date/time.
112             #
113             sub _datetime_to_iso8601_date {
114              
115 1622     1622   3944 my $self = shift;
116 1622   66     8821 my $gmtime = shift || $self->{_createtime};
117              
118 1622         5908 my ( $seconds, $minutes, $hours, $day, $month, $year ) = @$gmtime;
119              
120 1622         3890 $month++;
121 1622         4657 $year += 1900;
122              
123 1622         10592 my $date = sprintf "%4d-%02d-%02dT%02d:%02d:%02dZ", $year, $month, $day,
124             $hours, $minutes, $seconds;
125             }
126              
127              
128             ###############################################################################
129             #
130             # XML writing methods.
131             #
132             ###############################################################################
133              
134              
135             ###############################################################################
136             #
137             # _write_cp_core_properties()
138             #
139             # Write the element.
140             #
141             sub _write_cp_core_properties {
142              
143 811     811   2139 my $self = shift;
144 811         2047 my $xmlns_cp =
145             'http://schemas.openxmlformats.org/package/2006/metadata/core-properties';
146 811         1827 my $xmlns_dc = 'http://purl.org/dc/elements/1.1/';
147 811         2090 my $xmlns_dcterms = 'http://purl.org/dc/terms/';
148 811         1795 my $xmlns_dcmitype = 'http://purl.org/dc/dcmitype/';
149 811         1837 my $xmlns_xsi = 'http://www.w3.org/2001/XMLSchema-instance';
150              
151 811         4455 my @attributes = (
152             'xmlns:cp' => $xmlns_cp,
153             'xmlns:dc' => $xmlns_dc,
154             'xmlns:dcterms' => $xmlns_dcterms,
155             'xmlns:dcmitype' => $xmlns_dcmitype,
156             'xmlns:xsi' => $xmlns_xsi,
157             );
158              
159 811         5765 $self->xml_start_tag( 'cp:coreProperties', @attributes );
160             }
161              
162              
163             ###############################################################################
164             #
165             # _write_dc_creator()
166             #
167             # Write the element.
168             #
169             sub _write_dc_creator {
170              
171 811     811   2739 my $self = shift;
172 811   100     6380 my $data = $self->{_properties}->{author} || '';
173              
174 811         6251 $self->xml_data_element( 'dc:creator', $data );
175             }
176              
177              
178             ###############################################################################
179             #
180             # _write_cp_last_modified_by()
181             #
182             # Write the element.
183             #
184             sub _write_cp_last_modified_by {
185              
186 811     811   2233 my $self = shift;
187 811   100     5938 my $data = $self->{_properties}->{author} || '';
188              
189 811         3952 $self->xml_data_element( 'cp:lastModifiedBy', $data );
190             }
191              
192              
193             ###############################################################################
194             #
195             # _write_dcterms_created()
196             #
197             # Write the element.
198             #
199             sub _write_dcterms_created {
200              
201 811     811   2507 my $self = shift;
202 811         2414 my $date = $self->{_properties}->{created};
203 811         2132 my $xsi_type = 'dcterms:W3CDTF';
204              
205 811         4219 $date = $self->_datetime_to_iso8601_date( $date );
206              
207 811         3516 my @attributes = ( 'xsi:type' => $xsi_type, );
208              
209 811         4833 $self->xml_data_element( 'dcterms:created', $date, @attributes );
210             }
211              
212              
213             ###############################################################################
214             #
215             # _write_dcterms_modified()
216             #
217             # Write the element.
218             #
219             sub _write_dcterms_modified {
220              
221 811     811   2923 my $self = shift;
222 811         3589 my $date = $self->{_properties}->{created};
223 811         3252 my $xsi_type = 'dcterms:W3CDTF';
224              
225 811         4137 $date = $self->_datetime_to_iso8601_date( $date );
226              
227 811         4537 my @attributes = ( 'xsi:type' => $xsi_type, );
228              
229 811         4438 $self->xml_data_element( 'dcterms:modified', $date, @attributes );
230             }
231              
232              
233             ##############################################################################
234             #
235             # _write_dc_title()
236             #
237             # Write the element.
238             #
239             sub _write_dc_title {
240              
241 811     811   2219 my $self = shift;
242 811         2349 my $data = $self->{_properties}->{title};
243              
244 811 100       3373 return unless $data;
245              
246 2         17 $self->xml_data_element( 'dc:title', $data );
247             }
248              
249              
250             ##############################################################################
251             #
252             # _write_dc_subject()
253             #
254             # Write the element.
255             #
256             sub _write_dc_subject {
257              
258 811     811   2521 my $self = shift;
259 811         2706 my $data = $self->{_properties}->{subject};
260              
261 811 100       3894 return unless $data;
262              
263 2         8 $self->xml_data_element( 'dc:subject', $data );
264             }
265              
266              
267             ##############################################################################
268             #
269             # _write_cp_keywords()
270             #
271             # Write the element.
272             #
273             sub _write_cp_keywords {
274              
275 811     811   2457 my $self = shift;
276 811         2644 my $data = $self->{_properties}->{keywords};
277              
278 811 100       3377 return unless $data;
279              
280 2         9 $self->xml_data_element( 'cp:keywords', $data );
281             }
282              
283              
284             ##############################################################################
285             #
286             # _write_dc_description()
287             #
288             # Write the element.
289             #
290             sub _write_dc_description {
291              
292 811     811   2278 my $self = shift;
293 811         3152 my $data = $self->{_properties}->{comments};
294              
295 811 100       3038 return unless $data;
296              
297 2         11 $self->xml_data_element( 'dc:description', $data );
298             }
299              
300              
301             ##############################################################################
302             #
303             # _write_cp_category()
304             #
305             # Write the element.
306             #
307             sub _write_cp_category {
308              
309 811     811   2280 my $self = shift;
310 811         2701 my $data = $self->{_properties}->{category};
311              
312 811 100       4785 return unless $data;
313              
314 2         7 $self->xml_data_element( 'cp:category', $data );
315             }
316              
317              
318             ##############################################################################
319             #
320             # _write_cp_content_status()
321             #
322             # Write the element.
323             #
324             sub _write_cp_content_status {
325              
326 811     811   2538 my $self = shift;
327 811         2590 my $data = $self->{_properties}->{status};
328              
329 811 100       3946 return unless $data;
330              
331 2         9 $self->xml_data_element( 'cp:contentStatus', $data );
332             }
333              
334              
335             1;
336              
337              
338             __END__