blib/lib/Magpie/Transformer/XSLT.pm untested

filter: Statementstmt 0.0% 0 / 175 Branchbran 0.0% 0 / 42 Conditioncond 0.0% 0 / 31 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 19 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 274 CC 22 SCAR 42.2
CC
22
Cov
0%
get_content
182.0
transform
42.0
_build_document_root
12.0
CRAP 68.0
1 package Magpie::Transformer::XSLT;
2 # ABSTRACT: XSLT Pipeline Transformer
3 $Magpie::Transformer::XSLT::VERSION = '1.141660';
4 use Moose;
5 extends 'Magpie::Transformer';
6 use Magpie::Constants;
7 use MooseX::Types::Path::Class;
8 use XML::LibXML;
9 use XML::LibXSLT;
10 use Try::Tiny;
11 use Scalar::Util qw(blessed);
12 use Cwd ();
13 use File::Spec ();
14 use URI ();
15 use Carp qw(cluck);
16  
17 __PACKAGE__->register_events( qw(get_content transform));
18  
19 sub load_queue { return qw( get_content transform ) }
20  
21 has stylesheet_file => (
22 is => 'rw',
23 isa => 'Path::Class::File',
24 init_arg => 'stylesheet',
25 writer => 'stylesheet',
26 coerce => 1,
27 required => 1,
28 );
29  
30 has content_dom => (
31 is => 'rw',
32 isa => 'XML::LibXML::Document',
33 );
34  
35 has xml_parser => (
36 is => 'ro',
37 isa => 'XML::LibXML',
38 lazy_build => 1,
39 );
40  
41 sub _build_xml_parser {
42 return XML::LibXML->new();
43 }
44  
45 has xslt_processor => (
46 is => 'ro',
47 isa => 'XML::LibXSLT',
48 lazy_build => 1,
49 );
50  
51 sub _build_xslt_processor {
52 return XML::LibXSLT->new();
53 }
54  
55 has document_root => (
56 is => 'ro',
57 isa => 'Str',
58 lazy_build => 1,
59 );
60  
61 sub _build_document_root {
62 my $self = shift;
63 my $docroot = undef;
64  
65 if ( $self->resource->can('has_root') && $self->resource->has_root ) {
66 $docroot = $self->resource->root;
67 }
68 elsif ( defined $self->request->env->{DOCUMENT_ROOT} ) {
69 $docroot = $self->request->env->{DOCUMENT_ROOT};
70 }
71 else {
72 $docroot = Cwd::getcwd;
73 }
74  
75 return Cwd::realpath($docroot);
76 }
77  
78 sub absolute_path {
79 my $self = shift;
80 my $path = shift;
81 my $docroot = $self->document_root;
82 unless ($path =~ m|^\Q$docroot\E|) {
83 $docroot .= '/' unless $docroot =~ m|/$| || $path =~ m|^/|;
84 $path = $docroot . $path;
85 }
86 return $path;
87 }
88  
89 our $WTF = 0;
90  
91 use Data::Printer;
92  
93 sub get_content {
94 my $self = shift;
95 my $ctxt = shift;
96 my $dom = undef;
97  
98 my $xml_parser = XML::LibXML->new( expand_xinclude => 1, huge => 1, debug => 1, recover => 1, no_xinclude_nodes => 1, no_basefix => 1 );
99  
100 my $docroot = $self->document_root;
101 my $resource = $self->resource;
102  
103 # we only want to touch URIs that may need munging to
104 # resolve to a document root.
105 my $match_cb = sub {
106 my $uri_string = shift;
107 my $uri = URI->new($uri_string, 'file');
108 my $scheme = $uri->scheme;
109  
110 if ($resource && (!defined($scheme) || $scheme eq 'file') && -f $uri->path) {
111 my @stat = stat($uri->path);
112 my $mtime = @stat ? $stat[9] : -1;
113 $resource->add_dependency($uri->path => { mtime => $mtime, size => $stat[7]});
114 }
115 # don't handle URI's supported by libxml
116 return 0 if $uri_string =~ /^(https?|ftp|file):/;
117 return 0 if $docroot && $uri_string =~ m|^\Q$docroot\E|;
118 return 1;
119 };
120  
121 my $open_cb = sub {
122 my $uri = shift || './';
123 unless ($uri =~ m|^\Q$docroot\E|) {
124 $resource->delete_dependency($uri);
125 }
126  
127 my $file_path = $self->absolute_path($uri);
128 my $fh = IO::File->new($file_path) || die "Error opening file $uri ($file_path)";
129 my @stat = stat($file_path);
130 my $mtime = @stat ? $stat[9] : -1;
131 # mtime + size, for Etags
132 $resource->add_dependency($file_path => { mtime => $mtime, size => $stat[7]});
133  
134 local $/ = undef;
135 my $data = <$fh>;
136 return \$data;
137 };
138  
139 my $read_cb = sub {
140 my $string_ref = shift;
141 my $length = shift;
142 return substr($$string_ref, 0, $length, "");
143 };
144  
145 my $icb = XML::LibXML::InputCallback->new();
146 $icb->register_callbacks( [ $match_cb, $open_cb, $read_cb, sub {} ] );
147 $xml_parser->input_callbacks($icb);
148  
149 my $upstream = $resource->data;
150 #warn "upstream " . p($upstream);
151 if ($upstream) {
152 if (ref $upstream) {
153 if (blessed($upstream)) {
154 if ($upstream->isa('Plack::Util::IOWithPath')) {
155 try {
156 $dom = $xml_parser->load_xml( IO => $upstream );
157 }
158 catch {
159 warn "Error loading XML I/O: $_\n";
160 $self->set_error({ status_code => 500, reason => $_ });
161 };
162 }
163 elsif ($upstream->isa('XML::LibXML::Document')) {
164 $dom = $upstream;
165 }
166 }
167 }
168 else {
169 try {
170 $dom = $xml_parser->load_xml( string => $upstream );
171 }
172 catch {
173 warn "Error loading XML string: $_\n";
174 $self->set_error({ status_code => 500, reason => $_ });
175 };
176  
177 }
178 }
179 else {
180 $dom = XML::LibXML::Document->new();
181 }
182  
183 return DECLINED if $self->has_error;
184  
185 $self->content_dom( $dom );
186 $WTF++;
187 return OK;
188 }
189  
190 sub transform {
191 my $self = shift;
192 my $ctxt = shift;
193  
194 my $style = undef;
195 my $xslt_processor = XML::LibXSLT->new;
196  
197 my $docroot = $self->document_root;
198  
199 # we only want to touch URIs that may need munging to
200 # resolve to a document root.
201 my $match_cb = sub {
202 my $uri = shift;
203 # don't handle URI's supported by libxml
204 return 0 if $uri =~ /^(https?|ftp|file):/;
205 return 1;
206 };
207  
208 my $open_cb = sub {
209 my $uri = shift || './';
210  
211 my $file_path = $self->absolute_path($uri);
212 #warn "stylesheet open $uri";
213 my $fh = IO::File->new($file_path) || die "Error opening file $uri ($file_path)";
214 my @stat = stat($file_path);
215 my $mtime = @stat ? $stat[9] : -1;
216 # mtime + size, for Etags
217 $self->resource->add_dependency($file_path => { mtime => $mtime, size => $stat[7]});
218 local $/ = undef;
219 my $data = <$fh>;
220 return \$data;
221 };
222  
223 my $read_cb = sub {
224 my $string_ref = shift;
225 my $length = shift;
226 return substr($$string_ref, 0, $length, "");
227 };
228  
229 my $icb = XML::LibXML::InputCallback->new();
230 $icb->register_callbacks( [ $match_cb, $open_cb, $read_cb, sub {} ] );
231 $xslt_processor->input_callbacks($icb);
232  
233 my $stylesheet_file = $self->stylesheet_file;
234 unless ($stylesheet_file =~ m|^\Q$docroot\E| || -f $stylesheet_file) {
235 $stylesheet_file = $self->absolute_path($stylesheet_file);
236 }
237  
238  
239 try {
240 $style = $xslt_processor->parse_stylesheet_file( $stylesheet_file );
241 }
242 catch {
243 warn "Error parsing stylesheet file: $_\n";
244 $self->set_error({ status_code => 500, reason => $_ });
245 };
246  
247 # remember that Try::Tiny won't return() the way you think it does
248 return OK if $self->has_error;
249  
250 my $params = $self->request->parameters || {};
251  
252 my $result = undef;
253  
254 try {
255 $result = $style->transform( $self->content_dom, XML::LibXSLT::xpath_to_string(%{$params}) );
256 }
257 catch {
258 warn "Error applying stylesheet: $_\n";
259 $self->set_error({ status_code => 500, reason => $_ });
260 };
261  
262 return OK if $self->has_error;
263  
264 my $new_body = $style->output_as_bytes( $result );
265 my $content_type = $style->media_type;
266 my $encoding = $style->output_encoding;
267 $self->response->content_type("$content_type; charset=$encoding");
268 $self->response->content_length( length($new_body) );
269 $self->resource->data( $new_body );
270  
271 return OK;
272 }
273  
274 # SEEALSO: Magpie, XML::LibXSLT
275  
276 1;
277  
278 __END__
279 =pod
280  
281 =head1 NAME
282  
283 Magpie::Transformer::XSLT - XSLT Pipeline Transformer
284  
285 =head1 VERSION
286  
287 version 1.141660
288  
289 =head1 AUTHORS
290  
291 =over 4
292  
293 =item *
294  
295 Kip Hampton <kip.hampton@tamarou.com>
296  
297 =item *
298  
299 Chris Prather <chris.prather@tamarou.com>
300  
301 =back
302  
303 =head1 COPYRIGHT AND LICENSE
304  
305 This software is copyright (c) 2011 by Tamarou, LLC.
306  
307 This is free software; you can redistribute it and/or modify it under
308 the same terms as the Perl 5 programming language system itself.
309  
310 =cut
311