line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Templ::Spec::XML; |
2
|
1
|
|
|
1
|
|
6
|
use base 'Templ::Spec::Basic'; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
381
|
|
3
|
|
|
|
|
|
|
|
4
|
|
|
|
|
|
|
use Exporter; |
5
|
|
|
|
|
|
|
push @ISA, 'Exporter'; |
6
|
|
|
|
|
|
|
@EXPORT = qw(encode_xml encode_entities cdata); |
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
use strict; |
9
|
|
|
|
|
|
|
use warnings; |
10
|
|
|
|
|
|
|
use Templ (); # Loads all modules, but doesn't import anything |
11
|
|
|
|
|
|
|
use Carp qw(croak); |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
add Templ::Header 'use Templ::Spec::XML;'; |
14
|
|
|
|
|
|
|
add Templ::Tag::Filter '&' => 'encode_xml'; |
15
|
|
|
|
|
|
|
add Templ::Tag::Filter ';' => 'encode_entities'; |
16
|
|
|
|
|
|
|
add Templ::Tag::Filter '|' => 'cdata'; |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
my %entity_cache = ( |
19
|
|
|
|
|
|
|
'&' => '&', |
20
|
|
|
|
|
|
|
'"' => '"', |
21
|
|
|
|
|
|
|
"'" => ''', |
22
|
|
|
|
|
|
|
'<' => '<', |
23
|
|
|
|
|
|
|
'>' => '>', |
24
|
|
|
|
|
|
|
); |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
sub encode_xml { |
27
|
|
|
|
|
|
|
my $str = shift; |
28
|
|
|
|
|
|
|
return '' unless defined $str; |
29
|
|
|
|
|
|
|
$str =~ s/&/&/gs; |
30
|
|
|
|
|
|
|
$str =~ s/"/"/gs; |
31
|
|
|
|
|
|
|
$str =~ s/'/'/gs; |
32
|
|
|
|
|
|
|
$str =~ s/</gs; |
33
|
|
|
|
|
|
|
$str =~ s/>/>/gs; |
34
|
|
|
|
|
|
|
return $str; |
35
|
|
|
|
|
|
|
} |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
sub encode_entities { |
38
|
|
|
|
|
|
|
my $str = shift; |
39
|
|
|
|
|
|
|
return '' unless defined $str; |
40
|
|
|
|
|
|
|
$str =~ s<([^\ \!\#\$\%\x28-\x3B\=\x3F-\x7E])> |
41
|
|
|
|
|
|
|
< |
42
|
|
|
|
|
|
|
my $out = $entity_cache{$1}; |
43
|
|
|
|
|
|
|
unless (defined $out) { |
44
|
|
|
|
|
|
|
$out = sprintf '%X;', ord($1); |
45
|
|
|
|
|
|
|
$entity_cache{$1} = $out; |
46
|
|
|
|
|
|
|
} |
47
|
|
|
|
|
|
|
$out; |
48
|
|
|
|
|
|
|
>egsx; |
49
|
|
|
|
|
|
|
return $str; |
50
|
|
|
|
|
|
|
} |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
sub cdata { |
53
|
|
|
|
|
|
|
my $str = shift; |
54
|
|
|
|
|
|
|
croak "Cannot perform cdata insert: data contains string ']]>'" |
55
|
|
|
|
|
|
|
if $str =~ m/\]\]>/; |
56
|
|
|
|
|
|
|
return ""; |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
} |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
1; |