home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2004 December / PCpro_2004_12.ISO / files / webserver / tsw / TSW_3.4.0.exe / Apache2 / perl / LibXSLT.pm < prev    next >
Encoding:
Perl POD Document  |  2003-02-12  |  10.1 KB  |  337 lines

  1. # $Id: LibXSLT.pm,v 1.46 2003/02/12 16:48:02 matt Exp $
  2.  
  3. package XML::LibXSLT;
  4.  
  5. use strict;
  6. use vars qw($VERSION @ISA $USE_LIBXML_DATA_TYPES);
  7.  
  8. use XML::LibXML 1.49;
  9. use XML::LibXML::Literal;
  10. use XML::LibXML::Boolean;
  11. use XML::LibXML::Number;
  12. use XML::LibXML::NodeList;
  13.  
  14. require Exporter;
  15.  
  16. $VERSION = "1.53";
  17.  
  18. require DynaLoader;
  19.  
  20. @ISA = qw(DynaLoader);
  21.  
  22. bootstrap XML::LibXSLT $VERSION;
  23.  
  24. $USE_LIBXML_DATA_TYPES = 0;
  25.  
  26. sub new {
  27.     my $class = shift;
  28.     my %options = @_;
  29.     my $self = bless \%options, $class;
  30.     return $self;
  31. }
  32.  
  33. # ido - perl dispatcher
  34. sub perl_dispatcher {
  35.     my $func = shift;
  36.     my @params = @_;
  37.     my @perlParams;
  38.     
  39.     my $i = 0;
  40.     while (@params) {
  41.         my $type = shift(@params);
  42.         if ($type eq 'XML::LibXML::Literal' or 
  43.             $type eq 'XML::LibXML::Number' or
  44.             $type eq 'XML::LibXML::Boolean')
  45.         {
  46.             my $val = shift(@params);
  47.             unshift(@perlParams, $USE_LIBXML_DATA_TYPES ? $type->new($val) : $val);
  48.         }
  49.         elsif ($type eq 'XML::LibXML::NodeList') {
  50.             my $node_count = shift(@params);
  51.             my @nodes = splice(@params, 0, $node_count);
  52.             # warn($_->getName) for @nodes;
  53.             unshift(@perlParams, $type->new(@nodes));
  54.         }
  55.     }
  56.     
  57.     $func = "main::$func" unless ref($func) || $func =~ /(.+)::/;
  58.     no strict 'refs';
  59.     my $res = $func->(@perlParams);
  60.     return $res;
  61. }
  62.  
  63.  
  64. sub xpath_to_string {
  65.     my @results;
  66.     while (@_) {
  67.         my $value = shift(@_); $value = '' unless defined $value;
  68.         push @results, $value;
  69.         if (@results % 2) {
  70.             # key
  71.             $results[-1] =~ s/:/_/g; # XSLT doesn't like names with colons
  72.         }
  73.         else {
  74.             if ($value =~ s/'/', "'", '/g) {
  75.             $results[-1] = "concat('$value')";
  76.             }
  77.             else {
  78.                 $results[-1] = "'$results[-1]'";
  79.             }
  80.         }
  81.     }
  82.     return @results;
  83. }
  84.  
  85. sub callbacks {
  86.     die "callbacks() never worked and has been removed."
  87. }
  88.  
  89. sub match_callback {
  90.     die "match_callback never worked and has been removed.\nPlease set \$XML::LibXML::match_cb instead";
  91. }
  92.  
  93. sub open_callback {
  94.     die "open_callback never worked and has been removed.\nPlease set \$XML::LibXML::open_cb instead";
  95. }
  96.  
  97. sub read_callback {
  98.     die "read_callback never worked and has been removed.\nPlease set \$XML::LibXML::read_cb instead";
  99. }
  100.  
  101. sub close_callback {
  102.     die "close_callback never worked and has been removed.\nPlease set \$XML::LibXML::close_cb instead";
  103. }
  104.  
  105. sub parse_stylesheet {
  106.     my $self = shift;
  107.     if (!ref($self) || !$self->{XML_LIBXSLT_MATCH}) {
  108.         return $self->_parse_stylesheet(@_);
  109.     }
  110.     local $XML::LibXML::match_cb = $self->{XML_LIBXSLT_MATCH};
  111.     local $XML::LibXML::open_cb = $self->{XML_LIBXSLT_OPEN};
  112.     local $XML::LibXML::read_cb = $self->{XML_LIBXSLT_READ};
  113.     local $XML::LibXML::close_cb = $self->{XML_LIBXSLT_CLOSE};
  114.     $self->_parse_stylesheet(@_);
  115. }
  116.  
  117. sub parse_stylesheet_file {
  118.     my $self = shift;
  119.     if (!ref($self) || !$self->{XML_LIBXSLT_MATCH}) {
  120.         return $self->_parse_stylesheet_file(@_);
  121.     }
  122.     local $XML::LibXML::match_cb = $self->{XML_LIBXSLT_MATCH};
  123.     local $XML::LibXML::open_cb = $self->{XML_LIBXSLT_OPEN};
  124.     local $XML::LibXML::read_cb = $self->{XML_LIBXSLT_READ};
  125.     local $XML::LibXML::close_cb = $self->{XML_LIBXSLT_CLOSE};
  126.     $self->_parse_stylesheet_file(@_);
  127. }
  128.  
  129. sub register_xslt_module {
  130.     my $self = shift;
  131.     my $module = shift;
  132.     # Not implemented
  133. }
  134.  
  135. 1;
  136. __END__
  137.  
  138. =head1 NAME
  139.  
  140. XML::LibXSLT - Interface to the gnome libxslt library
  141.  
  142. =head1 SYNOPSIS
  143.  
  144.   use XML::LibXSLT;
  145.   use XML::LibXML;
  146.   
  147.   my $parser = XML::LibXML->new();
  148.   my $xslt = XML::LibXSLT->new();
  149.   
  150.   my $source = $parser->parse_file('foo.xml');
  151.   my $style_doc = $parser->parse_file('bar.xsl');
  152.   
  153.   my $stylesheet = $xslt->parse_stylesheet($style_doc);
  154.   
  155.   my $results = $stylesheet->transform($source);
  156.   
  157.   print $stylesheet->output_string($results);
  158.  
  159. =head1 DESCRIPTION
  160.  
  161. This module is an interface to the gnome project's libxslt. This is an
  162. extremely good XSLT engine, highly compliant and also very fast. I have
  163. tests showing this to be more than twice as fast as Sablotron.
  164.  
  165. =head1 OPTIONS
  166.  
  167. XML::LibXSLT has some global options. Note that these are probably not
  168. thread or even fork safe - so only set them once per process. Each one
  169. of these options can be called either as class methods, or as instance
  170. methods. However either way you call them, it still sets global options.
  171.  
  172. Each of the option methods returns its previous value, and can be called
  173. without a parameter to retrieve the current value.
  174.  
  175. =head2 max_depth
  176.  
  177.   XML::LibXSLT->max_depth(1000);
  178.  
  179. This option sets the maximum recursion depth for a stylesheet. See the
  180. very end of section 5.4 of the XSLT specification for more details on
  181. recursion and detecting it. If your stylesheet or XML file requires
  182. seriously deep recursion, this is the way to set it. Default value is
  183. 250.
  184.  
  185. =head2 debug_callback
  186.  
  187.   XML::LibXSLT->debug_callback($subref);
  188.  
  189. Sets a callback to be used for debug messages. If you don't set this,
  190. debug messages will be ignored.
  191.  
  192. =head2 register_function
  193.  
  194.   XML::LibXSLT->register_function($uri, $name, $subref);
  195.  
  196. Registers an XSLT extension function mapped to the given URI. For example:
  197.  
  198.   XML::LibXSLT->register_function("urn:foo", "bar",
  199.     sub { scalar localtime });
  200.  
  201. Will register a C<bar> function in the C<urn:foo> namespace (which you
  202. have to define in your XSLT using C<xmlns:...>) that will return the
  203. current date and time as a string:
  204.  
  205.   <xsl:stylesheet version="1.0"
  206.     xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  207.     xmlns:foo="urn:foo">
  208.   <xsl:template match="/">
  209.     The time is: <xsl:value-of select="foo:bar()"/>
  210.   </xsl:template>
  211.   </xsl:stylesheet>
  212.  
  213. Parameters can be in whatever format you like. If you pass in a nodelist
  214. it will be a XML::LibXML::NodeList object in your perl code, but ordinary
  215. values (strings, numbers and booleans) will be ordinary perl scalars. If
  216. you wish them to be C<XML::LibXML::Literal>, C<XML::LibXML::Number> and
  217. C<XML::LibXML::Number> values respectively then set the variable
  218. C<$XML::LibXSLT::USE_LIBXML_DATA_TYPES> to a true value. Return values can
  219. be a nodelist or a plain value - the code will just do the right thing.
  220. But only a single return value is supported (a list is not converted to
  221. a nodelist).
  222.  
  223. =head1 API
  224.  
  225. The following methods are available on the new XML::LibXSLT object:
  226.  
  227. =head2 parse_stylesheet($doc)
  228.  
  229. C<$doc> here is an XML::LibXML::Document object (see L<XML::LibXML>)
  230. representing an XSLT file. This method will return a 
  231. XML::LibXSLT::Stylesheet object, or undef on failure. If the XSLT is
  232. invalid, an exception will be thrown, so wrap the call to 
  233. parse_stylesheet in an eval{} block to trap this.
  234.  
  235. =head2 parse_stylesheet_file($filename)
  236.  
  237. Exactly the same as the above, but parses the given filename directly.
  238.  
  239. =head1 XML::LibXSLT::Stylesheet
  240.  
  241. The main API is on the stylesheet, though it is fairly minimal.
  242.  
  243. One of the main advantages of XML::LibXSLT is that you have a generic
  244. stylesheet object which you call the transform() method passing in a
  245. document to transform. This allows you to have multiple transformations
  246. happen with one stylesheet without requiring a reparse.
  247.  
  248. =head2 transform(doc, %params)
  249.  
  250.   my $results = $stylesheet->transform($doc, foo => "value);
  251.  
  252. Transforms the passed in XML::LibXML::Document object, and returns a
  253. new XML::LibXML::Document. Extra hash entries are used as parameters.
  254.  
  255. =head2 transform_file(filename, %params)
  256.  
  257.   my $results = $stylesheet->transform_file($filename, bar => "value");
  258.  
  259. =head2 output_string(result)
  260.  
  261. Returns a scalar that is the XSLT rendering of the XML::LibXML::Document
  262. object using the desired output format (specified in the xsl:output tag
  263. in the stylesheet). Note that you can also call $result->toString, but
  264. that will *always* output the document in XML format, and in UTF8, which
  265. may not be what you asked for in the xsl:output tag.
  266.  
  267. =head2 output_fh(result, fh)
  268.  
  269. Outputs the result to the filehandle given in C<$fh>.
  270.  
  271. =head2 output_file(result, filename)
  272.  
  273. Outputs the result to the file named in C<$filename>.
  274.  
  275. =head2 output_encoding
  276.  
  277. Returns the output encoding of the results. Defaults to "UTF-8".
  278.  
  279. =head2 media_type
  280.  
  281. Returns the output media_type of the results. Defaults to "text/html".
  282.  
  283. =head1 Parameters
  284.  
  285. LibXSLT expects parameters in XPath format. That is, if you wish to pass
  286. a string to the XSLT engine, you actually have to pass it as a quoted
  287. string:
  288.  
  289.   $stylesheet->transform($doc, param => "'string'");
  290.  
  291. Note the quotes within quotes there!
  292.  
  293. Obviously this isn't much fun, so you can make it easy on yourself:
  294.  
  295.   $stylesheet->transform($doc, XML::LibXSLT::xpath_to_string(
  296.         param => "string"
  297.         ));
  298.  
  299. The utility function does the right thing with respect to strings in XPath,
  300. including when you have quotes already embedded within your string.
  301.  
  302. =head1 BENCHMARK
  303.  
  304. Included in the distribution is a simple benchmark script, which has two
  305. drivers - one for LibXSLT and one for Sablotron. The benchmark requires
  306. the testcases files from the XSLTMark distribution which you can find
  307. at http://www.datapower.com/XSLTMark/
  308.  
  309. Put the testcases directory in the directory created by this distribution,
  310. and then run:
  311.  
  312.   perl benchmark.pl -h
  313.  
  314. to get a list of options.
  315.  
  316. The benchmark requires XML::XPath at the moment, but I hope to factor that
  317. out of the equation fairly soon. It also requires Time::HiRes, which I
  318. could be persuaded to factor out, replacing it with Benchmark.pm, but I
  319. haven't done so yet.
  320.  
  321. I would love to get drivers for XML::XSLT and XML::Transformiix, if you
  322. would like to contribute them. Also if you get this running on Win32, I'd
  323. love to get a driver for MSXSLT via OLE, to see what we can do against
  324. those Redmond boys!
  325.  
  326. =head1 AUTHOR
  327.  
  328. Matt Sergeant, matt@sergeant.org
  329.  
  330. Copyright 2001, AxKit.com Ltd. All rights reserved.
  331.  
  332. =head1 SEE ALSO
  333.  
  334. XML::LibXML
  335.  
  336. =cut
  337.