AxKit2
view release on metacpan or search on metacpan
lib/AxKit2/Transformer/XSLT.pm view on Meta::CPAN
use XML::LibXSLT;
use AxKit2::Constants;
use AxKit2::Utils qw(bytelength);
my $parser = XML::LibXML->new();
my $xslt = XML::LibXSLT->new();
sub new {
my $class = shift;
my $stylesheet = shift;
my @params = @_;
return bless { stylesheet => $stylesheet, params => \@params }, $class;
}
my %cache;
sub transform {
my $self = shift;
my ($pos, $processor) = @_;
my $dom = $processor->dom;
my $stylefile = $self->{stylesheet};
$self->log(LOGINFO, "Transformer::XSLT($stylefile) running");
my $stylesheet = $cache{$stylefile};
if (!$stylesheet) {
my $style_doc = $parser->parse_file($stylefile);
$stylesheet = $xslt->parse_stylesheet($style_doc);
$cache{$stylefile} = $stylesheet;
}
my $results = $stylesheet->transform($dom, fixup_params(@{ $self->{params} }));
return $results, sub { $self->output(@_) };
}
sub fixup_params {
my @results;
while (@_) {
push @results, XML::LibXSLT::xpath_to_string(
splice(@_, 0, 2)
);
}
return @results;
}
sub output {
my ($self, $client, $dom) = @_;
my $stylesheet = $cache{$self->{stylesheet}} || die "Not processed";
my $ct = $stylesheet->media_type;
my $enc = $stylesheet->output_encoding;
my $out = $stylesheet->output_string($dom);
$client->headers_out->header('Content-Length', bytelength($out));
$client->headers_out->header('Content-Type', "$ct; charset=$enc");
$client->send_http_headers;
$client->write($out);
}
1;
( run in 3.030 seconds using v1.01-cache-2.11-cpan-39bf76dae61 )