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-2020, 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 1081     1081   18917 use 5.008002;
  1081         3511  
17 1081     1081   5315 use strict;
  1081         3283  
  1081         21981  
18 1081     1081   7151 use warnings;
  1081         4715  
  1081         23606  
19 1081     1081   6236 use Carp;
  1081         4612  
  1081         54372  
20 1081     1081   7863 use Encode;
  1081         3569  
  1081         97187  
21 1081     1081   8595 use Excel::Writer::XLSX::Package::XMLwriter;
  1081         3419  
  1081         618417  
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 854     854 0 6363 my $class = shift;
43 854         1842 my $fh = shift;
44 854         3859 my $self = Excel::Writer::XLSX::Package::XMLwriter->new( $fh );
45              
46 854         3105 $self->{_strings} = [];
47 854         2502 $self->{_string_count} = 0;
48 854         2230 $self->{_unique_count} = 0;
49              
50 854         2286 bless $self, $class;
51              
52 854         2634 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 239     239   661 my $self = shift;
65              
66 239         2211 $self->xml_declaration;
67              
68             # Write the sst table.
69 239         1651 $self->_write_sst( $self->{_string_count}, $self->{_unique_count} );
70              
71             # Write the sst strings.
72 239         1038 $self->_write_sst_strings();
73              
74             # Close the sst tag.
75 239         1811 $self->xml_end_tag( 'sst' );
76              
77             # Close the XML writer filehandle.
78 239         2256 $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 239     239   660 my $self = shift;
91              
92 239         2238 $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 239     239   618 my $self = shift;
105              
106 239         693 $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 239     239   511 my $self = shift;
119              
120 239         703 $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 240     240   726 my $self = shift;
147 240         661 my $count = shift;
148 240         512 my $unique_count = shift;
149 240         724 my $schema = 'http://schemas.openxmlformats.org';
150 240         1022 my $xmlns = $schema . '/spreadsheetml/2006/main';
151              
152 240         1240 my @attributes = (
153             'xmlns' => $xmlns,
154             'count' => $count,
155             'uniqueCount' => $unique_count,
156             );
157              
158 240         1654 $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 239     239   592 my $self = shift;
171              
172 239         530 for my $string ( @{ $self->{_strings} } ) {
  239         985  
173 1063         2745 $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 1064     1064   1845 my $self = shift;
187 1064         1759 my $string = shift;
188 1064         1762 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 1064         2062 $string =~ s/(_x[0-9a-fA-F]{4}_)/_x005F$1/g;
197              
198             # Convert control character to the _xHHHH_ escape.
199 1064         2314 $string =~ s/([\x00-\x08\x0B-\x1F])/sprintf "_x%04X_", ord($1)/eg;
  30         104  
200              
201              
202             # Add attribute to preserve leading or trailing whitespace.
203 1064 100 100     5469 if ( $string =~ /^\s/ || $string =~ /\s$/ ) {
204 6         13 push @attributes, ( 'xml:space' => 'preserve' );
205             }
206              
207              
208             # Write any rich strings without further tags.
209 1064 100 66     4894 if ( $string =~ m{^} && $string =~ m{$} ) {
210              
211             # Prevent utf8 strings from getting double encoded.
212 17         318 $string = decode_utf8( $string );
213              
214 17         224 $self->xml_rich_si_element( $string );
215             }
216             else {
217 1047         3952 $self->xml_si_element( $string, @attributes );
218             }
219              
220             }
221              
222              
223             1;
224              
225              
226             __END__