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 If you do not pass a filename, the last file used is assumed.
135 Returns the transformed string.
136 Check the error number in err to know if something went wrong.
137 In that case do_not_return_source did determine the return value.
142 my ( $self, $orgxml, $file ) = @_;
145 if( !$self->{xslt_hash} ) {
149 $self->_set_error; #clear error
151 my $retval= $self->{do_not_return_source}? undef: $orgxml;
153 #check if no string passed
154 if( !defined $orgxml ) {
155 $self->_set_error(7);
156 return; #always undef
159 #If no file passed, use the last file again
161 if( !$self->{last_xsltfile} ) {
162 $self->_set_error(1);
165 $file= $self->{last_xsltfile};
169 my $stsh= $self->{xslt_hash}->{$file} // $self->_load($file);
170 return $retval if $self->{err};
172 #parse input and transform
173 my $parser = XML::LibXML->new();
174 my $source= eval { $parser->parse_string($orgxml) };
176 $self->_set_error(5, $@);
180 my $result= $stsh->transform($source);
181 $stsh->output_as_chars($result);
184 $self->_set_error(6, $@);
187 $self->{last_xsltfile}= $file;
193 $xslt_engine->refresh;
194 $xslt_engine->refresh( $xsltfilename );
196 Pass a file for an individual refresh or no file to refresh all.
197 Refresh returns the number of items affected.
198 What we actually do, is just clear the internal cache for reloading next
199 time when transform is called.
200 The return value is mainly theoretical. Since this is supposed to work
201 always(...), there is no actual need to test it.
202 Note that refresh does also clear the error information.
207 my ( $self, $file )= @_;
209 return if !$self->{xslt_hash};
212 $rv= delete $self->{xslt_hash}->{$file}? 1: 0;
215 $rv= scalar keys %{ $self->{xslt_hash} };
216 $self->{xslt_hash}= {};
221 # ************** INTERNAL ROUTINES ********************************************
224 # Internal routine for initialization.
230 $self->{xslt_hash}={};
231 $self->{do_not_return_source}=0 unless exists $self->{do_not_return_source};
232 #by default we return source on a failing transformation
233 #but it could be passed at construction time already
238 # Internal routine for loading a new stylesheet.
241 my ($self, $file)= @_;
243 if( !$file || !-e $file ) {
244 $self->_set_error(2);
249 my $parser = XML::LibXML->new;
250 my $style_doc = eval { $parser->load_xml( location => $file ) };
252 $self->_set_error(3, $@);
257 my $xslt = XML::LibXSLT->new;
258 $self->{xslt_hash}->{$file} = eval { $xslt->parse_stylesheet($style_doc) };
260 $self->_set_error(4, $@);
261 delete $self->{xslt_hash}->{$file};
264 return $self->{xslt_hash}->{$file};
268 # Internal routine for handling error information.
271 my ($self, $errno, $addmsg)= @_;
273 if(!$errno) { #clear the error
275 $self->{errstr}= undef;
279 $self->{err}= $errno;
281 $self->{errstr}= "No XSLT file passed.";
284 $self->{errstr}= "XSLT file not found.";
287 $self->{errstr}= "Error while loading stylesheet xml:";
290 $self->{errstr}= "Error while parsing stylesheet:";
293 $self->{errstr}= "Error while parsing input:";
296 $self->{errstr}= "Error while transforming input:";
299 $self->{errstr}= "No string to transform.";
303 $self->{errstr}.= " $addmsg";
310 Marcel de Rooy, Rijksmuseum Netherlands