File Coverage

blib/lib/WebService/Validator/CSS/W3C.pm
Criterion Covered Total %
statement 7 9 77.7
branch n/a
condition n/a
subroutine 3 3 100.0
pod n/a
total 10 12 83.3


line stmt bran cond sub pod time code
1             package WebService::Validator::CSS::W3C;
2 1     1   185438 use strict;
  1         15  
  1         150  
3 1     1   6 use warnings;
  1         2  
  1         40  
4            
5 1     1   1761 use SOAP::Lite 0.65;
  0            
  0            
6             use LWP::UserAgent qw//;
7             use URI qw//;
8             use URI::QueryParam qw//;
9             use Carp qw//;
10             use base qw/Class::Accessor/;
11            
12             our $VERSION = "0.3";
13            
14             # profiles currently supported by the W3C CSS Validator
15             our %PROFILES = map { $_ => 1 } qw/none css1 css2 css21 css3 svg svgbasic
16             svgtiny mobile atsc-tv tv/;
17            
18             # user media currently supported by the W3C CSS Validator
19             our %MEDIA = map { $_ => 1 } qw/all aural braille embossed handheld
20             print screen tty tv presentation/;
21            
22             # warnings level currently supported by the W3C CSS Validator
23             our %WARNINGS = map { $_ => 1 } qw/0 1 2 no/;
24            
25             __PACKAGE__->mk_accessors (qw/user_agent validator_uri/);
26             __PACKAGE__->mk_ro_accessors (qw/response request_uri som success/);
27            
28             sub new
29             {
30             my $proto = shift;
31             my $class = ref $proto || $proto;
32             my $self = bless {}, $class;
33             my $ua = shift;
34             my $uri = shift;
35            
36             if (defined $ua) {
37            
38             # check whether it really is
39             Carp::croak "$ua is not a LWP::UserAgent"
40             unless UNIVERSAL::isa($ua, 'LWP::UserAgent');
41            
42             $self->user_agent($ua);
43             } else {
44             my $ua = LWP::UserAgent->new(agent => __PACKAGE__."/".$VERSION);
45             $self->user_agent($ua);
46             }
47            
48             if (defined $uri) {
49             $self->validator_uri($uri);
50             } else {
51             $self->validator_uri("http://jigsaw.w3.org/css-validator/validator");
52             }
53            
54             return $self;
55             }
56            
57             sub _handle_response
58             {
59             my $self = shift;
60             my $res = shift;
61            
62             # internal or server errors...
63             return 0 unless $res->is_success;
64            
65             local $_ = $res->content;
66            
67             my $som;
68             eval { $som = SOAP::Deserializer->new->deserialize($_); };
69            
70             # Deserialization might fail if the response is not a legal
71             # SOAP response, e.g., if the response is ill-formed... Not
72             # sure how to make the reason for failure available to the
73             # application, suggestions welcome.
74             if ($@) {
75             # Carp::carp $@;
76             return 0;
77             }
78            
79             # memorize the SOAP object model object
80             $self->{'som'} = $som;
81            
82             # check whether this is really the CSS Validator responding
83             if ($som->match("/Envelope/Body/cssvalidationresponse")) {
84             $self->{'success'} = 1;
85             }
86             # if the response was a SOAP fault
87             elsif ($som->match("/Envelope/Body/Fault")) {
88             $self->{'success'} = 0;
89             }
90            
91             # return whether the response was successfully processed
92             return $self->{'success'};
93             }
94            
95             sub validate
96             {
97             my $self = shift;
98             my %parm = @_;
99             my $uri = URI->new($self->validator_uri);
100             my $ua = $self->user_agent;
101            
102             $self->{'success'} = 0;
103            
104             #
105             if (defined $parm{string}) {
106             $uri->query_param(text => $parm{string});
107             } elsif (defined $parm{uri}) {
108             $uri->query_param(uri => $parm{uri});
109             } else {
110             Carp::croak "you must supply a string/uri parameter\n";
111             }
112            
113             if (defined $parm{medium}) {
114             # check whether the medium is supported
115             Carp::croak "$parm{medium} is not a legal medium\n"
116             unless $MEDIA{$parm{medium}};
117            
118             $uri->query_param(medium => $parm{medium});
119             }
120            
121             if (defined $parm{profile}) {
122             # check whether the profile is supported
123             Carp::croak "$parm{profile} is not a legal profile\n"
124             unless $PROFILES{$parm{profile}};
125            
126             $uri->query_param(profile => $parm{profile});
127             }
128            
129             if (defined $parm{warnings}) {
130             Carp::croak "warnings must be either \"no\" or an integer from 0 to 2\n"
131             unless $WARNINGS{$parm{warnings}};
132             $uri->query_param(warning => $parm{warnings});
133             }
134            
135             # request SOAP 1.2 output
136             $uri->query_param(output => "soap12");
137            
138             # memorize request uri
139             $self->{'request_uri'} = $uri;
140            
141             # generate new HTTP::Request object
142             my $req = HTTP::Request->new(GET => $uri);
143            
144             # add an Accept-Language header if desired
145             if (defined $parm{language}) {
146             $req->header(Accept_Language => $parm{language});
147             }
148            
149             my $res = $ua->simple_request($req);
150            
151             # memorize response
152             $self->{'response'} = $res;
153            
154             return $self->_handle_response($res);
155             }
156            
157             sub is_valid
158             {
159             my $self = shift;
160             my $som = $self->som;
161            
162             # previous failure means the style sheet is invalid
163             return 0 unless $self->success and defined $som;
164            
165             # fetch validity field in reponse
166             my $validity = $som->valueof("/Envelope/Body/cssvalidationresponse/validity");
167            
168             # valid if m:validity is true
169             return 1 if defined $validity and $validity eq "true";
170            
171             # else invalid
172             return 0;
173             }
174            
175             sub errors
176             {
177             my $self = shift;
178             my $som = $self->som;
179            
180             return () unless defined $som;
181             return $som->valueof("//error");
182             }
183            
184             sub warnings
185             {
186             my $self = shift;
187             my $som = $self->som;
188            
189             return () unless defined $som;
190             return $som->valueof("//warning");
191             }
192            
193             sub errorcount
194             {
195             my $self = shift;
196             my $som = $self->som;
197            
198             return () unless defined $som;
199             return $som->valueof("//errorcount");
200             }
201            
202             sub warningcount
203             {
204             my $self = shift;
205             my $som = $self->som;
206            
207             return () unless defined $som;
208             return $som->valueof("//warningcount");
209             }
210            
211             1;
212            
213             __END__