| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  |  | 
| 2 |  |  |  |  |  |  | ############################################################################### | 
| 3 |  |  |  |  |  |  | # | 
| 4 |  |  |  |  |  |  | # SharedStrings - A class for writing the Excel XLSX sharedStrings file. | 
| 5 |  |  |  |  |  |  | # | 
| 6 |  |  |  |  |  |  | # Used in conjunction with Excel::Writer::XLSX | 
| 7 |  |  |  |  |  |  | # | 
| 8 |  |  |  |  |  |  | # Copyright 2000-2021, John McNamara, jmcnamara@cpan.org | 
| 9 |  |  |  |  |  |  | # | 
| 10 |  |  |  |  |  |  | # Documentation after __END__ | 
| 11 |  |  |  |  |  |  | # | 
| 12 |  |  |  |  |  |  |  | 
| 13 |  |  |  |  |  |  | # perltidy with the following options: -mbl=2 -pt=0 -nola | 
| 14 |  |  |  |  |  |  |  | 
| 15 |  |  |  |  |  |  | use 5.008002; | 
| 16 | 1124 |  |  | 1124 |  | 15289 | use strict; | 
|  | 1124 |  |  |  |  | 3188 |  | 
| 17 | 1124 |  |  | 1124 |  | 4830 | use warnings; | 
|  | 1124 |  |  |  |  | 1930 |  | 
|  | 1124 |  |  |  |  | 17299 |  | 
| 18 | 1124 |  |  | 1124 |  | 4481 | use Carp; | 
|  | 1124 |  |  |  |  | 1926 |  | 
|  | 1124 |  |  |  |  | 19997 |  | 
| 19 | 1124 |  |  | 1124 |  | 4676 | use Encode; | 
|  | 1124 |  |  |  |  | 2079 |  | 
|  | 1124 |  |  |  |  | 47648 |  | 
| 20 | 1124 |  |  | 1124 |  | 6173 | use Excel::Writer::XLSX::Package::XMLwriter; | 
|  | 1124 |  |  |  |  | 2137 |  | 
|  | 1124 |  |  |  |  | 77848 |  | 
| 21 | 1124 |  |  | 1124 |  | 6139 |  | 
|  | 1124 |  |  |  |  | 2131 |  | 
|  | 1124 |  |  |  |  | 559543 |  | 
| 22 |  |  |  |  |  |  | our @ISA     = qw(Excel::Writer::XLSX::Package::XMLwriter); | 
| 23 |  |  |  |  |  |  | our $VERSION = '1.09'; | 
| 24 |  |  |  |  |  |  |  | 
| 25 |  |  |  |  |  |  |  | 
| 26 |  |  |  |  |  |  | ############################################################################### | 
| 27 |  |  |  |  |  |  | # | 
| 28 |  |  |  |  |  |  | # Public and private API methods. | 
| 29 |  |  |  |  |  |  | # | 
| 30 |  |  |  |  |  |  | ############################################################################### | 
| 31 |  |  |  |  |  |  |  | 
| 32 |  |  |  |  |  |  |  | 
| 33 |  |  |  |  |  |  | ############################################################################### | 
| 34 |  |  |  |  |  |  | # | 
| 35 |  |  |  |  |  |  | # new() | 
| 36 |  |  |  |  |  |  | # | 
| 37 |  |  |  |  |  |  | # Constructor. | 
| 38 |  |  |  |  |  |  | # | 
| 39 |  |  |  |  |  |  |  | 
| 40 |  |  |  |  |  |  | my $class = shift; | 
| 41 |  |  |  |  |  |  | my $fh    = shift; | 
| 42 | 897 |  |  | 897 | 0 | 5369 | my $self  = Excel::Writer::XLSX::Package::XMLwriter->new( $fh ); | 
| 43 | 897 |  |  |  |  | 2062 |  | 
| 44 | 897 |  |  |  |  | 3708 | $self->{_strings}      = []; | 
| 45 |  |  |  |  |  |  | $self->{_string_count} = 0; | 
| 46 | 897 |  |  |  |  | 2812 | $self->{_unique_count} = 0; | 
| 47 | 897 |  |  |  |  | 2672 |  | 
| 48 | 897 |  |  |  |  | 2188 | bless $self, $class; | 
| 49 |  |  |  |  |  |  |  | 
| 50 | 897 |  |  |  |  | 2343 | return $self; | 
| 51 |  |  |  |  |  |  | } | 
| 52 | 897 |  |  |  |  | 2399 |  | 
| 53 |  |  |  |  |  |  |  | 
| 54 |  |  |  |  |  |  | ############################################################################### | 
| 55 |  |  |  |  |  |  | # | 
| 56 |  |  |  |  |  |  | # _assemble_xml_file() | 
| 57 |  |  |  |  |  |  | # | 
| 58 |  |  |  |  |  |  | # Assemble and write the XML file. | 
| 59 |  |  |  |  |  |  | # | 
| 60 |  |  |  |  |  |  |  | 
| 61 |  |  |  |  |  |  | my $self = shift; | 
| 62 |  |  |  |  |  |  |  | 
| 63 |  |  |  |  |  |  | $self->xml_declaration; | 
| 64 | 249 |  |  | 249 |  | 697 |  | 
| 65 |  |  |  |  |  |  | # Write the sst table. | 
| 66 | 249 |  |  |  |  | 2306 | $self->_write_sst( $self->{_string_count}, $self->{_unique_count} ); | 
| 67 |  |  |  |  |  |  |  | 
| 68 |  |  |  |  |  |  | # Write the sst strings. | 
| 69 | 249 |  |  |  |  | 1461 | $self->_write_sst_strings(); | 
| 70 |  |  |  |  |  |  |  | 
| 71 |  |  |  |  |  |  | # Close the sst tag. | 
| 72 | 249 |  |  |  |  | 998 | $self->xml_end_tag( 'sst' ); | 
| 73 |  |  |  |  |  |  |  | 
| 74 |  |  |  |  |  |  | # Close the XML writer filehandle. | 
| 75 | 249 |  |  |  |  | 1551 | $self->xml_get_fh()->close(); | 
| 76 |  |  |  |  |  |  | } | 
| 77 |  |  |  |  |  |  |  | 
| 78 | 249 |  |  |  |  | 2004 |  | 
| 79 |  |  |  |  |  |  | ############################################################################### | 
| 80 |  |  |  |  |  |  | # | 
| 81 |  |  |  |  |  |  | # _set_string_count() | 
| 82 |  |  |  |  |  |  | # | 
| 83 |  |  |  |  |  |  | # Set the total sst string count. | 
| 84 |  |  |  |  |  |  | # | 
| 85 |  |  |  |  |  |  |  | 
| 86 |  |  |  |  |  |  | my $self = shift; | 
| 87 |  |  |  |  |  |  |  | 
| 88 |  |  |  |  |  |  | $self->{_string_count} = shift; | 
| 89 |  |  |  |  |  |  | } | 
| 90 | 249 |  |  | 249 |  | 640 |  | 
| 91 |  |  |  |  |  |  |  | 
| 92 | 249 |  |  |  |  | 2079 | ############################################################################### | 
| 93 |  |  |  |  |  |  | # | 
| 94 |  |  |  |  |  |  | # _set_unique_count() | 
| 95 |  |  |  |  |  |  | # | 
| 96 |  |  |  |  |  |  | # Set the total of unique sst strings. | 
| 97 |  |  |  |  |  |  | # | 
| 98 |  |  |  |  |  |  |  | 
| 99 |  |  |  |  |  |  | my $self = shift; | 
| 100 |  |  |  |  |  |  |  | 
| 101 |  |  |  |  |  |  | $self->{_unique_count} = shift; | 
| 102 |  |  |  |  |  |  | } | 
| 103 |  |  |  |  |  |  |  | 
| 104 | 249 |  |  | 249 |  | 695 |  | 
| 105 |  |  |  |  |  |  | ############################################################################### | 
| 106 | 249 |  |  |  |  | 649 | # | 
| 107 |  |  |  |  |  |  | # _add_strings() | 
| 108 |  |  |  |  |  |  | # | 
| 109 |  |  |  |  |  |  | # Add the array ref of strings to be written. | 
| 110 |  |  |  |  |  |  | # | 
| 111 |  |  |  |  |  |  |  | 
| 112 |  |  |  |  |  |  | my $self = shift; | 
| 113 |  |  |  |  |  |  |  | 
| 114 |  |  |  |  |  |  | $self->{_strings} = shift; | 
| 115 |  |  |  |  |  |  | } | 
| 116 |  |  |  |  |  |  |  | 
| 117 |  |  |  |  |  |  |  | 
| 118 | 249 |  |  | 249 |  | 538 | ############################################################################### | 
| 119 |  |  |  |  |  |  | # | 
| 120 | 249 |  |  |  |  | 1049 | # Internal methods. | 
| 121 |  |  |  |  |  |  | # | 
| 122 |  |  |  |  |  |  | ############################################################################### | 
| 123 |  |  |  |  |  |  |  | 
| 124 |  |  |  |  |  |  |  | 
| 125 |  |  |  |  |  |  | ############################################################################### | 
| 126 |  |  |  |  |  |  | # | 
| 127 |  |  |  |  |  |  | # XML writing methods. | 
| 128 |  |  |  |  |  |  | # | 
| 129 |  |  |  |  |  |  | ############################################################################### | 
| 130 |  |  |  |  |  |  |  | 
| 131 |  |  |  |  |  |  |  | 
| 132 |  |  |  |  |  |  | ############################################################################## | 
| 133 |  |  |  |  |  |  | # | 
| 134 |  |  |  |  |  |  | # _write_sst() | 
| 135 |  |  |  |  |  |  | # | 
| 136 |  |  |  |  |  |  | # Write the <sst> element. | 
| 137 |  |  |  |  |  |  | # | 
| 138 |  |  |  |  |  |  |  | 
| 139 |  |  |  |  |  |  | my $self         = shift; | 
| 140 |  |  |  |  |  |  | my $count        = shift; | 
| 141 |  |  |  |  |  |  | my $unique_count = shift; | 
| 142 |  |  |  |  |  |  | my $schema       = 'http://schemas.openxmlformats.org'; | 
| 143 |  |  |  |  |  |  | my $xmlns        = $schema . '/spreadsheetml/2006/main'; | 
| 144 |  |  |  |  |  |  |  | 
| 145 |  |  |  |  |  |  | my @attributes = ( | 
| 146 | 250 |  |  | 250 |  | 573 | 'xmlns'       => $xmlns, | 
| 147 | 250 |  |  |  |  | 493 | 'count'       => $count, | 
| 148 | 250 |  |  |  |  | 500 | 'uniqueCount' => $unique_count, | 
| 149 | 250 |  |  |  |  | 702 | ); | 
| 150 | 250 |  |  |  |  | 957 |  | 
| 151 |  |  |  |  |  |  | $self->xml_start_tag( 'sst', @attributes ); | 
| 152 | 250 |  |  |  |  | 1021 | } | 
| 153 |  |  |  |  |  |  |  | 
| 154 |  |  |  |  |  |  |  | 
| 155 |  |  |  |  |  |  | ############################################################################### | 
| 156 |  |  |  |  |  |  | # | 
| 157 |  |  |  |  |  |  | # _write_sst_strings() | 
| 158 | 250 |  |  |  |  | 1489 | # | 
| 159 |  |  |  |  |  |  | # Write the sst string elements. | 
| 160 |  |  |  |  |  |  | # | 
| 161 |  |  |  |  |  |  |  | 
| 162 |  |  |  |  |  |  | my $self = shift; | 
| 163 |  |  |  |  |  |  |  | 
| 164 |  |  |  |  |  |  | for my $string ( @{ $self->{_strings} } ) { | 
| 165 |  |  |  |  |  |  | $self->_write_si( $string ); | 
| 166 |  |  |  |  |  |  | } | 
| 167 |  |  |  |  |  |  | } | 
| 168 |  |  |  |  |  |  |  | 
| 169 |  |  |  |  |  |  |  | 
| 170 | 249 |  |  | 249 |  | 554 | ############################################################################## | 
| 171 |  |  |  |  |  |  | # | 
| 172 | 249 |  |  |  |  | 540 | # _write_si() | 
|  | 249 |  |  |  |  | 981 |  | 
| 173 | 1075 |  |  |  |  | 2578 | # | 
| 174 |  |  |  |  |  |  | # Write the <si> element. | 
| 175 |  |  |  |  |  |  | # | 
| 176 |  |  |  |  |  |  |  | 
| 177 |  |  |  |  |  |  | my $self       = shift; | 
| 178 |  |  |  |  |  |  | my $string     = shift; | 
| 179 |  |  |  |  |  |  | my @attributes = (); | 
| 180 |  |  |  |  |  |  |  | 
| 181 |  |  |  |  |  |  | # Excel escapes control characters with _xHHHH_ and also escapes any | 
| 182 |  |  |  |  |  |  | # literal strings of that type by encoding the leading underscore. So | 
| 183 |  |  |  |  |  |  | # "\0" -> _x0000_ and "_x0000_" -> _x005F_x0000_. | 
| 184 |  |  |  |  |  |  | # The following substitutions deal with those cases. | 
| 185 |  |  |  |  |  |  |  | 
| 186 | 1076 |  |  | 1076 |  | 1507 | # Escape the escape. | 
| 187 | 1076 |  |  |  |  | 1635 | $string =~ s/(_x[0-9a-fA-F]{4}_)/_x005F$1/g; | 
| 188 | 1076 |  |  |  |  | 1703 |  | 
| 189 |  |  |  |  |  |  | # Convert control character to the _xHHHH_ escape. | 
| 190 |  |  |  |  |  |  | $string =~ s/([\x00-\x08\x0B-\x1F])/sprintf "_x%04X_", ord($1)/eg; | 
| 191 |  |  |  |  |  |  |  | 
| 192 |  |  |  |  |  |  |  | 
| 193 |  |  |  |  |  |  | # Add attribute to preserve leading or trailing whitespace. | 
| 194 |  |  |  |  |  |  | if ( $string =~ /^\s/ || $string =~ /\s$/ ) { | 
| 195 |  |  |  |  |  |  | push @attributes, ( 'xml:space' => 'preserve' ); | 
| 196 | 1076 |  |  |  |  | 2022 | } | 
| 197 |  |  |  |  |  |  |  | 
| 198 |  |  |  |  |  |  |  | 
| 199 | 1076 |  |  |  |  | 1992 | # Write any rich strings without further tags. | 
|  | 30 |  |  |  |  | 73 |  | 
| 200 |  |  |  |  |  |  | if ( $string =~ m{^<r>} && $string =~ m{</r>$} ) { | 
| 201 |  |  |  |  |  |  |  | 
| 202 |  |  |  |  |  |  | # Prevent utf8 strings from getting double encoded. | 
| 203 | 1076 | 100 | 100 |  |  | 4902 | $string = decode_utf8( $string ); | 
| 204 | 6 |  |  |  |  | 13 |  | 
| 205 |  |  |  |  |  |  | $self->xml_rich_si_element( $string ); | 
| 206 |  |  |  |  |  |  | } | 
| 207 |  |  |  |  |  |  | else { | 
| 208 |  |  |  |  |  |  | $self->xml_si_element( $string, @attributes ); | 
| 209 | 1076 | 100 | 66 |  |  | 3102 | } | 
| 210 |  |  |  |  |  |  |  | 
| 211 |  |  |  |  |  |  | } | 
| 212 | 17 |  |  |  |  | 106 |  | 
| 213 |  |  |  |  |  |  |  | 
| 214 | 17 |  |  |  |  | 1021 | 1; | 
| 215 |  |  |  |  |  |  |  | 
| 216 |  |  |  |  |  |  |  | 
| 217 | 1059 |  |  |  |  | 3332 |  | 
| 218 |  |  |  |  |  |  | =pod | 
| 219 |  |  |  |  |  |  |  | 
| 220 |  |  |  |  |  |  | =head1 NAME | 
| 221 |  |  |  |  |  |  |  | 
| 222 |  |  |  |  |  |  | SharedStrings - A class for writing the Excel XLSX sharedStrings.xml file. | 
| 223 |  |  |  |  |  |  |  | 
| 224 |  |  |  |  |  |  | =head1 SYNOPSIS | 
| 225 |  |  |  |  |  |  |  | 
| 226 |  |  |  |  |  |  | See the documentation for L<Excel::Writer::XLSX>. | 
| 227 |  |  |  |  |  |  |  | 
| 228 |  |  |  |  |  |  | =head1 DESCRIPTION | 
| 229 |  |  |  |  |  |  |  | 
| 230 |  |  |  |  |  |  | This module is used in conjunction with L<Excel::Writer::XLSX>. | 
| 231 |  |  |  |  |  |  |  | 
| 232 |  |  |  |  |  |  | =head1 AUTHOR | 
| 233 |  |  |  |  |  |  |  | 
| 234 |  |  |  |  |  |  | John McNamara jmcnamara@cpan.org | 
| 235 |  |  |  |  |  |  |  | 
| 236 |  |  |  |  |  |  | =head1 COPYRIGHT | 
| 237 |  |  |  |  |  |  |  | 
| 238 |  |  |  |  |  |  | (c) MM-MMXXI, John McNamara. | 
| 239 |  |  |  |  |  |  |  | 
| 240 |  |  |  |  |  |  | All Rights Reserved. This module is free software. It may be used, redistributed and/or modified under the same terms as Perl itself. | 
| 241 |  |  |  |  |  |  |  | 
| 242 |  |  |  |  |  |  | =head1 LICENSE | 
| 243 |  |  |  |  |  |  |  | 
| 244 |  |  |  |  |  |  | Either the Perl Artistic Licence L<http://dev.perl.org/licenses/artistic.html> or the GPL L<http://www.opensource.org/licenses/gpl-license.php>. | 
| 245 |  |  |  |  |  |  |  | 
| 246 |  |  |  |  |  |  | =head1 DISCLAIMER OF WARRANTY | 
| 247 |  |  |  |  |  |  |  | 
| 248 |  |  |  |  |  |  | See the documentation for L<Excel::Writer::XLSX>. | 
| 249 |  |  |  |  |  |  |  | 
| 250 |  |  |  |  |  |  | =cut |