| 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<URL::Transform::SAX::Filter> for | ||||||||||
| 18 | a L<URL::Transform>. | ||||||||||
| 19 | |||||||||||
| 20 | You can set which SAX driver will be used by: | ||||||||||
| 21 | |||||||||||
| 22 | $XML::SAX::ParserPackage = "XML::LibXML::SAX"; | ||||||||||
| 23 | |||||||||||
| 24 | See: L<XML::SAX::ParserFactory>. | ||||||||||
| 25 | |||||||||||
| 26 | This module lacks the advanced features of L<URL::Transform::using::HTML::Parser> | ||||||||||
| 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<HTML::Parser> vs | ||||||||||
| 29 | L<XML::SAX>. The L<HTML::Parser> turned out to be much more performant. | ||||||||||
| 30 | |||||||||||
| 31 | =cut | ||||||||||
| 32 | |||||||||||
| 33 | 2 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 34 | 2 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 35 | |||||||||||
| 36 | 2 | our $VERSION = '0.01'; | |||||||||
| 37 | |||||||||||
| 38 | 2 | ▶ | use XML::SAX; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 39 | 2 | ▶ | use XML::SAX::ParserFactory; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 40 | 2 | ▶ | use XML::SAX::Writer; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 41 | 2 | ▶ | use URL::Transform::SAX::Filter; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 42 | |||||||||||
| 43 | 2 | ▶ | use HTML::Tagset (); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 44 | 2 | ▶ | use Carp::Clan; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 45 | |||||||||||
| 46 | |||||||||||
| 47 | 2 | ▶ | use base 'Class::Accessor::Fast'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 48 | |||||||||||
| 49 | =head1 PROPERTIES | ||||||||||
| 50 | |||||||||||
| 51 | output_function | ||||||||||
| 52 | transform_function | ||||||||||
| 53 | |||||||||||
| 54 | _libxml_parser | ||||||||||
| 55 | |||||||||||
| 56 | =cut | ||||||||||
| 57 | |||||||||||
| 58 | 2 | __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<URL::Transform> for more details/example. | ||||||||||
| 78 | |||||||||||
| 79 | =cut | ||||||||||
| 80 | |||||||||||
| 81 | |||||||||||
| 82 | 1 | ▶ | sub new { | ||||||||
|
Subroutine
new: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||
| 83 | 1 | my $class = shift; | |||||||||
| 84 | 1 | my $self = $class->SUPER::new({ @_ }); | |||||||||
| 85 | |||||||||||
| 86 | 1 | my $output_function = $self->output_function; | |||||||||
| 87 | 1 | my $transform_function = $self->transform_function; | |||||||||
| 88 | |||||||||||
| 89 | 1 | ▶ | croak 'pass output function' | ||||||||
|
Branch: 1 branch
| |||||||||||
| 90 | if not (ref $output_function eq 'CODE'); | ||||||||||
| 91 | |||||||||||
| 92 | 1 | ▶ | croak 'pass transform url function' | ||||||||
|
Branch: 1 branch
| |||||||||||
| 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 | 38 | ▶ | my $writer = XML::SAX::Writer->new( Output => sub { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 99 | 38 | my $type = shift; | |||||||||
| 100 | 38 | $output_function->(@_); | |||||||||
| 101 | 1 | } ); | |||||||||
| 102 | 1 | my $filter = URL::Transform::SAX::Filter->new( | |||||||||
| 103 | Handler => $writer, | ||||||||||
| 104 | transform_function => $transform_function, | ||||||||||
| 105 | ); | ||||||||||
| 106 | |||||||||||
| 107 | 1 | my $sax_parser = XML::SAX::ParserFactory->parser( | |||||||||
| 108 | 'Handler' => $filter, | ||||||||||
| 109 | ); | ||||||||||
| 110 | |||||||||||
| 111 | 1 | $self->_sax_parser($sax_parser); | |||||||||
| 112 | |||||||||||
| 113 | 1 | return $self; | |||||||||
| 114 | } | ||||||||||
| 115 | |||||||||||
| 116 | |||||||||||
| 117 | =head2 parse_string($string) | ||||||||||
| 118 | |||||||||||
| 119 | Submit document as a string for parsing. | ||||||||||
| 120 | |||||||||||
| 121 | =cut | ||||||||||
| 122 | |||||||||||
| 123 | 1 | ▶ | sub parse_string { | ||||||||
|
Subroutine
parse_string: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 124 | 1 | my $self = shift; | |||||||||
| 125 | |||||||||||
| 126 | 1 | $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 | 1 | ▶ | sub parse_file { | ||||||||
|
Subroutine
parse_file: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||
| 137 | 1 | my $self = shift; | |||||||||
| 138 | 1 | my $file_name = shift; | |||||||||
| 139 | |||||||||||
| 140 | 1 | ▶ | open my $fh, '<', $file_name or croak 'Can not open '.$file_name.': '.$!; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 141 | |||||||||||
| 142 | 1 | $self->_sax_parser->parse_file($fh); | |||||||||
| 143 | } | ||||||||||
| 144 | |||||||||||
| 145 | |||||||||||
| 146 | 2 | 1; | |||||||||
| 147 | |||||||||||
| 148 | |||||||||||
| 149 | __END__ | ||||||||||