blib/lib/XML/Saxon/XSLT3.pm

filter: Statementstmt 88.2% 15 / 17 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 6 / 6 Podpod n/a totaltotal 91.3% 21 / 23 CC 1 SCAR 0.5
CC
1
Cov
88%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
↑ Summary
1 package XML::Saxon::XSLT3;
2  
3 1 ▶use 5.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use IO::Handle;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Scalar::Util qw[blessed];
Subroutine BEGIN: called CC=1 SCAR=0.0
9 0 ▶use XML::LibXML;
Subroutine BEGIN: called CC=1 SCAR=2.6
10 use Alien::Saxon;
11 require Inline;
12  
13 our $VERSION = '0.009';
14  
15 my $classpath = Alien::Saxon->jar;
16 sub import
17 {
18 my ($class, @args) = @_;
19 shift @args
20 if @args && exists $args[0] && defined $args[0] && $args[0] =~ /^[\d\.\_]{1,10}$/;
21 Inline->import(Java => 'DATA', CLASSPATH=>$classpath, @args);
22 }
23  
24 sub new
25 {
26 my ($class, $xslt, $baseurl) = @_;
27 $xslt = $class->_xml($xslt);
28
29 if ($baseurl)
30 {
31 return bless { 'transformer' => XML::Saxon::XSLT3::Transformer->new($xslt, $baseurl) }, $class;
32 }
33 else
34 {
35 return bless { 'transformer' => XML::Saxon::XSLT3::Transformer->new($xslt) }, $class;
36 }
37 }
38  
39 sub parameters
40 {
41 my ($self, %params) = @_;
42 $self->{'transformer'}->paramClear();
43 while (my ($k,$v) = each %params)
44 {
45 if (ref $v eq 'ARRAY')
46 {
47 (my $type, $v) = @$v;
48 my $func = 'paramAdd' . {
49 double => 'Double',
50 string => 'String',
51 long => 'Long',
52 'int' => 'Long',
53 integer => 'Long',
54 decimal => 'Decimal',
55 float => 'Float',
56 boolean => 'Boolean',
57 bool => 'Boolean',
58 qname => 'QName',
59 uri => 'URI',
60 date => 'Date',
61 datetime => 'DateTime',
62 }->{lc $type};
63 croak "$type is not a supported type"
64 if $func eq 'paramAdd';
65 $self->{'transformer'}->$func($k, $v);
66 }
67 elsif (blessed($v) && $v->isa('DateTime'))
68 {
69 $self->{'transformer'}->paramAddDateTime($k, "$v");
70 }
71 elsif (blessed($v) && $v->isa('Math::BigInt'))
72 {
73 $self->{'transformer'}->paramAddLong($k, $v->bstr);
74 }
75 elsif (blessed($v) && $v->isa('URI'))
76 {
77 $self->{'transformer'}->paramAddURI($k, "$v");
78 }
79 else
80 {
81 $self->{'transformer'}->paramAddString($k, "$v");
82 }
83 }
84 return $self;
85 }
86  
87 sub transform
88 {
89 my ($self, $doc, $type) = @_;
90 $type = ($type =~ /^(text|html|xhtml|xml)$/i) ? (lc $type) : 'default';
91 $doc = $self->_xml($doc);
92 return $self->{'transformer'}->transform($doc, $type);
93 }
94  
95 sub transform_document
96 {
97 my $self = shift;
98 my $r = $self->transform(@_);
99
100 $self->{'parser'} ||= XML::LibXML->new;
101 return $self->{'parser'}->parse_string($r);
102 }
103  
104 sub messages
105 {
106 my ($self) = @_;
107 return @{ $self->{'transformer'}->messages };
108 }
109  
110 sub media_type
111 {
112 my ($self, $default) = @_;
113 return $self->{'transformer'}->media_type || $default;
114 }
115  
116 sub doctype_public
117 {
118 my ($self, $default) = @_;
119 return $self->{'transformer'}->doctype_public || $default;
120 }
121  
122 sub doctype_system
123 {
124 my ($self, $default) = @_;
125 return $self->{'transformer'}->doctype_system || $default;
126 }
127  
128 sub version
129 {
130 my ($self, $default) = @_;
131 return $self->{'transformer'}->version || $default;
132 }
133  
134 sub encoding
135 {
136 my ($self, $default) = @_;
137 return $self->{'transformer'}->encoding || $default;
138 }
139  
140 sub _xml
141 {
142 my ($proto, $xml) = @_;
143
144 if (blessed($xml) && $xml->isa('XML::LibXML::Document'))
145 {
146 return $xml->toString;
147 }
148 elsif (blessed($xml) && $xml->isa('IO::Handle'))
149 {
150 local $/;
151 my $str = <$xml>;
152 return $str;
153 }
154 elsif (ref $xml eq 'GLOB')
155 {
156 local $/;
157 my $str = <$xml>;
158 return $str;
159 }
160
161 return $xml;
162 }
163  
164 1;
165  
166 =head1 NAME
167  
168 XML::Saxon::XSLT3 - process XSLT 3.0 using Saxon 9.x.
169  
170 =head1 SYNOPSIS
171  
172 use XML::Saxon::XSLT3;
173
174 # make sure to open filehandle in right encoding
175 open(my $input, '<:encoding(UTF-8)', 'path/to/xml') or die $!;
176 open(my $xslt, '<:encoding(UTF-8)', 'path/to/xslt') or die $!;
177
178 my $trans = XML::Saxon::XSLT3->new($xslt, $baseurl);
179 my $output = $trans->transform($input);
180 print $output;
181
182 my $output2 = $trans->transform_document($input);
183 my @paragraphs = $output2->getElementsByTagName('p');
184  
185 =head1 DESCRIPTION
186  
187 This module implements XSLT up to 3.0 using Saxon 9.x via L<Inline::Java>.
188  
189 It uses Saxon-HE for this, installed using L<Alien::Saxon>.
190  
191 =head2 Import
192  
193 use XML::Saxon::XSLT3;
194  
195 You can include additional parameters which will be passed straight on to
196 Inline::Java, like this:
197  
198 use XML::Saxon::XSLT3 EXTRA_JAVA_ARGS => '-Xmx256m';
199  
200 The C<import> function I<must> be called. If you load this module without
201 importing it, it will not work. (Don't worry, it won't pollute your namespace.)
202  
203 =head2 Constructor
204  
205 =over 4
206  
207 =item C<< XML::Saxon::XSLT3->new($xslt, [$baseurl]) >>
208  
209 Creates a new transformation. $xslt may be a string, a file handle or an
210 L<XML::LibXML::Document>. $baseurl is an optional base URL for resolving
211 relative URL references in, for instance, E<lt>xsl:importE<gt> links.
212 Otherwise, the current directory is assumed to be the base. (For base URIs
213 which are filesystem directories, remember to include the trailing slash.)
214  
215 =back
216  
217 =head2 Methods
218  
219 =over 4
220  
221 =item C<< $trans->parameters($key=>$value, $key2=>$value2, ...) >>
222  
223 Sets transformation parameters prior to running the transformation.
224  
225 Each key is a parameter name.
226  
227 Each value is the parameter value. This may be a scalar, in which case
228 it's treated as an xs:string; a L<DateTime> object, which is treated as
229 an xs:dateTime; a L<URI> object, xs:anyURI; a L<Math::BigInt>, xs:long;
230 or an arrayref where the first element is the type and the second the
231 value. For example:
232  
233 $trans->parameters(
234 now => DateTime->now,
235 madrid_is_capital_of_spain => [ boolean => 1 ],
236 price_of_fish => [ decimal => '1.99' ],
237 my_link => URI->new('http://example.com/'),
238 your_link => [ uri => 'http://example.net/' ],
239 );
240  
241 The following types are supported via the arrayref notation: float, double,
242 long (alias int, integer), decimal, bool (alias boolean), string, qname, uri,
243 date, datetime. These are case-insensitive.
244  
245 =item C<< $trans->transform($doc, [$output_method]) >>
246  
247 Run a transformation, returning the output as a string.
248  
249 $doc may be a string, a file handle or an L<XML::LibXML::Document>.
250  
251 $output_method may be 'xml', 'xhtml', 'html' or 'text' to override
252 the XSLT output method; or 'default' to use the output method specified
253 in the XSLT file. 'default' is the default. In the current release,
254 'default' is broken. :-(
255  
256 =item C<< $trans->transform_document($doc, [$output_method]) >>
257  
258 As per <transform>, but returns the output as an L<XML::LibXML::Document>.
259  
260 This method is slower than C<transform>.
261  
262 =item C<< $trans->messages >>
263  
264 Returns a list of string representations of messages output by
265 E<lt>xsl:messageE<gt> during the last transformation run.
266  
267 =item C<< $trans->media_type($default) >>
268  
269 Returns the output media type for the transformation.
270  
271 If the transformation doesn't specify an output type, returns the default.
272  
273 =item C<< $trans->doctype_public($default) >>
274  
275 Returns the output DOCTYPE public identifier for the transformation.
276  
277 If the transformation doesn't specify a doctype, returns the default.
278  
279 =item C<< $trans->doctype_system($default) >>
280  
281 Returns the output DOCTYPE system identifier for the transformation.
282  
283 If the transformation doesn't specify a doctype, returns the default.
284  
285 =item C<< $trans->version($default) >>
286  
287 Returns the output XML version for the transformation.
288  
289 If the transformation doesn't specify a version, returns the default.
290  
291 =item C<< $trans->encoding($default) >>
292  
293 Returns the output encoding for the transformation.
294  
295 If the transformation doesn't specify an encoding, returns the default.
296  
297 =back
298  
299 =head1 BUGS
300  
301 Please report any bugs to L<http://rt.cpan.org/>.
302  
303 =head1 SEE ALSO
304  
305 L<XML::LibXSLT> is probably more reliable in terms of easy installation on a
306 variety of platforms, and it allows you to define your own XSLT extension
307 functions. However, the libxslt library that it's based on only supports XSLT
308 1.0.
309  
310 This module uses L<Inline::Java>.
311  
312 L<http://saxon.sourceforge.net/>.
313  
314 =head1 AUTHOR
315  
316 Toby Inkster E<lt>tobyink@cpan.orgE<gt>.
317  
318 Updated to Saxon 9.8, to use L<Alien::Saxon>, and released as
319 C<XML::Saxon::XSLT3> by Ed J.
320  
321 =head1 COPYRIGHT
322  
323 Copyright 2010-2012 Toby Inkster
324  
325 This library is free software; you can redistribute it and/or modify it
326 under the same terms as Perl itself.
327  
328 =cut
329  
330 __DATA__
↑ Summary