WWW/Scraper/Response/ScraperDiscovery/FORM.pm untested

filter: Statementstmt 0.0% 0 / 61 Branchbran 0.0% 0 / 24 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 0.0% 0 / 6 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 99 CC 13 SCAR 34.8
CC
13
Cov
0%
asHtml
56.0
absolute
30.0
GenerateScraperRequest
12.0
CRAP 32.4
1 use strict;
2 package WWW::Scraper::Response::ScraperDiscovery::FORM;
3  
4 #####################################################################
5  
6 # This is an appropriate VERSION calculation to use for CVS revision numbering.
7 use vars qw($VERSION);
8 use base qw(WWW::Scraper::Response::ScraperDiscovery);
9 $VERSION = sprintf("%d.%02d", q$Revision: 1.0 $ =~ /(\d+).(\d+)/);
10  
11 sub asHtml {
12 my ($self, $baseUrl, $newModuleName) = @_;
13
14 my $attrList = '';
15 my %results = %{$self->GetFieldValues()};
16 my $val;
17 for ( keys %{$self->GetFieldTitles()} ) {
18 $val = $self->$_;
19 next unless defined $val;
20 $val = $$val;
21 # Let's escape quotes or apostrophes the cheapest way possible.
22 if ( $val !~ m{"} ) {
23 $val = "\"$val\"";
24 } elsif ( $val !~ m{'} ) {
25 $val = "\'$val\'" unless $val =~ m{'};
26 } else {
27 $val =~ s{'}{%27}g;
28 $val = "'$val'";
29 }
30 $attrList .= " $_=$val" if $val && $val ne ''
31 }
32  
33 my $content = '';
34 map { $content .= $_->asHtml } @{$self->SubHitList} if $self->SubHitList;
35 my $name = ${$self->name};
36 my $answer = <<EOT;
37 <form method='POST' name='formFor_$name' action='javascript:SubmitForm(document.formFor_$name)'>
38 <input type='hidden' name='scraperDiscoveryTargetForm' value='targetForm_$name'>
39 <input type='hidden' name='scraperDiscoveryOp' value='ScraperFrame'>
40 <input type='hidden' name='scraperDiscoveryModuleName' value='$newModuleName'>
41 $content
42 </form>
43 EOT
44 my $method = ${$self->method};
45 my $action = absolute(${$self->action}, $baseUrl);
46 $answer .= <<EOT;
47 <form name='targetForm_$name' method='$method' target='scraper_$name\_target_window' action='$action'>
48 </form>
49 EOT
50 return $answer;
51 }
52  
53 sub absolute {
54 my ($url, $baseUrl) = @_;
55
56 $url .= '?' unless $url =~ m{\?$};
57 if ( $url =~ m{^/} ) {
58 $baseUrl =~ m{(\w+://[^/]*)};
59 $url = "$1$url" unless $url =~ m{^https?://}i;
60 } elsif ( $url =! m{^https?://}i ) {
61 $url = "$baseUrl/$url" ;
62 }
63 return $url;
64 }
65  
66 sub GenerateScraperRequest {
67 my ($self, $baseUrl) = @_;
68  
69 my $type = ${$self->method};
70 $type = 'QUERY' if lc $type eq 'get';
71 my $url = absolute(${$self->action}, $baseUrl);
72
73 my $scraperRequest =
74 {
75 # This engine's method is QUERY
76 'type' => $type
77
78 # This is the basic URL on which to get the form to build the query.
79 ,'url' => $url
80  
81 # specify defaults, by native field names
82 ,'nativeQuery' => undef
83 ,'nativeDefaults' => {}
84
85 # specify translations from canonical fields to native fields
86 ,'defaultRequestClass' => undef
87 ,'fieldTranslations' =>
88 {
89 '*' =>
90 {
91 '*' => '*'
92 }
93 }
94 # Miscellaneous options for the Scraper operation.
95 ,'cookies' => 0
96 };
97  
98 map { $_->GenerateScraperRequest($scraperRequest) } @{$self->SubHitList} if $self->SubHitList;
99 return $scraperRequest;
100 }
101 1;
102  
103 __END__