blib/lib/Data/Stag/StagImpl.pm

filter: Statementstmt 58.5% 830 / 1418 Branchbran 48.1% 263 / 546 Conditioncond 45.0% 100 / 222 MC/DCmcdc 18.8% 23 / 122 Subroutinesub 60.4% 75 / 124 Podpod 0.0% 0 / 100 totaltotal 50.9% 1291 / 2532 CC 399 SCAR 37.8
CC
399
Cov
55%
parser
267.3
_gethandlerobj
165.8
sxpr2tree
132.0
CRAP 43.8
« blib/lib/Data/Stag/PodParser.pm ↑ Summary blib/lib/Data/Stag/SxprParser.pm »
1 # $Id: StagImpl.pm,v 1.67 2008/06/05 06:17:51 cmungall Exp $
2 #
3 # Author: Chris Mungall <cjm@fruitfly.org>
4 #
5 # You may distribute this module under the same terms as perl itself
6  
7 package Data::Stag::StagImpl;
8  
9 =head1 NAME
10  
11 Data::Stag::StagImpl - default implementation for Data::Stag
12  
13 =head1 SYNOPSIS
14  
15 use Data::Stag qw(:all);
16  
17 =head1 DESCRIPTION
18  
19 This is the default implementation for Data::Stag - please see L<Data::Stag>
20  
21 =cut
22  
23 20 ▶use FileHandle;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 20 ▶use IO::String;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 20 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 20 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 20 ▶use vars qw($AUTOLOAD $DEBUG);
Subroutine BEGIN: called CC=1 SCAR=0.0
28 20 ▶use Data::Stag::Base;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 20 ▶use Data::Stag::Util qw(rearrange);
Subroutine BEGIN: called CC=1 SCAR=0.0
30 20 ▶use base qw(Data::Stag::StagI);
Subroutine BEGIN: called CC=1 SCAR=0.0
31  
32 20 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
33 20 $VERSION="0.14";
34  
35  
36 180 ▶sub new {
Subroutine new: called CC=4 SCAR=15.9
Pod undocumented
37 180 my $proto = shift;
38 180 ▶ my $class = ref($proto) || $proto;
Condition33%1/3
ref $proto || $proto
l!l&&r!l&&!r
01800
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
39 180 ▶ if (@_ == 2 || @_ == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 2 or @_ == 0) { } 180 0 180
Condition66%2/3
@_ == 2 or @_ == 0
l!l&&r!l&&!r
17640
MC/DC0%0/2
@_ == 2 or @_ == 0
@_ == 2 @_ == 0
Truth table66%2/3
A or B
A= @_ == 2B= @_ == 0
ABresult
1X1
011
000
40 180 return bless [@_], $class;
41 }
42 else {
43 0 confess @_;
44 }
45 }
46  
47 11 ▶sub unflatten {
Subroutine unflatten: called CC=7 SCAR=20.0
Pod undocumented
48 11 my $proto = shift;
49 11 ▶ my $class = ref($proto) || $proto;
Condition33%1/3
ref $proto || $proto
l!l&&r!l&&!r
0110
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
50 11 my ($name, $flist) = @_;
51 11 my @uflist = ();
52 11 ▶ if (!ref($flist)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $flist) 0 11 11
53 0 return $class->new($name=>$flist);
54 }
55 11 ▶ if (ref($flist) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $flist eq "HASH") 9 2 11
56 # unpack hash into array
57 9 $flist = [%$flist];
58 }
59 11 ▶ if (ref($flist) ne 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $flist ne "ARRAY") 0 11 11
60 0 confess("$name => $flist not array");
61 }
62 11 ▶ while (@$flist) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$flist) 22 11 33
63 22 my $k = shift @$flist;
64 22 my $v = shift @$flist;
65 22 ▶ if (ref($v)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $v) { } 7 15 22
66 7 push(@uflist,
67 $class->unflatten($k=>$v));
68 }
69 else {
70 15 push(@uflist,
71 [$k=>$v]);
72 }
73 }
74 11 return $class->new($name=>[@uflist]);
75 }
76  
77 14 ▶sub unhash {
Subroutine unhash: called CC=7 SCAR=19.8
Pod undocumented
78 14 my $proto = shift;
79 14 ▶ my $class = ref($proto) || $proto;
Condition33%1/3
ref $proto || $proto
l!l&&r!l&&!r
0140
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
80 14 my %hash = @_;
81  
82 14 my @tags = ();
83 14 foreach my $k (keys %hash) {
84 46 my $v = $hash{$k};
85 46 ▶ if (ref($v)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $v) { } 14 32 46
86 14 ▶ if (ref($v) eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $v eq 'ARRAY') { } 4 10 14
elsif (ref $v eq 'HASH') { } 10 0 10
87 4 push(@tags, [$k=>$_]) foreach @$v;
88 }
89 elsif (ref($v) eq 'HASH') {
90 10 my $stag = unhash($class, %$v);
91 10 push(@tags, [$k=>$stag->data]);
92 }
93 else {
94 0 confess("cannot unhash $v");
95 }
96 }
97 else {
98 32 push(@tags, [$k=>$v]);
99 }
100 }
101 14 return $class->new(stag=>[@tags]);
102 }
103  
104 0 ▶sub unstone {
Subroutine unstone: not called CC=1 SCAR=6.9
Pod undocumented
105 0 my $tree = shift;
106 0 my $stone = shift;
107 0 my $xml = $stone->asXML;
108 0 from($tree, xmlstr=>$xml);
109 }
110  
111 0 ▶sub stone {
Subroutine stone: not called CC=1 SCAR=6.9
Pod undocumented
112 0 my $tree = shift;
113 0 my %h = hash($tree);
114 0 load_module("Stone");
115 0 Stone->new(%h);
116 }
117  
118 89 ▶sub load_module {
Subroutine load_module: called CC=4 SCAR=14.2
Pod undocumented
119  
120 89 my $classname = shift;
121 89 ▶ confess("no class") unless $classname;
Branch: 1 branch
BranchTrueFalseTotal
unless $classname 0 89 89
122 89 my $mod = $classname;
123 89 $mod =~ s/::/\//g;
124  
125 89 ▶ if (!$mod) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($mod) 0 89 89
126 0 confess("must supply as mod as argument");
127 }
128  
129 89 ▶ if ($main::{"_<$mod.pm"}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($main::main::{"_<$mod.pm"}) { } 2 87 89
130 }
131 else {
132 87 require "$mod.pm";
133 }
134 }
135  
136 # -------------------------------------
137 # ---- INPUT/OUTPUT FUNCTIONS -----
138 # -------------------------------------
139  
140  
141 17 ▶sub parser {
Subroutine parser: called CC=37 SCAR=55.9
Pod undocumented
142 17 my $tree = shift;
143 17 my ($fn, $fmt, $h, $eh, $str, $fh) =
144 rearrange([qw(file format handler errhandler str fh)], @_);
145  
146 17 ▶ if ($fn && $fn eq '-') {
Branch: 1 branch
BranchTrueFalseTotal
if ($fn and $fn eq "-") 0 17 17
Condition66%2/3
$fn and $fn eq "-"
!ll&&!rl&&r
4130
MC/DC0%0/2
$fn and $fn eq "-"
$fn $fn eq "-"
Truth table66%2/3
A and B
A= $fnB= $fn eq "-"
ABresult
0X0
100
111
147 0 $fn = '';
148 0 $fh = \*STDIN;
149 }
150 # GUESS FORMAT BASED ON FILENAME
151 17 ▶ if (!$fmt && $fn) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $fmt and $fn) 13 4 17
Condition100%3/3
not $fmt and $fn
!ll&&!rl&&r
3113
MC/DC100%2/2
not $fmt and $fn
not $fmt $fn
Truth table100%3/3
not(A) and B
A= not $fmtB= $fn
ABresult
0X0
100
111
152 13 ▶ if ($fn =~ /\.xml$/) {
Branch: 9 branches
BranchTrueFalseTotal
if ($fn =~ /\.xml$/) { } 0 13 13
elsif ($fn =~ /\.indent$/) { } 0 13 13
elsif ($fn =~ /\.ind$/) { } 0 13 13
elsif ($fn =~ /\.xtc$/) { } 0 13 13
elsif ($fn =~ /\.ite?xt$/) { } 10 3 13
elsif ($fn =~ /\.se?xpr$/) { } 1 2 3
elsif ($fn =~ /\.el$/) { } 2 0 2
elsif ($fn =~ /\.pl$/) { } 0 0 0
elsif ($fn =~ /\.perl$/) { } 0 0 0
153 0 $fmt = "xml";
154 }
155 elsif ($fn =~ /\.indent$/) {
156 0 $fmt = "indent";
157 }
158 elsif ($fn =~ /\.ind$/) {
159 0 $fmt = "indent";
160 }
161 elsif ($fn =~ /\.xtc$/) {
162 0 $fmt = "indent";
163 }
164 elsif ($fn =~ /\.ite?xt$/) {
165 10 $fmt = "itext";
166 }
167 elsif ($fn =~ /\.se?xpr$/) {
168 1 $fmt = "sxpr";
169 }
170 elsif ($fn =~ /\.el$/) {
171 2 $fmt = "sxpr";
172 }
173 elsif ($fn =~ /\.pl$/) {
174 0 $fmt = "perl";
175 }
176 elsif ($fn =~ /\.perl$/) {
177 0 $fmt = "perl";
178 }
179 else {
180 # default to xml
181 0 ▶ if (!$str && $fn) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $str and $fn) { } 0 0 0
Condition0%0/3
not $str and $fn
!ll&&!rl&&r
000
MC/DC0%0/2
not $str and $fn
not $str $fn
Truth table0%0/3
not(A) and B
A= not $strB= $fn
ABresult
0X0
100
111
182 0 ▶ my $fh = FileHandle->new($fn) ||
Condition0%0/2
'FileHandle'->new($fn) || confess("no such file $fn")
l!l
00
MC/DC0%0/1
'FileHandle'->new($fn) || confess("no such file $fn")
'FileHandle'->new($fn)
Truth table0%0/2
A
A= 'FileHandle'->new($fn)
Aresult
11
00
183 confess("no such file $fn");
184 # get the first line
185 0 $str = <$fh>;
186 0 chomp $str;
187 0 $fh->close;
188 }
189 else {
190 0 $fmt = "xml";
191 }
192 }
193 }
194  
195 # GUESS FORMAT BASED ON STR
196 17 ▶ if (!$fmt && $str) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $fmt and $str) 1 16 17
Condition66%2/3
not $fmt and $str
!ll&&!rl&&r
1601
MC/DC50%1/2
not $fmt and $str
not $fmt $str
Truth table66%2/3
not(A) and B
A= not $fmtB= $str
ABresult
0X0
100
111
197 1 ▶ if ($str =~ /^\s*\'/) {
Branch: 7 branches
BranchTrueFalseTotal
if ($str =~ /^\s*\'/) { } 0 1 1
elsif ($str =~ /^\s*\(/) { } 1 0 1
elsif ($str =~ /^\s*\;/) { } 0 0 0
elsif ($str =~ /^\s*\</) { } 0 0 0
elsif ($str =~ /^\s*\w+\:\s/) { } 0 0 0
elsif ($str =~ /^\s*\#/) { } 0 0 0
elsif ($str =~ /^\w+/) { } 0 0 0
198 0 $fmt = "sxpr";
199 }
200 elsif ($str =~ /^\s*\(/) {
201 1 $fmt = "sxpr";
202 }
203 elsif ($str =~ /^\s*\;/) {
204 0 $fmt = "sxpr";
205 }
206 elsif ($str =~ /^\s*\</) {
207 0 $fmt = "xml";
208 }
209 elsif ($str =~ /^\s*\w+\:\s/) {
210 0 $fmt = "itext";
211 }
212 elsif ($str =~ /^\s*\#/) {
213 0 $fmt = "indent";
214 }
215 elsif ($str =~ /^\w+/) {
216 0 $fmt = "indent";
217 }
218 else {
219 }
220 }
221  
222 17 my $parser;
223 17 ▶ if (!ref($fmt) && $fmt =~ /::/) {
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $fmt and $fmt =~ /::/) 0 17 17
Condition33%1/3
not ref $fmt and $fmt =~ /::/
!ll&&!rl&&r
0170
MC/DC0%0/2
not ref $fmt and $fmt =~ /::/
not ref $fmt $fmt =~ /::/
Truth table33%1/3
not(A) and B
A= not ref $fmtB= $fmt =~ /::/
ABresult
0X0
100
111
224 0 load_module($fmt);
225 0 $fmt = $fmt->new;
226 }
227  
228 17 ▶ if (ref($fmt)) {
Branch: 6 branches
BranchTrueFalseTotal
if (ref $fmt) { } 0 17 17
elsif ($fmt eq 'xml') { } 0 17 17
elsif ($fmt eq 'indent') { } 0 17 17
elsif ($fmt eq 'itext') { } 10 7 17
elsif ($fmt eq 'perl') { } 0 7 7
elsif ($fmt eq 'sxpr') { } 7 0 7
229 0 $parser = $fmt;
230 }
231 elsif ($fmt eq "xml") {
232 0 $parser = "Data::Stag::XMLParser";
233 }
234 elsif ($fmt eq "indent") {
235 0 $parser = "Data::Stag::IndentParser";
236 }
237 elsif ($fmt eq "itext") {
238 10 $parser = "Data::Stag::ITextParser";
239 }
240 elsif ($fmt eq "perl") {
241 0 $parser = "Data::Stag::PerlParser";
242 }
243 elsif ($fmt eq "sxpr") {
244 7 $parser = "Data::Stag::SxprParser";
245 }
246 else {
247 0 ▶ confess("cannot guess parser from fmt=\"$fmt\" @_") unless $parser;
Branch: 1 branch
BranchTrueFalseTotal
unless $parser 0 0 0
248 }
249 17 ▶ unless (ref($parser)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $parser) 17 0 17
250 17 load_module($parser);
251 17 $parser = $parser->new;
252 }
253 17 ▶ $parser->file($fn) if $fn;
Branch: 1 branch
BranchTrueFalseTotal
if $fn 13 4 17
254 # $parser->fh($fh) if $fh;
255 17 return $parser;
256 }
257  
258 17 ▶sub parse {
Subroutine parse: called CC=7 SCAR=19.6
Pod undocumented
259 17 my $tree = shift;
260 17 my ($fn, $fmt, $h, $eh, $str, $fh) =
261 rearrange([qw(file format handler errhandler str fh)], @_);
262  
263 17 ▶ if (!$tree || !ref($tree)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($tree and ref $tree) 9 8 17
Condition66%2/3
$tree and ref $tree
!ll&&!rl&&r
098
MC/DC50%1/2
$tree and ref $tree
$tree ref $tree
Truth table66%2/3
A and B
A= $treeB= ref $tree
ABresult
0X0
100
111
264 9 $tree = [];
265 }
266  
267 17 my $p = parser($tree, @_);
268 17 ▶ $h = Data::Stag::Base->new unless $h;
Branch: 1 branch
BranchTrueFalseTotal
unless $h 12 5 17
269 17 ▶ if (!$eh) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($eh) 17 0 17
270 17 $eh = getformathandler($tree, 'xml');
271 17 $eh->fh(\*STDERR);
272 }
273 17 $p->handler($h);
274 17 $p->errhandler($eh);
275 17 $p->parse(
276 -file=>$fn,
277 -str=>$str,
278 -fh=>$fh,
279 );
280 17 ▶ my $htree = $h->can("tree") ? $h->tree : [];
Branch: 1 branch
BranchTrueFalseTotal
$h->can('tree') ? : 17 0 17
281 17 Nodify($tree);
282 17 ▶ @$tree = @{$htree || []};
Condition50%1/2
$htree || []
l!l
170
MC/DC0%0/1
$htree || []
$htree
Truth table50%1/2
A
A= $htree
Aresult
11
00
283 17 return $tree;
284 }
285 20 *parseFile = \&parse;
286  
287 1 ▶sub parsestr {
Subroutine parsestr: called CC=1 SCAR=0.0
Pod undocumented
288 1 my $tree = shift;
289 1 my ($str, $fmt, $h, $eh) =
290 rearrange([qw(str format handler errhandler)], @_);
291 return
292 1 $tree->parse(-str=>$str,
293 -format=>$fmt,
294 -handler=>$h,
295 -errhandler=>$eh);
296
297 }
298 20 *parseStr = \&parsestr;
299  
300 3 ▶sub from {
Subroutine from: called CC=4 SCAR=17.9
Pod undocumented
301 3 my $class = shift;
302 3 my ($fmt, $file, $str) =
303 rearrange([qw(fmt file str)], @_);
304 3 ▶ if ($fmt eq 'xmlstr') {
Branch: 3 branches
BranchTrueFalseTotal
if ($fmt eq 'xmlstr') { } 0 3 3
elsif ($fmt =~ /(.*)str/) { } 3 0 3
elsif ($fmt eq 'xml') { } 0 0 0
305 0 return xmlstr2tree($file);
306 }
307 elsif ($fmt =~ /(.*)str/) {
308 3 $fmt = $1;
309 3 return parse([],
310 -str=>$file,
311 -format=>$fmt);
312
313 }
314 elsif ($fmt eq 'xml') {
315 0 return xml2tree($file);
316 }
317 else {
318 0 return parse([],
319 -file=>$file,
320 -format=>$fmt);
321 }
322 }
323  
324 57 ▶sub _gethandlerobj {
Subroutine _gethandlerobj: called CC=20 SCAR=51.1
325 57 ▶ my $tree = shift || [];
Condition50%1/2
shift() || []
l!l
570
MC/DC0%0/1
shift() || []
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
326 57 my ($fn, $fmt, $fh) =
327 rearrange([qw(file fmt fh)], @_);
328 57 ▶ if (!$fmt) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($fmt) 0 57 57
329 0 ▶ if (!$fn) {
Branch: 4 branches
BranchTrueFalseTotal
if (not $fn) { } 0 0 0
elsif ($fn =~ /\.xml$/) { } 0 0 0
elsif ($fn =~ /\.ite?xt$/) { } 0 0 0
elsif ($fn =~ /\.ind/) { } 0 0 0
330 0 $fmt = "xml";
331 }
332 elsif ($fn =~ /\.xml$/) {
333 0 $fmt = "xml";
334 }
335 elsif ($fn =~ /\.ite?xt$/) {
336 0 $fmt = "itext";
337 }
338 elsif ($fn =~ /\.ind/) {
339 0 $fmt = "indent";
340 }
341 else {
342 }
343 }
344 57 my $writer;
345 57 ▶ if (ref($fmt)) {
Branch: 13 branches
BranchTrueFalseTotal
if (ref $fmt) { } 0 57 57
elsif ($fmt =~ /xml/i) { } 45 12 57
elsif ($fmt =~ /itext/i) { } 1 11 12
elsif ($fmt =~ /indent/i) { } 0 11 11
elsif ($fmt =~ /sxpr/i) { } 11 0 11
elsif ($fmt =~ /yaml/i) { } 0 0 0
elsif ($fmt =~ /perl/i) { } 0 0 0
elsif ($fmt =~ /json/i) { } 0 0 0
elsif ($fmt =~ /dtd/i) { } 0 0 0
elsif ($fmt =~ /simple/i) { } 0 0 0
elsif ($fmt =~ m[xslt/(.+)]i) { } 0 0 0
elsif ($fmt =~ /::/) { } 0 0 0
elsif (not $fmt) { } 0 0 0
346 0 return $fmt;
347 }
348 elsif ($fmt =~ /xml/i) {
349 45 $writer = "Data::Stag::XMLWriter";
350 }
351 elsif ($fmt =~ /itext/i) {
352 1 $writer = "Data::Stag::ITextWriter";
353 }
354 elsif ($fmt =~ /indent/i) {
355 0 $writer = "Data::Stag::IndentWriter";
356 }
357 elsif ($fmt =~ /sxpr/i) {
358 11 $writer = "Data::Stag::SxprWriter";
359 }
360 elsif ($fmt =~ /yaml/i) {
361 0 $writer = "Data::Stag::YAMLWriter";
362 }
363 elsif ($fmt =~ /perl/i) {
364 0 $writer = "Data::Stag::PerlWriter";
365 }
366 elsif ($fmt =~ /json/i) {
367 0 $writer = "Data::Stag::JSONWriter";
368 }
369 elsif ($fmt =~ /dtd/i) {
370 0 $writer = "Data::Stag::DTDWriter";
371 }
372 elsif ($fmt =~ /simple/i) {
373 0 $writer = "Data::Stag::Simple";
374 }
375 elsif ($fmt =~ /xslt\/(.+)/i) {
376 0 my $xslt_file = $1;
377 0 $writer = "Data::Stag::XSLTHandler";
378 0 load_module($writer);
379 0 my $w = $writer->new(-file=>$fn, -fh=>$fh);
380 0 $w->xslt_file($xslt_file);
381 0 return $w;
382 }
383 elsif ($fmt =~ /::/) {
384 0 $writer = $fmt;
385 }
386 elsif (!$fmt) {
387 0 confess("no format/writer!");
388 }
389 else {
390 0 confess("unrecognised:$fmt");
391 }
392 57 load_module($writer);
393  
394 57 my $w = $writer->new(-file=>$fn, -fh=>$fh);
395 57 return $w;
396 }
397 20 *findhandler = \&_gethandlerobj;
398  
399 18 ▶sub getformathandler {
Subroutine getformathandler: called CC=1 SCAR=0.0
Pod undocumented
400 18 my $tree = shift;
401 18 my $fmt = shift;
402 18 return findhandler($tree, -fmt=>$fmt);
403 }
404  
405 38 ▶sub generate {
Subroutine generate: called CC=3 SCAR=11.3
Pod undocumented
406 38 ▶ my $tree = shift || [];
Condition50%1/2
shift() || []
l!l
380
MC/DC0%0/1
shift() || []
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
407 38 my $w = _gethandlerobj($tree, @_);
408 38 $w->is_buffered(1);
409 38 $w->event(@$tree);
410 38 ▶ return $w->popbuffer || '';
Condition50%1/2
$w->popbuffer || ''
l!l
380
MC/DC0%0/1
$w->popbuffer || ''
$w->popbuffer
Truth table50%1/2
A
A= $w->popbuffer
Aresult
11
00
411 }
412 20 *gen = \&generate;
413  
414 1 ▶sub write {
Subroutine write: called CC=2 SCAR=7.0
Pod undocumented
415 1 ▶ my $tree = shift || [];
Condition50%1/2
shift() || []
l!l
10
MC/DC0%0/1
shift() || []
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
416 1 my $w = _gethandlerobj($tree, @_);
417  
418 1 $w->is_buffered(0);
419 1 $w->event(@$tree);
420 1 $w->close_fh;
421 1 return;
422 }
423
424 0 ▶sub makexslhandler {
Subroutine makexslhandler: not called CC=1 SCAR=6.9
Pod undocumented
425 0 my $tree = shift;
426 0 my $xslt_file = shift;
427 0 load_module("Data::Stag::XSLTHandler");
428 0 my $handler = Data::Stag::XSLTHandler->new;
429 0 $handler->xslt_file($xslt_file);
430 0 return $handler;
431 }
432  
433 5 ▶sub makehandler {
Subroutine makehandler: called CC=5 SCAR=16.8
Pod undocumented
434 5 my $tree = shift;
435 5 my $handler;
436 5 ▶ if (@_ == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1) { } 0 5 5
437 0 my $module = shift;
438 0 ▶ if ($module =~ /\.xsl/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($module =~ /\.xsl/) { } 0 0 0
439 0 $handler = makexslhandler($tree, $module);
440 }
441 else {
442 0 load_module($module);
443 0 $handler = $module->new;
444 }
445 }
446 else {
447 5 my %trap_h = @_;
448 5 my %opt_h = ();
449 %trap_h =
450 map {
451 5 ▶ if ($_ =~ /^-(.*)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_ =~ /^-(.*)/) { } 1 11 12
452 1 $opt_h{lc($1)} = $trap_h{$_};
453 1 ();
454 }
455 else {
456 11 ($_ => $trap_h{$_})
457 }
458 } keys %trap_h;
459 5 load_module("Data::Stag::BaseHandler");
460 5 $handler = Data::Stag::BaseHandler->new;
461 5 $handler->trap_h(\%trap_h);
462
463 5 ▶ if ($opt_h{notree}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt_h{"notree"}) 1 4 5
464 1 load_module("Data::Stag::null");
465 1 my $null = Data::Stag::null->new;
466 1 my $ch = Data::Stag->chainhandlers([keys %trap_h],
467 $handler,
468 $null);
469 1 return $ch;
470 }
471 }
472 4 return $handler;
473 }
474 20 *mh = \&makehandler;
475  
476 2 ▶sub chainhandlers {
Subroutine chainhandlers: called CC=6 SCAR=22.7
Pod undocumented
477 2 my $tree = shift;
478 2 my $block = shift;
479 2 my @sh = @_;
480  
481 2 load_module("Data::Stag::ChainHandler");
482 2 my $handler = Data::Stag::ChainHandler->new;
483 $handler->subhandlers([
484 map {
485 2 ▶ if (ref($_)) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $_) { } 4 0 4
elsif (not $_) { } 0 0 0
486 4 ▶ if (ref($_) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_ eq 'HASH') { } 0 4 4
487 # make a new handler
488 0 makehandler($tree, %$_);
489 }
490 else {
491 # assume it is an object
492 4 $_;
493 }
494 }
495 elsif (!$_) {
496 ()
497 0 }
498 else {
499 # assume it is string specifying format
500 0 _gethandlerobj($tree, -fmt=>$_)
501 }
502 } @sh
503 ]);
504 2 $handler->blocked_event($block);
505  
506 # if no explicit blocked events set, then introspect
507 # the subhandlers to see if they declare what they emit
508 2 ▶ if (ref($block) && !@$block) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $block and not @$block) 0 2 2
Condition33%1/3
ref $block and not @$block
!ll&&!rl&&r
020
MC/DC0%0/2
ref $block and not @$block
ref $block not @$block
Truth table33%1/3
A and not(B)
A= ref $blockB= not @$block
ABresult
0X0
100
111
509 0 my @emits = map {$_->CONSUMES} @{$handler->subhandlers};
510 0 $handler->blocked_event(\@emits);
511 }
512 2 return $handler;
513 }
514  
515 0 ▶sub transform {
Subroutine transform: not called CC=1 SCAR=6.9
Pod undocumented
516 0 my $tree = shift;
517 0 my @T = @_;
518 my %trap_h =
519 map {
520 0 my ($from, $to) = @$_;
521 0 ▶ $from=> sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
522 0 my $self = shift;
523 0 my $stag = shift;
524 # print STDERR "Transforming $from => $to\n";
525 # print STDERR $stag->sxpr;
526 0 my $data = $stag->data;
527 0 my @path = splitpath($to);
528 0 my $node = [];
529 0 my $p = $node;
530 0 ▶ while (@path) {
Branch: 1 branch
BranchTrueFalseTotal
if (@path) 0 0 0
531 0 my $elt = shift @path;
532 0 $p->[0] = $elt;
533 0 ▶ if (@path) {
Branch: 1 branch
BranchTrueFalseTotal
if (@path) { } 0 0 0
534 0 my $newpath = [];
535 0 $p->[1] = [$newpath];
536 0 $p = $newpath;
537 }
538 else {
539 0 $p->[1] = $data;
540 }
541 }
542 # @$stag = @$node;
543 # print STDERR $stag->sxpr;
544 0 return $node;
545 # return 0;
546 }
547 0 } @T;
548 0 load_module("Data::Stag::BaseHandler");
549 0 my $handler = Data::Stag::BaseHandler->new;
550 0 $handler->trap_h(\%trap_h);
551 0 $tree->events($handler);
552 0 my $nu = $handler->stag;
553 0 @$tree = @$nu;
554 0 return;
555 }
556 20 *t = \&transform;
557  
558 # transform stag into hash datastruct;
559 # stag keys become hash keys (unordered)
560 # single valued keys map to single value (itself a hash or primitive)
561 # multivalued map to arrayrefs
562 7 ▶sub hash {
Subroutine hash: called CC=7 SCAR=19.6
Pod undocumented
563 7 my $tree = shift;
564 7 my ($ev, $subtree) = @$tree;
565  
566 # make sure we have non-terminal
567 7 ▶ if (ref($subtree)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $subtree) { } 7 0 7
568 # make hash using stag keys
569 7 my %h = ();
570 7 foreach my $subnode (@$subtree) {
571 26 my $k = $subnode->[0];
572 26 my $v;
573
574 # terminals map to data value; non-terminals
575 # get recursively mapped to hashes
576 26 ▶ if (isterminal($subnode)) {
Branch: 1 branch
BranchTrueFalseTotal
if (isterminal($subnode)) { } 21 5 26
577 21 $v = $subnode->[1];
578 }
579 else {
580 5 $v = {hash($subnode)};
581 }
582  
583 # determine if it is single-valued or multi-valued hash
584 26 my $curr = $h{$k};
585 26 ▶ if ($curr) {
Branch: 1 branch
BranchTrueFalseTotal
if ($curr) { } 3 23 26
586 3 ▶ if (ref($curr) && ref($curr) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $curr and ref $curr eq 'ARRAY') { } 1 2 3
Condition66%2/3
ref $curr and ref $curr eq "ARRAY"
!ll&&!rl&&r
201
MC/DC50%1/2
ref $curr and ref $curr eq "ARRAY"
ref $curr ref $curr eq "ARRAY"
Truth table66%2/3
A and B
A= ref $currB= ref $curr eq "ARRAY"
ABresult
0X0
100
111
587 1 push(@$curr, $v);
588 }
589 else {
590 2 $h{$k} = [$curr, $v];
591 }
592 }
593 else {
594 # if there is only one value, don't use array -- yet
595 23 $h{$k} = $v;
596 }
597 }
598 7 return %h;
599 }
600 else {
601 0 warn("can't make a hash from a terminal");
602 0 return ();
603 }
604 }
605 20 *tree2hash = \&hash;
606  
607 # this FLATTENS everything
608 # any non terminal node is flattened and lost
609 # does not check for duplicates!
610 0 ▶sub pairs {
Subroutine pairs: not called CC=1 SCAR=6.9
Pod undocumented
611 0 return @{(_pairs(@_))[1]};
612 }
613 20 *tree2pairs = \&pairs;
614 0 ▶sub _pairs {
Subroutine _pairs: not called CC=2 SCAR=17.9
615 0 my $tree = shift;
616 0 my ($ev, $subtree) = @$tree;
617 0 ▶ if (ref($subtree)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $subtree) { } 0 0 0
618 0 my @pairs = map { _pairs($_) } @$subtree;
619 0 return $ev=>[@pairs];
620 }
621 else {
622 0 return $ev=>$subtree;
623 }
624 }
625  
626 # PRIVATE
627 0 ▶sub tab {
Subroutine tab: not called CC=1 SCAR=6.9
Pod undocumented
628 0 my $t=shift;
629 0 return " " x $t;
630 }
631  
632 0 ▶sub dlist {
Subroutine dlist: not called CC=2 SCAR=17.9
Pod undocumented
633 0 my $tree = shift;
634 0 ▶ my $del = shift || '/';
Condition0%0/2
shift() || '/'
l!l
00
MC/DC0%0/1
shift() || '/'
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
635 0 _dlist($tree, $del, $del, 1);
636 }
637  
638 0 ▶sub _dlist {
Subroutine _dlist: not called CC=3 SCAR=24.8
639 0 my $tree = shift;
640 0 ▶ my $del = shift || '/';
Condition0%0/2
shift() || '/'
l!l
00
MC/DC0%0/1
shift() || '/'
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
641 0 my $root = shift;
642 0 my $nextid = shift;
643 0 my @kids = $tree->kids;
644 0 ▶ if (isterminal($tree)) {
Branch: 1 branch
BranchTrueFalseTotal
if (isterminal($tree)) { } 0 0 0
645 0 my $data = $tree->data;
646 0 $data =~ s/$del/\\$del/g;
647 # my $stem = $root . $tree->element . "[$nextid]";
648 # $root = $stem . $del;
649 # return ($root, $root . $del . $data);
650  
651 0 return ($root . $tree->element . ': ' . $data . "[$nextid]");
652 }
653 else {
654 0 my $id = 1;
655 0 my $stem = $root . $tree->element . "[$nextid]";
656 0 $root = $stem . $del;
657 my @dlist =
658 map {
659 0 _dlist($_, $del, $root, $id++);
660 } @kids;
661 0 return ($stem, @dlist);
662 }
663 # return (@dlist);
664 }
665  
666 28 ▶sub xml {
Subroutine xml: called CC=1 SCAR=0.0
Pod undocumented
667 28 my $tree = shift;
668 28 my $fn = shift;
669 28 generate($tree, $fn, 'xml', @_);
670 }
671 20 *tree2xml = \&xml;
672  
673 9 ▶sub sxpr {
Subroutine sxpr: called CC=1 SCAR=0.0
Pod undocumented
674 9 my $tree = shift;
675 9 my $fn = shift;
676 9 generate($tree, $fn, 'sxpr', @_);
677 }
678  
679 1 ▶sub itext {
Subroutine itext: called CC=1 SCAR=0.0
Pod undocumented
680 1 my $tree = shift;
681 1 my $fn = shift;
682 1 generate($tree, $fn, 'itext', @_);
683 }
684  
685 0 ▶sub indent {
Subroutine indent: not called CC=1 SCAR=6.9
Pod undocumented
686 0 my $tree = shift;
687 0 my $fn = shift;
688 0 generate($tree, $fn, 'indent', @_);
689 }
690  
691 0 ▶sub as {
Subroutine as: not called CC=1 SCAR=6.9
Pod undocumented
692 0 my $tree = shift;
693 0 my $fmt = shift;
694 0 my $fn = shift;
695 0 generate($tree, $fn, $fmt, @_);
696 }
697  
698 0 ▶sub perldump {
Subroutine perldump: not called CC=1 SCAR=6.9
Pod undocumented
699 0 my $tree = shift;
700 return
701 0 _tree2perldump($tree, 1) . ";\n";
702 }
703 20 *tree2perldump = \&perldump;
704  
705 0 ▶sub _tree2perldump {
Subroutine _tree2perldump: not called CC=4 SCAR=30.0
706 0 my $tree = shift;
707 0 ▶ my $indent = shift || 0;
Condition0%0/2
shift() || 0
l!l
00
MC/DC0%0/1
shift() || 0
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
708 0 my ($ev, $subtree) = @$tree;
709 0 ▶ if (ref($subtree)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $subtree) { } 0 0 0
710 0 $indent++;
711 return
712 sprintf("%s[ '$ev' => [\n%s%s]]",
713 # tab($indent++),
714 "",
715 join(",\n", map {
716 0 tab($indent) .
717 _tree2perldump($_, $indent)
718 } @$subtree),
719 # tab($indent-1),
720 "",
721 );
722 }
723 else {
724 return
725 0 ▶ sprintf("%s[ '$ev' => %s ]",
Condition0%0/2
perlesc($subtree) || ''
l!l
00
MC/DC0%0/1
perlesc($subtree) || ''
perlesc($subtree)
Truth table0%0/2
A
A= perlesc($subtree)
Aresult
11
00
726 # tab($indent),
727 "",
728 perlesc($subtree) || "");
729 }
730 }
731  
732 0 ▶sub perlesc {
Subroutine perlesc: not called CC=2 SCAR=17.9
Pod undocumented
733 0 my $val = shift;
734 0 ▶ return "undef" if !defined $val;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val 0 0 0
735 0 $val =~ s/\'/\\\'/g;
736 0 return "'$val'";
737 }
738  
739 1 ▶sub sax {
Subroutine sax: called CC=1 SCAR=0.0
Pod undocumented
740 1 my $tree = shift;
741 1 my $saxhandler = shift;
742 1 $saxhandler->start_document;
743 1 _tree2sax($tree, $saxhandler);
744 1 $saxhandler->end_document;
745 }
746 20 *tree2sax = \&sax;
747  
748 37 ▶sub _tree2sax {
Subroutine _tree2sax: called CC=2 SCAR=6.9
749 37 my $tree = shift;
750 37 my $saxhandler = shift;
751 37 my ($ev, $subtree) = @$tree;
752 37 $saxhandler->start_element({Name => $ev});
753 37 ▶ if (ref($subtree)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $subtree) { } 13 24 37
754 13 map { _tree2sax($_, $saxhandler) } @$subtree;
755 }
756 else {
757 24 $saxhandler->characters({Data => $subtree});
758
759 }
760 37 $saxhandler->end_element({Name => $ev});
761 }
762  
763 0 ▶sub xslt {
Subroutine xslt: not called CC=1 SCAR=6.9
Pod undocumented
764 0 my $tree = shift;
765 0 my $xsltstr = xsltstr($tree,@_);
766 0 return parsestr($tree,
767 -str=>$xsltstr,
768 -format=>'xml');
769 }
770  
771 0 ▶sub xsltstr {
Subroutine xsltstr: not called CC=1 SCAR=6.9
Pod undocumented
772 0 my $stag = shift;
773 0 my $xslt_file = shift;
774
775 0 load_module("XML::LibXML");
776 0 load_module("XML::LibXSLT");
777 0 my $parser = XML::LibXML->new();
778 0 my $source = $parser->parse_string($stag->xml);
779
780 0 my $xslt = XML::LibXSLT->new();
781 0 my $styledoc = $parser->parse_file($xslt_file);
782 0 my $stylesheet = $xslt->parse_stylesheet($styledoc);
783  
784 0 my $results = $stylesheet->transform($source);
785 0 return $results->toString;
786 }
787  
788  
789 0 ▶sub events {
Subroutine events: not called CC=1 SCAR=6.9
Pod undocumented
790 0 my $tree = shift;
791 0 my $handler = shift;
792 0 $handler->event(@$tree);
793 }
794  
795 0 ▶sub xmlesc {
Subroutine xmlesc: not called CC=2 SCAR=17.9
Pod undocumented
796 0 my $word = shift;
797 0 ▶ return '' unless defined $word;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $word 0 0 0
798 0 $word =~ s/\&/\&amp;/g;
799 0 $word =~ s/\</\&lt;/g;
800 0 $word =~ s/\>/\&gt;/g;
801 0 return $word;
802
803 }
804  
805 0 ▶sub xml2tree {
Subroutine xml2tree: not called CC=1 SCAR=6.9
Pod undocumented
806 0 my $file = shift;
807 0 my $handler = Data::Stag::Base->new;
808 0 load_module("XML::Parser::PerlSAX");
809 0 my $parser = XML::Parser::PerlSAX->new();
810 0 my %parser_args = (Source => {SystemId => $file},
811 Handler => $handler);
812 0 $parser->parse(%parser_args);
813 0 return Node(@{$handler->tree});
814 }
815  
816 0 ▶sub xmlstr2tree {
Subroutine xmlstr2tree: not called CC=1 SCAR=6.9
Pod undocumented
817 0 my $str = shift;
818 0 my $handler = Data::Stag::Base->new;
819 0 load_module("XML::Parser::PerlSAX");
820 0 my $parser = XML::Parser::PerlSAX->new();
821 0 my %parser_args = (Source => {String => $str},
822 Handler => $handler);
823 0 $parser->parse(%parser_args);
824 0 return Node(@{$handler->tree});
825 }
826  
827 # WANTARRAY
828 0 ▶sub sxpr2tree {
Subroutine sxpr2tree: not called CC=11 SCAR=48.8
Pod undocumented
829 0 my $sxpr = shift;
830 0 ▶ my $indent = shift || 0;
Condition0%0/2
shift() || 0
l!l
00
MC/DC0%0/1
shift() || 0
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
831 0 my $i=0;
832 0 my @args = ();
833 0 ▶ while ($i < length($sxpr)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < length $sxpr) 0 0 0
834 0 my $c = substr($sxpr, $i, 1);
835 ################################# print STDERR "c;$c i=$i\n";
836 0 $i++;
837 0 ▶ if ($c eq ')') {
Branch: 1 branch
BranchTrueFalseTotal
if ($c eq ")") 0 0 0
838 0 my $funcnode = shift @args;
839 # print STDERR "f = $funcnode->[1]\n";
840 0 map {print xml($_)} @args;
841 0 return [$funcnode->[1] =>[@args]], $i;
842 }
843 0 ▶ if ($c =~ /\s/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c =~ /\s/) 0 0 0
844 0 next;
845 }
846 0 ▶ if ($c eq '(') {
Branch: 1 branch
BranchTrueFalseTotal
if ($c eq '(') { } 0 0 0
847 0 my ($tree, $extra) = sxpr2tree(substr($sxpr, $i), $indent+1);
848 0 push(@args, $tree);
849 0 $i += $extra;
850 # printf STDERR "tail: %s\n", substr($sxpr, $i);
851 }
852 else {
853 # look ahead
854 0 my $v = "$c";
855 0 my $p=0;
856 0 ▶ while ($i+$p < length($sxpr)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i + $p < length $sxpr) 0 0 0
857 0 my $c = substr($sxpr, $i+$p, 1);
858 0 ▶ if ($c =~ /\s/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c =~ /\s/) 0 0 0
859 0 last;
860 }
861 0 ▶ if ($c eq '(') {
Branch: 1 branch
BranchTrueFalseTotal
if ($c eq "(") 0 0 0
862 0 last;
863 }
864 0 ▶ if ($c eq ')') {
Branch: 1 branch
BranchTrueFalseTotal
if ($c eq ")") 0 0 0
865 0 last;
866 }
867 0 $p++;
868 0 $v.= $c;
869 }
870 0 $i+=$p;
871 0 push(@args, [arg=>$v]);
872 }
873 }
874 # map {print xml($_)} @args;
875 0 map {Nodify($_)} @args;
876 0 ▶ if (wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) 0 0 0
877 0 return @args;
878 }
879
880 0 return $args[0];
881 }
882  
883 82 ▶sub addkid {
Subroutine addkid: called CC=6 SCAR=21.8
Pod undocumented
884 82 my $tree = shift;
885 82 my $newtree = shift;
886 82 ▶ confess("problem: $tree not arr") unless ref($tree) && ref($tree) eq "ARRAY" || isastag($tree);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $tree and ref $tree eq "ARRAY" or isastag($tree) 0 82 82
Condition33%2/6
ref $tree and ref $tree eq "ARRAY" or isastag($tree)
l!l&&r!l&&!r
0820
ref $tree and ref $tree eq "ARRAY"
!ll&&!rl&&r
0820
MC/DC0%0/3
ref $tree and ref $tree eq "ARRAY" or isastag($tree)
ref $tree ref $tree eq "ARRAY" isastag($tree)
Truth table20%1/5
A and B or C
A= ref $treeB= ref $tree eq "ARRAY"C= isastag($tree)
ABCresult
11X1
0X11
1011
0X00
1000
887 82 my ($ev, $subtree) = @$tree;
888 82 ▶ if (ref($newtree) && $newtree->[0] eq '@') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $newtree and $newtree->[0] eq '@') { } 0 82 82
Condition33%1/3
ref $newtree and $newtree->[0] eq "\@"
!ll&&!rl&&r
0820
MC/DC0%0/2
ref $newtree and $newtree->[0] eq "\@"
ref $newtree $newtree->[0] eq "\@"
Truth table33%1/3
A and B
A= ref $newtreeB= $newtree->[0] eq "\@"
ABresult
0X0
100
111
889 0 unshift(@$subtree, $newtree);
890 }
891 else {
892 82 push(@$subtree, $newtree);
893 }
894 82 $newtree;
895 }
896 20 *addChildTree = \&addkid;
897 20 *addchild = \&addkid;
898 20 *ak = \&addkid;
899  
900 #sub findChildren {
901 # my $tree = shift;
902 # confess("problem: $tree not arr") unless ref($tree) && ref($tree) eq "ARRAY" || isaNode($tree);
903 # my ($ev, $subtree) = @$tree;
904 # return @$subtree;
905 #}
906  
907 568 ▶sub findnode {
Subroutine findnode: called CC=12 SCAR=28.7
Pod undocumented
908 568 my $tree = shift;
909 568 my ($node, @path) = splitpath(shift);
910  
911 568 my $replace = shift;
912 568 ▶ confess("problem: $tree not arr") unless ref($tree) && ref($tree) eq "ARRAY" || isastag($tree);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $tree and ref $tree eq "ARRAY" or isastag($tree) 0 568 568
Condition66%4/6
ref $tree and ref $tree eq "ARRAY" or isastag($tree)
l!l&&r!l&&!r
1344340
ref $tree and ref $tree eq "ARRAY"
!ll&&!rl&&r
0434134
MC/DC0%0/3
ref $tree and ref $tree eq "ARRAY" or isastag($tree)
ref $tree ref $tree eq "ARRAY" isastag($tree)
Truth table40%2/5
A and B or C
A= ref $treeB= ref $tree eq "ARRAY"C= isastag($tree)
ABCresult
11X1
0X11
1011
0X00
1000
913  
914 568 ▶ if (@path) {
Branch: 1 branch
BranchTrueFalseTotal
if (@path) 0 568 568
915 0 my @r = map { $_->findnode(\@path, $replace) } findnode($tree, $node);
916 0 return @r;
917 }
918  
919 568 my ($ev, $subtree) = @$tree;
920 568 my @r = ();
921 568 ▶ if ($DEBUG) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Data::Stag::StagImpl::DEBUG) 0 568 568
922 0 print STDERR "$ev, $subtree;; replace = $replace\n";
923 }
924 568 ▶ if (test_eq($ev, $node)) {
Branch: 1 branch
BranchTrueFalseTotal
if (test_eq($ev, $node)) 46 522 568
925 46 ▶ if ($DEBUG) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Data::Stag::StagImpl::DEBUG) 0 46 46
926 0 print STDERR " MATCH\n";
927 }
928 46 ▶ if (defined $replace) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $replace) 0 46 46
929 0 my @old = @$tree;
930 0 @$tree = @$replace;
931 0 return Nodify([@old]);
932 }
933 # return [$ev=>$subtree] ;
934 # return Nodify($tree);
935 46 @r = (Nodify($tree));
936 }
937 568 my @nextlevel = ();
938 568 ▶ if ( ref($subtree)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $subtree) 201 367 568
939 @nextlevel =
940 map {
941 201 map {
942 541 Nodify($_)
943 } findnode($_, $node, $replace);
944
945 } @$subtree;
946 # get rid of empty nodes
947 # (can be caused by replacing)
948 201 ▶ @$subtree = map { ref($_) && !scalar(@$_) ? () : $_ } @$subtree;
Branch: 1 branch
BranchTrueFalseTotal
ref $_ && !scalar(@$_) ? : 0 541 541
Condition33%1/3
ref $_ and not scalar @$_
!ll&&!rl&&r
05410
MC/DC0%0/2
ref $_ and not scalar @$_
ref $_ not scalar @$_
Truth table33%1/3
A and not(B)
A= ref $_B= not scalar @$_
ABresult
0X0
100
111
949 }
950 # if (wantarray) {
951 # return $nextlevel[0];
952 # }
953 568 return (@r, @nextlevel);
954 }
955 20 *fn = \&findnode;
956 20 *findSubTree = \&findnode;
957 20 *fst = \&findnode;
958  
959 0 ▶sub remove {
Subroutine remove: not called CC=8 SCAR=42.8
Pod undocumented
960 0 my $tree = shift;
961 0 my ($node, @path) = splitpath(shift);
962  
963 0 my $replace = shift;
964 0 ▶ confess("problem: $tree not arr") unless ref($tree) && ref($tree) eq "ARRAY" || isastag($tree);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $tree and ref $tree eq "ARRAY" or isastag($tree) 0 0 0
Condition0%0/6
ref $tree and ref $tree eq "ARRAY" or isastag($tree)
l!l&&r!l&&!r
000
ref $tree and ref $tree eq "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/3
ref $tree and ref $tree eq "ARRAY" or isastag($tree)
ref $tree ref $tree eq "ARRAY" isastag($tree)
Truth table0%0/5
A and B or C
A= ref $treeB= ref $tree eq "ARRAY"C= isastag($tree)
ABCresult
11X1
0X11
1011
0X00
1000
965  
966 0 ▶ if (@path) {
Branch: 1 branch
BranchTrueFalseTotal
if (@path) 0 0 0
967 0 $_->remove(\@path) foreach findnode($tree, $node);
968 0 return;
969 }
970  
971 0 my ($ev, $subtree) = @$tree;
972 0 ▶ return unless ref $subtree;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $subtree 0 0 0
973 0 my @subnodes = @$subtree;
974 @subnodes =
975 grep {
976 0 $_->[0] ne $node;
977 } @subnodes;
978 0 @$subtree = @subnodes;
979 0 remove($_, $node) foreach @subnodes;
980 0 return;
981 }
982  
983 12 ▶sub set {
Subroutine set: called CC=12 SCAR=33.6
Pod undocumented
984 12 ▶ my $tree = shift || confess;
Condition50%1/2
shift() || confess()
l!l
120
MC/DC0%0/1
shift() || confess()
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
985 12 my ($node, @path) = splitpath(shift);
986 12 my @replace = @_;
987  
988 12 ▶ if (@path) {
Branch: 1 branch
BranchTrueFalseTotal
if (@path) 0 12 12
989 0 my $last = pop @path;
990 0 my @nodes = getnode($tree, [$node, @path]);
991 0 foreach (@nodes) {
992 0 set($_, $last, @replace);
993 }
994 0 return @replace;
995 }
996  
997 12 ▶ confess("problem: $tree not arr") unless ref($tree) && ref($tree) eq "ARRAY" || isastag($tree);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $tree and ref $tree eq "ARRAY" or isastag($tree) 0 12 12
Condition33%2/6
ref $tree and ref $tree eq "ARRAY" or isastag($tree)
l!l&&r!l&&!r
0120
ref $tree and ref $tree eq "ARRAY"
!ll&&!rl&&r
0120
MC/DC0%0/3
ref $tree and ref $tree eq "ARRAY" or isastag($tree)
ref $tree ref $tree eq "ARRAY" isastag($tree)
Truth table20%1/5
A and B or C
A= ref $treeB= ref $tree eq "ARRAY"C= isastag($tree)
ABCresult
11X1
0X11
1011
0X00
1000
998 12 my ($ev, $subtree) = @$tree;
999 12 ▶ confess("$subtree not arr [$ev IS A TERMINAL NODE!!]") unless ref($subtree);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $subtree 0 12 12
1000 12 my $is_set;
1001 12 my @nu = ();
1002 12 foreach my $st (@$subtree) {
1003 24 push(@nu, $st);
1004 24 my ($ev, $subtree) = @$st;
1005 24 ▶ if (test_eq($ev, $node)) {
Branch: 1 branch
BranchTrueFalseTotal
if (test_eq($ev, $node)) 0 24 24
1006 0 ▶ if (!$is_set) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $is_set) { } 0 0 0
1007 0 pop @nu;
1008 push(@nu,
1009 map {
1010 0 [$node => $_]
1011 } @replace);
1012 0 $is_set = 1;
1013 }
1014 else {
1015 0 pop @nu;
1016 }
1017 }
1018 }
1019
1020 # place at the end if not already present
1021 12 ▶ if (!$is_set) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $is_set) { } 12 0 12
1022 map {
1023 12 addkid($tree, [$node=>$_]);
1024 } @replace;
1025 }
1026 else {
1027 0 @$tree = ($ev, \@nu);
1028 }
1029 12 return @replace;
1030 }
1031 20 *s = \&set;
1032 20 *setSubTreeVal = \&set;
1033  
1034 0 ▶sub setl {
Subroutine setl: not called CC=3 SCAR=24.8
Pod undocumented
1035 0 ▶ my $tree = shift || confess;
Condition0%0/2
shift() || confess()
l!l
00
MC/DC0%0/1
shift() || confess()
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
1036 0 my @args = @_;
1037 0 ▶ while (@args) {
Branch: 1 branch
BranchTrueFalseTotal
if (@args) 0 0 0
1038 0 set($tree, splice(@args, 0, 2));
1039 }
1040 0 return;
1041 }
1042 20 *sl = \&setl;
1043 20 *setlist = \&setl;
1044  
1045 2 ▶sub setnode {
Subroutine setnode: called CC=1 SCAR=0.0
Pod undocumented
1046 2 my $tree = shift;
1047 2 my $elt = shift;
1048 2 my $nunode = shift;
1049 2 set($tree, $elt, data($nunode));
1050 }
1051 20 *sn = \&setnode;
1052 20 *setn = \&setnode;
1053 20 *settree = \&setnode;
1054  
1055 # EXPERIMENTAL
1056 7 ▶sub free {
Subroutine free: called CC=1 SCAR=0.0
Pod undocumented
1057 7 my $tree = shift;
1058 7 @$tree = ();
1059 }
1060  
1061 115 ▶sub add {
Subroutine add: called CC=16 SCAR=28.9
Pod undocumented
1062 115 ▶ my $tree = shift || confess;
Condition50%1/2
shift() || confess()
l!l
1150
MC/DC0%0/1
shift() || confess()
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
1063 115 my $node = shift;
1064 # usage1: stag_add($tree, $name, @nodes)
1065 115 my @v = @_; # nodes to be added
1066 115 ▶ if (ref($node)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $node) 0 115 115
1067 # usage2: stag_add($tree, $node)
1068 0 ▶ if ($node->isnull) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node->isnull) 0 0 0
1069 0 confess("cannot add null node");
1070 }
1071 # split node into name and data
1072 0 ($node, @v) = ($node->[0], [$node->[1]]);
1073 }
1074 115 ▶ if (ref($v[0]) && !ref($v[0]->[0])) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $v[0] and not ref $v[0][0]) 2 113 115
Condition100%3/3
ref $v[0] and not ref $v[0][0]
!ll&&!rl&&r
31102
MC/DC100%2/2
ref $v[0] and not ref $v[0][0]
ref $v[0] not ref $v[0][0]
Truth table100%3/3
A and not(B)
A= ref $v[0]B= not ref $v[0][0]
ABresult
0X0
100
111
1075 2 @v = map { $_->[1] } @v;
1076 }
1077 115 ▶ confess("problem: $tree not arr") unless ref($tree) && ref($tree) eq "ARRAY" || isastag($tree);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $tree and ref $tree eq "ARRAY" or isastag($tree) 0 115 115
Condition33%2/6
ref $tree and ref $tree eq "ARRAY" or isastag($tree)
l!l&&r!l&&!r
01150
ref $tree and ref $tree eq "ARRAY"
!ll&&!rl&&r
01150
MC/DC0%0/3
ref $tree and ref $tree eq "ARRAY" or isastag($tree)
ref $tree ref $tree eq "ARRAY" isastag($tree)
Truth table20%1/5
A and B or C
A= ref $treeB= ref $tree eq "ARRAY"C= isastag($tree)
ABCresult
11X1
0X11
1011
0X00
1000
1078 115 my ($ev, $subtree) = @$tree;
1079  
1080 115 my @nu_subtree = ();
1081 115 my $has_been_set = 0;
1082 115 ▶ for (my $i=0; $i<@$subtree; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$subtree) 319 115 434
1083 319 my $st = $subtree->[$i];
1084 319 my $next_st = $subtree->[$i+1];
1085 319 my ($ev, $subtree) = @$st;
1086 319 push(@nu_subtree, $st);
1087 319 ▶ if (!$has_been_set &&
Branch: 1 branch
BranchTrueFalseTotal
if (not $has_been_set and test_eq($ev, $node) and !$next_st || $next_st->[0] ne $ev) 45 274 319
Condition100%9/9
not $has_been_set and test_eq($ev, $node) and !$next_st || $next_st->[0] ne $ev
!ll&&!rl&&r
1829245
not $has_been_set and test_eq($ev, $node)
!ll&&!rl&&r
1181137
not $next_st or $next_st->[0] ne $ev
l!l&&r!l&&!r
44192
MC/DC100%4/4
not $has_been_set and test_eq($ev, $node) and !$next_st || $next_st->[0] ne $ev
not $has_been_set test_eq($ev, $node) not $next_st $next_st->[0] ne $ev
Truth table100%5/5
not(A) and B and not(C) or D
A= not $has_been_setB= test_eq($ev, $node)C= not $next_stD= $next_st->[0] ne $ev
ABCDresult
0XXX0
10XX0
11000
111X1
11011
1088 test_eq($ev, $node) &&
1089 (!$next_st ||
1090 $next_st->[0] ne $ev)) {
1091 push(@nu_subtree,
1092 45 map { [$ev=>$_] } @v);
1093 45 $has_been_set = 1;
1094 }
1095 }
1096 115 ▶ if (!$has_been_set) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $has_been_set) { } 70 45 115
1097 70 addkid($tree, [$node=>$_]) foreach @v;
1098 }
1099 else {
1100 45 @$subtree = @nu_subtree;
1101 }
1102 115 return;
1103 }
1104 20 *a = \&add;
1105 20 *addSubTreeVal = \&add;
1106  
1107 0 ▶sub addnode {
Subroutine addnode: not called CC=2 SCAR=17.9
Pod undocumented
1108 0 my $tree = shift;
1109 0 my $elt = shift;
1110 0 my $node = shift;
1111 0 my $nodename = $node->name;
1112 0 ▶ if ($nodename ne $elt) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nodename ne $elt) 0 0 0
1113 0 confess("$nodename ne $elt");
1114 }
1115 0 add($tree, $elt, $node->data);
1116 }
1117 20 *an = \&addnode;
1118 20 *addn = \&addnode;
1119  
1120 4 ▶sub unset {
Subroutine unset: called CC=10 SCAR=24.1
Pod undocumented
1121 4 ▶ my $tree = shift || confess;
Condition50%1/2
shift() || confess()
l!l
40
MC/DC0%0/1
shift() || confess()
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
1122 4 my ($node, @path) = splitpath(shift);
1123 4 ▶ confess("problem: $tree not arr") unless ref($tree) && ref($tree) eq "ARRAY" || isastag($tree);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $tree and ref $tree eq "ARRAY" or isastag($tree) 0 4 4
Condition33%2/6
ref $tree and ref $tree eq "ARRAY" or isastag($tree)
l!l&&r!l&&!r
040
ref $tree and ref $tree eq "ARRAY"
!ll&&!rl&&r
040
MC/DC0%0/3
ref $tree and ref $tree eq "ARRAY" or isastag($tree)
ref $tree ref $tree eq "ARRAY" isastag($tree)
Truth table20%1/5
A and B or C
A= ref $treeB= ref $tree eq "ARRAY"C= isastag($tree)
ABCresult
11X1
0X11
1011
0X00
1000
1124  
1125 4 ▶ if (@path) {
Branch: 1 branch
BranchTrueFalseTotal
if (@path) 1 3 4
1126 1 $_->unset(\@path) foreach findnode($tree, $node);
1127 1 return;
1128 }
1129  
1130 3 my ($ev, $subtree) = @$tree;
1131 3 ▶ return unless ref $subtree;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $subtree 0 3 3
1132 3 my @nu_tree = ();
1133 3 foreach my $st (@$subtree) {
1134 9 my ($ev, $subtree) = @$st;
1135 9 ▶ if ($ev ne $node) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ev ne $node) 6 3 9
1136 6 push(@nu_tree, $st);
1137 }
1138 }
1139 3 $tree->[1] = \@nu_tree;
1140 3 return;
1141 }
1142 20 *unsetSubTreeVal = \&unset;
1143 20 *u = \&unset;
1144  
1145  
1146 # WANTARRAY
1147 176 ▶sub find {
Subroutine find: called CC=13 SCAR=27.8
Pod undocumented
1148 176 ▶ my $tree = shift || confess;
Condition50%1/2
shift() || confess()
l!l
1760
MC/DC0%0/1
shift() || confess()
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
1149 176 my ($node, @path) = splitpath(shift);
1150 176 my $replace = shift;
1151  
1152 176 ▶ confess("problem: $tree not arr") unless (ref($tree) && ref($tree) eq "ARRAY") || isastag($tree);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $tree and ref $tree eq "ARRAY" or isastag($tree) 0 176 176
Condition66%4/6
ref $tree and ref $tree eq "ARRAY" or isastag($tree)
l!l&&r!l&&!r
51710
ref $tree and ref $tree eq "ARRAY"
!ll&&!rl&&r
01715
MC/DC0%0/3
ref $tree and ref $tree eq "ARRAY" or isastag($tree)
ref $tree ref $tree eq "ARRAY" isastag($tree)
Truth table40%2/5
A and B or C
A= ref $treeB= ref $tree eq "ARRAY"C= isastag($tree)
ABCresult
11X1
0X11
1011
0X00
1000
1153  
1154 176 my @r = ();
1155 176 ▶ if (@path) {
Branch: 1 branch
BranchTrueFalseTotal
if (@path) { } 2 174 176
1156 2 @r = map { $_->find(\@path, $replace) } findnode($tree, $node)
1157 }
1158 else {
1159 174 my ($ev, $subtree) = @$tree;
1160 174 ▶ if (test_eq($ev, $node)) {
Branch: 1 branch
BranchTrueFalseTotal
if (test_eq($ev, $node)) 23 151 174
1161 23 my $is_nt = ref($subtree);
1162 23 ▶ if (defined $replace) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $replace) 0 23 23
1163 0 ▶ if ($is_nt) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_nt) { } 0 0 0
1164 0 ▶ confess("use findval") unless ref($replace);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $replace 0 0 0
1165 0 @$tree = @$replace;
1166 }
1167 else {
1168 0 $tree->[1] = $replace;
1169 }
1170 }
1171 23 ▶ return $is_nt ? Nodify($tree) : $subtree;
Branch: 1 branch
BranchTrueFalseTotal
$is_nt ? : 12 11 23
1172 # @r = ($is_nt ? Nodify($tree) : $subtree);
1173 }
1174 151 ▶ return unless ref($subtree);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $subtree 87 64 151
1175 64 push(@r, map { find($_, $node, $replace) } @$subtree);
1176 }
1177 66 ▶ if (wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) 62 4 66
1178 62 return @r;
1179 }
1180 4 $r[0];
1181 }
1182 20 *f = \&find;
1183  
1184 135 ▶sub findval {
Subroutine findval: called CC=12 SCAR=28.3
Pod undocumented
1185 135 ▶ my $tree = shift || confess;
Condition50%1/2
shift() || confess()
l!l
1350
MC/DC0%0/1
shift() || confess()
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
1186 135 my ($node, @path) = splitpath(shift);
1187 135 my $replace = shift;
1188  
1189 135 ▶ confess("problem: $tree not arr") unless ref($tree) && ref($tree) eq "ARRAY" || isastag($tree);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $tree and ref $tree eq "ARRAY" or isastag($tree) 0 135 135
Condition66%4/6
ref $tree and ref $tree eq "ARRAY" or isastag($tree)
l!l&&r!l&&!r
31320
ref $tree and ref $tree eq "ARRAY"
!ll&&!rl&&r
01323
MC/DC0%0/3
ref $tree and ref $tree eq "ARRAY" or isastag($tree)
ref $tree ref $tree eq "ARRAY" isastag($tree)
Truth table40%2/5
A and B or C
A= ref $treeB= ref $tree eq "ARRAY"C= isastag($tree)
ABCresult
11X1
0X11
1011
0X00
1000
1190  
1191 135 my @r = ();
1192 135 ▶ if (@path) {
Branch: 1 branch
BranchTrueFalseTotal
if (@path) { } 0 135 135
1193 0 @r = map { $_->findval(\@path, $replace) } findnode($tree, $node)
1194 }
1195 else {
1196 135 my ($ev, $subtree) = @$tree;
1197 135 ▶ if (test_eq($ev, $node)) {
Branch: 1 branch
BranchTrueFalseTotal
if (test_eq($ev, $node)) 12 123 135
1198 12 my $dataref = \$tree->[1];
1199 12 ▶ if (ref($subtree)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $subtree) 0 12 12
1200 # check if it is the data node of
1201 # an element with attributes
1202 0 my @kids = grep {$_->[0] eq '.'} @$subtree;
1203 0 ▶ if (@kids == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@kids == 1) 0 0 0
1204 0 $dataref = \$kids[0]->[1];
1205 }
1206 }
1207 12 ▶ if (defined $replace) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $replace) 0 12 12
1208 0 $$dataref = $replace;
1209 }
1210 12 return $$dataref;
1211 }
1212 123 ▶ return unless ref($subtree);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $subtree 87 36 123
1213 36 @r = map { findval($_, $node, $replace) } @$subtree;
1214 }
1215 36 ▶ if (wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) 32 4 36
1216 32 return @r;
1217 }
1218 4 $r[0];
1219 }
1220 20 *fv = \&findval;
1221 20 *finddata = \&findval;
1222 20 *fd = \&findval;
1223 20 *findSubTreeVal = \&findval;
1224  
1225 # WANTARRAY
1226 2 ▶sub getdata {
Subroutine getdata: called CC=11 SCAR=26.6
Pod undocumented
1227 2 ▶ my $tree = shift || confess;
Condition50%1/2
shift() || confess()
l!l
20
MC/DC0%0/1
shift() || confess()
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
1228 2 my ($node, @path) = splitpath(shift);
1229 2 my $replace = shift;
1230  
1231 2 ▶ confess("problem: $tree not arr") unless ref($tree) && ref($tree) eq "ARRAY" || isastag($tree);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $tree and ref $tree eq "ARRAY" or isastag($tree) 0 2 2
Condition33%2/6
ref $tree and ref $tree eq "ARRAY" or isastag($tree)
l!l&&r!l&&!r
020
ref $tree and ref $tree eq "ARRAY"
!ll&&!rl&&r
020
MC/DC0%0/3
ref $tree and ref $tree eq "ARRAY" or isastag($tree)
ref $tree ref $tree eq "ARRAY" isastag($tree)
Truth table20%1/5
A and B or C
A= ref $treeB= ref $tree eq "ARRAY"C= isastag($tree)
ABCresult
11X1
0X11
1011
0X00
1000
1232  
1233 2 my @v = ();
1234 2 ▶ if (@path) {
Branch: 1 branch
BranchTrueFalseTotal
if (@path) { } 1 1 2
1235 1 @v = map { $_->getdata(\@path, $replace) } getnode($tree, $node)
1236 }
1237 else {
1238  
1239 1 my ($top_ev, $children) = @$tree;
1240 1 @v = ();
1241 1 foreach my $child (@$children) {
1242 3 ▶ confess unless ref $child;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $child 0 3 3
1243 3 my ($ev, $subtree) = @$child;
1244 3 ▶ if (test_eq($ev, $node)) {
Branch: 1 branch
BranchTrueFalseTotal
if (test_eq($ev, $node)) 3 0 3
1245 3 ▶ if (defined $replace) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $replace) 0 3 3
1246 0 $child->[1] = $replace;
1247 }
1248 3 push(@v, $subtree);
1249 }
1250 }
1251 }
1252 2 ▶ if (wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) 2 0 2
1253 2 return @v;
1254 }
1255 0 $v[0];
1256 }
1257 20 *gd = \&getdata;
1258  
1259 825 ▶sub get {
Subroutine get: called CC=14 SCAR=26.8
Pod undocumented
1260 825 ▶ my $tree = shift || confess;
Condition50%1/2
shift() || confess()
l!l
8250
MC/DC0%0/1
shift() || confess()
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
1261 825 my ($node, @path) = splitpath(shift);
1262 825 my $replace = shift;
1263 # confess("problem: $tree not arr") unless ref($tree) && (ref($tree) eq "ARRAY" || isastag($tree));
1264 825 ▶ if (!ref($tree->[1])) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $tree->[1]) 0 825 825
1265 # terminal node - always returns undef
1266 0 return;
1267 }
1268 825 my @v = ();
1269 825 ▶ if (@path) {
Branch: 1 branch
BranchTrueFalseTotal
if (@path) { } 21 804 825
1270 21 @v = map { $_->get(\@path, $replace) } getnode($tree, $node)
1271 }
1272 else {
1273  
1274 804 my ($top_ev, $children) = @$tree;
1275 804 @v = ();
1276 804 ▶ if (!ref($children)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $children) 0 804 804
1277 0 confess("problem with $node/$top_ev => $children; cannot call get on terminal node");
1278 }
1279 804 foreach my $child (@$children) {
1280 1782 ▶ confess unless ref $child;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $child 0 1782 1782
1281 1782 my ($ev, $subtree) = @$child;
1282 1782 ▶ if (test_eq($ev, $node)) {
Branch: 1 branch
BranchTrueFalseTotal
if (test_eq($ev, $node)) 813 969 1782
1283 813 my $is_nt = 0;
1284 813 ▶ if (ref($subtree)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $subtree) 14 799 813
1285 14 $is_nt = 1;
1286 }
1287 813 ▶ if (defined $replace) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $replace) 6 807 813
1288 # $tree->[1] = $replace;
1289 6 ▶ if ($is_nt) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_nt) { } 3 3 6
1290 3 ▶ if (!ref($replace)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $replace) 0 3 3
1291 0 confess("use getdata instead");
1292 }
1293 3 @$child = @$replace;
1294 }
1295 else {
1296 3 $child->[1] = $replace;
1297 }
1298 }
1299 813 ▶ if ($is_nt) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_nt) { } 14 799 813
1300 14 push(@v, Nodify($child));
1301 }
1302 else {
1303 799 push(@v, $subtree);
1304 }
1305 }
1306 }
1307 }
1308 825 ▶ if (wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) 98 727 825
1309 98 return @v;
1310 }
1311 727 $v[0];
1312 }
1313 20 *g = \&get;
1314  
1315 # WANTARRAY
1316 40 ▶sub getnode {
Subroutine getnode: called CC=11 SCAR=26.6
Pod undocumented
1317 40 ▶ my $tree = shift || confess;
Condition50%1/2
shift() || confess()
l!l
400
MC/DC0%0/1
shift() || confess()
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
1318 40 my ($node, @path) = splitpath(shift);
1319 40 my $replace = shift;
1320  
1321 40 ▶ confess("problem: $tree not arr") unless ref($tree) && ref($tree) eq "ARRAY" || isastag($tree);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $tree and ref $tree eq "ARRAY" or isastag($tree) 0 40 40
Condition33%2/6
ref $tree and ref $tree eq "ARRAY" or isastag($tree)
l!l&&r!l&&!r
0400
ref $tree and ref $tree eq "ARRAY"
!ll&&!rl&&r
0400
MC/DC0%0/3
ref $tree and ref $tree eq "ARRAY" or isastag($tree)
ref $tree ref $tree eq "ARRAY" isastag($tree)
Truth table20%1/5
A and B or C
A= ref $treeB= ref $tree eq "ARRAY"C= isastag($tree)
ABCresult
11X1
0X11
1011
0X00
1000
1322  
1323 40 my @v = ();
1324 40 ▶ if (@path) {
Branch: 1 branch
BranchTrueFalseTotal
if (@path) { } 3 37 40
1325 3 @v = map { $_->getnode(\@path, $replace) } getnode($tree, $node)
1326 }
1327 else {
1328  
1329 37 my ($top_ev, $children) = @$tree;
1330 37 ▶ if (!ref($children)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $children) 0 37 37
1331 0 confess("problem: $top_ev => \"$children\" not a tree");
1332 }
1333 37 foreach my $child (@$children) {
1334 128 my ($ev, $subtree) = @$child;
1335 128 ▶ if (test_eq($ev, $node)) {
Branch: 1 branch
BranchTrueFalseTotal
if (test_eq($ev, $node)) 61 67 128
1336 61 ▶ if (defined $replace) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $replace) 0 61 61
1337 0 $tree->[1] = $replace;
1338 }
1339 61 push(@v, Nodify($child));
1340 }
1341 }
1342 }
1343  
1344 40 ▶ if (wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) 40 0 40
1345 40 return @v;
1346 }
1347 0 $v[0];
1348 }
1349 20 *getn = \&getnode;
1350 20 *gn = \&getnode;
1351 20 *gettree = \&getnode;
1352  
1353 6 ▶sub sgetnode {
Subroutine sgetnode: called CC=1 SCAR=0.0
Pod undocumented
1354 6 my $tree = shift;
1355 6 my @v = getnode($tree, @_);
1356 6 return $v[0];
1357 }
1358 20 *sgetn = \&sgetnode;
1359 20 *sgn = \&sgetnode;
1360 20 *sgettree = \&sgetnode;
1361  
1362  
1363 0 ▶sub getl {
Subroutine getl: not called CC=8 SCAR=42.8
Pod undocumented
1364 0 ▶ my $tree = shift || confess;
Condition0%0/2
shift() || confess()
l!l
00
MC/DC0%0/1
shift() || confess()
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
1365 0 my @elts = @_;
1366 0 my %elth = map{$_=>1} @elts;
1367 0 my %valh = ();
1368 0 ▶ confess("problem: $tree not arr") unless ref($tree) && ref($tree) eq "ARRAY" || isastag($tree);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $tree and ref $tree eq "ARRAY" or isastag($tree) 0 0 0
Condition0%0/6
ref $tree and ref $tree eq "ARRAY" or isastag($tree)
l!l&&r!l&&!r
000
ref $tree and ref $tree eq "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/3
ref $tree and ref $tree eq "ARRAY" or isastag($tree)
ref $tree ref $tree eq "ARRAY" isastag($tree)
Truth table0%0/5
A and B or C
A= ref $treeB= ref $tree eq "ARRAY"C= isastag($tree)
ABCresult
11X1
0X11
1011
0X00
1000
1369 0 my ($top_ev, $children) = @$tree;
1370 0 my @v = ();
1371 0 foreach my $child (@$children) {
1372 0 my ($ev, $subtree) = @$child;
1373 0 my $is_nt = ref($subtree);
1374 0 ▶ if ($elth{$ev}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($elth{$ev}) 0 0 0
1375 # warn if dupl?
1376 0 ▶ $valh{$ev} = $is_nt ? $child : $subtree;
Branch: 1 branch
BranchTrueFalseTotal
$is_nt ? : 0 0 0
1377 }
1378 }
1379 0 return map {$valh{$_}} @elts;
1380 }
1381 20 *getlist = \&getl;
1382 20 *gl = \&getl;
1383  
1384 5 ▶sub sget {
Subroutine sget: called CC=1 SCAR=0.0
Pod undocumented
1385 5 my $tree = shift;
1386 5 my @v = get($tree, @_);
1387 # warn if multivalued?
1388 5 return $v[0];
1389 }
1390 20 *sg = \&sget;
1391  
1392 0 ▶sub sgetdata {
Subroutine sgetdata: not called CC=1 SCAR=6.9
Pod undocumented
1393 0 my $tree = shift;
1394 0 my @v = getdata($tree, @_);
1395 # warn if multivalued?
1396 0 return $v[0];
1397 }
1398 20 *sgd = \&sgetdata;
1399  
1400 0 ▶sub mapv {
Subroutine mapv: not called CC=3 SCAR=24.8
Pod undocumented
1401 0 my $tree = shift;
1402 0 my %maph = @_;
1403 0 foreach my $oldkey (keys %maph) {
1404 0 my $newkey = $maph{$oldkey};
1405 0 my @currv = get($tree, $newkey);
1406 0 ▶ next if @currv;
Branch: 1 branch
BranchTrueFalseTotal
if @currv 0 0 0
1407 0 my @v = get($tree, $oldkey);
1408 0 set($tree, $newkey, @v);
1409 0 unset($tree, $oldkey);
1410 }
1411 0 return;
1412 }
1413  
1414 0 ▶sub sgetmap {
Subroutine sgetmap: not called CC=4 SCAR=30.0
Pod undocumented
1415 0 my $tree = shift;
1416 0 my %maph = @_;
1417 0 my %vh = ();
1418 0 foreach my $oldkey (keys %maph) {
1419 # if 0 is supplied, use oldkey
1420 0 ▶ my $newkey = $maph{$oldkey} || $oldkey;
Condition0%0/3
$maph{$oldkey} || $oldkey
l!l&&r!l&&!r
000
MC/DC0%0/2
$maph{$oldkey} || $oldkey
$maph{$oldkey} $oldkey
Truth table0%0/3
A || B
A= $maph{$oldkey}B= $oldkey
ABresult
1X1
011
000
1421 0 my @v = get($tree, $oldkey);
1422 0 ▶ if (@v > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@v > 1) 0 0 0
1423 0 $tree->throw("multivalued key $oldkey");
1424 }
1425 0 $vh{$newkey} = $v[0];
1426 }
1427 0 return %vh;
1428 }
1429 20 *sgm = \&sgetmap;
1430  
1431 0 ▶sub sfindval {
Subroutine sfindval: not called CC=1 SCAR=6.9
Pod undocumented
1432 0 my $tree = shift;
1433 0 my @v = findval($tree, @_);
1434 # warn if multivalued?
1435 0 return $v[0];
1436 }
1437 20 *sfv = \&sfindval;
1438 20 *singlevalFindSubTreeVal = \&sfindval;
1439  
1440 # private
1441 4 ▶sub indexOn {
Subroutine indexOn: called CC=4 SCAR=13.9
Pod undocumented
1442 4 my $tree = shift;
1443 4 my $key = shift;
1444  
1445 4 my %h = ();
1446 4 my ($evParent, $stParent) = @$tree;
1447 4 foreach my $subtree (@$stParent) {
1448 10 my @vl = get($subtree, $key);
1449 10 foreach my $v (@vl) {
1450 10 ▶ if (!$h{$v}) { $h{$v} = [] }
Branch: 1 branch
BranchTrueFalseTotal
unless ($h{$v}) 10 0 10
1451 10 push(@{$h{$v}}, $subtree);
1452 }
1453 }
1454 4 return \%h;
1455 }
1456  
1457 # does a relational style join
1458 2 ▶sub ijoin {
Subroutine ijoin: called CC=2 SCAR=6.9
Pod undocumented
1459 2 my $tree = shift;
1460 2 my $element = shift; # name of element to join
1461 2 my $key = shift; # name of join element
1462 2 my $searchstruct = shift; # structure
1463 2 my @elts = $tree->fst($element);
1464 paste($_, $key, $searchstruct)
1465 2 foreach @elts;
1466
1467 2 return;
1468 }
1469 20 *ij = \&ijoin;
1470 20 *j = \&ijoin;
1471 20 *nj = \&ijoin;
1472 20 *njoin = \&ijoin;
1473  
1474 4 ▶sub paste {
Subroutine paste: called CC=6 SCAR=17.9
Pod undocumented
1475 4 my $tree = shift;
1476 4 my $key = shift;
1477 4 my $searchstruct = shift;
1478 # use indexing?
1479 4 my ($key1, $key2) = ($key, $key);
1480 4 ▶ if ($key =~ /(.*)=(.*)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key =~ /(.*)=(.*)/) 2 2 4
1481 2 ($key1, $key2) = ($1, $2);
1482 }
1483 4 my $ssidx = indexOn($searchstruct, $key2);
1484  
1485 4 my ($evParent, $stParent) = @$tree;
1486 4 my @children = ();
1487 4 foreach my $subtree (@$stParent) {
1488 14 my @nu = ($subtree);
1489 14 my ($ev, $st) = @$subtree;
1490 14 ▶ if ($ev eq $key1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ev eq $key1) 6 8 14
1491 6 ▶ $tree->throw("can't join on $ev - $st is not primitive")
Branch: 1 branch
BranchTrueFalseTotal
if ref $st 0 6 6
1492 if ref $st;
1493 6 ▶ my $replace = $ssidx->{$st} || [];
Condition100%2/2
$ssidx->{$st} || []
l!l
42
MC/DC100%1/1
$ssidx->{$st} || []
$ssidx->{$st}
Truth table100%2/2
A
A= $ssidx->{$st}
Aresult
11
00
1494 6 @nu = @$replace;
1495 }
1496 14 push(@children, @nu);
1497 }
1498 4 @$tree = ($evParent, \@children);
1499 4 return;
1500 }
1501  
1502 #
1503 0 ▶sub maptree {
Subroutine maptree: not called CC=6 SCAR=37.4
Pod undocumented
1504 0 my $tree = shift;
1505 0 my $code = shift;
1506 0 my $parent = shift;
1507 0 my $next = $code->($tree, $parent);
1508 0 ▶ if ($next) {
Branch: 1 branch
BranchTrueFalseTotal
if ($next) { } 0 0 0
1509 0 ▶ if (ref($next) && ref($next) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $next and ref $next eq "ARRAY") 0 0 0
Condition0%0/3
ref $next and ref $next eq "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/2
ref $next and ref $next eq "ARRAY"
ref $next ref $next eq "ARRAY"
Truth table0%0/3
A and B
A= ref $nextB= ref $next eq "ARRAY"
ABresult
0X0
100
111
1510 0 return @$next;
1511 }
1512 0 my $elt = $next->element;
1513 0 my @subnodes = subnodes($next);
1514 0 ▶ if (!@subnodes) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@subnodes) 0 0 0
1515 0 return $next;
1516 }
1517 0 my @new_subnodes = ();
1518 0 foreach (@subnodes) {
1519 0 my @nu = maptree($_, $code, $tree);
1520 0 push(@new_subnodes,@nu);
1521 }
1522 0 return Data::Stag->new($elt=>[@new_subnodes]);
1523 }
1524 else {
1525 0 return ();
1526 }
1527 }
1528  
1529 # iterate depth first through tree executing code
1530 396 ▶sub iterate {
Subroutine iterate: called CC=2 SCAR=6.9
Pod undocumented
1531 396 my $tree = shift;
1532 396 my $code = shift;
1533 396 my $parent = shift;
1534 396 $code->($tree, $parent);
1535 396 my @subnodes = subnodes($tree);
1536 396 foreach (@subnodes) {
1537 379 iterate($_, $code, $tree);
1538 }
1539 396 return;
1540 }
1541 20 *i = \&iterate;
1542  
1543 # takes a denormalized flat table of rows/columns
1544 # and turns it back into its original tree structure;
1545 # useful for querying databases
1546 # DEPRECATED
1547 3 ▶sub normalize {
Subroutine normalize: called CC=21 SCAR=30.8
Pod undocumented
1548 3 ▶ my $tree = shift || [];
Condition50%1/2
shift() || []
l!l
30
MC/DC0%0/1
shift() || []
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
1549 3 my ($schema,
1550 $rows,
1551 $top,
1552 $cols,
1553 $constraints,
1554 $path) =
1555 rearrange([qw(schema
1556 rows
1557 top
1558 cols
1559 constraints
1560 path)], @_);
1561 3 ▶ if (!$schema) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($schema) 2 1 3
1562 2 $schema = $tree->new(schema=>[]);
1563 }
1564 3 ▶ if (!isastag($schema)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (isastag($schema)) 1 2 3
1565 1 ▶ if (!ref($schema)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $schema) 1 0 1
1566 # it's a string - parse it
1567 # (assume sxpr)
1568 }
1569 1 $schema = $tree->from('sxprstr', $schema);
1570 }
1571 # TOP - this is the element name
1572 # to group the structs under.
1573 # [override if specified explicitly]
1574 3 ▶ if ($top) {
Branch: 1 branch
BranchTrueFalseTotal
if ($top) 2 1 3
1575 2 $schema->set_top($top);
1576 }
1577 3 ▶ $top = $schema->get_top || "set";
Condition50%1/2
$schema->get_top || 'set'
l!l
30
MC/DC0%0/1
$schema->get_top || 'set'
$schema->get_top
Truth table50%1/2
A
A= $schema->get_top
Aresult
11
00
1578 3 my $topstruct = $tree->new($top, []);
1579  
1580 # COLS - this is the columns (attribute names)
1581 # in the order they appear
1582 # [override if specified explicitly]
1583 3 ▶ if ($cols) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cols) 2 1 3
1584 my @ncols =
1585 map {
1586 2 ▶ if (ref($_)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_) { } 0 11 11
1587 0 $_
1588 }
1589 else {
1590 # presume it's a string
1591 # format = GROUP.ATTRIBUTENAME
1592 11 ▶ if (/(\w+)\.(\w+)/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/(\w+)\.(\w+)/) { } 11 0 11
1593 11 $tree->new(col=>[
1594 [group=>$1],
1595 [name=>$2]]);
1596 }
1597 else {
1598 0 confess $_;
1599 }
1600 }
1601 } @$cols;
1602 2 $schema->set_cols([@ncols]);
1603 }
1604  
1605  
1606 # PATH - this is the tree structure in
1607 # which the groups are structured
1608 # [override if specified explicitly]
1609 3 ▶ if ($path) {
Branch: 1 branch
BranchTrueFalseTotal
if ($path) 2 1 3
1610 2 ▶ if (ref($path)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $path) { } 0 2 2
1611 }
1612 else {
1613 2 $path = $tree->from('sxprstr', $path);
1614 }
1615 2 $schema->set_path([$path]);
1616 }
1617 3 $path = $schema->sgetnode_path;
1618 3 ▶ if (!$path) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($path) 0 3 3
1619 0 confess("no path!");
1620 }
1621
1622 # column headings
1623 3 my @cols = $schema->sgetnode_cols->getnode_col();
1624  
1625 # set the primary key for each group;
1626 # the default is all the columns in that group
1627 3 my %pkey_by_groupname = ();
1628 3 my %cols_by_groupname = ();
1629 3 foreach my $col (@cols) {
1630 19 my $groupname = $col->get_group;
1631 19 my $colname = $col->get_name;
1632 $pkey_by_groupname{$groupname} = []
1633 19 ▶ unless $pkey_by_groupname{$groupname};
Branch: 1 branch
BranchTrueFalseTotal
unless $pkey_by_groupname{$groupname} 10 9 19
1634 19 push(@{$pkey_by_groupname{$groupname}},
1635 $colname);
1636 $cols_by_groupname{$groupname} = []
1637 19 ▶ unless $cols_by_groupname{$groupname};
Branch: 1 branch
BranchTrueFalseTotal
unless $cols_by_groupname{$groupname} 10 9 19
1638 19 push(@{$cols_by_groupname{$groupname}},
1639 $colname);
1640 }
1641 3 my @groupnames = keys %pkey_by_groupname;
1642  
1643 # override PK if set as a constraint
1644 3 my @pks = $schema->findnode("primarykey");
1645 3 foreach my $pk (@pks) {
1646 2 my $groupname = $pk->get_group;
1647 2 my @cols = $pk->get_col;
1648 2 $pkey_by_groupname{$groupname} = [@cols];
1649 }
1650  
1651 # ------------------
1652 #
1653 # loop through denormalised rows,
1654 # grouping the columns into their
1655 # respecive groups
1656 #
1657 # eg
1658 #
1659 # <----- a -----> <-- b -->
1660 # a.1 a.2 a.3 b.1 b.2
1661 #
1662 # algorithm:
1663 # use path/tree to walk through
1664 #
1665 # ------------------
1666  
1667 # keep a hash of all groups by their primary key vals
1668 # outer key = groupname
1669 # inner key = pkval
1670 # hash val = group structure
1671 3 my %all_group_hh = ();
1672 3 foreach my $groupname (@groupnames) {
1673 10 $all_group_hh{$groupname} = {};
1674 }
1675  
1676 # keep an array of all groups
1677 # outer key = groupname
1678 # inner array = ordered list of groups
1679 # my %all_group_ah = ();
1680 # foreach my $groupname (keys %pkey_by_groupname) {
1681 # $all_group_ah{$groupname} = [];
1682 # }
1683  
1684 3 my ($first_in_path) = $path->subnodes;
1685 3 my $top_record_h =
1686 {
1687 child_h=>{
1688 $first_in_path->name=>{}
1689 },
1690 struct=>$topstruct
1691 };
1692 # loop through rows
1693 3 foreach my $row (@$rows) {
1694 47 my @colvals = @$row;
1695  
1696 # keep a record of all groups in
1697 # this row
1698 47 my %current_group_h = ();
1699 47 ▶ for (my $i=0; $i<@cols; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @cols) 321 47 368
1700 321 my $colval = $colvals[$i];
1701 321 my $col = $cols[$i];
1702 321 my $groupname = $col->get_group;
1703 321 my $colname = $col->get_name;
1704 321 my $group = $current_group_h{$groupname};
1705 321 ▶ if (!$group) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($group) 173 148 321
1706 173 $group = {};
1707 173 $current_group_h{$groupname} = $group;
1708 }
1709 321 $group->{$colname} = $colval;
1710 }
1711  
1712 # we now have a hash of hashes -
1713 # outer keyed by group id
1714 # inner keyed by group attribute name
1715
1716 # traverse depth first down path;
1717 # add new nodes as children of the parent
1718 155 ▶ sub make_a_tree {
Subroutine make_a_tree: called CC=9 SCAR=22.3
Pod undocumented
1719 155 my $class = shift;
1720 155 my $parent_rec_h = shift;
1721 155 my $node = shift;
1722 155 ▶ my %current_group_h= %{shift ||{}};
Condition50%1/2
shift() || {}
l!l
1550
MC/DC0%0/1
shift() || {}
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
1723 155 ▶ my %pkey_by_groupname = %{shift ||{}};
Condition50%1/2
shift() || {}
l!l
1550
MC/DC0%0/1
shift() || {}
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
1724 155 ▶ my %cols_by_groupname = %{shift ||{}};
Condition50%1/2
shift() || {}
l!l
1550
MC/DC0%0/1
shift() || {}
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
1725 155 my $groupname = $node->name;
1726 155 my $grouprec = $current_group_h{$groupname};
1727 155 my $pkcols = $pkey_by_groupname{$groupname};
1728 my $pkval =
1729 CORE::join("\t",
1730 map {
1731 155 ▶ esctab($grouprec->{$_} || '')
Condition50%1/2
$grouprec->{$_} || ''
l!l
2670
MC/DC0%0/1
$grouprec->{$_} || ''
$grouprec->{$_}
Truth table50%1/2
A
A= $grouprec->{$_}
Aresult
11
00
1732 } @$pkcols);
1733 155 my $rec = $parent_rec_h->{child_h}->{$groupname}->{$pkval};
1734 155 ▶ if (!$rec) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($rec) 110 45 155
1735 110 my $groupcols = $cols_by_groupname{$groupname};
1736 my $groupstruct =
1737 $class->new($groupname=>[
1738 map {
1739 110 [$_ => $grouprec->{$_}]
1740 } @$groupcols
1741 ]);
1742 110 my $parent_groupstruct = $parent_rec_h->{struct};
1743 110 ▶ if (!$parent_groupstruct) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($parent_groupstruct) 0 110 110
1744 0 confess("no parent for $groupname");
1745 }
1746 110 add($parent_groupstruct,
1747 $groupstruct->name,
1748 $groupstruct->data);
1749 110 $rec =
1750 {struct=>$groupstruct,
1751 child_h=>{}};
1752 110 foreach ($node->subnodes) {
1753 # keep index of children by PK
1754 64 $rec->{child_h}->{$_->name} = {};
1755 }
1756 110 $parent_rec_h->{child_h}->{$groupname}->{$pkval} = $rec;
1757 }
1758 155 foreach ($node->subnodes) {
1759 108 make_a_tree($class,
1760 $rec, $_, \%current_group_h,
1761 \%pkey_by_groupname, \%cols_by_groupname);
1762 }
1763 }
1764 47 make_a_tree($tree,
1765 $top_record_h, $first_in_path, \%current_group_h,
1766 \%pkey_by_groupname, \%cols_by_groupname);
1767 }
1768 3 return $topstruct;
1769 }
1770 20 *norm = \&normalize;
1771 20 *normalise = \&normalize;
1772  
1773  
1774 267 ▶sub esctab {
Subroutine esctab: called CC=1 SCAR=0.0
Pod undocumented
1775 267 my $w=shift;
1776 267 $w =~ s/\t/__MAGICTAB__/g;
1777 267 $w;
1778 }
1779  
1780 0 ▶sub findvallist {
Subroutine findvallist: not called CC=3 SCAR=24.8
Pod undocumented
1781 0 ▶ my $tree = shift || confess;
Condition0%0/2
shift() || confess()
l!l
00
MC/DC0%0/1
shift() || confess()
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
1782 0 my @nodes = @_;
1783 my @vals =
1784 map {
1785 0 my @v = findval($tree, $_);
1786 0 ▶ if (@v > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@v > 1) 0 0 0
1787 0 confess(">1 val for $_: @v");
1788 }
1789 0 $v[0];
1790 } @nodes;
1791 0 return @vals;
1792 }
1793 20 *findSubTreeValList = \&findvallist;
1794 20 *fvl = \&findvallist;
1795  
1796 0 ▶sub findChildVal {
Subroutine findChildVal: not called CC=6 SCAR=37.4
Pod undocumented
1797 0 confess("deprecated - use get");
1798 0 my $tree = shift;
1799 0 my $node = shift;
1800 0 my $replace = shift;
1801 0 ▶ confess unless ref($tree);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $tree 0 0 0
1802 0 my ($ev, $subtree) = @$tree;
1803 0 ▶ return $subtree if test_eq($ev, $node);
Branch: 1 branch
BranchTrueFalseTotal
if test_eq($ev, $node) 0 0 0
1804 0 ▶ return unless ref($subtree);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $subtree 0 0 0
1805 0 my @children = grep { $_->[0] eq $node } @$subtree;
1806 # print "@children\n";
1807 0 ▶ if (defined $replace) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $replace) 0 0 0
1808 return
1809 0 map { $_->[1] = $replace } @children;
1810 }
1811 0 my @r = map { $_->[1] } @children;
1812 0 ▶ if (wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) 0 0 0
1813 0 return @r;
1814 }
1815 0 $r[0];
1816 }
1817  
1818 13 ▶sub qmatch {
Subroutine qmatch: called CC=2 SCAR=7.1
Pod undocumented
1819 13 my $tree = shift;
1820 13 my $elt = shift;
1821 13 my $matchkey = shift;
1822 13 my $matchval = shift;
1823 13 my $replace = shift;
1824  
1825 13 my @st = findnode($tree, $elt);
1826 my @match =
1827 grep {
1828 # tmatch($_, $matchkey, $matchval);
1829 13 grep {$_ eq $matchval} $_->get($matchkey);
1830 } @st;
1831 13 ▶ if ($replace) {
Branch: 1 branch
BranchTrueFalseTotal
if ($replace) 0 13 13
1832 map {
1833 0 @$_ = @$replace;
1834 } @match;
1835 }
1836 13 return @match;
1837 }
1838 20 *qm = \&qmatch;
1839 20 *findSubTreeMatch = \&qmatch;
1840  
1841  
1842 8 ▶sub tmatch {
Subroutine tmatch: called CC=1 SCAR=0.0
Pod undocumented
1843 8 my $tree = shift;
1844 8 my $elt = shift;
1845 8 my $matchval = shift;
1846 8 my @vals = findval($tree, $elt);
1847 8 return grep {$_ eq $matchval} @vals;
1848 }
1849 20 *testSubTreeMatch = \&tmatch;
1850 20 *tm = \&tmatch;
1851  
1852  
1853 0 ▶sub tmatchhash {
Subroutine tmatchhash: not called CC=3 SCAR=24.8
Pod undocumented
1854 0 my $tree = shift;
1855 0 my $match = shift;
1856 0 my @mkeys = keys %$match;
1857 0 my @mvals = map {$match->{$_}} @mkeys;
1858  
1859 0 my @rvals = findvallist($tree, @mkeys);
1860 0 my $pass = 1;
1861 0 ▶ for (my $i=0; $i<@mvals; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @mvals) 0 0 0
1862 0 ▶ $pass = 0 if $mvals[$i] ne $rvals[$i];
Branch: 1 branch
BranchTrueFalseTotal
if $mvals[$i] ne $rvals[$i] 0 0 0
1863 }
1864 # print "CHECK @mvals eq @rvals [$pass]\n";
1865 0 return $pass;
1866 }
1867 20 *tmh = \&tmatchhash;
1868 20 *testSubTreeMatchHash = \&tmatchhash;
1869  
1870 0 ▶sub tmatchnode {
Subroutine tmatchnode: not called CC=11 SCAR=48.8
Pod undocumented
1871 0 my $tree = shift;
1872 0 my $matchtree = shift;
1873 0 my ($node, $subtree) = @$tree;
1874 0 ▶ confess unless ref $matchtree;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $matchtree 0 0 0
1875 0 my ($mnode, $msubtree) = @$matchtree;
1876 0 ▶ if ($node ne $mnode) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node ne $mnode) 0 0 0
1877 0 ▶ return unless ref $subtree;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $subtree 0 0 0
1878 return
1879 grep {
1880 0 testSubTreeMatchTree($_,
1881 $matchtree)
1882 } @$subtree;
1883 }
1884 0 ▶ if (!ref($subtree) && !ref($msubtree)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $subtree or ref $msubtree) 0 0 0
Condition0%0/3
ref $subtree or ref $msubtree
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $subtree or ref $msubtree
ref $subtree ref $msubtree
Truth table0%0/3
A or B
A= ref $subtreeB= ref $msubtree
ABresult
1X1
011
000
1885 0 return $subtree eq $msubtree;
1886 }
1887 0 ▶ if (ref($subtree) && ref($msubtree)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $subtree and ref $msubtree) 0 0 0
Condition0%0/3
ref $subtree and ref $msubtree
!ll&&!rl&&r
000
MC/DC0%0/2
ref $subtree and ref $msubtree
ref $subtree ref $msubtree
Truth table0%0/3
A and B
A= ref $subtreeB= ref $msubtree
ABresult
0X0
100
111
1888 0 my @got = ();
1889 0 ▶ for (my $i=0; $i<@$msubtree; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$msubtree) 0 0 0
1890 0 my $n = $msubtree->[$i]->[0];
1891 0 my ($x) = grep {$_->[0] eq $n} @$subtree;
1892 0 ▶ return unless $x;
Branch: 1 branch
BranchTrueFalseTotal
unless $x 0 0 0
1893 0 my $ok =
1894 testSubTreeMatchTree($x,
1895 $msubtree->[$i]);
1896 0 ▶ return unless $ok;
Branch: 1 branch
BranchTrueFalseTotal
unless $ok 0 0 0
1897
1898 }
1899 0 return 1;
1900 }
1901  
1902 }
1903 20 *tmn = \&tmatchnode;
1904 20 *testSubTreeMatchTree = \&tmatchnode;
1905  
1906 0 ▶sub cmatch {
Subroutine cmatch: not called CC=1 SCAR=6.9
Pod undocumented
1907 0 my $tree = shift;
1908 0 my $node = shift;
1909 0 my $matchval = shift;
1910 0 my @vals = findval($tree, $node);
1911 0 return scalar(grep {$_ eq $matchval} @vals);
1912 }
1913 20 *cm = \&cmatch;
1914 20 *countSubTreeMatch = \&cmatch;
1915  
1916  
1917 0 ▶sub OLDwhere {
Subroutine OLDwhere: not called CC=2 SCAR=17.9
Pod undocumented
1918 0 my $tree = shift;
1919 0 my $node = shift;
1920 0 my $testcode = shift;
1921 0 my $replace = shift;
1922 0 my @subtrees = findnode($tree, $node);
1923 my @match =
1924 grep {
1925 0 $testcode->($_);
1926 } @subtrees;
1927 0 ▶ if (defined $replace) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $replace) 0 0 0
1928 map {
1929 0 @$_ = @$replace;
1930 } @match;
1931 }
1932 0 return @match;
1933 }
1934 12 ▶sub where {
Subroutine where: called CC=2 SCAR=7.0
Pod undocumented
1935 12 my $tree = shift;
1936 12 my $node = shift;
1937 12 my $testcode = shift;
1938 12 my $replace = shift;
1939  
1940 12 my @match = ();
1941 iterate($tree,
1942 282 ▶ sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
1943 282 my $stag = shift;
1944 282 ▶ if (name($stag) eq $node) {
Branch: 1 branch
BranchTrueFalseTotal
if (name($stag) eq $node) 24 258 282
1945 24 ▶ if ($testcode->($stag)) {
Branch: 1 branch
BranchTrueFalseTotal
if (&$testcode($stag)) 12 12 24
1946 12 push(@match, $stag);
1947 }
1948 }
1949 12 });
1950  
1951 12 ▶ if (defined $replace) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $replace) 0 12 12
1952 map {
1953 0 @$_ = @$replace;
1954 } @match;
1955 }
1956 12 return @match;
1957 }
1958 20 *w = \&where;
1959 20 *findSubTreeWhere = \&where;
1960  
1961  
1962 #sub findSubTreeWhere {
1963 # my $tree = shift;
1964 # my $node = shift;
1965 # my $testcode = shift;
1966 # my $replace = shift;
1967 ## use Data::Dumper;
1968 ## print Dumper($expr);
1969 # my $call = $expr->name;
1970 # my @p = $expr->findChildVal('arg');
1971 ## print Dumper(\@p);
1972 # my @subtrees = findSubTree($tree, $node);
1973 # no strict 'refs';
1974 # my @match =
1975 # grep {
1976 # &$call($_, @p);
1977 # } @subtrees;
1978 # if (defined $replace) {
1979 # map {
1980 # print "WAS:";
1981 # print xml($_);
1982 # print "NOW:";
1983 # print xml($replace);
1984 # @$_ = @$replace;
1985 # } @match;
1986 # }
1987 # return @match;
1988 #}
1989  
1990 0 ▶sub run {
Subroutine run: not called CC=5 SCAR=30.8
Pod undocumented
1991 0 my $tree = shift;
1992 0 my $code = shift;
1993 0 my $func = $code->name;
1994 0 my @p = $code->children();
1995 0 my @args = ();
1996 0 foreach my $p (@p) {
1997 0 ▶ if ($p->name eq 'arg') {
Branch: 1 branch
BranchTrueFalseTotal
if ($p->name eq 'arg') { } 0 0 0
1998 0 ▶ if (isastag($p->children)) {
Branch: 1 branch
BranchTrueFalseTotal
if (isastag($p->children)) { } 0 0 0
1999 0 die;
2000 0 push(@args,
2001 evalTree($tree,
2002 $p->children));
2003
2004 }
2005 else {
2006 0 push(@args, $p->children);
2007 }
2008 }
2009 else {
2010 # print "rcall $tree $p\n";
2011 0 push(@args,
2012 evalTree($tree,
2013 $p));
2014 }
2015 }
2016 20 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
2017 0 my @r = &$func($tree, @p);
2018 0 ▶ return @r if wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 0 0 0
2019 0 return shift @r;
2020 }
2021 20 *evalTree = \&run;
2022  
2023  
2024 # ------------------------
2025 # collapseElement($tree, $elt)
2026 #
2027 # eg if we have
2028 #
2029 # +personset
2030 # +person
2031 # +name jim
2032 # +job sparky
2033 # +person
2034 # +name jim
2035 # +hair green
2036 # +person
2037 # +name eck
2038 # +hair blue
2039 #
2040 # then execute
2041 # collapseElement($tree, 'person', 'name')
2042 #
2043 # we end up with
2044 #
2045 # +personset
2046 # +person
2047 # +name jim
2048 # +job sparky
2049 # +hair green
2050 # +person
2051 # +name eck
2052 # +hair blue
2053 #
2054 # OR if we have
2055 #
2056 # +personset
2057 # +person
2058 # +name jim
2059 # +job sparky
2060 # +petname bubbles
2061 # +pettype chimp
2062 # +person
2063 # +name jim
2064 # +job sparky
2065 # +petname flossie
2066 # +pettype sheep
2067 # +person
2068 # +name eck
2069 # +job bus driver
2070 # +petname gnasher
2071 # +pettype dug
2072 #
2073 #
2074 # then execute
2075 # collapseElement($tree, 'name', 'job')
2076 #
2077 # we end up with
2078 #
2079 # +personset
2080 # +person
2081 # +name jim
2082 # +job sparky
2083 # +pet
2084 # +petname bubbles
2085 # +pettype chimp
2086 # +pet
2087 # +petname flossie
2088 # +pettype sheep
2089 # +person
2090 # +name eck
2091 # +job bus driver
2092 # +pet
2093 # +petname gnasher
2094 # +pettype dug
2095 #
2096 #
2097 # warning: element should be unique
2098 # todo: allow person/name
2099 #
2100 # ------------------------
2101 0 ▶sub collapse {
Subroutine collapse: not called CC=4 SCAR=30.0
Pod undocumented
2102 0 my $tree = shift;
2103 0 my $elt = shift;
2104 0 my $merge_elt = shift;
2105 0 my @subtrees = findnode($tree, $elt);
2106 0 my %treeh = ();
2107 0 my @elt_order = (); # preserve ordering
2108 map {
2109 0 my @v = findval($_, $merge_elt);
2110 0 ▶ die unless scalar(@v) == 1;
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @v == 1 0 0 0
2111 0 my $val = shift @v;
2112 0 ▶ push(@elt_order, $val) unless $treeh{$val};
Branch: 1 branch
BranchTrueFalseTotal
unless $treeh{$val} 0 0 0
2113 0 ▶ $treeh{$val} = [] unless $treeh{$val};
Branch: 1 branch
BranchTrueFalseTotal
unless $treeh{$val} 0 0 0
2114 0 push(@{$treeh{$val}}, $_);
2115 } @subtrees;
2116 my @new_subtrees =
2117 map {
2118 0 my $trees = $treeh{$_};
2119 0 my ($v) = findval($trees->[0], $merge_elt);
2120 [
2121 $elt=>[
2122 [$merge_elt=>$v],
2123 map {
2124 0 my ($ev, $subtree) = @$_;
2125 grep {
2126 0 $_->[0] ne $merge_elt
2127 } @$subtree;
2128 } @$trees,
2129 ]
2130 ]
2131 } @elt_order;
2132  
2133 0 findnode($tree, $elt, []);
2134 0 push(@{$tree->[1]}, @new_subtrees);
2135 0 return $tree;
2136 }
2137 20 *collapseElement = \&collapse;
2138  
2139 0 ▶sub merge {
Subroutine merge: not called CC=6 SCAR=37.4
Pod undocumented
2140 0 my $tree = shift;
2141 0 ▶ my @elts = @{shift || []};
Condition0%0/2
shift() || []
l!l
00
MC/DC0%0/1
shift() || []
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
2142 0 my $merge_key = shift;
2143  
2144 0 ▶ return unless @elts;
Branch: 1 branch
BranchTrueFalseTotal
unless @elts 0 0 0
2145 0 my $e1 = $elts[0];
2146 0 my ($type, $v) = @$e1;
2147 0 my @cur_elts = findnode($tree, $type);
2148 0 foreach my $elt (@elts) {
2149 0 my ($v) = findval($elt, $merge_key);
2150 0 foreach my $cur_elt (@cur_elts) {
2151 0 my ($cv) = findval($cur_elt, $merge_key);
2152 0 ▶ if ($cv eq $v) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cv eq $v) 0 0 0
2153 # merge
2154 0 my $cur_children = $cur_elt->[1];
2155 0 my $children = $elt->[1];
2156 push(@$cur_children,
2157 grep {
2158 0 $_->[0] ne $merge_key
2159 } @$children);
2160 }
2161 }0
2162 0 }
2163 0 return $tree;
2164 }
2165 20 *mergeElements = \&merge;
2166  
2167 0 ▶sub makeattrsnodes {
Subroutine makeattrsnodes: not called CC=5 SCAR=34.0
Pod undocumented
2168 0 my $tree = shift;
2169 0 ▶ return if isterminal($tree);
Branch: 1 branch
BranchTrueFalseTotal
if isterminal($tree) 0 0 0
2170 0 my @attrs = get($tree,'@');
2171 0 ▶ if (@attrs) {
Branch: 1 branch
BranchTrueFalseTotal
if (@attrs) 0 0 0
2172 0 my @nu = ();
2173 0 foreach (@attrs) {
2174 0 push(@nu, kids($_));
2175 }
2176 0 unset($tree, '@');
2177 0 unshift(@{$tree->[1]},@nu);
2178 }
2179 0 my @subnodes = subnodes($tree);
2180 0 makeattrsnodes($_) foreach @subnodes;
2181 0 return;
2182 }
2183  
2184 7 ▶sub duplicate {
Subroutine duplicate: called CC=2 SCAR=7.0
Pod undocumented
2185 7 my $tree = shift;
2186 7 load_module('Data::Dumper');
2187 20 ▶ use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
2188 7 my $nu;
2189 7 my $d = Data::Dumper->new( [$tree], [qw($nu)] );
2190 7 my $dump = $d->Dump;
2191 7 eval $dump;
2192 7 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 7 7
2193 0 confess $@;
2194 }
2195 7 return stagify($nu);
2196 }
2197 20 *d = \&duplicate;
2198 20 *clone = \&duplicate;
2199  
2200 995 ▶sub isastag {
Subroutine isastag: called CC=2 SCAR=7.1
Pod undocumented
2201 995 my $node = shift;
2202 995 ▶ return UNIVERSAL::isa($node, "Data::Stag::StagI") ||
Condition66%2/3
UNIVERSAL::isa($node, 'Data::Stag::StagI') || UNIVERSAL::isa($node, 'Node')
l!l&&r!l&&!r
99401
MC/DC50%1/2
UNIVERSAL::isa($node, 'Data::Stag::StagI') || UNIVERSAL::isa($node, 'Node')
UNIVERSAL::isa($node, 'Data::Stag::StagI') UNIVERSAL::isa($node, 'Node')
Truth table66%2/3
A || B
A= UNIVERSAL::isa($node, 'Data::Stag::StagI')B= UNIVERSAL::isa($node, 'Node')
ABresult
1X1
011
000
2203 UNIVERSAL::isa($node, "Node");
2204 }
2205 20 *isanode = \&isastag;
2206 20 *isa_node = \&isanode;
2207 20 *isaNode = \&isanode;
2208  
2209 2 ▶sub isnull {
Subroutine isnull: called CC=2 SCAR=7.6
Pod undocumented
2210 2 my $node = shift;
2211 2 ▶ if (@$node) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$node) 0 2 2
2212 0 return 0;
2213 }
2214 2 return 1;
2215 }
2216  
2217 0 ▶sub node {
Subroutine node: not called CC=1 SCAR=6.9
Pod undocumented
2218 0 return Data::Stag::StagImpl->new(@_);
2219 }
2220 20 *Node = \&node;
2221  
2222 809 ▶sub stagify {
Subroutine stagify: called CC=3 SCAR=11.8
Pod undocumented
2223 809 my $tree = shift;
2224 809 ▶ if (!ref($tree)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $tree) 0 809 809
2225 # allow static or nonstatic usage
2226 0 $tree = shift;
2227 }
2228 809 ▶ confess unless ref $tree;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $tree 0 809 809
2229 809 my $class = "Data::Stag::StagImpl";
2230 809 bless $tree, $class
2231 }
2232 20 *nodify = \&stagify;
2233 20 *Nodify = \&stagify;
2234  
2235 0 ▶sub xpath {
Subroutine xpath: not called CC=1 SCAR=6.9
Pod undocumented
2236 0 my $tree = shift;
2237 0 load_module("XML::XPath");
2238 0 my $xp = XML::XPath->new(xml=>xml($tree));
2239 0 return $xp;
2240 }
2241 20 *xp = \&xpath;
2242 20 *getxpath = \&xpath;
2243 20 *tree2xpath = \&xpath;
2244  
2245  
2246 0 ▶sub xpquery {
Subroutine xpquery: not called CC=1 SCAR=6.9
Pod undocumented
2247 0 my $tree = shift;
2248 0 my @args = @_;
2249 0 load_module("XML::XPath::XMLParser");
2250 0 my $xp = $tree->getXPath;
2251 0 my $nodeset = $xp->find(@args);
2252 my @nodes =
2253 map {
2254 0 xmlstr2tree(XML::XPath::XMLParser::as_string($_));
2255 } $nodeset->get_nodelist;
2256 0 return @nodes;
2257 }
2258 20 *xpq = \&xpquery;
2259 20 *xpathquery = \&xpquery;
2260 20 *xpfind = \&xpquery;
2261 20 *xpFind = \&xpquery;
2262  
2263 0 ▶sub grammarparser {
Subroutine grammarparser: not called CC=2 SCAR=17.9
Pod undocumented
2264 0 my $tree = shift;
2265 0 my $grammar = shift;
2266 0 load_module("Parse::RecDescent");
2267 0 $::RD_AUTOACTION = q
2268 { use Data::Stag;
2269 $#item == 1 && !ref $item[1] ? $item[1] : Data::Stag->new(shift @item, [map {if(ref($_)) {$_} else {[arg=>$_]}} @item ]);
2270 };
2271 0 ▶ my $parser = Parse::RecDescent->new($grammar) or confess "Bad grammar!\n";
Branch: 1 branch
BranchTrueFalseTotal
my $parser = "Parse::RecDescent"->new($grammar) or confess("Bad grammar!\n") 0 0 0
2272 0 return $parser;
2273 }
2274  
2275 #use overload
2276 # '.' => sub {my @r=findnodeVal($_[0],$_[1]);$r[0]},
2277 # '-' => sub {my @r=findnodeVal($_[0],$_[1]);$r[0]},
2278 # '+' => sub {my @r=$_[0]->findnode($_[1]);return $r[0]},
2279 # '/' => sub {[findnodeVal($_[0],$_[1])]},
2280 # '*' => sub {[$_[0]->findnode($_[1])]},
2281 # '<' => sub {Data::Stag::testSubTreeMatchTree($_[0], $_[1])},
2282 # qw("" stringify);
2283  
2284 #sub stringify {
2285 # $_[0];
2286 #}
2287  
2288  
2289 679 ▶sub kids {
Subroutine kids: called CC=3 SCAR=11.3
Pod undocumented
2290 679 my $self = shift;
2291 679 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 679 679
2292 0 @$self = ($self->[0], [map {Nodify($_)} @_]);
2293 }
2294 679 my ($name, $kids) = @$self;
2295 679 ▶ if (!ref($kids)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $kids) 395 284 679
2296 395 return $kids;
2297 }
2298 284 return map {Nodify($_)} @$kids;
2299 }
2300 20 *k = \&kids;
2301 20 *children = \&kids;
2302 20 *getChildren = \&kids;
2303 20 *getKids = \&kids;
2304  
2305 677 ▶sub subnodes {
Subroutine subnodes: called CC=1 SCAR=0.0
Pod undocumented
2306 677 my $self = shift;
2307 677 return grep {ref($_)} kids($self);
2308 }
2309  
2310 # non-terminal nodes
2311 0 ▶sub ntnodes {
Subroutine ntnodes: not called CC=1 SCAR=6.9
Pod undocumented
2312 0 my $self = shift;
2313 0 my @subnodes = $self->subnodes;
2314 0 return grep {!$_->isterminal} @subnodes;
2315 }
2316 20 *nonterminalnodes = \&ntnodes;
2317 20 *nonterminals = \&ntnodes;
2318  
2319 # terminal nodes
2320 0 ▶sub tnodes {
Subroutine tnodes: not called CC=1 SCAR=6.9
Pod undocumented
2321 0 my $self = shift;
2322 0 my @subnodes = $self->subnodes;
2323 0 return grep {$_->isterminal} @subnodes;
2324 }
2325 20 *terminalnodes = \&tnodes;
2326 20 *terminals = \&tnodes;
2327  
2328 859 ▶sub element {
Subroutine element: called CC=2 SCAR=6.9
Pod undocumented
2329 859 my $self = shift;
2330 859 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 11 848 859
2331 11 $self->[0] = shift;
2332 }
2333 859 return $self->[0];
2334 }
2335 20 *e = \&element;
2336 20 *name = \&element;
2337 20 *tagname = \&element;
2338  
2339 169 ▶sub data {
Subroutine data: called CC=2 SCAR=6.9
Pod undocumented
2340 169 my $self = shift;
2341 169 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 20 149 169
2342 20 $self->[1] = shift;
2343 }
2344 169 return $self->[1];
2345 }
2346  
2347 0 ▶sub rename {
Subroutine rename: not called CC=3 SCAR=24.8
Pod undocumented
2348 0 my $tree = shift;
2349 0 my $from = shift;
2350 0 my $to = shift;
2351 0 foreach (kids($tree)) {
2352 0 ▶ if ($_->[0] eq $from) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_->[0] eq $from) 0 0 0
2353 0 $_->[0] = $to;
2354 }
2355 }
2356 0 return;
2357 }
2358  
2359 134 ▶sub isterminal {
Subroutine isterminal: called CC=1 SCAR=0.0
Pod undocumented
2360 134 my $self = shift;
2361 134 return !ref($self->[1]);
2362 }
2363  
2364 31 ▶sub _min {
Subroutine _min: called CC=4 SCAR=13.9
2365 31 my $x = shift;
2366 31 my $y = shift;
2367 31 ▶ return $x if !defined($y);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $y 19 12 31
2368 12 ▶ return $y if !defined($x);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $x 0 12 12
2369 12 ▶ $x < $y ? $x : $y;
Branch: 1 branch
BranchTrueFalseTotal
$x < $y ? : 2 10 12
2370 }
2371 31 ▶sub _max {
Subroutine _max: called CC=4 SCAR=13.9
2372 31 ▶ my $x = shift || 0;
Condition100%2/2
shift() || 0
l!l
292
MC/DC100%1/1
shift() || 0
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
2373 31 ▶ my $y = shift || 0;
Condition100%2/2
shift() || 0
l!l
1219
MC/DC100%1/1
shift() || 0
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
2374 31 ▶ $x > $y ? $x : $y;
Branch: 1 branch
BranchTrueFalseTotal
$x > $y ? : 19 12 31
2375 }
2376  
2377 # automatically deduces schema
2378 # FORMAT MAY CHANGE!!!!!
2379 1 ▶sub autoschema {
Subroutine autoschema: called CC=1 SCAR=0.0
Pod undocumented
2380 1 my $tree = shift;
2381 1 my $schema = _autoschema($tree);
2382 # use Data::Dumper;
2383 # print Dumper $schema;
2384 1 my $nu = genschema($tree, undef, $tree->element, $schema);
2385 20 ▶ $nu->iterate(sub{
Subroutine __ANON__: called CC=2 SCAR=6.9
2386 20 my $node = shift;
2387 20 ▶ if ($node->name =~ /(\S+)\.(\S+)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node->name =~ /(\S+)\.(\S+)/) 11 9 20
2388 11 $node->name($2); }
2389 1 });
2390 1 return $nu;
2391 }
2392  
2393 0 ▶sub dtd {
Subroutine dtd: not called CC=6 SCAR=37.4
Pod undocumented
2394 0 my $tree = shift;
2395 0 my ($name, $subtree) = @$tree;
2396 0 ▶ my $done_h = shift || {};
Condition0%0/2
shift() || {}
l!l
00
MC/DC0%0/1
shift() || {}
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
2397 0 $name =~ s/[\+\?\*]$//;
2398 0 ▶ return '' if $done_h->{$name};
Branch: 1 branch
BranchTrueFalseTotal
if $done_h->{$name} 0 0 0
2399 0 my $is_nt = ref($subtree);
2400 0 my $s;
2401 0 ▶ if ($is_nt) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_nt) { } 0 0 0
2402 0 my $s2 = join('', map {dtd($_,$done_h)} @$subtree);
2403 0 my @subnames = map {$_->[0]} @$subtree;
2404 0 my $S = join('|',@subnames);
2405 0 ▶ if (@subnames < 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (@subnames < 2) 0 0 0
2406 0 $S = "@subnames";
2407 0 ▶ if (!@subnames) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@subnames) 0 0 0
2408 0 $S = 'EMPTY';
2409 }
2410 }
2411  
2412 0 $s = "\n<!-- $name: (node) -->\n<!ELEMENT $name ($S)+>\n$s2";
2413 }
2414 else {
2415 0 $s = "\n<!-- $name: ($subtree) -->\n<!ELEMENT $name (#PCDATA)>\n";
2416 }
2417 0 $done_h->{$name} = 1;
2418 0 $s;
2419 }
2420  
2421 20 ▶sub genschema {
Subroutine genschema: called CC=9 SCAR=22.1
Pod undocumented
2422 20 my $tree = shift;
2423 20 my $parent = shift;
2424 20 my $root = shift;
2425 20 my $schema = shift;
2426 20 ▶ my $path = shift || [];
Condition100%2/2
shift() || []
l!l
191
MC/DC100%1/1
shift() || []
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
2427  
2428 20 my $cycle = 0;
2429 20 ▶ if (grep {$_ eq $root} @$path) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {$_ eq $root;} @$path) 0 20 20
2430 0 $cycle = 1;
2431 0 warn "cycle detected: @$path $root";
2432 0 return (Data::Stag->new($root=>[]));
2433 }
2434 20 push(@$path, $root);
2435  
2436 20 my $data = $schema->{data};
2437 20 my $childh = $schema->{childh};
2438  
2439 20 my $card = "";
2440 20 ▶ if ($parent) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parent) 19 1 20
2441 19 my $link = "$parent $root";
2442 19 my $min = $schema->{mincard}->{$link};
2443 19 my $max = $schema->{maxcard}->{$link};
2444 19 ▶ die if !$max;
Branch: 1 branch
BranchTrueFalseTotal
unless $max 0 19 19
2445 19 ▶ if ($min == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($min == 0) { } 2 17 19
2446 2 ▶ if ($max == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($max == 1) { } 1 1 2
2447 1 $card = '?';
2448 }
2449 else {
2450 1 $card = '*';
2451 }
2452 }
2453 else {
2454 # $min >= 1
2455 17 ▶ if ($max == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($max == 1) { } 13 4 17
2456 13 $card = '';
2457 }
2458 else {
2459 4 $card = '+';
2460 }
2461 }
2462 }
2463 20 my $ss = Data::Stag->new($root.$card=>[]);
2464 20 ▶ if ($data->{$root}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data->{$root}) { } 11 9 20
2465 11 $ss->data($data->{$root});
2466 }
2467 else {
2468 9 my $c = $childh->{$root};
2469 my @sn =
2470 map {
2471 9 genschema($tree, $root, $_, $schema, $path);
2472 } @$c;
2473 9 $ss->data([@sn]);
2474 }
2475 20 return $ss;
2476 }
2477  
2478 # automatically deduces schema
2479 13 ▶sub _autoschema {
Subroutine _autoschema: called CC=22 SCAR=31.8
2480 13 my $tree = shift;
2481 13 ▶ my $schema = shift || {data=>{},
Condition100%2/2
shift() || {'data', {}, 'childh', {}, 'mincard', {}, 'maxcard', {}}
l!l
121
MC/DC100%1/1
shift() || {'data', {}, 'childh', {}, 'mincard', {}, 'maxcard', {}}
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
2482 childh=>{},
2483 mincard=>{},
2484 maxcard=>{},
2485 };
2486 13 my $data = $schema->{data};
2487 13 my $childh = $schema->{childh};
2488 13 my $mincard = $schema->{mincard};
2489 13 my $maxcard = $schema->{maxcard};
2490  
2491 13 my $elt = $tree->element;
2492 13 my @sn = $tree->subnodes;
2493 # CARD: default(blank) : 1
2494 # + : 1 or more
2495 # * : 0 or more
2496 # ? : 0 or one
2497 13 my %lcard = (); # local cardinality
2498 13 foreach (@sn) {
2499 # nonterminal nodes are uniquely defined
2500 # by the node name
2501 36 my $se = element($_);
2502 36 ▶ if (isterminal($_)) {
Branch: 1 branch
BranchTrueFalseTotal
if (isterminal($_)) 24 12 36
2503 # a terminal node is uniquely defined
2504 # by parent.node
2505 24 $se = "$elt.$se";
2506 }
2507 19 push(@{$childh->{$elt}}, $se)
2508 unless $childh->{$elt} &&
2509 36 ▶ grep { $_ eq $se } @{$childh->{$elt}};
Branch: 1 branch
BranchTrueFalseTotal
unless $childh->{$elt} and grep {$_ eq $se;} @{$$childh{$elt};} 19 17 36
Condition100%3/3
$childh->{$elt} and grep {$_ eq $se;} @{$$childh{$elt};}
!ll&&!rl&&r
91017
MC/DC100%2/2
$childh->{$elt} and grep {$_ eq $se;} @{$$childh{$elt};}
$childh->{$elt} grep {$_ eq $se;} @{$$childh{$elt};}
Truth table100%3/3
A and B
A= $childh->{$elt}B= grep {$_ eq $se;} @{$$childh{$elt};}
ABresult
0X0
100
111
2510 36 ▶ $lcard{$se} = 0 unless $lcard{$se};
Branch: 1 branch
BranchTrueFalseTotal
unless $lcard{$se} 29 7 36
2511 36 $lcard{$se}++;
2512 }
2513 # foreach (keys %lcard) {
2514 13 foreach (@{$childh->{$elt}}) {
2515 31 my $link = "$elt $_";
2516 # print "$link :: $lcard{$_}\n";
2517 $mincard->{$link} =
2518 _min($lcard{$_} || 0,
2519 31 ▶ $mincard->{$link});
Condition100%2/2
$lcard{$_} || 0
l!l
292
MC/DC100%1/1
$lcard{$_} || 0
$lcard{$_}
Truth table100%2/2
A
A= $lcard{$_}
Aresult
11
00
2520 $maxcard->{$link} =
2521 _max($lcard{$_},
2522 31 $maxcard->{$link});
2523 }
2524 13 foreach (grep {isterminal($_)} @sn) {
2525 24 my $elt = $elt.'.'.element($_);
2526 # push(@{$data->{$elt}}, $_->data);
2527 24 my $in = $_->data;
2528 24 ▶ my $d = $data->{$elt} || 'INT';
Condition100%2/2
$data->{$elt} || 'INT'
l!l
1311
MC/DC100%1/1
$data->{$elt} || 'INT'
$data->{$elt}
Truth table100%2/2
A
A= $data->{$elt}
Aresult
11
00
2529 24 ▶ if (!$in) {
Branch: 3 branches
BranchTrueFalseTotal
if (not $in) { } 0 24 24
elsif ($in =~ /^\-?\d+$/) { } 6 18 24
elsif ($in =~ /^\-?\d*\.\d+$/) { } 0 18 18
2530 }
2531 elsif ($in =~ /^\-?\d+$/) { # LOOKS LIKE INT
2532 # CHANGE SIZE IF IT IS
2533 6 ▶ if ($d eq 'INT') {
Branch: 1 branch
BranchTrueFalseTotal
if ($d eq "INT") 6 0 6
2534 6 $d = 'INT';
2535 6 my $lin = length($in);
2536 6 ▶ if ($lin > 10) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lin > 10) 0 6 6
2537 # too big for an int
2538 # TODO: largeint?
2539 0 $d = _mkvarchar($lin);
2540 }
2541 }
2542 }
2543 elsif ($in =~ /^\-?\d*\.\d+$/) { # LOOKS LIKE FLOAT
2544 # PROMOTE TO FLOAT IF INT
2545 0 ▶ if ($d eq 'INT') {
Branch: 1 branch
BranchTrueFalseTotal
if ($d eq "INT") 0 0 0
2546 0 $d = 'FLOAT';
2547 }
2548 }
2549 else {
2550 18 ▶ my $lin = length($in) || 0;
Condition50%1/2
length $in || 0
l!l
180
MC/DC0%0/1
length $in || 0
length $in
Truth table50%1/2
A
A= length $in
Aresult
11
00
2551 18 ▶ if ($d =~ /VARCHAR\((\d+)\)/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($d =~ /VARCHAR\((\d+)\)/) { } 10 8 18
elsif ($d =~ /TEXT/) { } 0 8 8
2552 10 ▶ if ($lin > $1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lin > $1) { } 0 10 10
2553 0 $d = _mkvarchar($lin);
2554 }
2555 else {
2556 }
2557 }
2558 elsif ($d =~ /TEXT/) {
2559 }
2560 else {
2561 8 $d = _mkvarchar($lin);
2562 }
2563 }
2564 24 $data->{$elt} = $d;
2565 }
2566 13 foreach (grep {!isterminal($_)} @sn) {
2567 12 _autoschema($_, $schema);
2568 }
2569 13 return $schema;
2570 }
2571  
2572 8 ▶sub _mkvarchar {
Subroutine _mkvarchar: called CC=3 SCAR=12.0
2573 8 ▶ my $size = shift || 1;
Condition50%1/2
shift() || 1
l!l
80
MC/DC0%0/1
shift() || 1
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
2574 # round up to log2
2575 8 my $s2 = 2**(int(log($size) / log(2))+2) -1;
2576 8 ▶ if ($s2 > 255) {
Branch: 1 branch
BranchTrueFalseTotal
if ($s2 > 255) 0 8 8
2577 0 return 'TEXT';
2578 }
2579 8 return "VARCHAR($s2)";
2580 }
2581  
2582 1515 ▶sub AUTOLOAD {
Subroutine AUTOLOAD: called CC=4 SCAR=14.1
2583 1515 my $self = shift;
2584 1515 my @args = @_;
2585  
2586 1515 my $name = $AUTOLOAD;
2587 1515 $name =~ s/.*://; # strip fully-qualified portion
2588  
2589 1515 ▶ if ($name eq "DESTROY") {
Branch: 1 branch
BranchTrueFalseTotal
if ($name eq "DESTROY") 750 765 1515
2590 # we dont want to propagate this!!
2591 750 return;
2592 }
2593  
2594 765 ▶ if ($name =~ /^([a-zA-Z]+)_(\w+)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name =~ /^([a-zA-Z]+)_(\w+)/) 765 0 765
2595 765 ▶ if ($self->can($1)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->can($1)) 765 0 765
2596 765 return $self->$1($2, @args);
2597 }
2598 }
2599 0 confess("no such method:$name");
2600 }
2601  
2602 # --MISC--
2603  
2604 1762 ▶sub splitpath {
Subroutine splitpath: called CC=4 SCAR=13.9
Pod undocumented
2605 1762 my $node = shift;
2606 1762 ▶ if (ref($node) && ref($node) eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $node and ref $node eq 'ARRAY') { } 36 1726 1762
elsif ($node =~ m[/]) { } 21 1705 1726
Condition66%2/3
ref $node and ref $node eq "ARRAY"
!ll&&!rl&&r
1726036
MC/DC50%1/2
ref $node and ref $node eq "ARRAY"
ref $node ref $node eq "ARRAY"
Truth table66%2/3
A and B
A= ref $nodeB= ref $node eq "ARRAY"
ABresult
0X0
100
111
2607 36 @$node;
2608 }
2609 elsif ($node =~ /\//) {
2610 21 (split(/\//, $node));
2611 }
2612 else {
2613 1705 ($node);
2614 }
2615 }
2616  
2617 3132 ▶sub test_eq {
Subroutine test_eq: called CC=4 SCAR=14.6
Pod undocumented
2618 3132 my ($ev, $node) = @_;
2619 3132 ▶ $ev = '' unless defined $ev;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ev 0 3132 3132
2620 3132 ▶ $node = '' unless defined $node;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $node 0 3132 3132
2621 3132 ▶ return $ev eq $node || $node eq '*';
Condition66%2/3
$ev eq $node || $node eq '*'
l!l&&r!l&&!r
109502037
MC/DC50%1/2
$ev eq $node || $node eq '*'
$ev eq $node $node eq '*'
Truth table66%2/3
A || B
A= $ev eq $nodeB= $node eq '*'
ABresult
1X1
011
000
2622 }
2623  
2624 20 1;
2625  
« blib/lib/Data/Stag/PodParser.pm ↑ Summary blib/lib/Data/Stag/SxprParser.pm »