Any-Renderer

 view release on metacpan or  search on metacpan

lib/Any/Renderer/XSLT.pm  view on Meta::CPAN

package Any::Renderer::XSLT;

# $Id: XSLT.pm,v 1.14 2006/09/04 12:15:53 johna Exp $

use strict;
use Cache::AgainstFile;
use XML::LibXSLT;
use XML::LibXML;
use Any::Renderer::XML;

# a global XML parser and cache 
use vars qw($VERSION $XMLParser $Cache $CacheMaxItems $CacheMaxAtime $CachePurgeInterval $CacheLastPurged);

#Package-level cache and settings
$Cache = undef; #Generated on first use
$CacheMaxItems = 1000;
$CacheMaxAtime = 6 * 60 * 60; # seconds (6 hours)
$CachePurgeInterval = 60 * 60 ; # seconds (1 hour)
$CacheLastPurged = undef;

$VERSION = sprintf"%d.%03d", q$Revision: 1.14 $ =~ /: (\d+)\.(\d+)/;

use constant FORMAT_NAME => "XSLT";

sub new
{
  my ( $class, $format, $options ) = @_;
  die("Invalid format $format") unless($format eq FORMAT_NAME);

  $Cache ||= _init_cache ();

  my $self = {
    'template_file' => $options->{'Template'} || $options->{'TemplateFilename'},
    'template_string' => $options->{'TemplateString'},
    'options' => $options || {},
    'xml_renderer' => new Any::Renderer::XML('XML',$options),
    'cache' => $options->{'NoCache'}? undef: $Cache,
  };

  die("You must specify either a template filename or string containing a template") unless($self->{template_file} || defined $self->{template_string});
  bless $self, $class;

  return $self;
}

sub render
{
  my ( $self, $data ) = @_;

  TRACE ( "Rendering XSLT" );
  DUMP ( $data );

  my $stylesheet = '';
  my $template_file = $self->{template_file};
  my $template_string = $self->{template_string};
  if ( $template_file )
  {
    if( $self->{cache} )
    {
      TRACE ( "Fetching XSLT from cache" );
      $stylesheet = $self->{cache}->get( $template_file );
      _purge_cache($self->{cache}) if(time - $CacheLastPurged > $CachePurgeInterval);      
    }
    else
    {
      TRACE ( "Compiling XSLT from filesystem" );    
      $stylesheet = _xslt_from_file ( $template_file );
    }
  }
  elsif ( $template_string )
  {

lib/Any/Renderer/XSLT.pm  view on Meta::CPAN

=item XSLT

=back

=head1 METHODS

=over 4

=item $r = new Any::Renderer::XSLT($format,\%options)

C<$format> must be C<XSLT>.
See L</OPTIONS> for a description of valid C<%options>.

=item $scalar = $r->render($data_structure)

The main method.

=item $bool = Any::Renderer::XSLT::requires_template($format)

True in this case.

=item $list_ref = Any::Renderer::XSLT::available_formats()

Just the one - C<XSLT>.

=back

=head1 OPTIONS

=over 4

=item XmlOptions

A hash reference to options passed to XML::Simple::XMLout to control the generation of the interstitial XML.
See L<XML::Simple> for a detailed description of all of the available options.

=item VariableName

Set the XML root element name in the interstitial XML. You can also achieve this by setting the
C<RootName> or C<rootname> options passed to XML::Simple in the C<XML> options
hash. This is a shortcut to make this renderer behave like some of the other
renderer backends.

=item Template (aka TemplateFilename)

Filename of XSL template.  Mandatory unless TemplateString is defined.

=item TemplateString

String containing XSL template.  Mandatory unless Template or TemplateFilename is defined.

=item NoCache

Disable in-memory caching of XSLTs loaded from the filesystem.

=back 

=head1 GLOBAL VARIABLES

The package-level template cache is created on demand the first time it's needed.
There are a few global variables which you can tune before it's created (i.e. before you create any objects):

=over 4

=item $Any::Renderer::XSLT::CacheMaxItems

Maximum number of template objects held in the cache.  Default is 1000.

=item $Any::Renderer::XSLT::CacheMaxAtime

Items older than this will be purged from the cache when the next purge() call happens.  In Seconds.  Default is 6 hours.

=item $Any::Renderer::XSLT::CachePurgeInterval

How often to purge the cache.  In Seconds.  Default is 1 hour.

=back

=head1 SEE ALSO

L<Any::Renderer::XML>, L<Any::Renderer>, L<Cache::AgainstFile>

=head1 VERSION

$Revision: 1.14 $ on $Date: 2006/09/04 12:15:53 $ by $Author: johna $

=head1 AUTHOR

Matt Wilson and John Alden <cpan _at_ bbc _dot_ co _dot_ uk>

=head1 COPYRIGHT

(c) BBC 2006. This program is free software; you can redistribute it and/or modify it under the GNU GPL.

See the file COPYING in this distribution, or http://www.gnu.org/licenses/gpl.txt

=cut



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