Transform/SAX/Filter.pm

filter: Statementstmt 97.5% 39 / 40 Branchbran 62.5% 5 / 8 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 88.8% 8 / 9 Podpod 100% 3 / 3 totaltotal 91.6% 55 / 60 CC 6 SCAR 9.8
CC
6
Cov
92%
start_element
5.0
new
2.0
xml_decl
2.0
CRAP 2.7
« Transform/using/CSS/RegExp.pm ↑ Summary Transform/using/HTML/Meta.pm »
1 package URL::Transform::SAX::Filter;
2  
3 =head1 NAME
4  
5 URL::Transform::SAX::Filter - SAX filter to execute url transformation function when an attribute with url is found
6  
7 =head1 SYNOPSIS
8  
9 my $writer = XML::SAX::Writer->new( Output => sub {
10 my $type = shift;
11 $output_function->(@_);
12 } );
13 my $filter = URL::Transform::SAX::Filter->new(
14 Handler => $writer,
15 transform_function => sub { return join('|', @_) },
16 );
17
18 my $sax_parser = XML::SAX::ParserFactory->parser(
19 'Handler' => $filter,
20 );
21
22 $sax_parser->parse_file('test.html');
23  
24 =head1 DESCRIPTION
25  
26 This filter examines every start tag for a presence of tags and their
27 attributes which may hold link attributes. (SEE L<HTML::Tagset::linkElements>)
28  
29 For each of them the 'transform_function' is triggered which can
30 modify the url. This function receives following arguments:
31  
32 $self->{'transform_function'}->(
33 'tag_name' => 'img',
34 'attribute_name' => 'src',
35 'url' => 'http://search.cpan.org/s/img/cpan_banner.png',
36 );
37  
38 =cut
39  
40 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
41 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
42  
43 2 our $VERSION = '0.01';
44  
45 2 ▶use URL::Transform ();
Subroutine BEGIN: called CC=1 SCAR=0.0
46 2 ▶use List::Util 'first';
Subroutine BEGIN: called CC=1 SCAR=0.0
47 2 ▶use Carp::Clan 'croak';
Subroutine BEGIN: called CC=1 SCAR=0.0
48  
49 # Construct a hash of tag names that may have links.
50 2 my $_link_tags = URL::Transform::link_tags();
51  
52 2 ▶use base 'XML::SAX::Base';
Subroutine BEGIN: called CC=1 SCAR=0.0
53  
54 =head1 METHODS
55  
56  
57 =head2 new()
58  
59 Object constructor.
60  
61 Requires the 'transform_function' as the argument.
62  
63 =cut
64  
65 1 ▶sub new {
Subroutine new: called CC=2 SCAR=7.0
Pod documented
66 1 my $class = shift;
67 1 my %args = @_;
68
69 1 ▶ my $transform_function = delete $args{'transform_function'}
Branch: 1 branch
BranchTrueFalseTotal
my $transform_function = delete $args{"transform_function"} or croak("pass \"transform_function\" os an argument") 1 0 1
70 or croak 'pass "transform_function" os an argument';
71
72 1 my $self = $class->SUPER::new(%args);
73
74 1 $self->{'transform_function'} = $transform_function;
75
76 1 return $self;
77 }
78  
79  
80 =head2 start_element()
81  
82 This function handles the 'transform_function' triggering with a proper
83 arguments.
84  
85 =cut
86  
87 10 ▶sub start_element {
Subroutine start_element: called CC=5 SCAR=16.2
Pod documented
88 10 my $self = shift;
89 10 my $data = shift;
90  
91 10 my $attr = $data->{Attributes};
92 10 my $tag_name = lc $data->{'LocalName'};
93  
94 # if the tag belongs to the list of tags that can have a link
95 10 ▶ if (my $link_tag = $_link_tags->{$tag_name}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $link_tag = $_link_tags->{$tag_name}) 8 2 10
96 # loop through it's attributes
97 8 foreach my $ns_attribute_name (keys %$attr) {
98 # extract the attribute name and it's namespace
99 4 ▶ die 'unknown formated attribute name "'.$ns_attribute_name.'"'
Branch: 1 branch
BranchTrueFalseTotal
unless $ns_attribute_name =~ /^{([^}]*)}(.+)$/ 0 4 4
100 if not $ns_attribute_name =~ m/^{([^}]*)}(.+)$/;
101 4 my $attribute_ns = $1; #we don't use it for the moment
102 4 my $attribute_name = $2;
103
104 # if the attribute is link attribute then execute transform function
105 4 ▶ if ($link_tag->{$attribute_name}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($link_tag->{$attribute_name}) 4 0 4
106 $attr->{$ns_attribute_name}->{'Value'} =
107 $self->{'transform_function'}->(
108 'tag_name' => $tag_name,
109 'attribute_name' => $attribute_name,
110 4 'url' => $attr->{$ns_attribute_name}->{'Value'},
111 );
112 }
113 }
114 }
115  
116 10 return $self->SUPER::start_element($data);
117 }
118  
119  
120 =head2 xml_decl
121  
122 Just ignoring xml declaration. Otherwise we'll end-up with
123 C<< <?xml version="1.0"?> >> added to all documents.
124  
125 =cut
126  
127 0 ▶sub xml_decl {
Subroutine xml_decl: not called CC=1 SCAR=6.9
Pod documented
128 0 return;
129
130 }
131  
132  
133 2 1;
134  
135  
136 __END__
« Transform/using/CSS/RegExp.pm ↑ Summary Transform/using/HTML/Meta.pm »