WebSource/Envelope.pm

filter: Statementstmt 16.2% 20 / 123 Branchbran 0.0% 0 / 46 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 37.5% 6 / 16 Podpod 0.0% 0 / 10 totaltotal 12.0% 26 / 215 CC 30 SCAR 33.4
CC
30
Cov
11%
dataAsHttpRequest
56.0
new_from_file
42.0
dataXML
42.0
CRAP 28.1
« WebSource/Module.pm ↑ Summary WebSource/Extract/regexp.pm »
1 package WebSource::Envelope;
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use HTTP::Request::Common;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use XML::LibXML::Common qw(:w3c);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use XML::LibXML;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 =head1 NAME
11  
12 WebSource::Envelope - Container for exchanged data
13  
14 =head1 DESCRIPTION
15  
16 A WebSource::Enveloppe is used to encapsulate the data
17 going from one module to another. This alows to attach
18 meta-information such as a document's base uri
19  
20 For the moment these types are known :
21  
22 =over 2
23  
24 =item - B<xmlnode>
25  
26 data is an XML::LibXML Node
27  
28 =item - B<string>
29  
30 data is a string
31  
32 =item - B<uri>
33  
34 data is a URI object
35  
36 =item - B<http-request>
37  
38 data is an HTTP::Request object
39  
40 =back
41  
42 =head1 SYNOPSIS
43  
44 use WebSource::Envelope;
45 ...
46 my $env = WebSource::Envelope->new(
47 type => $type,
48 data => $data
49 ...
50 );
51 ...
52  
53  
54 =head1 METHODS
55  
56 =cut
57  
58 2 our %knowntypes = (
59 "object/dom-node" => 1,
60 "object/http-request" => 1,
61 "text/string" => 1,
62 "text/html" => 1,
63 "text/xml" => 1,
64 "object/uri" => 1,
65 "application/pdf" => 1,
66 "empty" => 1
67 );
68  
69 0 ▶sub new {
Subroutine new: not called CC=3 SCAR=24.8
Pod undocumented
70 0 my $class = shift;
71 0 my %params = @_;
72 0 my $self = bless \%params, $class;
73 0 ▶ $self->{type} or croak("No type given");
Branch: 1 branch
BranchTrueFalseTotal
$self->{"type"} or croak("No type given") 0 0 0
74 0 ▶ $knowntypes{$self->{type}} or carp("Type ",$self->{type}," is not known");
Branch: 1 branch
BranchTrueFalseTotal
$WebSource::Envelope::knowntypes{$self->{"type"}} or carp("Type ", $self->{"type"}, " is not known") 0 0 0
75 0 return $self;
76 }
77  
78 0 ▶sub new_from_file {
Subroutine new_from_file: not called CC=6 SCAR=37.4
Pod undocumented
79 0 my ($class,$filename) = @_;
80 0 my $parser = XML::LibXML->new();
81 0 my $doc = $parser->parse_file($filename);
82 0 my $envRoot = $doc->documentElement();
83 0 my %params;
84 0 foreach my $attr ($envRoot->attributes()) {
85 0 $params{$attr->nodeName} = $attr->nodeValue;
86 }
87 0 my $self = bless \%params, $class;
88 0 ▶ $self->{type} or croak("No type given");
Branch: 1 branch
BranchTrueFalseTotal
$self->{"type"} or croak("No type given") 0 0 0
89 0 ▶ $knowntypes{$self->{type}} or carp("Type ",$self->{type}," is not known");
Branch: 1 branch
BranchTrueFalseTotal
$WebSource::Envelope::knowntypes{$self->{"type"}} or carp("Type ", $self->{"type"}, " is not known") 0 0 0
90 0 ▶ if($self->{type} eq 'object/dom-node') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'type'} eq 'object/dom-node') { } 0 0 0
91 0 my @content = $envRoot->findnodes('child::*');
92 0 ▶ if(@content) {
Branch: 1 branch
BranchTrueFalseTotal
if (@content) { } 0 0 0
93 0 $self->{data} = $content[0];
94 } else {
95 0 croak("No content for dom-node");
96 }
97 } else {
98 0 $self->{data} = $envRoot->findvalue('text()');
99 }
100 0 return $self;
101 }
102  
103 0 ▶sub type {
Subroutine type: not called CC=1 SCAR=6.9
Pod undocumented
104 0 my $self = shift;
105 0 return $self->{type};
106 }
107  
108 0 ▶sub data {
Subroutine data: not called CC=1 SCAR=6.9
Pod undocumented
109 0 my $self = shift;
110 0 return $self->{data};
111 }
112  
113 0 ▶sub dataString {
Subroutine dataString: not called CC=4 SCAR=30.0
Pod undocumented
114 0 my $self = shift;
115 0 my $t = $self->type;
116 0 my $d = $self->data;
117 0 ▶ if($t eq "object/dom-node") {
Branch: 2 branches
BranchTrueFalseTotal
if ($t eq 'object/dom-node') { } 0 0 0
elsif ($t =~ m[^object/] and $d->can('as_string')) { } 0 0 0
118 0 $d->textContent;
119 ▶ } elsif($t =~ m{^object/} && $d->can("as_string")) {
Condition0%0/3
$t =~ m[^object/] and $d->can("as_string")
!ll&&!rl&&r
000
MC/DC0%0/2
$t =~ m[^object/] and $d->can("as_string")
$t =~ m[^object/] $d->can("as_string")
Truth table0%0/3
A and B
A= $t =~ m[^object/]B= $d->can("as_string")
ABresult
0X0
100
111
120 0 $d->as_string;
121 } else {
122 0 $d;
123 }
124 }
125  
126 0 ▶sub dataXML {
Subroutine dataXML: not called CC=6 SCAR=37.4
Pod undocumented
127 0 my $self = shift;
128 0 my %params = @_;
129 0 my $t = $self->type;
130 0 my $d = $self->data;
131 0 ▶ if($t eq "object/dom-node") {
Branch: 2 branches
BranchTrueFalseTotal
if ($t eq 'object/dom-node') { } 0 0 0
elsif ($t =~ m[^object/] and $d->can('as_string')) { } 0 0 0
132 0 ▶ if($params{wantdoc} && $d->nodeType ne "#document") {
Branch: 1 branch
BranchTrueFalseTotal
if ($params{'wantdoc'} and $d->nodeType ne '#document') { } 0 0 0
Condition0%0/3
$params{"wantdoc"} and $d->nodeType ne "#document"
!ll&&!rl&&r
000
MC/DC0%0/2
$params{"wantdoc"} and $d->nodeType ne "#document"
$params{"wantdoc"} $d->nodeType ne "#document"
Truth table0%0/3
A and B
A= $params{"wantdoc"}B= $d->nodeType ne "#document"
ABresult
0X0
100
111
133 0 my $doc = XML::LibXML::Document->createDocument( "1.0", "utf-8" );
134
135 0 my $clone = $d->cloneNode(1);
136 0 $doc->setDocumentElement($clone);
137 0 $doc->toString(1);
138 } else {
139 0 $d->toString(1);
140 }
141 ▶ } elsif($t =~ m{^object/} && $d->can("as_string")) {
Condition0%0/3
$t =~ m[^object/] and $d->can("as_string")
!ll&&!rl&&r
000
MC/DC0%0/2
$t =~ m[^object/] and $d->can("as_string")
$t =~ m[^object/] $d->can("as_string")
Truth table0%0/3
A and B
A= $t =~ m[^object/]B= $d->can("as_string")
ABresult
0X0
100
111
142 0 "<content>" . $d->as_string . "</content>";
143 } else {
144 0 "<data>" . $d . "</data>";
145 }
146 }
147  
148 0 ▶sub dataAsURI {
Subroutine dataAsURI: not called CC=4 SCAR=30.0
Pod undocumented
149 0 my $self = shift;
150 0 my $t = $self->type;
151 0 ▶ if($t eq "object/uri") {
Branch: 1 branch
BranchTrueFalseTotal
if ($t eq "object/uri") 0 0 0
152 0 return $self->data;
153 }
154 0 ▶ if($t eq "object/http-request") {
Branch: 1 branch
BranchTrueFalseTotal
if ($t eq "object/http-request") 0 0 0
155 0 return URI->new($self->data->uri)
156 }
157 0 ▶ if($self->{baseuri}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'baseuri'}) { } 0 0 0
158 0 return URI->new_abs($self->dataString,$self->{baseuri});
159 } else {
160 0 return URI->new($self->dataString);
161 }
162 }
163
164 0 ▶sub dataAsHttpRequest {
Subroutine dataAsHttpRequest: not called CC=7 SCAR=40.3
Pod undocumented
165 0 my $self = shift;
166 0 my $t = $self->type;
167 # print "Got data of type $t\n";
168 0 ▶ if($t eq "object/http-request") {
Branch: 1 branch
BranchTrueFalseTotal
if ($t eq "object/http-request") 0 0 0
169 0 return $self->data;
170 }
171 0 ▶ if($t eq "object/dom-node") {
Branch: 1 branch
BranchTrueFalseTotal
if ($t eq 'object/dom-node') { } 0 0 0
172 0 my $n = $self->data;
173 0 ▶ if($n->nodeType == DOCUMENT_NODE) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n->nodeType == 9) 0 0 0
174 0 $n = $n->documentElement;
175 }
176 # print "Namespace URI : ", $n->namespaceURI,"\n";
177 # print "Local name : ", $n->localName,"\n";
178 0 ▶ if($n->namespaceURI eq "http://wwwsource.free.fr/ns/websource-types"
Branch: 1 branch
BranchTrueFalseTotal
if ($n->namespaceURI eq 'http://wwwsource.free.fr/ns/websource-types' and $n->localName eq 'http-request') { } 0 0 0
Condition0%0/3
$n->namespaceURI eq "http://wwwsource.free.fr/ns/websource-types" and $n->localName eq "http-request"
!ll&&!rl&&r
000
MC/DC0%0/2
$n->namespaceURI eq "http://wwwsource.free.fr/ns/websource-types" and $n->localName eq "http-request"
$n->namespaceURI eq "http://wwwsource.free.fr/ns/websource-types" $n->localName eq "http-request"
Truth table0%0/3
A and B
A= $n->namespaceURI eq "http://wwwsource.free.fr/ns/websource-types"B= $n->localName eq "http-request"
ABresult
0X0
100
111
179 && $n->localName eq "http-request") {
180 0 my $pre = $n->prefix;
181 0 my $base = $n->getAttribute("base");
182 0 my $method = $n->getAttribute("method");
183 # print $n->toString(1),"\n";
184 # print "Base : $base\n";
185 # print "Method : $method\n";
186 0 my $url = URI->new($base);
187 my @query = map {
188 0 $_->getAttribute("name") => $_->getAttribute("value")
189 } $n->findnodes("${pre}:param");
190 0 my $htreq;
191 0 ▶ if ($method =~ m/GET/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($method =~ /GET/i) { } 0 0 0
192 0 $url->query_form(\@query);
193 0 $htreq = HTTP::Request->new("GET",$url);
194 } else {
195 0 $htreq = POST $url, \@query;
196 }
197 } else {
198 0 return HTTP::Request->new("GET",$self->dataAsURI->as_string);
199 }
200 } else {
201 0 return HTTP::Request->new("GET",$self->dataAsURI->as_string);
202 }
203 }
204  
205 0 ▶sub as_string {
Subroutine as_string: not called CC=2 SCAR=17.9
Pod undocumented
206 0 my $self = shift;
207 return "[[" . join(" ", map {
208 0 my $str = "";
209 0 $str .= $self->{$_};
210 0 my $l = length($str);
211 0 ▶ $l > 70 and $str = substr($str,0,35) . " ... " . substr($str,$l-35,35);
Branch: 1 branch
BranchTrueFalseTotal
$l > 70 and $str = substr($str, 0, 35) . ' ... ' . substr($str, $l - 35, 35) 0 0 0
212 0 $_ . " => " . $str
213 } keys(%$self)) . "]]";
214 }
215  
216 0 ▶sub to_file {
Subroutine to_file: not called CC=5 SCAR=34.0
Pod undocumented
217 0 my ($self,$filename) = @_;
218 0 my $parser = new XML::LibXML;
219 0 my $envDoc = $parser->parse_string('<?xml version="1.0" ?><ws:envelope xmlns:ws="http://wwwsource.free.fr/ns/websource" />');
220 0 my $envRoot = $envDoc->documentElement();
221 0 foreach my $key (keys(%$self)) {
222 0 ▶ if($key ne 'data') {
Branch: 1 branch
BranchTrueFalseTotal
if ($key ne "data") 0 0 0
223 0 my $value = $self->{$key};
224 0 $envRoot->setAttribute($key,$value);
225 }
226 }
227  
228 0 my $t = $self->type;
229 0 my $d = $self->data;
230 0 ▶ if($t eq "object/dom-node") {
Branch: 1 branch
BranchTrueFalseTotal
if ($t eq 'object/dom-node') { } 0 0 0
231 0 ▶ if($d->nodeType == XML_DOCUMENT_NODE) {
Branch: 1 branch
BranchTrueFalseTotal
if ($d->nodeType == 9) { } 0 0 0
232 0 $envRoot->appendChild($d->documentElement);
233 } else {
234 0 $envRoot->appendChild($d);
235 }
236 } else {
237 0 $envRoot->appendChild($envDoc->create($d));
238 }
239
240
241 0 open(my $fh,">",$filename);
242 0 print $fh $envDoc->toString;
243 0 close($fh);
244 }
245  
246 =head1 SEE ALSO
247  
248 WebSource
249  
250 =cut
251  
252 2 1;
« WebSource/Module.pm ↑ Summary WebSource/Extract/regexp.pm »