AxKit2

 view release on metacpan or  search on metacpan

lib/AxKit2/Transformer/TAL.pm  view on Meta::CPAN


use AxKit2::Utils qw(bytelength);

my $parser  = XML::LibXML->new();
my $xslt    = XML::LibXSLT->new();
my $data    = do { local $/; <DATA> };

my %cache;
sub transform {
    my $self = shift;
    my ($pos, $processor) = @_;
    
    my $dom = $processor->dom;
    
    my $stylefile = $self->{stylesheet};
    
    my $stylesheet = $cache{$stylefile};
    if (!$stylesheet) {
        my $style_doc = $parser->parse_file($stylefile);
        my $xslparams = $self->mk_params();
        my $tal_style = $data;
        $tal_style    =~ s/!TALPARAMS!/$xslparams/;
        # print "Parsing: $tal_style\n";
        my $tal2xsl   = $xslt->parse_stylesheet($parser->parse_string($tal_style));
        my $xslt_dom  = $tal2xsl->transform($style_doc);
        $stylesheet   = $xslt->parse_stylesheet($xslt_dom);
        
        $cache{$stylefile} = $stylesheet;
    }
    
    my $results = $stylesheet->transform($dom, AxKit2::Transformer::XSLT::fixup_params(@{ $self->{params} }));
    
    return $results, sub { $self->output(@_) };
}

sub mk_params {
    my $self = shift;
    my %params = @{$self->{params}};
    my $output = '';
    for (keys %params) {
        $output .= "<xsl:param name='$_'/>\n";
    }
    return $output;
}

sub output {
    my ($self, $client, $dom) = @_;
    
    my ($out, $ct);
    if (lc($dom->documentElement->nodeName) eq 'html') {
        $out = $dom->toStringHTML();
        $ct  = "text/html";
    }
    else {
        $out = $dom->toStringHTML();
        $ct  = "application/xml";
    }
    my $enc = "UTF-8";
    
    $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;

__DATA__
<?xml version="1.0"?>

<!--
  Copyright 2004-2005 Bitflux GmbH

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
  
  $Id: popoon.php 4323 2005-05-25 17:45:38Z chregu 

-->

<xsl:stylesheet version="1.0"  
    xmlns:metal="http://xml.zope.org/namespaces/metal" 
    xmlns:bxf="http://bitflux.org/functions" 
    xmlns:tal="http://xml.zope.org/namespaces/tal" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:xslout="whatever" 
    xmlns:func="http://exslt.org/functions" 
    extension-element-prefixes="func">

    <xsl:namespace-alias stylesheet-prefix="xslout" result-prefix="xsl"/>
    <func:function name="bxf:tales">
        <xsl:param name="path"/>
        <xsl:choose>
            <xsl:when test="$path = ''">
                <func:result select="'node()'"/>
            </xsl:when>
            <xsl:otherwise>
                <func:result select="$path"/>
            </xsl:otherwise>
        </xsl:choose>
    </func:function>
    <xsl:template match="/">
        <xslout:stylesheet version="1.0" exclude-result-prefixes="bxf tal metal">
            <xsl:choose>
              <xsl:when test="local-name(/node()) = 'html'">
                <xslout:output encoding="utf-8" method="xml" doctype-public="-//W3C//DTD XHTML 1.0 Transitional//EN" doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"/>
              </xsl:when>
              <xsl:otherwise>
                <xslout:output encoding="utf-8" method="xml"/>
              </xsl:otherwise>
            </xsl:choose>
            !TALPARAMS!



( run in 1.338 second using v1.01-cache-2.11-cpan-39bf76dae61 )