File Coverage

lib/URL/Transform/using/XML/SAX.pm
Criterion Covered Total %
statement 46 46 100.0
branch 3 6 50.0
condition n/a
subroutine 13 13 100.0
pod 3 3 100.0
total 65 68 95.5


line stmt bran cond sub pod time code
1             package URL::Transform::using::XML::SAX;
2              
3             =head1 NAME
4              
5             URL::Transform::using::XML::SAX - XML::SAX parsing of the html/xml for url transformation
6              
7             =head1 SYNOPSIS
8              
9             my $urlt = URL::Transform::using::XML::SAX->new(
10             'output_function' => sub { $output .= "@_" },
11             'transform_function' => sub { return (join '|', @_) },
12             );
13             $urlt->parse_file($Bin.'/data/URL-Transform-01.html');
14              
15             =head1 DESCRIPTION
16              
17             This is a helper module to set-up L for
18             a L.
19              
20             You can set which SAX driver will be used by:
21              
22             $XML::SAX::ParserPackage = "XML::LibXML::SAX";
23              
24             See: L.
25              
26             This module lacks the advanced features of L
27             like transforming the urls in the inside document elements types (CSS/JavaScript/Meta)
28             because it was used mosty to benchmark the performance of the L vs
29             L. The L turned out to be much more performant.
30              
31             =cut
32              
33 1     1   6 use warnings;
  1         1  
  1         37  
34 1     1   6 use strict;
  1         2  
  1         53  
35              
36             our $VERSION = '0.01';
37              
38 1     1   805 use XML::SAX;
  1         5340  
  1         49  
39 1     1   9 use XML::SAX::ParserFactory;
  1         3  
  1         45  
40 1     1   977 use XML::SAX::Writer;
  1         50339  
  1         32  
41 1     1   768 use URL::Transform::SAX::Filter;
  1         3  
  1         25  
42              
43 1     1   8 use HTML::Tagset ();
  1         1  
  1         16  
44 1     1   5 use Carp::Clan;
  1         1  
  1         5  
45              
46              
47 1     1   170 use base 'Class::Accessor::Fast';
  1         2  
  1         998  
48              
49             =head1 PROPERTIES
50              
51             output_function
52             transform_function
53              
54             _libxml_parser
55              
56             =cut
57              
58             __PACKAGE__->mk_accessors(qw{
59             output_function
60             transform_function
61              
62             _sax_parser
63             });
64              
65             =head1 METHODS
66              
67              
68             =head2 new
69              
70             Object constructor.
71              
72             Requires:
73              
74             output_function
75             transform_function
76              
77             Which are the code refs. See L for more details/example.
78              
79             =cut
80              
81              
82             sub new {
83 1     1 1 25 my $class = shift;
84 1         12 my $self = $class->SUPER::new({ @_ });
85              
86 1         14 my $output_function = $self->output_function;
87 1         11 my $transform_function = $self->transform_function;
88            
89 1 50       7 croak 'pass output function'
90             if not (ref $output_function eq 'CODE');
91            
92 1 50       4 croak 'pass transform url function'
93             if not (ref $transform_function eq 'CODE');
94            
95             # FIXME reuse URL::Transform::using::HTML::Parser::transform_function_wrapper()
96             # for handling special "hidden" urls
97            
98             my $writer = XML::SAX::Writer->new( Output => sub {
99 38     38   5111 my $type = shift;
100 38         175 $output_function->(@_);
101 1         10 } );
102 1         206 my $filter = URL::Transform::SAX::Filter->new(
103             Handler => $writer,
104             transform_function => $transform_function,
105             );
106            
107 1         8 my $sax_parser = XML::SAX::ParserFactory->parser(
108             'Handler' => $filter,
109             );
110            
111 1         55973 $self->_sax_parser($sax_parser);
112              
113 1         15 return $self;
114             }
115              
116              
117             =head2 parse_string($string)
118              
119             Submit document as a string for parsing.
120              
121             =cut
122              
123             sub parse_string {
124 1     1 1 2 my $self = shift;
125            
126 1         6 $self->_sax_parser->parse_string(@_);
127             }
128              
129              
130             =head2 parse_file($file_name)
131              
132             Submit file for parsing.
133              
134             =cut
135              
136             sub parse_file {
137 1     1 1 14 my $self = shift;
138 1         3 my $file_name = shift;
139              
140 1 50       50 open my $fh, '<', $file_name or croak 'Can not open '.$file_name.': '.$!;
141            
142 1         4 $self->_sax_parser->parse_file($fh);
143             }
144              
145              
146             1;
147              
148              
149             __END__