lib/OWL/Utils.pm

filter: Statementstmt 71.9% 59 / 82 Branchbran 41.6% 10 / 24 Conditioncond 16.6% 2 / 12 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 85.7% 12 / 14 Podpod 80.0% 4 / 5 totaltotal 60.0% 87 / 145 CC 19 SCAR 30.3
CC
19
Cov
60%
serialize
72.0
getHttpRequestByURL
5.9
find_file
5.1
CRAP 20.6
« lib/OWL/Base.pm ↑ Summary blib/script/owl2perl-install.pl »
1 #-----------------------------------------------------------------
2 # OWL::Utils
3 # Author: Edward Kawas <edward.kawas@gmail.com>
4 # For copyright and disclaimer see LICENSE.
5 #
6 # $Id: Utils.pm,v 1.2 2009-10-02 15:53:46 ubuntu Exp $
7 #-----------------------------------------------------------------
8
9 package OWL::Utils;
10 3 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use HTTP::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
13
14 # use statements for serializing OWL class
15 3 ▶use RDF::Core::Storage::Memory;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 3 ▶use RDF::Core::Model;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 3 ▶use RDF::Core::Model::Serializer;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 3 ▶use Scalar::Util 'blessed';
Subroutine BEGIN: called CC=1 SCAR=0.0
19
20 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
21
22 # add versioning to this module
23 3 ▶use vars qw /$VERSION/;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 3 $VERSION = sprintf "%d.%02d", q$Revision: 1.4 $ =~ /: (\d+)\.(\d+)/;
25
26 =head1 NAME
27
28 OWL::Utils - what does not fit elsewhere
29
30 =cut
31
32 =head1 SYNOPSIS
33
34 # load the Utils module
35 use OWL::Utils;
36
37 # find a file located somewhere in @INC
38 my $file = OWL::Utils->find_file ('resource.file');
39
40 # get file from url
41 $file = OWL::Utils->getHttpRequestByURL('http://sadiframework.org');
42
43 # remove leading/trailing whitespace from a string
44 print OWL::Utils->trim(' http://sadiframework.org ');
45
46 =cut
47
48 =head1 DESCRIPTION
49
50 General purpose utilities.
51
52 =cut
53
54 =head1 AUTHORS
55
56 Edward Kawas (edward.kawas [at] gmail [dot] com)
57 Martin Senger (martin.senger [at] gmail [dot] com)
58
59 =head1 SUBROUTINES
60
61 =cut
62
63 #-----------------------------------------------------------------
64 # find_file
65 #-----------------------------------------------------------------
66
67 =head2 find_file
68
69 Try to locate a file whose name is created from the C<$default_start>
70 and all elements of C<@names>. If it does not exist, try to replace
71 the C<$default_start> by elements of @INC (one by one). If neither of
72 them points to an existing file, go back and return the
73 C<$default_start> and all elements of C<@names> (even - as we know now
74 - such file does not exist).
75
76 There are two or more arguments: C<$default_start> and C<@names>.
77
78 =cut
79
80 3 my %full_path_of = ();
81
82 74 ▶sub find_file {
Subroutine find_file: called CC=5 SCAR=16.2
Pod documented
83 74 my ($self, $default_start, @names) = @_;
84 74 my $fixed_part = File::Spec->catfile (@names);
85 74 ▶ return $full_path_of{ $fixed_part } if exists $full_path_of{ $fixed_part };
Branch: 1 branch
BranchTrueFalseTotal
if exists $full_path_of{$fixed_part} 67 7 74
86
87 7 my $result = File::Spec->catfile ($default_start, $fixed_part);
88 7 ▶ if (-e $result) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $result) 0 7 7
89 0 $full_path_of{ $fixed_part } = $result;
90 0 return $result;
91 }
92
93 7 foreach my $idx (0 .. $#INC) {
94 27 $result = File::Spec->catfile ($INC[$idx], $fixed_part);
95 27 ▶ if (-e $result) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $result) 6 21 27
96 6 $full_path_of{ $fixed_part } = $result;
97 6 return $result;
98 }
99 }
100 1 $result = File::Spec->catfile ($default_start, $fixed_part);
101 1 $full_path_of{ $fixed_part } = $result;
102 1 return $result;
103 }
104
105 =head2 getHttpRequestByURL
106
107 returns a scalar of text obtained from the url or dies if there was no success
108
109 =cut
110
111 2 ▶sub getHttpRequestByURL {
Subroutine getHttpRequestByURL: called CC=5 SCAR=17.8
Pod documented
112 2 my ($self, $url) = @_;
113 2 ▶ $url = $self
Branch: 1 branch
BranchTrueFalseTotal
unless ref($self) =~ /^OWL::Utils/ 2 0 2
114 unless ref($self) =~ m/^OWL::Utils/;
115 2 my $ua = LWP::UserAgent->new;
116 2 $ua->agent( "Owl2perl/$VERSION");
117
118 2 my $req =
119 HTTP::Request->new( GET =>
120 $url );
121
122 # accept gzip encoding
123 2 $req->header( 'Accept-Encoding' => 'gzip' );
124
125 # send request
126 2 my $res = $ua->request($req);
127
128 # check the outcome
129 2 ▶ if ( $res->is_success ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->is_success) { } 2 0 2
130 2 ▶ if ( $res->header('content-encoding') and $res->header('content-encoding') eq 'gzip' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->header('content-encoding') and $res->header('content-encoding') eq 'gzip') { } 0 2 2
Condition33%1/3
$res->header("content-encoding") and $res->header("content-encoding") eq "gzip"
!ll&&!rl&&r
200
MC/DC0%0/2
$res->header("content-encoding") and $res->header("content-encoding") eq "gzip"
$res->header("content-encoding") $res->header("content-encoding") eq "gzip"
Truth table33%1/3
A and B
A= $res->header("content-encoding")B= $res->header("content-encoding") eq "gzip"
ABresult
0X0
100
111
131 0 return $res->decoded_content;
132 } else {
133 2 return $res->content;
134 }
135 } else {
136 0 die "Error getting data from URL:\n\t" . $res->status_line;
137 }
138 }
139
140 =head2 empty_rdf
141
142 returns a string of RDF that represents a syntactically correct RDF file
143
144 =cut
145
146 0 ▶sub empty_rdf {
Subroutine empty_rdf: not called CC=1 SCAR=6.9
Pod documented
147 0 return <<'END_OF_RDF';
148 <?xml version="1.0"?>
149 <rdf:RDF
150 xmlns:b="http://www.w3.org/2000/01/rdf-schema#"
151 xmlns:a="http://protege.stanford.edu/plugins/owl/dc/protege-dc.owl#"
152 xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
153 </rdf:RDF>
154 END_OF_RDF
155 }
156
157 =head2 trim
158
159 trims whitespace from the begining and end of a string
160
161 =cut
162
163 9 ▶sub trim {
Subroutine trim: called CC=4 SCAR=15.0
Pod documented
164 9 my ($self, $text) = @_;
165 9 ▶ $text = $self
Branch: 1 branch
BranchTrueFalseTotal
unless ref($self) =~ /^OWL::Utils/ or $self =~ /^OWL::Utils/ 0 9 9
Condition33%1/3
ref($self) =~ /^OWL::Utils/ or $self =~ /^OWL::Utils/
l!l&&r!l&&!r
090
MC/DC0%0/2
ref($self) =~ /^OWL::Utils/ or $self =~ /^OWL::Utils/
ref($self) =~ /^OWL::Utils/ $self =~ /^OWL::Utils/
Truth table33%1/3
A or B
A= ref($self) =~ /^OWL::Utils/B= $self =~ /^OWL::Utils/
ABresult
1X1
011
000
166 unless ref($self) =~ m/^OWL::Utils/ or $self =~ m/^OWL::Utils/;
167 # return empty string if $text is not defined
168 9 ▶ return "" unless $text;
Branch: 1 branch
BranchTrueFalseTotal
unless $text 0 9 9
169 9 $text =~ s/^\s+//;
170 9 $text =~ s/\s+$//;
171 9 return $text;
172 }
173
174 0 ▶sub serialize {
Subroutine serialize: not called CC=8 SCAR=42.8
Pod undocumented
175 0 my ($rdf_list) = @_;
176
177 # where we passed a list of items or a single item?
178 0 ▶ $rdf_list = [$rdf_list] if blessed $rdf_list && $rdf_list->isa('OWL::Data::OWL::Class');
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($rdf_list) and $rdf_list->isa("OWL::Data::OWL::Class") 0 0 0
Condition0%0/3
builtin::blessed($rdf_list) and $rdf_list->isa("OWL::Data::OWL::Class")
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($rdf_list) and $rdf_list->isa("OWL::Data::OWL::Class")
builtin::blessed($rdf_list) $rdf_list->isa("OWL::Data::OWL::Class")
Truth table0%0/3
A and B
A= builtin::blessed($rdf_list)B= $rdf_list->isa("OWL::Data::OWL::Class")
ABresult
0X0
100
111
179
180 # construct a model
181 0 my $model = new RDF::Core::Model( Storage => new RDF::Core::Storage::Memory );
182
183 # our xml string
184 0 my $xml = '';
185
186 # iterate over the list
187 0 foreach my $class (@$rdf_list) {
188 0 ▶ if (blessed $class && $class->isa('OWL::Data::OWL::Class')) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($class) and $class->isa("OWL::Data::OWL::Class")) 0 0 0
Condition0%0/3
builtin::blessed($class) and $class->isa("OWL::Data::OWL::Class")
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($class) and $class->isa("OWL::Data::OWL::Class")
builtin::blessed($class) $class->isa("OWL::Data::OWL::Class")
Truth table0%0/3
A and B
A= builtin::blessed($class)B= $class->isa("OWL::Data::OWL::Class")
ABresult
0X0
100
111
189 # clear the statements
190 0 $class->clear_statements;
191 # add each statement
192 0 my $enumerator = $class->_get_statements();
193 0 ▶ next unless defined $enumerator;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $enumerator 0 0 0
194 0 my $statement = $enumerator->getFirst;
195 0 ▶ while (defined $statement) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $statement) 0 0 0
196 0 $model->addStmt($statement);
197 0 $statement = $enumerator->getNext
198 }
199 0 $enumerator->close;
200 # clear the statements
201 0 $class->clear_statements;
202 }
203 }
204
205 # print out the XML
206 0 my $serializer = new RDF::Core::Model::Serializer(
207 Model => $model,
208 Output => \$xml,
209 );
210 0 $serializer->serialize;
211
212 0 return $xml;
213 }
214
215 3 1;
216 __END__
« lib/OWL/Base.pm ↑ Summary blib/script/owl2perl-install.pl »