WebSource/XMLParser.pm

filter: Statementstmt 46.3% 25 / 54 Branchbran 0.0% 0 / 10 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 77.7% 7 / 9 Podpod 100% 1 / 1 totaltotal 44.5% 33 / 74 CC 6 SCAR 21.7
CC
6
Cov
39%
handle
20.0
_init_
12.0
BEGIN
1.0
CRAP 8.8
« WebSource/Logger.pm ↑ Summary WebSource/Queue.pm »
1 package WebSource::XMLParser;
2 1 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 # use WebSource::Parser;
6 1 ▶use WebSource::Module;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Encode;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Encode::Guess;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 our @ISA = ('WebSource::Module');
12  
13 1 my %html_options = (
14 recover => 2
15 );
16 1 my %xml_options = (
17 recover => 2
18 );
19  
20 =head1 NAME
21  
22 WebSource::XMLParser : Builds a document out of an http-response
23 containing an XML or HTML file
24  
25 =head1 DESCRIPTION
26  
27 An XMLParser operator is declared as follows :
28  
29 <ws:xmlparser name="opname" forward-to="ops"/>
30  
31  
32 =head1 SYNOPSIS
33  
34 $fetcher = WebSource::XMLParser->new(wsnode => $node);
35  
36 # for the rest it works as a WebSource::Module
37  
38 =head1 METHODS
39  
40 =over 2
41  
42 =item B<< $parser = WebSource::XMLParser->new(desc => $node); >>
43  
44 Create a new Fetcher;
45  
46 =cut
47  
48 0 ▶sub _init_ {
Subroutine _init_: not called CC=3 SCAR=24.8
49 0 my $self = shift;
50 0 $self->SUPER::_init_;
51 0 ▶ $self->{parser} or $self->{parser} = XML::LibXML->new;
Branch: 1 branch
BranchTrueFalseTotal
$self->{"parser"} or $self->{"parser"} = "XML::LibXML"->new 0 0 0
52 0 my $wsd = $self->{wsdnode};
53 0 ▶ if($wsd) {
Branch: 1 branch
BranchTrueFalseTotal
if ($wsd) 0 0 0
54 0 $self->{forceEncoding} = $wsd->getAttribute('force-encoding');
55 }
56 0 return $self;
57 }
58  
59  
60 =item B<< $parser->handle($env); >>
61  
62 Parses the content of an http-response
63  
64 =cut
65  
66 0 ▶sub handle {
Subroutine handle: not called CC=4 SCAR=30.0
Pod documented
67 0 my $self = shift;
68 0 my $env = shift;
69
70 # $env->type eq "text/html" or
71 # ( $env->type eq "text/xml" or
72 # return () );
73
74 0 my $ct = $env->data;
75 0 my $base = $env->{baseuri};
76 0 my $doc = eval {
77 0 $self->log(2,"Found doctype of '". $env->type . "' with encoding '" . $env->{encoding} ."'");
78 0 $self->log(5,"-------- data -------------\n" . $ct);
79 0 my %options;
80 0 ▶ if($self->{forceEncoding}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"forceEncoding"}) 0 0 0
81 0 $ct = decode($self->{forceEncoding},$ct);
82 }
83 # elsif($env->{encoding}) {
84 # $ct = decode($env->{encoding},$ct);
85 # }
86 0 ▶ if ($env->type eq "text/html") {
Branch: 1 branch
BranchTrueFalseTotal
if ($env->type eq 'text/html') { } 0 0 0
87 0 $self->{parser}->parse_html_string($ct,\%html_options,%options);
88 } else {
89 0 $self->{parser}->parse_string($ct,\%xml_options,%options);
90 }
91 };
92 0 $doc->setEncoding('utf-8');
93 0 ▶ if(!$doc) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($doc) 0 0 0
94 0 $self->log(1,"Couldn't parse document $base : $@");
95 0 $self->log(3,">> here is the content <<\n",$ct,"\n");
96 0 return ();
97 }
98 0 my $bytes = $doc->toString(1,'utf-8');
99 0 $self->log(6,"-------- parsed -------------\n" . $bytes);
100 0 my %meta = %$env;
101 0 return WebSource::Envelope->new(
102 %meta,
103 type => "object/dom-node",
104 baseuri => $base,
105 data => $doc);
106 }
107  
108 =back
109  
110 =head1 SEE ALSO
111  
112 WebSource::Module
113  
114 =cut
115  
116 1 1;
« WebSource/Logger.pm ↑ Summary WebSource/Queue.pm »