1 package Koha::XSLT_Handler;
3 # Copyright 2014 Rijksmuseum
5 # This file is part of Koha.
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 3 of the License, or (at your option) any later
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License along
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 Koha::XSLT_Handler - Facilitate use of XSLT transformations
26 use Koha::XSLT_Handler;
27 my $xslt_engine = Koha::XSLT_Handler->new;
28 my $output = $xslt_engine->transform($xml, $xsltfilename);
29 my $err= $xslt_engine->err; # error number
30 my $errstr= $xslt_engine->errstr; # error message
31 $xslt_engine->refresh($xsltfilename);
35 A XSLT handler object on top of LibXML and LibXSLT, allowing you to
36 run XSLT stylesheets repeatedly without loading them again.
37 Errors occurring during loading, parsing or transforming are reported
38 via the err and errstr attributes.
39 Reloading XSLT files can be done with the refresh method.
45 Create handler object (via Class::Accessor)
49 Run transformation for specific string and stylesheet
53 Allow to reload stylesheets when transforming again
59 Error number (see list of ERROR CODES)
65 =head2 do_not_return_source
67 If true, transform returns undef on failure. By default, it returns the
68 original string passed. Errors are reported as described.
82 Error while loading stylesheet xml: [furter information]
86 Error while parsing stylesheet: [furter information]
90 Error while parsing input: [furter information]
94 Error while transforming input: [furter information]
98 No string to transform
102 For documentation purposes. You are not encouraged to access them.
106 Contains the last successfully executed XSLT filename
110 Hash reference to loaded stylesheets
112 =head1 ADDITIONAL COMMENTS
120 use base qw(Class::Accessor);
122 __PACKAGE__->mk_ro_accessors(qw( err errstr ));
123 __PACKAGE__->mk_accessors(qw( do_not_return_source ));
127 my $output= $xslt_engine->transform( $xml, $xsltfilename );
128 if( $xslt_engine->err ) {
129 #decide what to do on failure..
131 my $output2= $xslt_engine->transform( $xml2 );
133 Pass a xml string and a fully qualified path of a XSLT file.
134 Instead of a filename, you may also pass a URL.
135 If you do not pass a filename, the last file used is assumed.
136 Returns the transformed string.
137 Check the error number in err to know if something went wrong.
138 In that case do_not_return_source did determine the return value.
143 my ( $self, $orgxml, $file ) = @_;
146 if( !$self->{xslt_hash} ) {
150 $self->_set_error; #clear error
152 my $retval= $self->{do_not_return_source}? undef: $orgxml;
154 #check if no string passed
155 if( !defined $orgxml ) {
156 $self->_set_error(7);
157 return; #always undef
160 #If no file passed, use the last file again
162 if( !$self->{last_xsltfile} ) {
163 $self->_set_error(1);
166 $file= $self->{last_xsltfile};
170 my $stsh= $self->{xslt_hash}->{$file} // $self->_load($file);
171 return $retval if $self->{err};
173 #parse input and transform
174 my $parser = XML::LibXML->new();
175 my $source= eval { $parser->parse_string($orgxml) };
177 $self->_set_error(5, $@);
181 my $result= $stsh->transform($source);
182 $stsh->output_as_chars($result);
185 $self->_set_error(6, $@);
188 $self->{last_xsltfile}= $file;
194 $xslt_engine->refresh;
195 $xslt_engine->refresh( $xsltfilename );
197 Pass a file for an individual refresh or no file to refresh all.
198 Refresh returns the number of items affected.
199 What we actually do, is just clear the internal cache for reloading next
200 time when transform is called.
201 The return value is mainly theoretical. Since this is supposed to work
202 always(...), there is no actual need to test it.
203 Note that refresh does also clear the error information.
208 my ( $self, $file )= @_;
210 return if !$self->{xslt_hash};
213 $rv= delete $self->{xslt_hash}->{$file}? 1: 0;
216 $rv= scalar keys %{ $self->{xslt_hash} };
217 $self->{xslt_hash}= {};
222 # ************** INTERNAL ROUTINES ********************************************
225 # Internal routine for initialization.
231 $self->{xslt_hash}={};
232 $self->{do_not_return_source}=0 unless exists $self->{do_not_return_source};
233 #by default we return source on a failing transformation
234 #but it could be passed at construction time already
239 # Internal routine for loading a new stylesheet.
242 my ($self, $file)= @_;
244 if( !$file || ( $file!~ /^https?:\/\// && !-e $file ) ) {
245 $self->_set_error(2);
250 my $parser = XML::LibXML->new;
251 my $style_doc = eval { $parser->load_xml( location => $file ) };
253 $self->_set_error(3, $@);
258 my $xslt = XML::LibXSLT->new;
259 $self->{xslt_hash}->{$file} = eval { $xslt->parse_stylesheet($style_doc) };
261 $self->_set_error(4, $@);
262 delete $self->{xslt_hash}->{$file};
265 return $self->{xslt_hash}->{$file};
269 # Internal routine for handling error information.
272 my ($self, $errno, $addmsg)= @_;
274 if(!$errno) { #clear the error
276 $self->{errstr}= undef;
280 $self->{err}= $errno;
282 $self->{errstr}= "No XSLT file passed.";
285 $self->{errstr}= "XSLT file not found.";
288 $self->{errstr}= "Error while loading stylesheet xml:";
291 $self->{errstr}= "Error while parsing stylesheet:";
294 $self->{errstr}= "Error while parsing input:";
297 $self->{errstr}= "Error while transforming input:";
300 $self->{errstr}= "No string to transform.";
304 $self->{errstr}.= " $addmsg";
311 Marcel de Rooy, Rijksmuseum Netherlands