# $Id$

package Apache::AxKit::Language::Petal;

use strict;
use vars qw/@ISA $VERSION/;
use Petal;
use Apache;
use Apache::Request;
use Apache::AxKit::Language;
use Apache::AxKit::Provider;
use XML::Simple;

@ISA = 'Apache::AxKit::Language';

$VERSION = 1.0; # this fixes a CPAN.pm bug. Bah!

sub handler {
    my $class = shift;
    my ($r, $xml, $style, $last_in_chain) = @_;
    
    my $xmlstring;
    
    AxKit::Debug(7, "[Petal] getting the XML");
    
    if (my $dom = $r->pnotes('dom_tree')) {
        $xmlstring = $dom->toString;
        delete $r->pnotes()->{'dom_tree'};
    }
    else {
        $xmlstring = $r->pnotes('xml_string');
    }
    
    # Setup petal variables
    local $Petal::INPUT = 'XML';
    local $Petal::OUTPUT = $last_in_chain ? 'XHTML' : 'XML';
    local $Petal::DISK_CACHE = 1;
    local $Petal::MEMORY_CACHE = 1;
    
    AxKit::Debug(7, "[Petal] parsing stylesheet");
    my $stylesheet = Petal->new(file => ugh);
    
    AxKit::Debug(7, "[Petal] parsing input");
    my $hash = XMLin($xmlstring, forcearray => 1);
    
    AxKit::Debug(7, "[Petal] performing transformation");
    $r->print( $stylesheet->process($hash) );
    
    return Apache::Constants::OK;
}

1;

