blib/lib/XML/Pastor/Generator.pm

filter: Statementstmt 98.2% 341 / 347 Branchbran 73.7% 90 / 122 Conditioncond 56.9% 45 / 79 MC/DCmcdc 29.1% 14 / 48 Subroutinesub 71.0% 27 / 38 Podpod 100% 2 / 2 totaltotal 81.6% 519 / 636 CC 108 SCAR 25.1
CC
108
Cov
87%
_calculateUses
26.8
_fabricateCode
23.7
_fabricatePod
20.6
CRAP 12.4
« blib/lib/XML/Pastor/Schema/Parser.pm ↑ Summary blib/lib/XML/Pastor/Schema/Object.pm »
1 10 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 10 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 10 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 10 ▶no warnings qw(uninitialized);
Subroutine BEGIN: called CC=1 SCAR=0.0
5
6 #=======================================================
7 package XML::Pastor::Generator;
8
9 10 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 10 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 10 ▶use File::Path;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 10 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 10 ▶use Class::Accessor;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 10 ▶use XML::Pastor::Util qw(mergeHash module_path);
Subroutine BEGIN: called CC=1 SCAR=0.0
15
16 10 our @ISA = qw(Class::Accessor);
17
18 #--------------------------------------------
19 9 ▶sub new {
Subroutine new: called CC=2 SCAR=7.4
Pod documented
20 9 my $proto = shift;
21 9 ▶ my $class = ref($proto) || $proto;
Condition33%1/3
ref $proto || $proto
l!l&&r!l&&!r
090
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
22 9 my $self = {@_};
23 9 return bless $self, $class;
24 }
25
26 #--------------------------------------------
27 # Generate Perl code from a given schema model (produced by the Parser) by the
28 # argument 'model'.
29 #
30 # Understands several modes of functioning => generate, eval, return
31 #
32 # mode = 'offline' : Generate perl code and write it to one or more module files.
33 # mode = 'eval' : Generate perl code and 'evaluate' it in situ without writing out to any file.
34 # mode = 'return' : Generate perl code and return the resulting string to the caller.
35 #
36 # Understands also two styles:
37 # style = 'single' : Generate one big chunk of code with all the packages in it.
38 # Note that this is the forced case when mode is 'eval' or 'return'
39 # style = 'multiple': Generate one chunk of code for each class and write it out to multiple
40 # module files within the 'destination' directory.
41 #----------------------------------------------------------------------
42 9 ▶sub generate {
Subroutine generate: called CC=13 SCAR=27.2
Pod documented
43 9 my $self = shift;
44 9 my $args = {@_};
45 9 ▶ my $model = $args->{model} or die "Pastor: Code generation requires a 'model'!\n";
Branch: 1 branch
BranchTrueFalseTotal
my $model = $args->{"model"} or die "Pastor: Code generation requires a 'model'!\n" 9 0 9
46 9 my $mode = $args->{mode};
47 9 ▶ if ( ($mode =~ /eval/) || ($mode =~ /return/)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mode =~ /eval/ or $mode =~ /return/) 8 1 9
Condition66%2/3
$mode =~ /eval/ or $mode =~ /return/
l!l&&r!l&&!r
801
MC/DC50%1/2
$mode =~ /eval/ or $mode =~ /return/
$mode =~ /eval/ $mode =~ /return/
Truth table66%2/3
A or B
A= $mode =~ /eval/B= $mode =~ /return/
ABresult
1X1
011
000
48 8 $args->{style} = "single"; # force single module generation
49 }
50 9 my $style = $args->{style};
51 9 ▶ my $destination = $args->{destination} || '/tmp/lib/perl/';
Condition50%1/2
$args->{'destination'} || '/tmp/lib/perl/'
l!l
90
MC/DC0%0/1
$args->{'destination'} || '/tmp/lib/perl/'
$args->{'destination'}
Truth table50%1/2
A
A= $args->{'destination'}
Aresult
11
00
52 9 ▶ my $verbose = $self->{verbose} || 0;
Condition50%1/2
$self->{'verbose'} || 0
l!l
09
MC/DC0%0/1
$self->{'verbose'} || 0
$self->{'verbose'}
Truth table50%1/2
A
A= $self->{'verbose'}
Aresult
11
00
53
54 # If 'destination' doesn't end with a trailing slash, add one.
55 9 ▶ if ($destination && ($destination !~ /\/$/) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($destination and not $destination =~ m[/$]) 0 9 9
Condition33%1/3
$destination and not $destination =~ m[/$]
!ll&&!rl&&r
090
MC/DC0%0/2
$destination and not $destination =~ m[/$]
$destination not $destination =~ m[/$]
Truth table33%1/3
A and not(B)
A= $destinationB= not $destination =~ m[/$]
ABresult
0X0
100
111
56 0 $destination .= '/';
57 }
58 9 $args->{destination}= $destination;
59
60 9 ▶ my $class_prefix = $args->{class_prefix} || '';
Condition50%1/2
$args->{'class_prefix'} || ''
l!l
90
MC/DC0%0/1
$args->{'class_prefix'} || ''
$args->{'class_prefix'}
Truth table50%1/2
A
A= $args->{'class_prefix'}
Aresult
11
00
61 9 ▶ while (($class_prefix) && ($class_prefix !~ /::$/)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class_prefix and not $class_prefix =~ /::$/) 14 9 23
Condition66%2/3
$class_prefix and not $class_prefix =~ /::$/
!ll&&!rl&&r
0914
MC/DC50%1/2
$class_prefix and not $class_prefix =~ /::$/
$class_prefix not $class_prefix =~ /::$/
Truth table66%2/3
A and not(B)
A= $class_prefixB= not $class_prefix =~ /::$/
ABresult
0X0
100
111
62 14 $class_prefix .= ':';
63 }
64 9 $args->{metaModule} = $class_prefix . "Pastor::Meta";
65
66
67 9 ▶ print STDERR "\nGenerating code...\n" if ($verbose >=2);
Branch: 1 branch
BranchTrueFalseTotal
if $verbose >= 2 0 9 9
68 9 ▶ if ($style =~ /single/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($style =~ /single/) { } 8 1 9
69 8 $self->_generateSingle(%$args);
70 }else {
71 1 $self->_generateMultiple(%$args);
72 }
73 }
74
75 #--------------------------------------------
76 # Generate a single chunk of code, putting all the classes (packages) into that
77 # chunk. Then write to a module file if requested
78 # or otherwise evaluate it or just return it (depending on the 'mode')
79 8 ▶sub _generateSingle {
Subroutine _generateSingle: called CC=10 SCAR=24.1
80 8 my $self = shift;
81 8 my $args = {@_};
82 8 my $model = $args->{model};
83 8 my $mode = $args->{mode};
84 8 my $class_prefix = $args->{class_prefix};
85 8 ▶ my $module = $args->{module} || $class_prefix;
Condition33%1/3
$args->{'module'} || $class_prefix
l!l&&r!l&&!r
080
MC/DC0%0/2
$args->{'module'} || $class_prefix
$args->{'module'} $class_prefix
Truth table33%1/3
A || B
A= $args->{'module'}B= $class_prefix
ABresult
1X1
011
000
86 8 my $code = $self->_fabricatePrelude(@_);
87 8 ▶ my $destination = $args->{destination} || '/tmp/lib/perl/';
Condition50%1/2
$args->{'destination'} || '/tmp/lib/perl/'
l!l
80
MC/DC0%0/1
$args->{'destination'} || '/tmp/lib/perl/'
$args->{'destination'}
Truth table50%1/2
A
A= $args->{'destination'}
Aresult
11
00
88 8 ▶ my $verbose = $self->{verbose} || 0;
Condition50%1/2
$self->{'verbose'} || 0
l!l
08
MC/DC0%0/1
$self->{'verbose'} || 0
$self->{'verbose'}
Truth table50%1/2
A
A= $self->{'verbose'}
Aresult
11
00
89
90 8 foreach my $items ($model->type(), $model->element()) {
91 16 foreach my $key (sort keys %$items) {
92 134 $code .= $self->_fabricateCode(@_, object=>$items->{$key});
93 }
94 }
95
96 8 $code .= $self->_fabricateHeaderModuleCode(@_);
97 8 $code .= $self->_fabricateMetaModuleCode(@_);
98
99 # Perl modules must return TRUE
100 8 $code .= "\n\n1;\n";
101
102 8 ▶ print STDERR "\n****** CODE STARTS *******" . $code . "\n***** CODE ENDS *****\n" if ($verbose >=9);
Branch: 1 branch
BranchTrueFalseTotal
if $verbose >= 9 0 8 8
103
104 8 ▶ if ($mode =~ /eval/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($mode =~ /eval/) { } 8 0 8
elsif ($mode =~ /return/) { } 0 0 0
105 # evaluate the code
106 8 ▶ eval $code;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine code: not called CC=1 SCAR=0.0
Subroutine code: not called CC=1 SCAR=0.0
Subroutine code: not called CC=1 SCAR=0.0
Subroutine date: not called CC=1 SCAR=0.0
Subroutine figure: not called CC=1 SCAR=0.0
Subroutine lang: not called CC=1 SCAR=0.0
Subroutine name: not called CC=1 SCAR=0.0
Subroutine airport: not called CC=1 SCAR=0.0
Subroutine borderCrossing: not called CC=1 SCAR=0.0
Subroutine code: not called CC=1 SCAR=0.0
Subroutine code2: not called CC=1 SCAR=0.0
107 8 ▶ $@ and die "$@\n";
Branch: 1 branch
BranchTrueFalseTotal
$@ and die "$@\n" 0 8 8
108 }elsif ($mode =~ /return/) {
109 # just return the code
110 0 return $code;
111 }else {
112 # generate module
113 0 my $file = module_path(module => $module, destination => $destination);
114 0 $self->_writeCode(@_, file=>$file, code=>$code);
115 }
116 8 return $self;
117 }
118
119 #--------------------------------------------
120 # Generate a multiple 'modules' of code, one for each class.
121 # Then write the code to module files within the 'destination' directory
122 31 ▶sub _generateMultiple {
Subroutine _generateMultiple: called CC=6 SCAR=18.0
123 31 my $self = shift;
124 1 my $args = {@_};
125 13 my $model = $args->{model};
126 1 my $class_prefix = $args->{class_prefix};
127 4 ▶ my $module = $args->{module} || $class_prefix;
Condition33%1/3
$args->{'module'} || $class_prefix
l!l&&r!l&&!r
010
MC/DC0%0/2
$args->{'module'} || $class_prefix
$args->{'module'} $class_prefix
Truth table33%1/3
A || B
A= $args->{'module'}B= $class_prefix
ABresult
1X1
011
000
128 1 my $metaModule = $args->{metaModule};
129
130 4 my $types = $model->type();
131 5 ▶ my $destination = $args->{destination} || '/tmp/lib/perl/';
Condition50%1/2
$args->{'destination'} || '/tmp/lib/perl/'
l!l
10
MC/DC0%0/1
$args->{'destination'} || '/tmp/lib/perl/'
$args->{'destination'}
Truth table50%1/2
A
A= $args->{'destination'}
Aresult
11
00
132
133 3 foreach my $items ($model->type(), $model->element()) {
134 2 foreach my $key (sort keys %$items) {
135 15 my $object = $items->{$key};
136 15 my $code = $self->_fabricateCode(@_, object=>$object);
137 15 $code .= "\n\n__END__\n\n";
138 15 $code .=$self->_fabricatePod(@_, object=>$object);
139
140 15 my $file = module_path(module => $object->class(), destination => $destination);
141 15 $self->_writeCode(@_, file=>$file, code=>$code);
142 }
143 }
144
145 # META mdoule
146 1 my $code = $self->_fabricateMetaModuleCode(@_);
147 1 my $file = module_path(module => $metaModule, destination => $destination);
148 1 $self->_writeCode(@_, file=>$file, code=>$code);
149
150
151 # HEADER module
152 1 ▶ if ($module) {
Branch: 1 branch
BranchTrueFalseTotal
if ($module) 1 0 1
153 # Generate the module with all the 'use' statements for different modules.
154 1 my $code = $self->_fabricateHeaderModuleCode(@_);
155
156 1 my $file = module_path(module => $module, destination => $destination);
157 1 $self->_writeCode(@_, file=>$file, code=>$code);
158 }
159
160 1 return $self;
161 }
162
163
164 #--------------------------------------------
165 # Fabricate the code for the module that will 'use' all the generated classes.
166 #--------------------------------------------
167 9 ▶sub _fabricateHeaderModuleCode {
Subroutine _fabricateHeaderModuleCode: called CC=9 SCAR=22.0
168 9 my $self = shift;
169 9 my $args = {@_};
170 9 my $model = $args->{model};
171 9 my $style = $args->{style};
172 9 my $class_prefix = $args->{class_prefix};
173 9 ▶ my $module = $args->{module} || $class_prefix;
Condition33%1/3
$args->{'module'} || $class_prefix
l!l&&r!l&&!r
090
MC/DC0%0/2
$args->{'module'} || $class_prefix
$args->{'module'} $class_prefix
Truth table33%1/3
A || B
A= $args->{'module'}B= $class_prefix
ABresult
1X1
011
000
174 9 my $metaModule = $args->{metaModule};
175 9 my $code;
176
177 # get ride of any trailing columns.
178 9 ▶ while ($module =~ /:$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($module =~ /:$/) 4 9 13
179 4 $module =~ s/:$//;
180 }
181 9 ▶ $code = _fabricatePrelude(@_) unless ($style =~ /single/i);
Branch: 1 branch
BranchTrueFalseTotal
unless $style =~ /single/i 1 8 9
182 9 $code .= "\n\npackage $module;\n";
183
184 # USE
185 9 ▶ unless ($style =~ /single/i) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($style =~ /single/i) 1 8 9
186 1 foreach my $items ($model->type(), $model->element()) {
187 2 foreach my $name (sort keys %$items) {
188 15 my $object = $items->{$name};
189 15 my $class = $object->class();
190 15 $code .= "\nuse $class;"
191 }
192 }
193
194 1 ▶ $code .= "\n\nuse $metaModule;" if ($metaModule);
Branch: 1 branch
BranchTrueFalseTotal
if $metaModule 1 0 1
195 }
196
197 # EPILOGUE
198 9 ▶ unless ($style =~ /single/i) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($style =~ /single/i) 1 8 9
199 # Perl modules must return TRUE
200 1 $code .= "\n\n1;\n";
201 }
202
203 9 return $code;
204 }
205
206
207 #--------------------------------------------
208 # Fabricate the first prelude stub of code for each module.
209 # Needed only in the begining of each physical module file.
210 37 ▶sub _fabricatePrelude {
Subroutine _fabricatePrelude: called CC=1 SCAR=0.0
211 25 my $self = shift;
212 25 my $code = "";
213
214 25 $code .= "\n#PASTOR: Code generated by XML::Pastor/" . XML::Pastor->version() . " at '" . localtime() . "'\n";
215 25 $code .= "\nuse utf8;";
216 25 $code .= "\nuse strict;";
217 25 $code .= "\nuse warnings;";
218 25 $code .= "\nno warnings qw(uninitialized);";
219
220 25 $code .= "\n\nuse XML::Pastor;\n\n";
221 25 return $code;
222 }
223
224 #--------------------------------------------
225 # Fabricate the code for the META module.
226 #--------------------------------------------
227 9 ▶sub _fabricateMetaModuleCode {
Subroutine _fabricateMetaModuleCode: called CC=3 SCAR=11.0
228 9 my $self = shift;
229 9 my $args = {@_};
230 9 my $model = $args->{model};
231 9 my $style = $args->{style};
232 9 my $module = $args->{metaModule};
233 9 my $isa = [qw(XML::Pastor::Meta)];
234 9 my $code = "";
235
236
237 9 ▶ $code = _fabricatePrelude(@_) unless ($style =~ /single/i);
Branch: 1 branch
BranchTrueFalseTotal
unless $style =~ /single/i 1 8 9
238
239 9 $code .= "\n\npackage $module;\n";
240
241 # ISA
242 9 $code .= "\n\nour " . '@ISA=qw(' . join (' ', @$isa) . ");";
243
244 # Model
245 9 $code .= "\n\n$module->Model( " ;
246 9 $code .= _dumpObject($model);
247 9 $code =~ s/\n$//;
248 9 $code .=" );";
249
250 # EPILOGUE
251 9 ▶ unless ($style =~ /single/i) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($style =~ /single/i) 1 8 9
252 # Perl modules must return TRUE
253 1 $code .= "\n\n1;\n";
254 }
255
256 9 return $code;
257 }
258
259
260
261
262 #--------------------------------------------
263 # Fabricate the code for one given class (type).
264 # Can work in 'single' or 'multiple' style (knows to distinguish them).
265 # Will create code in a separate 'package' section for the class.
266 152 ▶sub _fabricateCode {
Subroutine _fabricateCode: called CC=23 SCAR=31.6
267 149 my $self = shift;
268 149 my $args = {@_};
269 149 ▶ my $object = $args->{object} or die "Pastor: _fabricateCode: Need a type!\n";
Branch: 1 branch
BranchTrueFalseTotal
my $object = $args->{"object"} or die "Pastor: _fabricateCode: Need a type!\n" 149 0 149
270 149 my $style = $args->{style};
271 149 my $class = $object->class();
272 149 ▶ my $isa = $object->baseClasses() || [];
Condition50%1/2
$object->baseClasses || []
l!l
1490
MC/DC0%0/1
$object->baseClasses || []
$object->baseClasses
Truth table50%1/2
A
A= $object->baseClasses
Aresult
11
00
273 149 ▶ my $verbose = $self->{verbose} || 0;
Condition50%1/2
$self->{'verbose'} || 0
l!l
0149
MC/DC0%0/1
$self->{'verbose'} || 0
$self->{'verbose'}
Truth table50%1/2
A
A= $self->{'verbose'}
Aresult
11
00
274 149 my $code = "";
275
276 149 ▶ print STDERR "\nFabricating code for class '$class' ..." if ($verbose >= 3);
Branch: 1 branch
BranchTrueFalseTotal
if $verbose >= 3 0 149 149
277
278 # PRELUDE
279 149 ▶ $code .= $self->_fabricatePrelude(@_) unless ($style =~ /single/i);
Branch: 1 branch
BranchTrueFalseTotal
unless $style =~ /single/i 15 134 149
280
281 # package
282 149 $code .= "\n\n#================================================================";
283 149 $code .= "\n\npackage $class;\n";
284
285 # use
286 # in "single" style, we won't need the use clauses because all packages will be in one module.
287 149 ▶ unless ($style =~ /single/i) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($style =~ /single/i) 15 134 149
288 15 my $uses = $self->_calculateUses($object, {@_});
289 15 foreach my $use (sort keys %$uses) {
290 33 ▶ next if $use eq $class; # We won't be using ourselves!
Branch: 1 branch
BranchTrueFalseTotal
if $use eq $class 15 18 33
291 18 $code .= "\nuse $use;";
292 }
293 }
294
295 # ISA
296 149 $code .= "\n\nour " . '@ISA=qw(' . join (' ', @$isa) . ");";
297
298 # mk_accessors
299 149 my $accessors = [];
300 149 ▶ if (UNIVERSAL::can($object, "attributes")) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::can($object, "attributes")) 58 91 149
301 58 my $attribPfx = "";
302 58 ▶ $attribPfx = $object->attributePrefix() if (UNIVERSAL::can($object, "attributePrefix"));
Branch: 1 branch
BranchTrueFalseTotal
if UNIVERSAL::can($object, "attributePrefix") 58 0 58
303 58 my $fields = [map {$attribPfx . $_} @{$object->attributes()}];
304 58 push @$accessors, @$fields;
305 }
306 149 ▶ if (UNIVERSAL::can($object, "elements")) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::can($object, "elements")) 58 91 149
307 58 push @$accessors, @{$object->elements()};
308 }
309 149 ▶ if ( scalar(@$accessors) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @$accessors) 58 91 149
310 58 $code .= "\n\n$class". '->mk_accessors( qw(' . join (' ', @$accessors) . "));";
311 }
312
313
314 # Attribute accessor aliases
315 149 ▶ if (UNIVERSAL::can($object, "attributes")) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::can($object, "attributes")) 58 91 149
316 58 my $attributes = $object->attributes();
317 58 ▶ my $attribPfx = (UNIVERSAL::can($object, "attributePrefix")) ? $object->attributePrefix() : "";
Branch: 1 branch
BranchTrueFalseTotal
UNIVERSAL::can($object, 'attributePrefix') ? : 58 0 58
318
319 58 ▶ if (scalar(@$attributes) && $attribPfx) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @$attributes and $attribPfx) 48 10 58
Condition66%2/3
scalar @$attributes and $attribPfx
!ll&&!rl&&r
10048
MC/DC50%1/2
scalar @$attributes and $attribPfx
scalar @$attributes $attribPfx
Truth table66%2/3
A and B
A= scalar @$attributesB= $attribPfx
ABresult
0X0
100
111
320 48 $code .= "\n\n# Attribute accessor aliases\n";
321 48 ▶ my $elementInfo = (UNIVERSAL::can($object, "effectiveElementInfo")) ? $object->effectiveElementInfo() : {};
Branch: 1 branch
BranchTrueFalseTotal
UNIVERSAL::can($object, 'effectiveElementInfo') ? : 48 0 48
322 48 foreach my $attribute (@$attributes) {
323 90 ▶ next if defined($elementInfo->{$attribute}); # Attribute/Element name conflict. No alias possible
Branch: 1 branch
BranchTrueFalseTotal
if defined $elementInfo->{$attribute} 0 90 90
324 90 my $field = $attribPfx . $attribute;
325 90 ▶ next if ($field eq $attribute); # No attribute prefix. No need for an alias.
Branch: 1 branch
BranchTrueFalseTotal
if $field eq $attribute 0 90 90
326 90 $code .= "\nsub $attribute { &" . $field . '; }';
327 }
328
329 }
330
331 }
332
333
334 # XmlSchemaInfo
335 149 ▶ if (UNIVERSAL::isa($object, "XML::Pastor::Schema::Element")) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::isa($object, 'XML::Pastor::Schema::Element')) { } 41 108 149
336 41 $code .= "\n\n$class->XmlSchemaElement( " ;
337 }else {
338 108 $code .= "\n\n$class->XmlSchemaType( " ;
339 }
340 149 $code .= _dumpObject($object);
341 149 $code =~ s/\n$//;
342 149 $code .=" );";
343
344
345 # EPILOGUE
346 149 ▶ unless ($style =~ /single/i) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($style =~ /single/i) 15 134 149
347 # Perl modules must return TRUE
348 15 $code .= "\n\n1;\n";
349 }
350
351 149 return $code;
352 }
353
354 #--------------------------------------------
355 # Figure out the classes that need to be 'used' by a given class.
356 # This is needed to create the 'use' stubs in code generation.
357 31 ▶sub _calculateUses {
Subroutine _calculateUses: called CC=25 SCAR=32.9
358 31 my $self = shift;
359 31 my $object = shift;
360 31 my $opts = shift;
361 31 my $result = {};
362
363 31 ▶ if (UNIVERSAL::can($object, "class") && $object->class()) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::can($object, "class") and $object->class) 31 0 31
Condition33%1/3
UNIVERSAL::can($object, "class") and $object->class
!ll&&!rl&&r
0031
MC/DC0%0/2
UNIVERSAL::can($object, "class") and $object->class
UNIVERSAL::can($object, "class") $object->class
Truth table33%1/3
A and B
A= UNIVERSAL::can($object, "class")B= $object->class
ABresult
0X0
100
111
364 31 my $class = $object->class();
365
366 # Consider the class as 'used' unless it starts with XML::Pastor which is handled differently
367 # But XML::Pastor::Test::* are used for testing purposes.
368 SWITCH: {
369 31 (($class =~ /^XML::Pastor::/) &&
370 31 ▶ ($class !~ /^XML::Pastor::Test/)) and do {last SWITCH;};
Branch: 1 branch
BranchTrueFalseTotal
$class =~ /^XML::Pastor::/ and not $class =~ /^XML::Pastor::Test/ and do { last SWITCH } 7 24 31
Condition66%2/3
$class =~ /^XML::Pastor::/ and not $class =~ /^XML::Pastor::Test/
!ll&&!rl&&r
0247
MC/DC50%1/2
$class =~ /^XML::Pastor::/ and not $class =~ /^XML::Pastor::Test/
$class =~ /^XML::Pastor::/ not $class =~ /^XML::Pastor::Test/
Truth table66%2/3
A and not(B)
A= $class =~ /^XML::Pastor::/B= not $class =~ /^XML::Pastor::Test/
ABresult
0X0
100
111
371 24 OTHERWISE: { $result->{$class}=1; last SWITCH;}
372 }
373
374 }
375
376 31 ▶ if (UNIVERSAL::can($object, "itemClass") && $object->itemClass()) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::can($object, "itemClass") and $object->itemClass) 1 30 31
Condition100%3/3
UNIVERSAL::can($object, "itemClass") and $object->itemClass
!ll&&!rl&&r
2461
MC/DC100%2/2
UNIVERSAL::can($object, "itemClass") and $object->itemClass
UNIVERSAL::can($object, "itemClass") $object->itemClass
Truth table100%3/3
A and B
A= UNIVERSAL::can($object, "itemClass")B= $object->itemClass
ABresult
0X0
100
111
377 1 my $class = $object->itemClass();
378
379 # Consider the class as 'used' unless it starts with XML::Pastor which is handled differently
380 # But XML::Pastor::Test::* are used for testing purposes.
381 SWITCH: {
382 1 (($class =~ /^XML::Pastor::/) &&
383 0 ▶ ($class !~ /^XML::Pastor::Test/)) and do {last SWITCH;};
Branch: 1 branch
BranchTrueFalseTotal
$class =~ /^XML::Pastor::/ and not $class =~ /^XML::Pastor::Test/ and do { last SWITCH } 0 1 1
Condition33%1/3
$class =~ /^XML::Pastor::/ and not $class =~ /^XML::Pastor::Test/
!ll&&!rl&&r
010
MC/DC0%0/2
$class =~ /^XML::Pastor::/ and not $class =~ /^XML::Pastor::Test/
$class =~ /^XML::Pastor::/ not $class =~ /^XML::Pastor::Test/
Truth table33%1/3
A and not(B)
A= $class =~ /^XML::Pastor::/B= not $class =~ /^XML::Pastor::Test/
ABresult
0X0
100
111
384 1 OTHERWISE: { $result->{$class}=1; last SWITCH;}
385
386 }
387
388 }
389
390 31 ▶ if (UNIVERSAL::can($object, "baseClasses") && $object->baseClasses()) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::can($object, "baseClasses") and $object->baseClasses) 18 13 31
Condition100%3/3
UNIVERSAL::can($object, "baseClasses") and $object->baseClasses
!ll&&!rl&&r
7618
MC/DC100%2/2
UNIVERSAL::can($object, "baseClasses") and $object->baseClasses
UNIVERSAL::can($object, "baseClasses") $object->baseClasses
Truth table100%3/3
A and B
A= UNIVERSAL::can($object, "baseClasses")B= $object->baseClasses
ABresult
0X0
100
111
391 18 my $isa = $object->baseClasses();
392 18 foreach my $class (@$isa) {
393 # Consider the class as 'used' unless it starts with XML::Pastor which is handled differently
394 # But XML::Pastor::Test::* are used for testing purposes.
395 SWITCH: {
396 24 (($class =~ /^XML::Pastor::/) &&
397 24 ▶ ($class !~ /^XML::Pastor::Test/)) and do {last SWITCH;};
Branch: 1 branch
BranchTrueFalseTotal
$class =~ /^XML::Pastor::/ and not $class =~ /^XML::Pastor::Test/ and do { last SWITCH } 18 6 24
Condition66%2/3
$class =~ /^XML::Pastor::/ and not $class =~ /^XML::Pastor::Test/
!ll&&!rl&&r
0618
MC/DC50%1/2
$class =~ /^XML::Pastor::/ and not $class =~ /^XML::Pastor::Test/
$class =~ /^XML::Pastor::/ not $class =~ /^XML::Pastor::Test/
Truth table66%2/3
A and not(B)
A= $class =~ /^XML::Pastor::/B= not $class =~ /^XML::Pastor::Test/
ABresult
0X0
100
111
398 6 OTHERWISE: { $result->{$class}=1; last SWITCH;}
399 }
400 }
401 }
402
403 31 ▶ if (UNIVERSAL::can($object, "memberClasses") && $object->memberClasses()) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::can($object, "memberClasses") and $object->memberClasses) 2 29 31
Condition100%3/3
UNIVERSAL::can($object, "memberClasses") and $object->memberClasses
!ll&&!rl&&r
2452
MC/DC100%2/2
UNIVERSAL::can($object, "memberClasses") and $object->memberClasses
UNIVERSAL::can($object, "memberClasses") $object->memberClasses
Truth table100%3/3
A and B
A= UNIVERSAL::can($object, "memberClasses")B= $object->memberClasses
ABresult
0X0
100
111
404 2 my $mbcs = $object->memberClasses();
405 2 foreach my $class (@$mbcs) {
406 # Consider the class as 'used' unless it starts with XML::Pastor which is handled differently
407 # But XML::Pastor::Test::* are used for testing purposes.
408 SWITCH: {
409 5 (($class =~ /^XML::Pastor::/) &&
410 0 ▶ ($class !~ /^XML::Pastor::Test/)) and do {last SWITCH;};
Branch: 1 branch
BranchTrueFalseTotal
$class =~ /^XML::Pastor::/ and not $class =~ /^XML::Pastor::Test/ and do { last SWITCH } 0 5 5
Condition33%1/3
$class =~ /^XML::Pastor::/ and not $class =~ /^XML::Pastor::Test/
!ll&&!rl&&r
050
MC/DC0%0/2
$class =~ /^XML::Pastor::/ and not $class =~ /^XML::Pastor::Test/
$class =~ /^XML::Pastor::/ not $class =~ /^XML::Pastor::Test/
Truth table33%1/3
A and not(B)
A= $class =~ /^XML::Pastor::/B= not $class =~ /^XML::Pastor::Test/
ABresult
0X0
100
111
411 5 OTHERWISE: { $result->{$class}=1; last SWITCH;}
412
413 }
414 }
415 }
416
417
418 31 ▶ if (UNIVERSAL::can($object, "definition") && $object->definition()) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::can($object, "definition") and $object->definition) 3 28 31
Condition66%2/3
UNIVERSAL::can($object, "definition") and $object->definition
!ll&&!rl&&r
0283
MC/DC50%1/2
UNIVERSAL::can($object, "definition") and $object->definition
UNIVERSAL::can($object, "definition") $object->definition
Truth table66%2/3
A and B
A= UNIVERSAL::can($object, "definition")B= $object->definition
ABresult
0X0
100
111
419 3 mergeHash($result, $self->_calculateUses($object->definition(), $opts));
420 }
421
422 31 ▶ if (UNIVERSAL::can($object, "attributeInfo")) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::can($object, "attributeInfo")) 5 26 31
423 5 foreach my $attrib (values %{$object->attributeInfo()} ){
424 7 mergeHash($result, $self->_calculateUses($attrib, $opts));
425 }
426 }
427
428 31 ▶ if (UNIVERSAL::can($object, "elementInfo")) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::can($object, "elementInfo")) 5 26 31
429 5 foreach my $elem (values %{$object->elementInfo()} ){
430 6 mergeHash($result, $self->_calculateUses($elem, $opts));
431 }
432 }
433
434 31 return $result;
435 }
436
437 #--------------------------------------------
438 # Fabricate the POD for one given class (type).
439 #--------------------------------------------
440 18 ▶sub _fabricatePod {
Subroutine _fabricatePod: called CC=20 SCAR=30.3
441 15 my $self = shift;
442 15 my $args = {@_};
443 15 ▶ my $object = $args->{object} or die "Pastor: _fabricatePod: Need a type!\n";
Branch: 1 branch
BranchTrueFalseTotal
my $object = $args->{"object"} or die "Pastor: _fabricatePod: Need a type!\n" 15 0 15
444 15 my $style = $args->{style};
445 15 my $class = $object->class();
446 15 ▶ my $isa = $object->baseClasses() || [];
Condition50%1/2
$object->baseClasses || []
l!l
150
MC/DC0%0/1
$object->baseClasses || []
$object->baseClasses
Truth table50%1/2
A
A= $object->baseClasses
Aresult
11
00
447 15 ▶ my $verbose = $self->{verbose} || 0;
Condition50%1/2
$self->{'verbose'} || 0
l!l
015
MC/DC0%0/1
$self->{'verbose'} || 0
$self->{'verbose'}
Truth table50%1/2
A
A= $self->{'verbose'}
Aresult
11
00
448 15 my $pod = "";
449
450 15 ▶ print STDERR "\nFabricating POD for class '$class' ..." if ($verbose >= 3);
Branch: 1 branch
BranchTrueFalseTotal
if $verbose >= 3 0 15 15
451
452 # NAME
453 15 $pod .= "\n\n=head1 NAME\n\nB<$class> - A class generated by L<XML::Pastor> . \n";
454
455 # DESCRIPTION
456 15 ▶ if (defined $object->documentation) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $object->documentation) 1 14 15
457 1 $pod .= "\n\n=head1 DESCRIPTION\n";
458 1 foreach my $doc (@{$object->documentation}) {
459 1 my $text = $doc->text;
460 1 $text =~ s/^\s+//; # chop leading space.
461 1 $pod .= "\n" . $text . "\n";
462 }
463 }
464
465
466 # ISA
467 15 $pod .= "\n\n=head1 ISA\n\nThis class descends from " . join(', ', map {"L<$_>"} @$isa) . ".\n";
468
469 # CODE GENERATION
470 15 $pod .= "\n\n=head1 CODE GENERATION\n\nThis module was automatically generated by L<XML::Pastor> version " . $XML::Pastor::VERSION . " at '" . localtime() . "'\n";
471
472 # ATTRIBUTE Accessors
473 15 ▶ if (UNIVERSAL::can($object, "attributes") && scalar(@{$object->attributes})) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::can($object, "attributes") and scalar @{$object->attributes;}) 5 10 15
Condition100%2/2
UNIVERSAL::can($object, "attributes") and scalar @{$object->attributes;}
!ll
105
MC/DC100%1/1
UNIVERSAL::can($object, "attributes") and scalar @{$object->attributes;}
UNIVERSAL::can($object, "attributes")
Truth table100%2/2
A
A= UNIVERSAL::can($object, "attributes")
Aresult
00
11
474 5 my $attributes = $object->attributes();
475 5 my $attribPfx = "";
476 5 ▶ $attribPfx = $object->attributePrefix() if (UNIVERSAL::can($object, "attributePrefix"));
Branch: 1 branch
BranchTrueFalseTotal
if UNIVERSAL::can($object, "attributePrefix") 5 0 5
477 5 my $attribInfo = $object->effectiveAttributeInfo();
478 5 ▶ my $elementInfo = UNIVERSAL::can($object, 'effectiveElementInfo') ? $object->effectiveElementInfo() : {};
Branch: 1 branch
BranchTrueFalseTotal
UNIVERSAL::can($object, 'effectiveElementInfo') ? : 5 0 5
479
480 5 $pod .= "\n\n=head1 ATTRIBUTE ACCESSORS\n";
481 5 $pod .= "\n=over\n";
482
483 5 foreach my $attribute_name (sort @$attributes) {
484 7 my $accessor = $attribPfx . $attribute_name;
485 7 my $attrib = $attribInfo->{$attribute_name};
486 7 ▶ next unless (defined $attrib);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $attrib 0 7 7
487
488 7 $pod .= "\n=item B<$accessor>()";
489
490 # Attribute accessor alias (if there is no conflict with an element name)
491 7 ▶ unless (exists($elementInfo->{$attribute_name})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $elementInfo->{$attribute_name}) 7 0 7
492 7 $pod .= ", B<$attribute_name>()";
493 }
494
495 7 my $aclass = $attrib->class;
496 7 ▶ if (defined $aclass) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $aclass) 7 0 7
497 7 $pod .= " - See L<$aclass>.";
498 }
499
500 7 $pod .= "\n";
501 }
502 5 $pod .= "\n=back\n";
503 }
504
505
506 # CHILD ELEMENT accessors
507 15 ▶ if (UNIVERSAL::can($object, "elements") && scalar(@{$object->elements})) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::can($object, "elements") and scalar @{$object->elements;}) 2 13 15
Condition100%2/2
UNIVERSAL::can($object, "elements") and scalar @{$object->elements;}
!ll
105
MC/DC100%1/1
UNIVERSAL::can($object, "elements") and scalar @{$object->elements;}
UNIVERSAL::can($object, "elements")
Truth table100%2/2
A
A= UNIVERSAL::can($object, "elements")
Aresult
00
11
508 2 my $elementInfo = $object->effectiveElementInfo();
509
510 2 $pod .= "\n\n=head1 CHILD ELEMENT ACCESSORS\n";
511 2 $pod .= "\n=over\n";
512
513 2 foreach my $accessor (sort @{$object->elements()}) {
514 6 my $element = $elementInfo->{$accessor};
515 6 ▶ next unless (defined $element);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $element 0 6 6
516
517 6 $pod .= "\n=item B<$accessor>()";
518 6 my $aclass = $element->class;
519 6 ▶ if (defined $aclass) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $aclass) 6 0 6
520 6 $pod .= " - See L<$aclass>.";
521 }
522
523 6 $pod .= "\n";
524
525 }
526 2 $pod .= "\n=back\n";
527 }
528
529 # SEE ALSO
530 15 my @see_also = (@$isa, 'XML::Pastor', 'XML::Pastor::Type', 'XML::Pastor::ComplexType', 'XML::Pastor::SimpleType');
531 15 @see_also = map {"L<$_>"} @see_also;
532
533 15 $pod .= "\n\n=head1 SEE ALSO\n\n";
534 15 $pod .= join (", ", @see_also);
535 15 $pod .= "\n";
536
537 # EPILOGUE
538 15 $pod .= "\n\n=cut\n";
539
540
541 15 return $pod;
542 }
543
544
545 #--------------------------------------------
546 # Dump the data for a given "schema type".
547 # Note the use of "Deepcopy" and "Terse"
548 # in order to avoid having references in the dumped structure.
549 # So we have a clean structure with eventual duplication of data but witout references.
550 162 ▶sub _dumpObject {
Subroutine _dumpObject: called CC=1 SCAR=0.0
551 158 my $object = shift;
552 158 my $d = Data::Dumper->new([$object]);
553 158 $d->Sortkeys(1);
554 158 $d->Deepcopy(1);
555 158 $d->Terse(1);
556 158 return $d->Dump();
557 }
558
559 #--------------------------------------------
560 # Write a chunk of code onto a given file.
561 # Used for writing out the code to the output module files.
562 # Works for both 'single' and 'multiple' style
563 19 ▶sub _writeCode {
Subroutine _writeCode: called CC=6 SCAR=18.7
564 17 my $self = shift;
565 17 my $args = {@_};
566 17 ▶ my $code = $args->{code} || "";
Condition50%1/2
$args->{'code'} || ''
l!l
170
MC/DC0%0/1
$args->{'code'} || ''
$args->{'code'}
Truth table50%1/2
A
A= $args->{'code'}
Aresult
11
00
567 17 ▶ my $file = $args->{file} or die "Pastor : Generator : _writeCode : requires a file name\n";
Branch: 1 branch
BranchTrueFalseTotal
my $file = $args->{"file"} or die "Pastor : Generator : _writeCode : requires a file name\n" 17 0 17
568 17 ▶ my $verbose = $self->{verbose} || 0;
Condition50%1/2
$self->{'verbose'} || 0
l!l
017
MC/DC0%0/1
$self->{'verbose'} || 0
$self->{'verbose'}
Truth table50%1/2
A
A= $self->{'verbose'}
Aresult
11
00
569
570 17 ▶ print STDERR "\nWriting module '$file' ..." if ($verbose >=2);
Branch: 1 branch
BranchTrueFalseTotal
if $verbose >= 2 0 17 17
571 17 my ($volume,$directories,$filebase) = File::Spec->splitpath( $file );
572 17 File::Path::mkpath($volume.$directories);
573 17 ▶ my $handle = IO::File->new($file, "w") or die "Pastor : Generator : _writeCode : Can't open file : $file\n";
Branch: 1 branch
BranchTrueFalseTotal
my $handle = "IO::File"->new($file, "w") or die "Pastor : Generator : _writeCode : Can't open file : $file\n" 17 0 17
574
575 17 print $handle $code;
576 17 $handle->close();
577 }
578
579
580 10 1;
581
582 __END__
« blib/lib/XML/Pastor/Schema/Parser.pm ↑ Summary blib/lib/XML/Pastor/Schema/Object.pm »