# $Id: XSLT.pm,v 1.13 2000/06/02 18:27:35 matt Exp $

package Apache::AxKit::Language::XSLT;

use strict;
use XML::XSLT;
use Apache::AxKit::Language;

use vars qw/@ISA/;

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

sub handler {
	my $class = shift;
	my ($r, $xml, $style) = @_;

#	warn "Parsing stylefile '$stylefile'\n";
	my $parser = XML::XSLT->new($style->get_strref(), "STRING");

	if (my $dom_tree = $r->pnotes('dom_tree')) {
#		warn "Parsing dom_tree: ", $dom_tree->toString, "\n";
		$parser->transform_document($dom_tree, "DOM");
	}
	elsif (my $xmlstr = $r->notes('xml_string')) {
#		warn "Parsing string:\n$xml\n";
		$parser->transform_document($xmlstr, "STRING");
	}
	else {
#		warn "Parsing file '$xmlfile'\n";
		$parser->transform_document($xml->get_strref(), "STRING");
	}

	if (my $dom = $r->pnotes('dom_tree')) {
		$dom->dispose;
		delete $r->pnotes()->{'dom_tree'};
	}
	
	$r->pnotes('dom_tree', $parser->result_tree());
	
	# hack to not dispose our results tree
	delete $parser->{result};
	
	$parser->dispose();
}

1;
