File Coverage

blib/lib/Excel/Writer/XLSX/Package/Custom.pm
Criterion Covered Total %
statement 70 70 100.0
branch 10 10 100.0
condition n/a
subroutine 15 15 100.0
pod 0 1 0.0
total 95 96 98.9


line stmt bran cond sub pod time code
1             package Excel::Writer::XLSX::Package::Custom;
2              
3             ###############################################################################
4             #
5             # Custom - A class for writing the Excel XLSX custom.xml file for custom
6             # workbook properties.
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   17807 use 5.008002;
  1081         3680  
18 1081     1081   9392 use strict;
  1081         3347  
  1081         21938  
19 1081     1081   4676 use warnings;
  1081         3342  
  1081         25239  
20 1081     1081   6155 use Carp;
  1081         3496  
  1081         52856  
21 1081     1081   7766 use Excel::Writer::XLSX::Package::XMLwriter;
  1081         3709  
  1081         661174  
22              
23             our @ISA = qw(Excel::Writer::XLSX::Package::XMLwriter);
24             our $VERSION = '1.07';
25              
26              
27             ###############################################################################
28             #
29             # Public and private API methods.
30             #
31             ###############################################################################
32              
33              
34             ###############################################################################
35             #
36             # new()
37             #
38             # Constructor.
39             #
40             sub new {
41              
42 849     849 0 2388 my $class = shift;
43 849         3133 my $fh = shift;
44 849         3218 my $self = Excel::Writer::XLSX::Package::XMLwriter->new( $fh );
45              
46 849         3191 $self->{_properties} = [];
47 849         2727 $self->{_pid} = 1;
48              
49 849         2463 bless $self, $class;
50              
51 849         2456 return $self;
52             }
53              
54              
55             ###############################################################################
56             #
57             # _assemble_xml_file()
58             #
59             # Assemble and write the XML file.
60             #
61             sub _assemble_xml_file {
62              
63 4     4   8 my $self = shift;
64              
65 4         26 $self->xml_declaration;
66              
67 4         15 $self->_write_properties();
68              
69 4         14 $self->xml_end_tag( 'Properties' );
70              
71             # Close the XML writer filehandle.
72 4         13 $self->xml_get_fh()->close();
73             }
74              
75              
76             ###############################################################################
77             #
78             # _set_properties()
79             #
80             # Set the document properties.
81             #
82             sub _set_properties {
83              
84 4     4   9 my $self = shift;
85 4         6 my $properties = shift;
86              
87 4         22 $self->{_properties} = $properties;
88             }
89              
90              
91             ###############################################################################
92             #
93             # Internal methods.
94             #
95             ###############################################################################
96              
97              
98             ###############################################################################
99             #
100             # XML writing methods.
101             #
102             ###############################################################################
103              
104              
105             ###############################################################################
106             #
107             # _write_properties()
108             #
109             # Write the element.
110             #
111             sub _write_properties {
112              
113 4     4   8 my $self = shift;
114 4         8 my $schema = 'http://schemas.openxmlformats.org/officeDocument/2006/';
115 4         13 my $xmlns = $schema . 'custom-properties';
116 4         9 my $xmlns_vt = $schema . 'docPropsVTypes';
117              
118 4         14 my @attributes = (
119             'xmlns' => $xmlns,
120             'xmlns:vt' => $xmlns_vt,
121             );
122              
123 4         16 $self->xml_start_tag( 'Properties', @attributes );
124              
125 4         9 for my $property ( @{ $self->{_properties} } ) {
  4         12  
126              
127             # Write the property element.
128 18         36 $self->_write_property( $property );
129             }
130             }
131              
132             ##############################################################################
133             #
134             # _write_property()
135             #
136             # Write the element.
137             #
138             sub _write_property {
139              
140 18     18   25 my $self = shift;
141 18         22 my $property = shift;
142 18         24 my $fmtid = '{D5CDD505-2E9C-101B-9397-08002B2CF9AE}';
143              
144 18         24 $self->{_pid}++;
145              
146 18         40 my ( $name, $value, $type ) = @$property;
147              
148              
149             my @attributes = (
150             'fmtid' => $fmtid,
151             'pid' => $self->{_pid},
152 18         40 'name' => $name,
153             );
154              
155 18         42 $self->xml_start_tag( 'property', @attributes );
156              
157 18 100       70 if ( $type eq 'date' ) {
    100          
    100          
    100          
158              
159             # Write the vt:filetime element.
160 2         7 $self->_write_vt_filetime( $value );
161             }
162             elsif ( $type eq 'number' ) {
163              
164             # Write the vt:r8 element.
165 4         8 $self->_write_vt_r8( $value );
166             }
167             elsif ( $type eq 'number_int' ) {
168              
169             # Write the vt:i4 element.
170 2         7 $self->_write_vt_i4( $value );
171             }
172             elsif ( $type eq 'bool' ) {
173              
174             # Write the vt:bool element.
175 4         7 $self->_write_vt_bool( $value );
176             }
177             else {
178              
179             # Write the vt:lpwstr element.
180 6         19 $self->_write_vt_lpwstr( $value );
181             }
182              
183              
184 18         45 $self->xml_end_tag( 'property' );
185             }
186              
187              
188             ##############################################################################
189             #
190             # _write_vt_lpwstr()
191             #
192             # Write the element.
193             #
194             sub _write_vt_lpwstr {
195              
196 6     6   10 my $self = shift;
197 6         9 my $data = shift;
198              
199 6         20 $self->xml_data_element( 'vt:lpwstr', $data );
200             }
201              
202              
203             ##############################################################################
204             #
205             # _write_vt_i4()
206             #
207             # Write the element.
208             #
209             sub _write_vt_i4 {
210              
211 2     2   4 my $self = shift;
212 2         4 my $data = shift;
213              
214 2         4 $self->xml_data_element( 'vt:i4', $data );
215             }
216              
217              
218             ##############################################################################
219             #
220             # _write_vt_r8()
221             #
222             # Write the element.
223             #
224             sub _write_vt_r8 {
225              
226 4     4   6 my $self = shift;
227 4         6 my $data = shift;
228              
229 4         8 $self->xml_data_element( 'vt:r8', $data );
230             }
231              
232              
233             ##############################################################################
234             #
235             # _write_vt_bool()
236             #
237             # Write the element.
238             #
239             sub _write_vt_bool {
240              
241 4     4   4 my $self = shift;
242 4         8 my $data = shift;
243              
244 4 100       8 if ( $data ) {
245 2         4 $data = 'true';
246             }
247             else {
248 2         4 $data = 'false';
249             }
250              
251 4         13 $self->xml_data_element( 'vt:bool', $data );
252             }
253              
254             ##############################################################################
255             #
256             # _write_vt_filetime()
257             #
258             # Write the element.
259             #
260             sub _write_vt_filetime {
261              
262 2     2   4 my $self = shift;
263 2         5 my $data = shift;
264              
265 2         6 $self->xml_data_element( 'vt:filetime', $data );
266             }
267              
268              
269             1;
270              
271              
272             __END__