line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Template::Plugin::XSLT; |
2
|
1
|
|
|
1
|
|
876
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
37
|
|
3
|
1
|
|
|
1
|
|
5
|
use warnings; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
35
|
|
4
|
1
|
|
|
1
|
|
14
|
use base 'Template::Plugin::Filter'; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
920
|
|
5
|
|
|
|
|
|
|
our $VERSION = '1.2'; |
6
|
1
|
|
|
1
|
|
2377
|
use XML::LibXSLT; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
use XML::LibXML; |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
sub init { |
10
|
|
|
|
|
|
|
my $self = shift; |
11
|
|
|
|
|
|
|
# TODO Consider checking file name here is inside standard Template |
12
|
|
|
|
|
|
|
# Directories (for security). |
13
|
|
|
|
|
|
|
my $file = $self->{ _ARGS }->[0] |
14
|
|
|
|
|
|
|
or return $self->error('No filename specified!'); |
15
|
|
|
|
|
|
|
$self->{ _DYNAMIC } = 1; |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
$self->{ parser } = XML::LibXML->new(); |
18
|
|
|
|
|
|
|
$self->{ XSLT } = XML::LibXSLT->new(); |
19
|
|
|
|
|
|
|
my $xml; |
20
|
|
|
|
|
|
|
eval { |
21
|
|
|
|
|
|
|
$xml = $self->{ parser }->parse_file($file); |
22
|
|
|
|
|
|
|
}; |
23
|
|
|
|
|
|
|
return $self->error("Stylesheet parsing error: $@") if $@; |
24
|
|
|
|
|
|
|
return $self->error("Stylesheet parsing errored") unless $xml; |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
eval { |
27
|
|
|
|
|
|
|
$self->{ stylesheet } = |
28
|
|
|
|
|
|
|
$self->{ XSLT }->parse_stylesheet( $xml ); |
29
|
|
|
|
|
|
|
}; |
30
|
|
|
|
|
|
|
return $self->error("Stylesheet not valid XSL: $@") if $@; |
31
|
|
|
|
|
|
|
return $self->error("Stylesheet parsing errored") unless $self->{stylesheet}; |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
return $self; |
34
|
|
|
|
|
|
|
} |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
sub filter { |
37
|
|
|
|
|
|
|
my ($self, $text, $args, $conf) = @_; |
38
|
|
|
|
|
|
|
my $xml; |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
$conf = $self->merge_config($conf); |
41
|
|
|
|
|
|
|
eval { |
42
|
|
|
|
|
|
|
$xml = $self->{ parser }->parse_string($text); |
43
|
|
|
|
|
|
|
}; |
44
|
|
|
|
|
|
|
return $self->error("XML parsing error: $@") if $@; |
45
|
|
|
|
|
|
|
return $self->error("XML parsing errored") unless $xml; |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
return $self->{ stylesheet}->output_string( |
48
|
|
|
|
|
|
|
$self->{ stylesheet }->transform( $xml, %{$conf || {}} ) |
49
|
|
|
|
|
|
|
); |
50
|
|
|
|
|
|
|
} |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
# Preloaded methods go here. |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
1; |
55
|
|
|
|
|
|
|
__END__ |