blib/lib/Plack/App/DAIA/Validator.pm

filter: Statementstmt 61.2% 79 / 129 Branchbran 32.7% 19 / 58 Conditioncond 33.3% 11 / 33 MC/DCmcdc 0.0% 0 / 18 Subroutinesub 100% 11 / 11 Podpod 100% 2 / 2 totaltotal 48.6% 122 / 251 CC 44 SCAR 56.3
CC
44
Cov
50%
call
393.1
BEGIN
6.3
init
2.5
CRAP 277.6
↑ Summary blib/lib/Plack/App/DAIA/Test/Suite.pm »
1 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 package Plack::App::DAIA::Validator;
4 #ABSTRACT: DAIA validator and converter
5 3 our $VERSION = '0.55'; #VERSION
6 3 ▶use CGI qw(:standard);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use Encode;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use File::ShareDir qw(dist_dir);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use File::Spec::Functions qw(catfile);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 3 ▶use parent 'Plack::App::DAIA';
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use Plack::Util::Accessor qw(xsd xslt warnings);
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 3 our $HAS_LIBXML = 0;
15  
16 3 our ($FORMATS);
17 3 ▶BEGIN {
Subroutine BEGIN: called CC=6 SCAR=18.4
18 3 my %f = DAIA->formats;
19 3 $FORMATS = { map { $_ => $_ } keys %f };
20 3 $FORMATS->{html} = 'DAIA/HTML';
21 3 $FORMATS->{json} = 'DAIA/JSON';
22 3 $FORMATS->{xml} = 'DAIA/XML';
23 3 $FORMATS->{rdfjson} = 'DAIA/RDF (JSON)';
24 3 ▶ $FORMATS->{turtle} = 'DAIA/RDF (Turtle)' if $FORMATS->{turtle};
Branch: 1 branch
BranchTrueFalseTotal
if $Plack::App::DAIA::Validator::FORMATS->{"turtle"} 0 3 3
25 3 ▶ $FORMATS->{ntriples} = 'DAIA/RDF (NTriples)' if $FORMATS->{ntriples};
Branch: 1 branch
BranchTrueFalseTotal
if $Plack::App::DAIA::Validator::FORMATS->{"ntriples"} 0 3 3
26 3 ▶ $FORMATS->{rdfxml} = 'DAIA/RDF (RDF/XML)' if $FORMATS->{rdfxml};
Branch: 1 branch
BranchTrueFalseTotal
if $Plack::App::DAIA::Validator::FORMATS->{"rdfxml"} 0 3 3
27 3 foreach (qw(dot svg)) {
28 6 ▶ $FORMATS->{$_} = "DAIA/RDF graph ($_)" if $FORMATS->{$_};
Branch: 1 branch
BranchTrueFalseTotal
if $Plack::App::DAIA::Validator::FORMATS->{$_} 0 6 6
29 }
30 }
31  
32 1 ▶sub init {
Subroutine init: called CC=2 SCAR=9.2
Pod documented
33 1 my $self = shift;
34 1 ▶ if ($self->xsd) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->xsd) 0 1 1
35 ## no critic
36 0 eval "use XML::LibXML";
37 0 $HAS_LIBXML = !$@;
38 }
39 }
40  
41 2 ▶sub call {
Subroutine call: called CC=38 SCAR=59.7
Pod documented
42 2 my ($self, $env) = @_;
43 2 my $req = Plack::Request->new($env);
44  
45 # serve parts of the XSLT client
46 2 my $res = $self->call_client($req);
47 2 ▶ return $res if $res;
Branch: 1 branch
BranchTrueFalseTotal
if $res 1 1 2
48  
49 1 my $msg = "";
50 1 my $error = "";
51 1 ▶ my $url = $req->param('url') || '';
Condition50%1/2
$req->param('url') || ''
l!l
01
MC/DC0%0/1
$req->param('url') || ''
$req->param('url')
Truth table50%1/2
A
A= $req->param('url')
Aresult
11
00
52 1 ▶ my $data = $req->param('data') || '';
Condition50%1/2
$req->param('data') || ''
l!l
01
MC/DC0%0/1
$req->param('data') || ''
$req->param('data')
Truth table50%1/2
A
A= $req->param('data')
Aresult
11
00
53 #eval{ $data = Encode::decode_utf8( $data ); }; # icoming raw data is UTF-8
54  
55 1 my $eurl = $url; # url_encode
56 0 $eurl =~ s/([^A-Za-z0-9])/sprintf("%%%02X", ord($1))/seg;
57  
58 1 my $xsd = $self->xsd;
59  
60 1 ▶ my $informat = lc($req->param('in') || '');
Condition50%1/2
$req->param('in') || ''
l!l
01
MC/DC0%0/1
$req->param('in') || ''
$req->param('in')
Truth table50%1/2
A
A= $req->param('in')
Aresult
11
00
61 1 ▶ my $outformat = lc($req->param('out') || $req->param('format') || 'html');
Condition40%2/5
$req->param('out') || $req->param('format') || 'html'
l!l
01
$req->param("out") or $req->param("format")
l!l&&r!l&&!r
001
MC/DC0%0/2
$req->param('out') || $req->param('format') || 'html'
$req->param("out") $req->param("format")
Truth table33%1/3
A or B
A= $req->param("out")B= $req->param("format")
ABresult
1X1
011
000
62  
63 1 ▶ my $callback = $req->param('callback') || "";
Condition50%1/2
$req->param('callback') || ''
l!l
01
MC/DC0%0/1
$req->param('callback') || ''
$req->param('callback')
Truth table50%1/2
A
A= $req->param('callback')
Aresult
11
00
64 1 ▶ $callback = "" unless $callback =~ /^[a-z][a-z0-9._\[\]]*$/i;
Branch: 1 branch
BranchTrueFalseTotal
unless $callback =~ /^[a-z][a-z0-9._\[\]]*$/i 1 0 1
65  
66 1 my @daiaobjs;
67  
68 # parse DAIA
69 1 ▶ if ( $data ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($data) { } 0 1 1
elsif ($url) { } 0 1 1
70 0 @daiaobjs = eval { DAIA->parse( data => $data, format => $informat ) };
71 } elsif( $url ) {
72 0 @daiaobjs = eval { DAIA->parse( file => $url, format => $informat ) };
73 }
74 1 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 1 0 1
75 1 $error = $@;
76 1 $error =~ s/DAIA::([A-Z]+::)?[a-z_]+\(\):| at .* line.*//ig;
77 }
78  
79 1 my $daia;
80 1 ▶ if (@daiaobjs > 1) {
Branch: 2 branches
BranchTrueFalseTotal
if (@daiaobjs > 1) { } 0 1 1
elsif (@daiaobjs) { } 0 1 1
81 0 $error = "Found multiple DAIA elements (".(scalar @daiaobjs)."), but expected one";
82 } elsif (@daiaobjs) {
83 0 $daia = shift @daiaobjs;
84 }
85  
86 1 ▶ if ( $FORMATS->{$outformat} and $outformat ne 'html' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($Plack::App::DAIA::Validator::FORMATS->{$outformat} and $outformat ne 'html') { } 0 1 1
elsif ($outformat ne 'html') { } 0 1 1
Condition33%1/3
$Plack::App::DAIA::Validator::FORMATS->{$outformat} and $outformat ne "html"
!ll&&!rl&&r
010
MC/DC0%0/2
$Plack::App::DAIA::Validator::FORMATS->{$outformat} and $outformat ne "html"
$Plack::App::DAIA::Validator::FORMATS->{$outformat} $outformat ne "html"
Truth table33%1/3
A and B
A= $Plack::App::DAIA::Validator::FORMATS->{$outformat}B= $outformat ne "html"
ABresult
0X0
100
111
87 0 ▶ $daia = DAIA::Response->new() unless $daia;
Branch: 1 branch
BranchTrueFalseTotal
unless $daia 0 0 0
88 0 ▶ $daia->addMessage( $error, errno => 500, lang => 'en' ) if $error;
Branch: 1 branch
BranchTrueFalseTotal
if $error 0 0 0
89 0 return $self->as_psgi( 200, $daia, $outformat, $req->param('callback') );
90 } elsif ( $outformat ne 'html' ) {
91 0 $error = "Unknown output format - using HTML instead";
92 }
93  
94 # HTML output
95 1 ▶ $error = "<div class='error'>".escapeHTML($error)."!</div>" if $error;
Branch: 1 branch
BranchTrueFalseTotal
if $error 1 0 1
96 1 ▶ if ( $url and not $data ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($url and not $data) 0 1 1
Condition33%1/3
$url and not $data
!ll&&!rl&&r
100
MC/DC0%0/2
$url and not $data
$url not $data
Truth table33%1/3
A and not(B)
A= $urlB= not $data
ABresult
0X0
100
111
97 0 $msg = "Data was fetched from URL " . a({href=>$url},escapeHTML($url));
98 0 ▶ $msg .= " (" . a({href=>'#result'}, "result...") . ")" if $daia;
Branch: 1 branch
BranchTrueFalseTotal
if $daia 0 0 0
99 0 $msg = div({class=>'msg'},$msg);
100 # $msg .= div({class=>'msg'},"Use ".
101 # a({href=>url()."?url=$eurl"},'this URL') .
102 # " to to directly pass the URL to this script.");
103 }
104  
105 1 my $html = <<HTML;
106 <html>
107 <head>
108 <title>DAIA Validator</title>
109 <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
110 <style>
111 body { font-family: arial, sans-serif;}
112 h1, p { margin: 0; text-align: center; }
113 h2 { margin-top: 2px; border-bottom: 1px dotted #666;}
114 form { margin: 1em; border: 1px solid #333; }
115 fieldset { border: 1px solid #fff; }
116 label, .error, .msg { font-weight: bold; }
117 .submit, .error { font-size: 120%; }
118 .error { color: #A00; margin: 1em; }
119 .ok { color: #0A0; margin: 1em; }
120 .msg { color: #0A0; margin: 1em; }
121 .footer { font-size: small; margin: 1em; }
122 #result { border: 1px dotted #666; margin: 1em; padding: 0.5em; }
123 </style>
124 </head>
125 <body>
126 <h1 id='top'>DAIA Validator/Converter</h1>
127 <p>Convert and Validate <a href="http://purl.org/NET/DAIA">DAIA response format</a></p>
128 <form method="post" accept-charset="utf-8" action="">
129 HTML
130  
131 $html .= $msg . $error .
132 fieldset(label('Input: ',
133 popup_menu('in',['','json','xml'],'',
134 {''=>'Guess','json'=>'DAIA/JSON','xml'=>'DAIA/XML'})
135 )).
136 fieldset('either', label('URL: ', textfield(-name=>'url', -size=>70, -value => $url)),
137 'or', label('Data:'),
138 textarea( -name=>'data', -rows=>20, -cols=>80, -value => $data),
139 ).
140 fieldset(
141 label('Output: ',
142 popup_menu('out',
143 1 [ sort { $FORMATS->{$a} cmp $FORMATS->{$b} } keys %$FORMATS ],
144 $outformat, $FORMATS )
145 ), '&#xA0;',
146 label('JSONP Callback: ', textfield(-name=>'callback',-value=>$callback))
147 ).
148 fieldset('<input type="submit" value="Convert" class="submit" />')
149 ;
150 1 my $has_graphviz = grep /^(svg|dot)$/, keys %$FORMATS;
151 1 ▶ if ( $has_graphviz && $url && !$data) {
Branch: 1 branch
BranchTrueFalseTotal
if ($has_graphviz and $url and not $data) 0 1 1
Condition33%2/6
$has_graphviz and $url and not $data
!ll&&!rl&&r
100
$has_graphviz and $url
!ll&&!rl&&r
100
MC/DC0%0/3
$has_graphviz and $url and not $data
$has_graphviz $url not $data
Truth table25%1/4
A and B and not(C)
A= $has_graphvizB= $urlC= not $data
ABCresult
0XX0
10X0
1100
1111
152 0 $html .= "<fieldset>See RDF graph <a href=\"?url=$eurl&format=svg\">as SVG</a></fieldset>";
153 }
154 1 $html .= '</form>';
155  
156 1 ▶ if ($daia) {
Branch: 1 branch
BranchTrueFalseTotal
if ($daia) 0 1 1
157 0 ▶ if ( $informat eq 'xml' or DAIA::guess($data) eq 'xml' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($informat eq 'xml' or DAIA::guess($data) eq 'xml') { } 0 0 0
Condition0%0/3
$informat eq "xml" or DAIA::guess($data) eq "xml"
l!l&&r!l&&!r
000
MC/DC0%0/2
$informat eq "xml" or DAIA::guess($data) eq "xml"
$informat eq "xml" DAIA::guess($data) eq "xml"
Truth table0%0/3
A or B
A= $informat eq "xml"B= DAIA::guess($data) eq "xml"
ABresult
1X1
011
000
158 # TODO: move this into module DAIA (validate option when parsing)
159 0 my ($schema, $parser);
160 0 ▶ if ($xsd) {
Branch: 1 branch
BranchTrueFalseTotal
if ($xsd) 0 0 0
161 0 ▶ if (!$HAS_LIBXML) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $Plack::App::DAIA::Validator::HAS_LIBXML) { } 0 0 0
162 0 $error = "XML::LibXML not found - validating skipped";
163 } else {
164 0 $parser = XML::LibXML->new;
165 0 $schema = eval {
166 0 XML::LibXML::Schema->new(
167 location => catfile(dist_dir('Plack-App-DAIA'),'daia.xsd')
168 );
169 };
170 0 ▶ if ($schema) {
Branch: 1 branch
BranchTrueFalseTotal
if ($schema) { } 0 0 0
171 0 my $doc = $parser->parse_string( $data );
172 0 eval { $schema->validate($doc) };
173 0 ▶ $error = "DAIA/XML not valid but parseable: " . $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
174 } else {
175 0 $error = "Could not load XML Schema - validating skipped";
176 }
177 }
178 }
179 0 ▶ if ( $error ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($error) { } 0 0 0
180 0 $html .= "<p class='error'>".escapeHTML($error)."</p>";
181 } else {
182 0 $html .= "<p class='ok'>DAIA/XML valid according to ".a({href=>$xsd},"this XML Schema")."</p>";
183 }
184 } else {
185 0 $html .= p("validation is rather lax so the input may be invalid - but it was parseable");
186 }
187 0 $html .= "<div id='result'>";
188 0 my ($pjson, $pxml, $pttl) = ("","","");
189 0 ▶ if (!$data && $url) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $data and $url) 0 0 0
Condition0%0/3
not $data and $url
!ll&&!rl&&r
000
MC/DC0%0/2
not $data and $url
not $data $url
Truth table0%0/3
not(A) and B
A= not $dataB= $url
ABresult
0X0
100
111
190 0 $pjson = $pxml = $pttl = "?callback=$callback&url=$eurl";
191 0 $pjson = " (<a href='$pjson&format=json'>get via proxy</a>)";
192 0 $pxml = " (<a href='$pxml&format=xml'>get via proxy</a>)";
193 #$pttl = " (<a href='$pttl&format=turtle'>get via proxy</a>)";
194 }
195 0 $html .= "<h2 id='json'>Result in DAIA/JSON$pjson <a href='#top'>&#x2191;</a> <a href='#xml'>&#x2193;</a></h2>";
196 0 $html .= pre(escapeHTML( encode('utf8',$daia->json( $callback ) )));
197 0 $html .= "<h2 id='xml'>Result in DAIA/XML$pxml <a href='#json'>&#x2191;</a></h2>";
198 0 $html .= pre(escapeHTML( encode('utf8',$daia->xml( xmlns => 1 ) )));
199 0 ▶ if ($FORMATS->{turtle}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Plack::App::DAIA::Validator::FORMATS->{"turtle"}) 0 0 0
200 0 $html .= "<h2 id='ttl'>Result in DAIA/RDF (Turtle) <a href='#json'>&#x2191;</a></h2>";
201 0 my $ttl = $daia->serialize('turtle');
202 0 $html .= pre(escapeHTML( encode('utf8', $ttl )));
203 }
204 0 $html .= "</div>";
205 }
206  
207 1 $html .= "<div class='footer'>Based on ";
208 $html .= join ' and ', map {
209 1 ▶ "<a href='http://search.cpan.org/perldoc?$_'>$_</a> " . ($_->VERSION || '');
Condition50%1/2
$_->VERSION || ''
l!l
20
MC/DC0%0/1
$_->VERSION || ''
$_->VERSION
Truth table50%1/2
A
A= $_->VERSION
Aresult
11
00
210 } qw(Plack::App::DAIA::Validator DAIA);
211 1 $html .= <<HTML;
212 . Visit the <a href="http://github.com/gbv/daia/">DAIA project at github</a> for sources and details.
213 </div></body>
214 HTML
215  
216 1 return [ 200, [ 'Content-Type' => 'text/html; charset=utf-8' ], [ $html ] ];
217 }
218  
219 3 1;
220  
221 __END__
↑ Summary blib/lib/Plack/App/DAIA/Test/Suite.pm »