File Coverage

blib/lib/Excel/Writer/XLSX/Package/SharedStrings.pm
Criterion Covered Total %
statement 60 60 100.0
branch 4 4 100.0
condition 5 6 83.3
subroutine 14 14 100.0
pod 0 1 0.0
total 83 85 97.6


line stmt bran cond sub pod time code
1             package Excel::Writer::XLSX::Package::SharedStrings;
2              
3             ###############################################################################
4             #
5             # SharedStrings - A class for writing the Excel XLSX sharedStrings 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   17641 use 5.008002;
  1040         5152  
17 1040     1040   6849 use strict;
  1040         3376  
  1040         22647  
18 1040     1040   6132 use warnings;
  1040         3350  
  1040         24454  
19 1040     1040   6429 use Carp;
  1040         2271  
  1040         58375  
20 1040     1040   9531 use Encode;
  1040         2461  
  1040         101901  
21 1040     1040   8569 use Excel::Writer::XLSX::Package::XMLwriter;
  1040         3584  
  1040         655851  
22              
23             our @ISA = qw(Excel::Writer::XLSX::Package::XMLwriter);
24             our $VERSION = '1.03';
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 814     814 0 6806 my $class = shift;
43 814         1966 my $fh = shift;
44 814         3877 my $self = Excel::Writer::XLSX::Package::XMLwriter->new( $fh );
45              
46 814         3403 $self->{_strings} = [];
47 814         2535 $self->{_string_count} = 0;
48 814         2304 $self->{_unique_count} = 0;
49              
50 814         2281 bless $self, $class;
51              
52 814         2882 return $self;
53             }
54              
55              
56             ###############################################################################
57             #
58             # _assemble_xml_file()
59             #
60             # Assemble and write the XML file.
61             #
62             sub _assemble_xml_file {
63              
64 236     236   693 my $self = shift;
65              
66 236         2427 $self->xml_declaration;
67              
68             # Write the sst table.
69 236         1683 $self->_write_sst( $self->{_string_count}, $self->{_unique_count} );
70              
71             # Write the sst strings.
72 236         1203 $self->_write_sst_strings();
73              
74             # Close the sst tag.
75 236         1690 $self->xml_end_tag( 'sst' );
76              
77             # Close the XML writer filehandle.
78 236         2374 $self->xml_get_fh()->close();
79             }
80              
81              
82             ###############################################################################
83             #
84             # _set_string_count()
85             #
86             # Set the total sst string count.
87             #
88             sub _set_string_count {
89              
90 236     236   719 my $self = shift;
91              
92 236         2319 $self->{_string_count} = shift;
93             }
94              
95              
96             ###############################################################################
97             #
98             # _set_unique_count()
99             #
100             # Set the total of unique sst strings.
101             #
102             sub _set_unique_count {
103              
104 236     236   684 my $self = shift;
105              
106 236         818 $self->{_unique_count} = shift;
107             }
108              
109              
110             ###############################################################################
111             #
112             # _add_strings()
113             #
114             # Add the array ref of strings to be written.
115             #
116             sub _add_strings {
117              
118 236     236   572 my $self = shift;
119              
120 236         745 $self->{_strings} = shift;
121             }
122              
123              
124             ###############################################################################
125             #
126             # Internal methods.
127             #
128             ###############################################################################
129              
130              
131             ###############################################################################
132             #
133             # XML writing methods.
134             #
135             ###############################################################################
136              
137              
138             ##############################################################################
139             #
140             # _write_sst()
141             #
142             # Write the element.
143             #
144             sub _write_sst {
145              
146 237     237   848 my $self = shift;
147 237         588 my $count = shift;
148 237         771 my $unique_count = shift;
149 237         649 my $schema = 'http://schemas.openxmlformats.org';
150 237         1201 my $xmlns = $schema . '/spreadsheetml/2006/main';
151              
152 237         1165 my @attributes = (
153             'xmlns' => $xmlns,
154             'count' => $count,
155             'uniqueCount' => $unique_count,
156             );
157              
158 237         1688 $self->xml_start_tag( 'sst', @attributes );
159             }
160              
161              
162             ###############################################################################
163             #
164             # _write_sst_strings()
165             #
166             # Write the sst string elements.
167             #
168             sub _write_sst_strings {
169              
170 236     236   639 my $self = shift;
171              
172 236         535 for my $string ( @{ $self->{_strings} } ) {
  236         1077  
173 1057         2755 $self->_write_si( $string );
174             }
175             }
176              
177              
178             ##############################################################################
179             #
180             # _write_si()
181             #
182             # Write the element.
183             #
184             sub _write_si {
185              
186 1058     1058   1953 my $self = shift;
187 1058         1883 my $string = shift;
188 1058         2038 my @attributes = ();
189              
190             # Excel escapes control characters with _xHHHH_ and also escapes any
191             # literal strings of that type by encoding the leading underscore. So
192             # "\0" -> _x0000_ and "_x0000_" -> _x005F_x0000_.
193             # The following substitutions deal with those cases.
194              
195             # Escape the escape.
196 1058         2072 $string =~ s/(_x[0-9a-fA-F]{4}_)/_x005F$1/g;
197              
198             # Convert control character to the _xHHHH_ escape.
199 1058         2463 $string =~ s/([\x00-\x08\x0B-\x1F])/sprintf "_x%04X_", ord($1)/eg;
  30         101  
200              
201              
202             # Add attribute to preserve leading or trailing whitespace.
203 1058 100 100     5916 if ( $string =~ /^\s/ || $string =~ /\s$/ ) {
204 6         18 push @attributes, ( 'xml:space' => 'preserve' );
205             }
206              
207              
208             # Write any rich strings without further tags.
209 1058 100 66     4128 if ( $string =~ m{^} && $string =~ m{$} ) {
210              
211             # Prevent utf8 strings from getting double encoded.
212 17         113 $string = decode_utf8( $string );
213              
214 17         1097 $self->xml_rich_si_element( $string );
215             }
216             else {
217 1041         4017 $self->xml_si_element( $string, @attributes );
218             }
219              
220             }
221              
222              
223             1;
224              
225              
226             __END__