lib/MOSES/MOBY/Base.pm

filter: Statementstmt 50.1% 155 / 309 Branchbran 11.8% 14 / 118 Conditioncond 14.2% 7 / 49 MC/DCmcdc 3.4% 1 / 29 Subroutinesub 58.7% 37 / 63 Podpod 23.0% 6 / 26 totaltotal 37.0% 220 / 594 CC 81 SCAR 37.5
CC
81
Cov
37%
check_type
237.5
_reformat_stacktrace
90.0
throw
53.6
CRAP 42.6
« lib/MOSES/MOBY/Cache/Registries.pm ↑ Summary lib/MOSES/MOBY/Def/Data.pm »
1 #-----------------------------------------------------------------
2 # MOSES::MOBY::Base
3 # Author: Martin Senger <martin.senger@gmail.com>
4 # For copyright and disclaimer see below.
5 #
6 # $Id: Base.pm,v 1.6 2008/11/06 18:32:33 kawas Exp $
7 #-----------------------------------------------------------------
8  
9 package MOSES::MOBY::Base;
10  
11 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 2 ▶use HTTP::Date;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 2 ▶use vars qw( $VERSION $Revision $AUTOLOAD @EXPORT @ISA );
Subroutine BEGIN: called CC=1 SCAR=0.0
16 2 ▶use vars qw( $LOG $LOGGER_NAME $CONFIG_NAMESPACE );
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 # names of attribute's types
19 2 ▶use constant STRING => 'string';
Subroutine BEGIN: called CC=1 SCAR=0.0
20 2 ▶use constant INTEGER => 'integer';
Subroutine BEGIN: called CC=1 SCAR=0.0
21 2 ▶use constant FLOAT => 'float';
Subroutine BEGIN: called CC=1 SCAR=0.0
22 2 ▶use constant BOOLEAN => 'boolean';
Subroutine BEGIN: called CC=1 SCAR=0.0
23 2 ▶use constant DATETIME => 'datetime';
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 # names of attribute's properties
26 2 ▶use constant TYPE => 'type';
Subroutine BEGIN: called CC=1 SCAR=0.0
27 2 ▶use constant POST => 'post';
Subroutine BEGIN: called CC=1 SCAR=0.0
28 2 ▶use constant ISARRAY => 'is_array';
Subroutine BEGIN: called CC=1 SCAR=0.0
29 2 ▶use constant READONLY => 'readonly';
Subroutine BEGIN: called CC=1 SCAR=0.0
30  
31 2 ▶use overload q("") => "as_string";
Subroutine BEGIN: called CC=1 SCAR=0.0
32  
33 2 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.4
34 2 @ISA = qw( Exporter );
35 2 @EXPORT = qw( $LOG );
36  
37 2 $VERSION = sprintf "%d.%02d", q$Revision: 1.6 $ =~ /: (\d+)\.(\d+)/;
38 2 $Revision = '$Id: Base.pm,v 1.6 2008/11/06 18:32:33 kawas Exp $';
39  
40 # initiate error handling
41 2 require Carp; import Carp qw( confess );
42  
43 # read default configuration file and import configuration
44 # parameters into 'MOBYCFG namespace'
45 2 ▶ use MOSES::MOBY::Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
46 2 $CONFIG_NAMESPACE = 'MOBYCFG';
47 2 ▶ sub init_config {
Subroutine init_config: called CC=1 SCAR=0.0
Pod documented
48 2 shift; # invocant ignored
49 2 MOSES::MOBY::Config->init (@_);
50 2 MOSES::MOBY::Config->import_names ($CONFIG_NAMESPACE);
51 }
52 2 MOSES::MOBY::Base->init_config;
53  
54 # initiate logging
55 2 ▶ use Log::Log4perl qw(get_logger :levels :no_extra_logdie_message);
Subroutine BEGIN: called CC=1 SCAR=0.0
56 2 $LOGGER_NAME = 'services';
57 2 ▶ sub init_logging {
Subroutine init_logging: called CC=8 SCAR=29.1
Pod undocumented
58 2 ▶ if ($MOBYCFG::LOG_CONFIG) {
Branch: 1 branch
BranchTrueFalseTotal
if ($MOBYCFG::LOG_CONFIG) 0 2 2
59 0 eval { Log::Log4perl->init ($MOBYCFG::LOG_CONFIG) };
60 0 ▶ $LOG = get_logger ($LOGGER_NAME) and return unless $@;
Branch: 1 branch
BranchTrueFalseTotal
unless $@ 0 0 0
Condition0%0/5
$@ || ($MOSES::MOBY::Base::LOG = get_logger($MOSES::MOBY::Base::LOGGER_NAME)) && (return)
l!l&&r!l&&!r
000
$MOSES::MOBY::Base::LOG = get_logger($MOSES::MOBY::Base::LOGGER_NAME) and return
!ll
00
MC/DC0%0/2
$@ || ($MOSES::MOBY::Base::LOG = get_logger($MOSES::MOBY::Base::LOGGER_NAME)) && (return)
$@ $MOSES::MOBY::Base::LOG = get_logger($MOSES::MOBY::Base::LOGGER_NAME)
Truth table0%0/3
A || B
A= $@B= $MOSES::MOBY::Base::LOG = get_logger($MOSES::MOBY::Base::LOGGER_NAME)
ABresult
1X1
011
000
61 0 print STDERR "Problem with configuration file '$MOBYCFG::LOG_CONFIG': $@\n";
62 }
63 # configuration for logging not found; make some easy logging
64 2 my $logfile = $MOBYCFG::LOG_FILE;
65 2 ▶ my $loglevel = $MOBYCFG::LOG_LEVEL || $ERROR;
Condition33%1/3
$MOBYCFG::LOG_LEVEL || $MOSES::MOBY::Base::ERROR
l!l&&r!l&&!r
020
MC/DC0%0/2
$MOBYCFG::LOG_LEVEL || $MOSES::MOBY::Base::ERROR
$MOBYCFG::LOG_LEVEL $MOSES::MOBY::Base::ERROR
Truth table33%1/3
A || B
A= $MOBYCFG::LOG_LEVELB= $MOSES::MOBY::Base::ERROR
ABresult
1X1
011
000
66 2 ▶ my $pattern = $MOBYCFG::LOG_PATTERN || '%d (%r) %p> [%x] %F{1}:%L - %m%n';
Condition50%1/2
$MOBYCFG::LOG_PATTERN || '%d (%r) %p> [%x] %F{1}:%L - %m%n'
l!l
02
MC/DC0%0/1
$MOBYCFG::LOG_PATTERN || '%d (%r) %p> [%x] %F{1}:%L - %m%n'
$MOBYCFG::LOG_PATTERN
Truth table50%1/2
A
A= $MOBYCFG::LOG_PATTERN
Aresult
11
00
67 2 $LOG = get_logger ($LOGGER_NAME);
68 2 $LOG->level (uc $loglevel);
69 2 ▶ my $appender =
Branch: 1 branch
BranchTrueFalseTotal
$logfile && !($logfile =~ /^stderr$/i) ? : 0 2 2
Condition33%1/3
$logfile and not $logfile =~ /^stderr$/i
!ll&&!rl&&r
200
MC/DC0%0/2
$logfile and not $logfile =~ /^stderr$/i
$logfile not $logfile =~ /^stderr$/i
Truth table33%1/3
A and not(B)
A= $logfileB= not $logfile =~ /^stderr$/i
ABresult
0X0
100
111
70 ($logfile and $logfile !~ /^stderr$/i) ?
71 Log::Log4perl::Appender->new ("Log::Log4perl::Appender::File",
72 name => 'Log',
73 filename => $logfile,
74 mode => 'append') :
75 Log::Log4perl::Appender->new ("Log::Log4perl::Appender::Screen",
76 name => 'Screen');
77 2 $LOG->add_appender ($appender);
78 2 my $layout = Log::Log4perl::Layout::PatternLayout->new ($pattern);
79 2 $appender->layout ($layout);
80 }
81 2 MOSES::MOBY::Base->init_logging;
82 }
83  
84  
85 #-----------------------------------------------------------------
86 # These methods are called by set/get methods of the sub-classes. If
87 # it comes here, it indicates that an attribute being get/set does not
88 # exist.
89 #-----------------------------------------------------------------
90  
91 {
92 2 my %_allowed =
93 (
94 );
95  
96 0 ▶ sub _accessible {
Subroutine _accessible: not called CC=1 SCAR=6.9
97 0 my ($self, $attr) = @_;
98 0 exists $_allowed{$attr};
99 }
100 4 ▶ sub _attr_prop {
Subroutine _attr_prop: called CC=3 SCAR=12.5
101 4 my ($self, $attr_name, $prop_name) = @_;
102 4 my $attr = $_allowed {$attr_name};
103 4 ▶ return ref ($attr) ? $attr->{$prop_name} : $attr if $attr;
Branch: 2 branches
BranchTrueFalseTotal
if $attr 0 4 4
ref $attr ? : 0 0 0
104 4 return undef;
105 }
106 }
107  
108 #-----------------------------------------------------------------
109 # new
110 #-----------------------------------------------------------------
111 2 ▶sub new {
Subroutine new: called CC=4 SCAR=14.1
Pod documented
112 6 my ($class, @args) = @_;
113 # $LOG->debug ("NEW: $class - " . join (", ", @args)) if $LOG->is_debug;
114  
115 # create an object
116 6 ▶ my $self = bless {}, ref ($class) || $class;
Condition33%1/3
ref $class || $class
l!l&&r!l&&!r
060
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table33%1/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
117  
118 # initialize the object
119 6 $self->init();
120  
121 # set all @args into this object with 'set' values
122 6 ▶ my (%args) = (@args == 1 ? (value => $args[0]) : @args);
Branch: 1 branch
BranchTrueFalseTotal
@args == 1 ? : 0 6 6
123 6 foreach my $key (keys %args) {
124 2 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
125 2 $self->$key ($args {$key});
126 }
127  
128 # done
129 6 return $self;
130 }
131  
132 #-----------------------------------------------------------------
133 # init
134 #-----------------------------------------------------------------
135 2 ▶sub init {
Subroutine init: called CC=1 SCAR=0.0
Pod documented
136 2 my ($self) = shift;
137 }
138  
139 #-----------------------------------------------------------------
140 # toString
141 #-----------------------------------------------------------------
142 0 ▶sub toString {
Subroutine toString: not called CC=2 SCAR=17.9
Pod documented
143 0 my ($self, $something_else) = @_;
144 0 require Data::Dumper;
145 0 ▶ if ($something_else) {
Branch: 1 branch
BranchTrueFalseTotal
if ($something_else) { } 0 0 0
146 0 return Data::Dumper->Dump ( [$something_else], ['M']);
147 } else {
148 0 return Data::Dumper->Dump ( [$self], ['M']);
149 }
150 }
151  
152 #-----------------------------------------------------------------
153 # module_name_escape
154 #-----------------------------------------------------------------
155 0 ▶sub module_name_escape {
Subroutine module_name_escape: not called CC=1 SCAR=6.9
Pod undocumented
156 0 my ($self, $name) = @_;
157 0 $name =~ tr/-/_/;
158 0 return $name;
159 }
160  
161 #-----------------------------------------------------------------
162 # datatype2module
163 #-----------------------------------------------------------------
164 0 ▶sub datatype2module {
Subroutine datatype2module: not called CC=2 SCAR=17.9
Pod undocumented
165 0 my ($self, $datatype_name) = @_;
166 0 ▶ return undef unless $datatype_name;
Branch: 1 branch
BranchTrueFalseTotal
unless $datatype_name 0 0 0
167 0 return 'MOSES::MOBY::Data::' . $self->module_name_escape ($datatype_name);
168 }
169  
170 #-----------------------------------------------------------------
171 # service2module
172 #-----------------------------------------------------------------
173 0 ▶sub service2module {
Subroutine service2module: not called CC=3 SCAR=24.8
Pod undocumented
174 0 my ($self, $authority, $service_name) = @_;
175  
176 # default values that will be, at the end, however, rarely used
177 0 ▶ $authority = 'org.biomoby.service' unless $authority;
Branch: 1 branch
BranchTrueFalseTotal
unless $authority 0 0 0
178 0 ▶ $service_name = 'TheService' unless $service_name;
Branch: 1 branch
BranchTrueFalseTotal
unless $service_name 0 0 0
179  
180 return
181 0 join ('::', reverse split (/\./, $self->module_name_escape ($authority))) .
182 '::' .
183 $service_name .
184 'Base';
185 }
186  
187 #-----------------------------------------------------------------
188 # escape_name
189 #-----------------------------------------------------------------
190 0 ▶sub escape_name {
Subroutine escape_name: not called CC=2 SCAR=17.9
Pod undocumented
191 0 my ($self, $name) = @_;
192 0 $name =~ s/\W/_/g;
193 0 ▶ return ($name =~ /^\d/ ? "_$name" : $name);
Branch: 1 branch
BranchTrueFalseTotal
$name =~ /^\d/ ? : 0 0 0
194 }
195  
196  
197 #-----------------------------------------------------------------
198 #
199 # Error handling
200 #
201 #-----------------------------------------------------------------
202  
203 2 my $DEFAULT_THROW_WITH_LOG = 0;
204 2 my $DEFAULT_THROW_WITH_STACK = 1;
205  
206 #-----------------------------------------------------------------
207 # throw
208 #-----------------------------------------------------------------
209 0 ▶sub throw {
Subroutine throw: not called CC=8 SCAR=39.8
Pod documented
210 0 my ($self, $msg) = @_;
211 0 ▶ $msg .= "\n" unless $msg =~ /\n$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $msg =~ /\n$/ 0 0 0
212  
213 # make an instance, if called as a class method
214 0 ▶ unless (ref $self) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $self) 0 0 0
215 2 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
216 0 $self = $self->new;
217 }
218  
219 # add (optionally) stack trace
220 0 ▶ $msg ||= 'An error.';
Condition0%0/2
$msg ||= "An error."
l!l
00
MC/DC0%0/1
$msg ||= "An error."
$msg
Truth table0%0/2
A
A= $msg
Aresult
11
00
221 0 ▶ my $with_stack = (defined $self->enable_throw_with_stack ?
Branch: 1 branch
BranchTrueFalseTotal
defined $self->enable_throw_with_stack ? : 0 0 0
222 $self->enable_throw_with_stack :
223 $DEFAULT_THROW_WITH_STACK);
224 0 ▶ my $result = ($with_stack ? $self->format_stack ($msg) : $msg);
Branch: 1 branch
BranchTrueFalseTotal
$with_stack ? : 0 0 0
225  
226 # die or log and die?
227 0 ▶ my $with_log = (defined $self->enable_throw_with_log ?
Branch: 1 branch
BranchTrueFalseTotal
defined $self->enable_throw_with_log ? : 0 0 0
228 $self->enable_throw_with_log :
229 $DEFAULT_THROW_WITH_LOG);
230 0 ▶ if ($with_log) {
Branch: 1 branch
BranchTrueFalseTotal
if ($with_log) { } 0 0 0
231 0 $LOG->logdie ($result);
232 } else {
233 0 die ($result);
234 }
235 }
236  
237 #-----------------------------------------------------------------
238 # Some throwing options
239 #
240 # These options are not set by using AUTOLOAD (as other regular
241 # attributes) because AUTOLOAD could raise exception and we would be
242 # in a deep..., well deep recursion.
243 #
244 # Default values are: NO enable_throw_with_log
245 # YES enable_throw_with_stack
246 # (but they are globally changeable by calling
247 # default_throw_with_log and default_throw_with_stack)
248 #
249 #-----------------------------------------------------------------
250 0 ▶sub enable_throw_with_log {
Subroutine enable_throw_with_log: not called CC=3 SCAR=24.8
Pod undocumented
251 0 my ($self, $value) = @_;
252 0 ▶ $self->{enable_throw_with_log} = ($value ? 1 : 0)
Branch: 2 branches
BranchTrueFalseTotal
if defined $value 0 0 0
$value ? : 0 0 0
253 if (defined $value);
254 0 return $self->{enable_throw_with_log};
255 }
256  
257 0 ▶sub default_throw_with_log {
Subroutine default_throw_with_log: not called CC=3 SCAR=24.8
Pod undocumented
258 0 my ($self, $value) = @_;
259 0 ▶ $DEFAULT_THROW_WITH_LOG = ($value ? 1 : 0)
Branch: 2 branches
BranchTrueFalseTotal
if defined $value 0 0 0
$value ? : 0 0 0
260 if defined $value;
261 0 return $DEFAULT_THROW_WITH_LOG;
262 }
263  
264 0 ▶sub enable_throw_with_stack {
Subroutine enable_throw_with_stack: not called CC=3 SCAR=24.8
Pod undocumented
265 0 my ($self, $value) = @_;
266 0 ▶ $self->{enable_throw_with_stack} = ($value ? 1 : 0)
Branch: 2 branches
BranchTrueFalseTotal
if defined $value 0 0 0
$value ? : 0 0 0
267 if defined $value;
268 0 return $self->{enable_throw_with_stack};
269 }
270  
271 0 ▶sub default_throw_with_stack {
Subroutine default_throw_with_stack: not called CC=3 SCAR=24.8
Pod undocumented
272 0 my ($self, $value) = @_;
273 0 ▶ $DEFAULT_THROW_WITH_STACK = ($value ? 1 : 0)
Branch: 2 branches
BranchTrueFalseTotal
if defined $value 0 0 0
$value ? : 0 0 0
274 if defined $value;
275 0 return $DEFAULT_THROW_WITH_STACK;
276 }
277  
278 #-----------------------------------------------------------------
279 # format_stack
280 #-----------------------------------------------------------------
281 0 ▶sub format_stack {
Subroutine format_stack: not called CC=2 SCAR=17.9
Pod documented
282 0 my ($self, $msg) = @_;
283 0 my $stack = $self->_reformat_stacktrace ($msg);
284 0 ▶ my $class = ref ($self) || $self;
Condition0%0/3
ref $self || $self
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table0%0/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
285  
286 0 my $title = "------------- EXCEPTION: $class -------------";
287 0 my $footer = "\n" . '-' x CORE::length ($title);
288 0 return "\n$title\nMSG: $msg\n" . $stack . $footer . "\n";
289 }
290  
291  
292 #-----------------------------------------------------------------
293 # _reformat_stacktrace
294 # Taken from bioperl.
295 #
296 # Takes one argument - an error message. It uses it to remove its
297 # repeated occurences from each line (not to print it).
298 #
299 # Reformatting of the stack:
300 # 1. Shift the file:line data in line i to line i+1.
301 # 2. change xxx::__ANON__() to "try{} block"
302 # 3. skip the "require" and "Error::subs::try" stack entries (boring)
303 # This means that the first line in the stack won't have
304 # any file:line data.
305 #-----------------------------------------------------------------
306 0 ▶sub _reformat_stacktrace {
Subroutine _reformat_stacktrace: not called CC=9 SCAR=45.0
307 0 my ($self, $msg) = @_;
308 0 my $stack = Carp->longmess;
309 0 $stack =~ s/\Q$msg//;
310 0 my @stack = split( /\n/, $stack);
311 0 my @new_stack = ();
312 0 my ($method, $file, $linenum, $prev_file, $prev_linenum);
313 0 my $stack_count = 0;
314 0 foreach my $i ( 0..$#stack ) {
315 0 ▶ if ( ($stack[$i] =~ /^\s*([^(]+)\s*\(.*\) called at (\S+) line (\d+)/) ||
Branch: 1 branch
BranchTrueFalseTotal
if ($stack[$i] =~ /^\s*([^(]+)\s*\(.*\) called at (\S+) line (\d+)/ or $stack[$i] =~ /^\s*(require 0) called at (\S+) line (\d+)/) { } 0 0 0
Condition0%0/3
$stack[$i] =~ /^\s*([^(]+)\s*\(.*\) called at (\S+) line (\d+)/ or $stack[$i] =~ /^\s*(require 0) called at (\S+) line (\d+)/
l!l&&r!l&&!r
000
MC/DC0%0/2
$stack[$i] =~ /^\s*([^(]+)\s*\(.*\) called at (\S+) line (\d+)/ or $stack[$i] =~ /^\s*(require 0) called at (\S+) line (\d+)/
$stack[$i] =~ /^\s*([^(]+)\s*\(.*\) called at (\S+) line (\d+)/ $stack[$i] =~ /^\s*(require 0) called at (\S+) line (\d+)/
Truth table0%0/3
A or B
A= $stack[$i] =~ /^\s*([^(]+)\s*\(.*\) called at (\S+) line (\d+)/B= $stack[$i] =~ /^\s*(require 0) called at (\S+) line (\d+)/
ABresult
1X1
011
000
316 ($stack[$i] =~ /^\s*(require 0) called at (\S+) line (\d+)/) ) {
317 0 ($method, $file, $linenum) = ($1, $2, $3);
318 0 $stack_count++;
319 } else {
320 0 next;
321 }
322 0 ▶ if( $stack_count == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($stack_count == 1) 0 0 0
323 0 push @new_stack, "STACK: $method";
324 0 ($prev_file, $prev_linenum) = ($file, $linenum);
325 0 next;
326 }
327  
328 0 ▶ if( $method =~ /__ANON__/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($method =~ /__ANON__/) 0 0 0
329 0 $method = "try{} block";
330 }
331 0 ▶ if( ($method =~ /^require/ and $file =~ /Error\.pm/ ) ||
Branch: 1 branch
BranchTrueFalseTotal
if ($method =~ /^require/ and $file =~ /Error\.pm/ or $method =~ /^Error::subs::try/) 0 0 0
Condition0%0/6
$method =~ /^require/ and $file =~ /Error\.pm/ or $method =~ /^Error::subs::try/
l!l&&r!l&&!r
000
$method =~ /^require/ and $file =~ /Error\.pm/
!ll&&!rl&&r
000
MC/DC0%0/3
$method =~ /^require/ and $file =~ /Error\.pm/ or $method =~ /^Error::subs::try/
$method =~ /^require/ $file =~ /Error\.pm/ $method =~ /^Error::subs::try/
Truth table0%0/5
A and B or C
A= $method =~ /^require/B= $file =~ /Error\.pm/C= $method =~ /^Error::subs::try/
ABCresult
11X1
0X11
1011
0X00
1000
332 ($method =~ /^Error::subs::try/ ) ) {
333 0 last;
334 }
335 0 push @new_stack, "STACK: $method $prev_file:$prev_linenum";
336 0 ($prev_file, $prev_linenum) = ($file, $linenum);
337 }
338 0 push @new_stack, "STACK: $prev_file:$prev_linenum";
339  
340 0 return join "\n", @new_stack;
341 }
342  
343 #-----------------------------------------------------------------
344 # Set methods test whether incoming value is of a correct type.
345 # Here we return message explaining that it isn't.
346 #-----------------------------------------------------------------
347 0 ▶sub _wrong_type_msg {
Subroutine _wrong_type_msg: not called CC=2 SCAR=17.9
348 0 my ($self, $given_type_or_value, $expected_type, $method) = @_;
349 0 my $msg = 'In method ';
350 0 ▶ if (defined $method) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $method) { } 0 0 0
351 0 $msg .= $method;
352 } else {
353 0 $msg .= (caller(1))[3];
354 }
355 0 return ("$msg: Trying to set '$given_type_or_value' but '$expected_type' is expected.");
356 }
357  
358  
359 #-----------------------------------------------------------------
360 #
361 # Dealing with creating XML...
362 #
363 #-----------------------------------------------------------------
364 2 ▶use MOSES::MOBY::Tags;
Subroutine BEGIN: called CC=1 SCAR=0.0
365 2 ▶use XML::LibXML;
Subroutine BEGIN: called CC=1 SCAR=0.0
366 {
367  
368 # using a counter in order to avoid to clean namespaces several
369 # times - there must be a better way to do it (TBD?, well but
370 # how?)
371  
372 2 my $xml_counter = 0;
373  
374 0 ▶ sub increaseXMLCounter { $xml_counter++; }
Subroutine increaseXMLCounter: not called CC=1 SCAR=6.9
Pod undocumented
375 0 ▶ sub decreaseXMLCounter { $xml_counter--; }
Subroutine decreaseXMLCounter: not called CC=1 SCAR=6.9
Pod undocumented
376 0 ▶ sub emptyXMLCounter { return $xml_counter == 0; }
Subroutine emptyXMLCounter: not called CC=1 SCAR=6.9
Pod undocumented
377 }
378  
379 # called at the end of toXML() methods: it returns $root
380 2 ▶sub closeXML {
Subroutine closeXML: not called CC=2 SCAR=16.1
Pod undocumented
381 0 my ($self, $root) = @_;
382 0 $self->decreaseXMLCounter;
383 0 ▶ if ($self->emptyXMLCounter) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->emptyXMLCounter) { } 0 0 0
384 0 my $parser = XML::LibXML::->new();
385 0 $parser->clean_namespaces (1);
386 0 my $doc = $parser->parse_string ($root->toString());
387 0 return $doc->documentElement;
388 } else {
389 0 return $root;
390 }
391 }
392  
393 # creates a prefixed LibXML::Element given a name, in the MOBY XML
394 # namespace
395 0 ▶sub createXMLElement {
Subroutine createXMLElement: not called CC=1 SCAR=6.9
Pod undocumented
396 0 my ($self, $elementName) = @_;
397 0 my $element = XML::LibXML::Element->new ($elementName);
398 0 $element->setNamespace (MOBY_XML_NS, MOBY_XML_NS_PREFIX);
399 0 return $element;
400 }
401  
402 # set $value as an (MOBY namespaced) XML attribute $name into $element
403 # (an XML::LibXML type); do it only if $value is not empty
404 0 ▶sub setXMLAttribute {
Subroutine setXMLAttribute: not called CC=4 SCAR=30.0
Pod undocumented
405 0 my ($self, $element, $name, $value) = @_;
406 0 ▶ return unless $value and $name;
Branch: 1 branch
BranchTrueFalseTotal
unless $value and $name 0 0 0
Condition0%0/3
$value and $name
!ll&&!rl&&r
000
MC/DC0%0/2
$value and $name
$value $name
Truth table0%0/3
A and B
A= $valueB= $name
ABresult
0X0
100
111
407  
408 # trim the value
409 0 $value =~ s/^\s*//;
410 0 $value =~ s/\s*$//;
411 0 ▶ return unless $value;
Branch: 1 branch
BranchTrueFalseTotal
unless $value 0 0 0
412  
413 0 $element->setAttributeNS (MOBY_XML_NS, $name, $value);
414 }
415  
416 # return a value of an attribute named $name from an $element (an
417 # XML::LibXML type); try both with and without namespaces; return
418 # undef if no such sttribute found
419 0 ▶sub getXMLAttribute {
Subroutine getXMLAttribute: not called CC=2 SCAR=17.9
Pod undocumented
420 0 my ($self, $element, $name) = @_;
421 return
422 0 ▶ $element->getAttribute ($name) ||
Condition0%0/3
$element->getAttribute($name) || $element->getAttributeNS('http://www.biomoby.org/moby', $name)
l!l&&r!l&&!r
000
MC/DC0%0/2
$element->getAttribute($name) || $element->getAttributeNS('http://www.biomoby.org/moby', $name)
$element->getAttribute($name) $element->getAttributeNS('http://www.biomoby.org/moby', $name)
Truth table0%0/3
A || B
A= $element->getAttribute($name)B= $element->getAttributeNS('http://www.biomoby.org/moby', $name)
ABresult
1X1
011
000
423 $element->getAttributeNS (MOBY_XML_NS, $name);
424 }
425  
426 # return an XML document: calling first toXML on the caller, then
427 # wraps it as an XML document
428 0 ▶sub toXMLdocument {
Subroutine toXMLdocument: not called CC=1 SCAR=6.9
Pod undocumented
429 0 my $self = shift;
430 0 my $doc = XML::LibXML->createDocument;
431 0 $doc->setDocumentElement ($self->toXML);
432 0 return $doc;
433 }
434  
435  
436 #-----------------------------------------------------------------
437 # Deal with 'set', 'get' and 'add_' methods.
438 #-----------------------------------------------------------------
439 4 ▶sub AUTOLOAD {
Subroutine AUTOLOAD: called CC=8 SCAR=26.2
440 4 my ($self, @new_values) = @_;
441 4 my $ref_sub;
442 4 ▶ if ($AUTOLOAD =~ /.*::(\w+)/ && $self->_accessible ("$1")) {
Branch: 2 branches
BranchTrueFalseTotal
if ($MOSES::MOBY::Base::AUTOLOAD =~ /.*::(\w+)/ and $self->_accessible("$1")) { } 4 0 4
elsif ($MOSES::MOBY::Base::AUTOLOAD =~ /.*::add_(\w+)/ and $self->_accessible("$1")) { } 0 0 0
Condition33%1/3
$MOSES::MOBY::Base::AUTOLOAD =~ /.*::(\w+)/ and $self->_accessible("$1")
!ll&&!rl&&r
004
MC/DC0%0/2
$MOSES::MOBY::Base::AUTOLOAD =~ /.*::(\w+)/ and $self->_accessible("$1")
$MOSES::MOBY::Base::AUTOLOAD =~ /.*::(\w+)/ $self->_accessible("$1")
Truth table33%1/3
A and B
A= $MOSES::MOBY::Base::AUTOLOAD =~ /.*::(\w+)/B= $self->_accessible("$1")
ABresult
0X0
100
111
443  
444 # get/set method
445 4 my $attr_name = "$1";
446 4 ▶ my $attr_type = $self->_attr_prop ($attr_name, TYPE) || STRING;
Condition100%2/2
$self->_attr_prop($attr_name, 'type') || 'string'
l!l
22
MC/DC100%1/1
$self->_attr_prop($attr_name, 'type') || 'string'
$self->_attr_prop($attr_name, 'type')
Truth table100%2/2
A
A= $self->_attr_prop($attr_name, 'type')
Aresult
11
00
447 4 my $attr_post = $self->_attr_prop ($attr_name, POST);
448 4 my $attr_is_array = $self->_attr_prop ($attr_name, ISARRAY);
449 4 my $attr_readonly = $self->_attr_prop ($attr_name, READONLY);
450 $ref_sub =
451 22 ▶ sub {
Subroutine __ANON__: called CC=7 SCAR=19.6
452 22 local *__ANON__ = "__ANON__$attr_name" . "_" . ref ($self);
453 22 my ($this, @values) = @_;
454 22 ▶ return $this->_getter ($attr_name) unless @values;
Branch: 1 branch
BranchTrueFalseTotal
unless @values 12 10 22
455 10 ▶ $self->throw ("Sorry, the attribute '$attr_name' is read-only.")
Branch: 1 branch
BranchTrueFalseTotal
if $attr_readonly 0 10 10
456 if $attr_readonly;
457  
458 # here we continue with 'set' method:
459 10 ▶ if ($attr_is_array) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attr_is_array) { } 2 8 10
460 2 ▶ my @result = (ref ($values[0]) eq 'ARRAY' ? @{$values[0]} : @values);
Branch: 1 branch
BranchTrueFalseTotal
ref $values[0] eq 'ARRAY' ? : 2 0 2
461 2 foreach my $value (@result) {
462 0 $value = $this->check_type ($AUTOLOAD, $attr_type, $value);
463 }
464 2 $this->_setter ($attr_name, $attr_type, \@result);
465 } else {
466 8 $this->_setter ($attr_name, $attr_type, $this->check_type ($AUTOLOAD, $attr_type, @values));
467 }
468  
469 # call post-procesing (if defined)
470 10 ▶ $this->$attr_post ($this->{$attr_name}) if $attr_post;
Branch: 1 branch
BranchTrueFalseTotal
if $attr_post 3 7 10
471  
472 10 return $this->{$attr_name};
473 4 };
474  
475 ▶ } elsif ($AUTOLOAD =~ /.*::add_(\w+)/ && $self->_accessible ("$1")) {
Condition0%0/3
$MOSES::MOBY::Base::AUTOLOAD =~ /.*::add_(\w+)/ and $self->_accessible("$1")
!ll&&!rl&&r
000
MC/DC0%0/2
$MOSES::MOBY::Base::AUTOLOAD =~ /.*::add_(\w+)/ and $self->_accessible("$1")
$MOSES::MOBY::Base::AUTOLOAD =~ /.*::add_(\w+)/ $self->_accessible("$1")
Truth table0%0/3
A and B
A= $MOSES::MOBY::Base::AUTOLOAD =~ /.*::add_(\w+)/B= $self->_accessible("$1")
ABresult
0X0
100
111
476  
477 # add_XXXX method
478 0 my $attr_name = "$1";
479 0 ▶ if ($self->_attr_prop ($attr_name, ISARRAY)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_attr_prop($attr_name, 'is_array')) { } 0 0 0
480 0 ▶ my $attr_type = $self->_attr_prop ($attr_name, TYPE) || STRING;
Condition0%0/2
$self->_attr_prop($attr_name, 'type') || 'string'
l!l
00
MC/DC0%0/1
$self->_attr_prop($attr_name, 'type') || 'string'
$self->_attr_prop($attr_name, 'type')
Truth table0%0/2
A
A= $self->_attr_prop($attr_name, 'type')
Aresult
11
00
481 $ref_sub =
482 0 ▶ sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
483 0 local *__ANON__ = "__ANON__$attr_name" . "_" . ref ($self);
484 0 my ($this, @values) = @_;
485 0 ▶ if (@values) {
Branch: 1 branch
BranchTrueFalseTotal
if (@values) 0 0 0
486 0 ▶ my @result = (ref ($values[0]) eq 'ARRAY' ? @{$values[0]} : @values);
Branch: 1 branch
BranchTrueFalseTotal
ref $values[0] eq 'ARRAY' ? : 0 0 0
487 0 foreach my $value (@result) {
488 0 $value = $this->check_type ($AUTOLOAD, $attr_type, $value);
489 }
490 0 $this->_adder ($attr_name, $attr_type, @result);
491 }
492 0 return $this;
493 }
494 0 } else {
495 0 $self->throw ("Method '$AUTOLOAD' is allowed only for array-type attributes.");
496 }
497  
498 } else {
499 0 $self->throw ("No such method: $AUTOLOAD");
500 }
501  
502 2 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
503 4 *{$AUTOLOAD} = $ref_sub;
504 2 ▶ use strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
505 4 return $ref_sub->($self, @new_values);
506 }
507  
508 #-----------------------------------------------------------------
509 # The low level get/set methods. They are called from AUTOLOAD, and
510 # they are separated here so they can be overriten - as they are in
511 # the service skeletons, for example. Also, there may be situation
512 # that one can call them if other features (such as type checking) are
513 # not requiered.
514 #-----------------------------------------------------------------
515 12 ▶sub _getter {
Subroutine _getter: called CC=1 SCAR=0.0
516 12 my ($self, $attr_name) = @_;
517 12 return $self->{$attr_name};
518 }
519  
520 10 ▶sub _setter {
Subroutine _setter: called CC=1 SCAR=0.0
521 10 my ($self, $attr_name, $attr_type, $value) = @_;
522 10 $self->{$attr_name} = $value;
523 }
524  
525 0 ▶sub _adder {
Subroutine _adder: not called CC=1 SCAR=6.9
526 0 my ($self, $attr_name, $attr_type, @values) = @_;
527 0 push ( @{ $self->{$attr_name} }, @values );
528 }
529  
530 #-----------------------------------------------------------------
531 # Keep it here! The reason is the existence of AUTOLOAD...
532 #-----------------------------------------------------------------
533 0 ▶sub DESTROY {
Subroutine DESTROY: not called CC=1 SCAR=0.0
534 }
535  
536 #-----------------------------------------------------------------
537 #
538 # Check type of @value against $expected_type. Return checked $value
539 # (perhaps trimmed, or otherwise corrected - e.g. wrapped in an
540 # appropriate object), or undef if the $value is of a wrong type.
541 #
542 #-----------------------------------------------------------------
543 8 ▶sub check_type {
Subroutine check_type: called CC=17 SCAR=54.7
Pod undocumented
544 8 my ($self, $name, $expected_type, @values) = @_;
545 8 my $value = $values[0];
546  
547 # first process cases when an expected type is a simple string,
548 # integer etc. (not MOSES::MOBY::Data::String etc.) - e.g. when an ID
549 # attribute is being set
550  
551 8 ▶ if ($expected_type eq STRING) {
Branch: 5 branches
BranchTrueFalseTotal
if ($expected_type eq 'string') { } 8 0 8
elsif ($expected_type eq 'integer') { } 0 0 0
elsif ($expected_type eq 'float') { } 0 0 0
elsif ($expected_type eq 'boolean') { } 0 0 0
elsif ($expected_type eq 'datetime') { } 0 0 0
552 8 return $value;
553  
554 } elsif ($expected_type eq INTEGER) {
555 0 ▶ $self->throw ($self->_wrong_type_msg ($value, $expected_type, $name))
Branch: 1 branch
BranchTrueFalseTotal
unless $value =~ /^\s*[+-]?\s*\d+\s*$/ 0 0 0
556 unless $value =~ m/^\s*[+-]?\s*\d+\s*$/;
557 0 $value =~ s/\s//g;
558 0 return $value;
559  
560 } elsif ($expected_type eq FLOAT) {
561 0 ▶ $self->throw ($self->_wrong_type_msg ($value, $expected_type, $name))
Branch: 1 branch
BranchTrueFalseTotal
unless $value =~ /^\s*[+-]?\s*(\d+(\.\d*)?|\.\d+)([eE][+-]?\d+)?\s*$/ 0 0 0
562 unless $value =~ m/^\s*[+-]?\s*(\d+(\.\d*)?|\.\d+)([eE][+-]?\d+)?\s*$/;
563 0 $value =~ s/\s//g;
564 0 return $value;
565  
566 } elsif ($expected_type eq BOOLEAN) {
567 0 ▶ return ($value =~ /true|\+|1|yes|ano/ ? '1' : '0');
Branch: 1 branch
BranchTrueFalseTotal
$value =~ /true|\+|1|yes|ano/ ? : 0 0 0
568  
569 } elsif ($expected_type eq DATETIME) {
570 0 my $iso;
571 0 eval {
572 0 $iso = (HTTP::Date::time2isoz (HTTP::Date::str2time (HTTP::Date::parse_date ($value))));
573 };
574 0 ▶ $self->throw ($self->_wrong_type_msg ($value, 'ISO-8601', $name))
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
575 if $@;
576 0 return $iso; ### $iso =~ s/ /T/; ??? TBD
577  
578 } else {
579  
580 # Then process cases when the expected type is a name of a
581 # real object (e.g. MOSES::MOBY::Data::Xref); for these cases the
582 # $value[0] can be already such object - in which case nothing
583 # to be done; or $value[0] can be HASH, or @values can be a
584 # list of name/value pairs, in which case a new object (of
585 # type $expected_type) has to be created and initialized by
586 # @values; and, still in the latter case, if the @values has
587 # just one element (XX), this element is considered a 'value':
588 # it is treated as a a hash {value => XX}.
589  
590 0 ▶ return $value if UNIVERSAL::isa ($value, $expected_type);
Branch: 1 branch
BranchTrueFalseTotal
if UNIVERSAL::isa($value, $expected_type) 0 0 0
591  
592 0 ▶ $value = { value => $value }
Branch: 1 branch
BranchTrueFalseTotal
unless ref $value or @values > 1 0 0 0
Condition0%0/3
ref $value or @values > 1
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $value or @values > 1
ref $value @values > 1
Truth table0%0/3
A or B
A= ref $valueB= @values > 1
ABresult
1X1
011
000
593 unless ref ($value) || @values > 1;
594  
595 0 my ($value_ref_type) = ref ($value);
596 0 ▶ if ($value_ref_type eq 'HASH') {
Branch: 3 branches
BranchTrueFalseTotal
if ($value_ref_type eq 'HASH') { } 0 0 0
elsif ($value_ref_type eq 'ARRAY') { } 0 0 0
elsif ($value_ref_type) { } 0 0 0
597 # e.g. $sequence->Length ( { value => 12, id => 'IR64'} )
598 0 return $self->create_member ($name, $expected_type, %$value);
599  
600 } elsif ($value_ref_type eq 'ARRAY') {
601 # e.g. $sequence->Length ( [ value => 12, id => 'IR64'] )
602 0 return $self->create_member ($name, $expected_type, @$value);
603  
604 } elsif ($value_ref_type) {
605 # e.g. $sequence->Length ( new MOSES::MOBY::Data::Integer ( value => 12) )
606 0 ▶ $self->throw ($self->_wrong_type_msg ($value_ref_type, $expected_type, $name))
Branch: 1 branch
BranchTrueFalseTotal
unless UNIVERSAL::isa($value, $expected_type) 0 0 0
607 unless UNIVERSAL::isa ($value, $expected_type);
608 0 return $value;
609  
610 } else {
611 # e.g. $sequence->Length (value => 12, id => 'IR64')
612 0 return $self->create_member ($name, $expected_type, @values);
613  
614 }
615 }
616 }
617  
618 #-----------------------------------------------------------------
619 #
620 #-----------------------------------------------------------------
621 0 ▶sub create_member {
Subroutine create_member: not called CC=2 SCAR=17.9
Pod undocumented
622 0 my ($self, $name, $expected_type, @values) = @_;
623 0 eval "require $expected_type";
624 0 ▶ $self->throw ($self->_wrong_type_msg ($values[0], $expected_type, $name))
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
625 if $@;
626 0 return "$expected_type"->new (@values);
627 }
628  
629 #-----------------------------------------------------------------
630 # as_string (an "" operator overloading)
631 #-----------------------------------------------------------------
632 2 my $DUMPER;
633 2 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
634 2 ▶ use Dumpvalue;
Subroutine BEGIN: called CC=1 SCAR=0.0
635 2 ▶ use IO::String;
Subroutine BEGIN: called CC=1 SCAR=0.0
636 2 $DUMPER = Dumpvalue->new();
637 # $DUMPER->set (veryCompact => 1);
638 }
639 4 ▶sub as_string {
Subroutine as_string: called CC=1 SCAR=0.0
Pod undocumented
640 4 my $self = shift;
641 4 my $dump_str;
642 4 my $io = IO::String->new (\$dump_str);
643 4 my $oio = select ($io);
644 4 $DUMPER->dumpValue (\$self);
645 4 select ($oio);
646 4 return $dump_str;
647 }
648  
649 2 1;
650 __END__
« lib/MOSES/MOBY/Cache/Registries.pm ↑ Summary lib/MOSES/MOBY/Def/Data.pm »