Transform.pm

filter: Statementstmt 85.1% 120 / 141 Branchbran 53.8% 28 / 52 Conditioncond 60.0% 6 / 10 MC/DCmcdc 33.3% 2 / 6 Subroutinesub 92.5% 25 / 27 Podpod 100% 11 / 11 totaltotal 77.7% 192 / 247 CC 32 SCAR 19.7
CC
32
Cov
76%
decode_string
24.9
new
10.2
default_for
9.3
CRAP 7.2
↑ Summary Transform/using/HTML/Parser.pm »
1 package URL::Transform;
2  
3 =head1 NAME
4  
5 URL::Transform - perform URL transformations in various document types
6  
7 =head1 SYNOPSIS
8  
9 my $output;
10 my $urlt = URL::Transform->new(
11 'document_type' => 'text/html;charset=utf-8',
12 'content_encoding' => 'gzip',
13 'output_function' => sub { $output .= "@_" },
14 'transform_function' => sub { return (join '|', @_) },
15 );
16 $urlt->parse_file($Bin.'/data/URL-Transform-01.html');
17  
18 print "and this is the output: ", $output;
19  
20 =head1 DESCRIPTION
21  
22 URL::Transform is a generic module to perform an url transformation in
23 a documents. Accepts callback function using which the url link can be
24 changed.
25  
26 There are different modules to handle different document types, elements
27 or attributes:
28  
29 =over 4
30  
31 =item C<text/html>, C<text/vnd.wap.wml>, C<application/xhtml+xml>, C<application/vnd.wap.xhtml+xml>
32  
33 L<URL::Transform::using::HTML::Parser>,
34 L<URL::Transform::using::XML::SAX> (incomplete was used only to benchmark)
35  
36 =item C<text/css>
37  
38 L<URL::Transform::using::CSS::RegExp>
39  
40 =item C<text/html/meta-content>
41  
42 L<URL::Transform::using::HTML::Meta>
43  
44 =item C<application/x-javascript>
45  
46 L<URL::Transform::using::Remove>
47  
48 =back
49  
50 By passing C<parser> option to the C<< URL::Transform->new() >> constructor you
51 can set what library will be used to parse and execute the output and transform
52 functions. Note that the elements inside for example C<text/html> that are
53 of a different type will be transformed via L</default_for($document_type)> modules.
54  
55 C<transform_function> is called with following arguments:
56  
57 transform_function->(
58 'tag_name' => 'img',
59 'attribute_name' => 'src',
60 'url' => 'http://search.cpan.org/s/img/cpan_banner.png',
61 );
62  
63 and must return (un)modified url as the return value.
64  
65 C<output_function> is called with (already modified) document chunk for outputting.
66  
67 =cut
68  
69 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
70 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
71  
72 3 our $VERSION = '0.01';
73  
74 3 ▶use Carp::Clan;
Subroutine BEGIN: called CC=1 SCAR=0.0
75 3 ▶use English '$EVAL_ERROR';
Subroutine BEGIN: called CC=1 SCAR=0.0
76 3 ▶use HTML::Tagset;
Subroutine BEGIN: called CC=1 SCAR=0.0
77 3 ▶use Compress::Zlib;
Subroutine BEGIN: called CC=1 SCAR=0.0
78 3 ▶use File::Slurp 'read_file';
Subroutine BEGIN: called CC=1 SCAR=0.0
79  
80 3 ▶use base 'Class::Accessor::Fast';
Subroutine BEGIN: called CC=1 SCAR=0.0
81  
82 =head1 PROPERTIES
83  
84 content_encoding
85 document_type
86 parser
87 transform_function
88 output_function
89  
90 =over 4
91  
92 =item parser
93  
94 For HTML/XML can be HTML::Parser, XML::SAX
95  
96 =item document_type
97  
98 text/html - default
99  
100 =item transform_function
101  
102 Function that will be called to make the transformation. The function will receive
103 one argument - url text.
104  
105 =item output_function
106  
107 Reference to function that will receive resulting output. The default one is to use
108 print.
109  
110 =item content_encoding
111  
112 Can be set to C<gzip> or C<deflate>. By default it is C<undef>, so there is
113 no content encoding.
114  
115 =back
116  
117 =cut
118  
119 3 __PACKAGE__->mk_accessors(qw{
120 document_type
121 content_encoding
122 transform_function
123 output_function
124 parser
125 supported_document_types
126 });
127  
128 =head1 METHODS
129  
130 =head2 new
131  
132 Object constructor.
133  
134 Requires C<transform_function> a CODE ref argument.
135  
136 The rest of the arguments are optional. Here is the list with defaults:
137  
138 document_type => 'text/html;charset=utf-8',
139 output_function => sub { print @_ },
140 parser => 'HTML::Parser',
141 content_encoding => undef,
142  
143 =cut
144  
145 3 my %supported_document_types = (
146 'text/html' => 'HTML::Parser',
147 'text/vnd.wap.wml' => 'HTML::Parser',
148 'application/xhtml+xml' => 'HTML::Parser',
149 'application/vnd.wap.xhtml+xml' => 'HTML::Parser',
150 'text/css' => 'CSS::RegExp',
151 'text/html/meta-content' => 'HTML::Meta',
152 'application/x-javascript' => 'Remove',
153 );
154  
155 3 my %supported_content_encoding = (
156 'gzip' => 1,
157 'deflate' => 1,
158 );
159  
160 6 ▶sub new {
Subroutine new: called CC=10 SCAR=23.2
Pod documented
161 6 my $class = shift;
162 6 my $self = $class->SUPER::new({
163 'supported_document_types' => {},
164 @_
165 });
166  
167 6 ▶ croak 'pass transform_function'
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self->transform_function eq "CODE" 0 6 6
168 if not ref $self->transform_function eq 'CODE';
169  
170 # default document type
171 6 ▶ my ($document_type, $encoding) = split ';', $self->document_type || '';
Condition100%2/2
$self->document_type || ''
l!l
51
MC/DC100%1/1
$self->document_type || ''
$self->document_type
Truth table100%2/2
A
A= $self->document_type
Aresult
11
00
172 6 ▶ $document_type ||= 'text/html';
Condition100%2/2
$document_type ||= "text/html"
l!l
51
MC/DC100%1/1
$document_type ||= "text/html"
$document_type
Truth table100%2/2
A
A= $document_type
Aresult
11
00
173  
174 # default output function
175 0 ▶ $self->output_function(sub { print @_ })
Subroutine __ANON__: not called CC=1 SCAR=6.9
176 6 ▶ if not defined $self->output_function;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->output_function 0 6 6
177  
178 # setup thinks for parsing html documents
179 6 ▶ my $parser = $self->parser || $supported_document_types{$document_type};
Condition66%2/3
$self->parser || $supported_document_types{$document_type}
l!l&&r!l&&!r
150
MC/DC0%0/2
$self->parser || $supported_document_types{$document_type}
$self->parser $supported_document_types{$document_type}
Truth table66%2/3
A || B
A= $self->parserB= $supported_document_types{$document_type}
ABresult
1X1
011
000
180 6 ▶ croak 'unsupported document type: ', $document_type
Branch: 1 branch
BranchTrueFalseTotal
unless $parser 0 6 6
181 if not $parser;
182  
183 # check content_encoding
184 6 my $content_encoding = $self->content_encoding;
185 6 ▶ if ($content_encoding){
Branch: 1 branch
BranchTrueFalseTotal
if ($content_encoding) 1 5 6
186 croak 'unsupported content_encoding: ', $content_encoding
187 1 ▶ if (not $supported_content_encoding{$content_encoding});
Branch: 1 branch
BranchTrueFalseTotal
unless $supported_content_encoding{$content_encoding} 0 1 1
188 }
189  
190 # construct parser object
191 6 eval {
192 3 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
193 6 $parser = 'URL::Transform::using::'.$parser;
194 2 ▶ eval 'use '.$parser;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
195 $parser = $parser->new(
196 'output_function' => $self->output_function,
197 'transform_function' => $self->transform_function,
198 64 ▶ 'parser_for' => sub { $self->default_for(@_) },
Subroutine __ANON__: called CC=1 SCAR=0.0
199 6 );
200 };
201 6 ▶ croak 'error loading parser "'.$parser.'" - '.$EVAL_ERROR.' ' if $EVAL_ERROR;
Branch: 1 branch
BranchTrueFalseTotal
if $URL::Transform::EVAL_ERROR 0 6 6
202  
203 6 $self->parser($parser);
204  
205 6 return $self;
206 }
207  
208  
209 =head2 default_for($document_type)
210  
211 Returns default parser for a supplied $document_type.
212  
213 Can be used also as a set function with additional argument - parser name.
214  
215 If called as object method set the default parser for the object.
216 If called as module function set the default parser for a whole module.
217  
218 =cut
219  
220 64 ▶sub default_for {
Subroutine default_for: called CC=5 SCAR=22.3
Pod documented
221 64 my $self = shift;
222
223 # if called from object get/set object parsers for different content types
224 64 ▶ if (ref $self) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self) { } 64 0 64
225 64 my $document_type = shift;
226  
227 # if case of set
228 64 ▶ if (@_ > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 0) 0 64 64
229 0 $self->supported_document_types->{$document_type} = shift;
230 }
231
232 return exists($self->{supported_document_types}->{$document_type})
233 ? $self->{supported_document_types}->{$document_type}
234 64 ▶ : $supported_document_types{$document_type};
Branch: 1 branch
BranchTrueFalseTotal
exists $self->{'supported_document_types'}{$document_type} ? : 0 64 64
235 }
236 # if called directly get/set the module defaults
237 else {
238 0 my $document_type = $self;
239
240 # if case of set
241 0 ▶ if (@_ > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 0) 0 0 0
242 0 $supported_document_types{$document_type} = shift;
243 }
244
245 0 return $supported_document_types{$document_type};
246 }
247 }
248  
249  
250 =head2 parse_string($string)
251  
252 Submit document as a string for parsing.
253  
254 This some function must be implemented by helper parsing classes.
255  
256 =cut
257  
258 3 ▶sub parse_string {
Subroutine parse_string: called CC=1 SCAR=0.0
Pod documented
259 3 my $self = shift;
260 3 my $data = shift;
261  
262 3 return $self->parser->parse_string(
263 $self->decode_string($data)
264 );
265 }
266  
267  
268 =head2 parse_chunk($chunk)
269  
270 Submit chunk of a document for parsing.
271  
272 This some function should be implemented by helper parsing classes.
273  
274 =cut
275  
276 6 ▶sub parse_chunk {
Subroutine parse_chunk: called CC=2 SCAR=7.2
Pod documented
277 6 my $self = shift;
278 6 my $data = shift;
279  
280 6 my $parser = $self->parser;
281  
282 6 ▶ if ($self->can_parse_chunks) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->can_parse_chunks) { } 6 0 6
283 6 return $self->parser->parse_chunk($data);
284 }
285 else {
286 0 die $self->parser.' is not able to parse in chunks. :(';
287 }
288 }
289  
290  
291 =head2 can_parse_chunks
292  
293 Return true/false if the parser can parse in chunks.
294  
295 =cut
296  
297 7 ▶sub can_parse_chunks {
Subroutine can_parse_chunks: called CC=2 SCAR=7.4
Pod documented
298 7 my $self = shift;
299 7 my $parser = $self->parser;
300  
301 7 ▶ if ( defined $self->content_encoding ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->content_encoding) 0 7 7
302 0 return 0;
303 }
304  
305 7 return $parser->can('parse_chunk');
306 }
307  
308  
309 =head2 parse_file($file_name)
310  
311 Submit file for parsing.
312  
313 This some function should be implemented by helper parsing classes.
314  
315 =cut
316  
317 7 ▶sub parse_file {
Subroutine parse_file: called CC=2 SCAR=6.9
Pod documented
318 7 my $self = shift;
319  
320 # if the content is not encoded the call parser parse_file method
321 7 ▶ return $self->parser->parse_file(@_)
Branch: 1 branch
BranchTrueFalseTotal
unless $self->content_encoding 6 1 7
322 if not $self->content_encoding;
323  
324 # otherwise use parse_string that uses decode_string
325 1 return $self->parse_string(scalar read_file(@_))
326 }
327  
328  
329 =head2 link_tags
330  
331 # To simplify things, reformat the %HTML::Tagset::linkElements
332 # hash so that it is always a hash of hashes.
333  
334 # Construct a hash of tag names that may have links.
335  
336 =cut
337  
338 # FIXME should be moved outside or URL::Transform because it HTML specific
339 3 my $_link_tags;
340 4 ▶sub link_tags {
Subroutine link_tags: called CC=6 SCAR=17.9
Pod documented
341 # if it's already generate return the reference right away
342 4 ▶ return $_link_tags if defined $_link_tags;
Branch: 1 branch
BranchTrueFalseTotal
if defined $_link_tags 1 3 4
343  
344 3 my %link_tags;
345  
346 # meta can have a refresh url in the content, wml has some special tags
347 3 my %link_elements = (
348 %HTML::Tagset::linkElements,
349 'meta' => 'content',
350 'go' => 'href',
351 'card' => 'ontimer',
352 'option' => 'onpick',
353 );
354
355 # To simplify things, reformat the %HTML::Tagset::linkElements
356 # hash so that it is always a hash of hashes.
357 3 ▶ while (my($k,$v) = each %link_elements) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %link_elements) 96 3 99
358 96 ▶ if (ref($v)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $v) { } 84 12 96
359 84 $v = { map {$_ => 1} @$v };
360 }
361 else {
362 12 $v = { $v => 1};
363 }
364 96 $link_tags{$k} = $v;
365 }
366  
367 # attributes that match all tags
368 $link_tags{''} = {}
369 3 ▶ if not exists $link_tags{''};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $link_tags{""} 3 0 3
370  
371 # add tags with style
372 3 $link_tags{''}->{'style'} = 1;
373 3 $link_tags{'style'}->{''} = 1;
374  
375 # add tags with javascript
376 3 foreach my $attr (keys %{js_attributes()}) {
377 54 $link_tags{''}->{$attr} = 1;
378 }
379 3 $link_tags{'script'}->{''} = 1;
380  
381 # Uncomment this to see what HTML::Tagset::linkElements thinks are
382 # the tags with link attributes
383 #use Data::Dump; Data::Dump::dump(\%link_tags); exit;
384
385 3 $_link_tags = \%link_tags;
386 3 return $_link_tags;
387 }
388  
389  
390 =head2 js_attributes
391  
392 # Construct a hash of all possible JavaScript attribute names
393  
394 =cut
395  
396 # FIXME should be moved outside or URL::Transform because it HTML specific
397 my $_js_attributes;
398 5 ▶sub js_attributes {
Subroutine js_attributes: called CC=2 SCAR=6.9
Pod documented
399 # if it's already generate return the reference right away
400 5 ▶ return $_js_attributes if defined $_js_attributes;
Branch: 1 branch
BranchTrueFalseTotal
if defined $_js_attributes 2 3 5
401  
402 # taken from http://www.w3.org/TR/html401/interact/scripts.html#h-18.2.3
403 3 my @js_attributes = qw(
404 onload
405 onunload
406 onclick
407 ondblclick
408 onmousedown
409 onmouseup
410 onmouseover
411 onmousemove
412 onmouseout
413 onfocus
414 onblur
415 onkeypress
416 onkeydown
417 onkeyup
418 onsubmit
419 onreset
420 onselect
421 onchange
422 );
423  
424 $_js_attributes = {
425 3 map { $_ => 1 } @js_attributes
426 };
427 3 return $_js_attributes;
428 }
429  
430  
431 =head2 decode_string($string)
432  
433 Will return decoded string suitable for parsing. Decoding
434 is chosen according to the $self->content_encoding.
435  
436 Decoding is run automatically for every chunk/string/file.
437  
438 =cut
439  
440 3 ▶sub decode_string {
Subroutine decode_string: called CC=7 SCAR=32.1
Pod documented
441 3 my $self = shift;
442 3 my $data = shift;
443  
444 3 my $content_encoding = $self->content_encoding;
445  
446 # just ignore without content encoding
447 3 ▶ return $data
Branch: 1 branch
BranchTrueFalseTotal
unless $content_encoding 2 1 3
448 if (not $content_encoding);
449  
450 1 ▶ return Compress::Zlib::memGunzip($data)
Branch: 1 branch
BranchTrueFalseTotal
if $content_encoding eq "gzip" 1 0 1
451 if ($content_encoding eq 'gzip');
452
453 0 ▶ if ($content_encoding eq 'deflate') {
Branch: 1 branch
BranchTrueFalseTotal
if ($content_encoding eq "deflate") 0 0 0
454 0 my $raw = Compress::Zlib::uncompress($data);
455 0 ▶ if ( defined $raw ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $raw) { } 0 0 0
456 0 return $raw;
457 }
458 else {
459 # "Content-Encoding: deflate" is supposed to mean the "zlib"
460 # format of RFC 1950, but Microsoft got that wrong, so some
461 # servers sends the raw compressed "deflate" data. This
462 # tries to inflate this format.
463 0 my($i, $status) = Compress::Zlib::inflateInit(
464 WindowBits => -Compress::Zlib::MAX_WBITS(),
465 );
466 0 my $OK = Compress::Zlib::Z_OK();
467 0 ▶ die "Can't init inflate object" unless $i && $status == $OK;
Branch: 1 branch
BranchTrueFalseTotal
unless $i and $status == $OK 0 0 0
Condition0%0/3
$i and $status == $OK
!ll&&!rl&&r
000
MC/DC0%0/2
$i and $status == $OK
$i $status == $OK
Truth table0%0/3
A and B
A= $iB= $status == $OK
ABresult
0X0
100
111
468 0 ($raw, $status) = $i->inflate(\$data);
469 0 return $raw;
470 }
471 }
472  
473 0 return $data;
474 }
475  
476  
477 =head2 encode_string($string)
478  
479 Will return encoded string. Encoding
480 is chosen according to the $self->content_encoding.
481  
482 NOTE if you want to have your content encoded back to the
483 $self->content_encoding you will have to run this method
484 in your code. Argument to the C<output_function()> are always
485 plain text.
486  
487 =cut
488  
489 1 ▶sub encode_string {
Subroutine encode_string: called CC=4 SCAR=17.2
Pod documented
490 1 my $self = shift;
491 1 my $data = shift;
492  
493 1 my $content_encoding = $self->content_encoding;
494  
495 # just ignore without content encoding
496 1 ▶ return $data
Branch: 1 branch
BranchTrueFalseTotal
unless $content_encoding 0 1 1
497 if not $content_encoding;
498  
499 1 ▶ return Compress::Zlib::memGzip($data)
Branch: 1 branch
BranchTrueFalseTotal
if $content_encoding eq "gzip" 1 0 1
500 if ($content_encoding eq 'gzip');
501  
502 0 ▶ return Compress::Zlib::compress($data)
Branch: 1 branch
BranchTrueFalseTotal
if $content_encoding eq "deflate" 0 0 0
503 if ($content_encoding eq 'deflate');
504  
505 0 return $data;
506 }
507  
508  
509 =head2 get_supported_content_encodings()
510  
511 Returns hash reference of supported content encodings.
512  
513 =cut
514  
515 0 ▶sub get_supported_content_encodings {
Subroutine get_supported_content_encodings: not called CC=1 SCAR=6.9
Pod documented
516 0 return \%supported_content_encoding;
517 }
518  
519  
520 3 1;
521  
522  
523 __END__
↑ Summary Transform/using/HTML/Parser.pm »