WE/Util/GenericTree.pm

filter: Statementstmt 43.1% 63 / 146 Branchbran 28.5% 20 / 70 Conditioncond 46.6% 7 / 15 MC/DCmcdc 11.1% 1 / 9 Subroutinesub 47.6% 10 / 21 Podpod 41.1% 7 / 17 totaltotal 38.8% 108 / 278 CC 46 SCAR 30.2
CC
46
Cov
39%
insert_tree
90.0
is_descendent
20.0
find_by_pathname
20.0
CRAP 20.5
« WE_Multisite/Root.pm ↑ Summary WE/Util/HWRights.pm »
1 # -*- perl -*-
2  
3 #
4 # $Id: GenericTree.pm,v 1.4 2004/02/02 08:11:59 eserte Exp $
5 # Author: Slaven Rezic
6 #
7 # Copyright (c) 1995-2000 Slaven Rezic. All rights reserved.
8 # Copyright (C) 2000,2002 Online Office Berlin. All rights reserved.
9 # Copyright (c) 2002,2004 Slaven Rezic. All rights reserved.
10 # This program is free software; you can redistribute it and/or
11 # modify it under the same terms as Perl itself.
12 #
13 # Mail: slaven@rezic.de
14 # WWW: http://we-framework.sourceforge.net
15 #
16 # This is derived from Timex::Project.
17 #
18  
19 package WE::Util::GenericTree;
20  
21 =head1 NAME
22  
23 WE::Util::GenericTree - generic class for tree representations
24  
25 =head1 SYNOPSIS
26  
27 $tree = new WE::Util::GenericTree $data
28  
29 =head1 DESCRIPTION
30  
31 =cut
32  
33 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
34 2 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
35 2 $VERSION = sprintf("%d.%02d", q$Revision: 1.4 $ =~ /(\d+)\.(\d+)/);
36  
37 2 ▶use fields qw(Data Id Subtrees Parent Modified Separator);
Subroutine BEGIN: called CC=1 SCAR=0.0
38  
39 =head2 new
40  
41 $tree = new WE::Util::GenericTree $data
42  
43 Construct a new GenericTree object with content $data.
44  
45 =cut
46  
47 92 ▶sub new {
Subroutine new: called CC=3 SCAR=11.4
Pod documented
48 92 my WE::Util::GenericTree $self;
49  
50 92 ▶ if ($] < 5.006) {
Branch: 1 branch
BranchTrueFalseTotal
if ($] < 5.006) { } 0 92 92
51 0 my $class = shift;
52 2 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
53 0 $self = bless [\%{"$class\::FIELDS"}], $class;
54 } else {
55 92 $self = shift;
56 92 ▶ $self = fields::new($self) unless ref $self;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self 92 0 92
57 }
58  
59 92 my $data = shift;
60 92 $self->data($data);
61  
62 92 $self->{Subtrees} = [];
63 92 $self->{Parent} = undef;
64 92 $self->{Modified} = 0;
65 92 $self->{Separator} = "/";
66  
67 92 $self;
68 }
69  
70 0 ▶sub maybe_construct {
Subroutine maybe_construct: not called CC=2 SCAR=17.9
Pod undocumented
71 0 my $self = shift;
72 0 my $obj = shift;
73 0 ▶ if (UNIVERSAL::isa($obj, __PACKAGE__)) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::isa($obj, __PACKAGE__)) { } 0 0 0
74 0 $obj;
75 } else {
76 0 $self->new($obj);
77 }
78 }
79  
80 624 ▶sub data {
Subroutine data: called CC=8 SCAR=22.1
Pod undocumented
81 624 my $self = shift;
82  
83 624 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 92 532 624
84 92 my $data = shift;
85  
86 92 $self->{Data} = $data;
87 92 ▶ if (ref $data && UNIVERSAL::can($data,"id")) {
Branch: 3 branches
BranchTrueFalseTotal
if (ref $data and UNIVERSAL::can($data, 'id')) { } 0 92 92
elsif (ref $data and UNIVERSAL::isa($data, 'HASH') and exists $data->{'Id'}) { } 12 80 92
elsif (defined $data) { } 80 0 80
Condition66%2/3
ref $data and UNIVERSAL::can($data, "id")
!ll&&!rl&&r
80120
MC/DC0%0/2
ref $data and UNIVERSAL::can($data, "id")
ref $data UNIVERSAL::can($data, "id")
Truth table66%2/3
A and B
A= ref $dataB= UNIVERSAL::can($data, "id")
ABresult
0X0
100
111
88 0 $self->{Id} = $data->id;
89 ▶ } elsif (ref $data && UNIVERSAL::isa($data, "HASH") && exists $data->{Id}) {
Condition66%4/6
ref $data and UNIVERSAL::isa($data, "HASH") and exists $data->{"Id"}
!ll&&!rl&&r
80012
ref $data and UNIVERSAL::isa($data, "HASH")
!ll&&!rl&&r
80012
MC/DC33%1/3
ref $data and UNIVERSAL::isa($data, "HASH") and exists $data->{"Id"}
ref $data UNIVERSAL::isa($data, "HASH") exists $data->{"Id"}
Truth table50%2/4
A and B and C
A= ref $dataB= UNIVERSAL::isa($data, "HASH")C= exists $data->{"Id"}
ABCresult
0XX0
10X0
1100
1111
90 12 $self->{Id} = $data->{Id};
91 } elsif (defined $data) {
92 80 $self->{Id} = $data;
93 } else {
94 0 die "No id found";
95 }
96 }
97  
98 624 $self->{Data};
99 }
100  
101 0 ▶sub id { $_[0]->{Id} }
Subroutine id: not called CC=1 SCAR=6.9
Pod undocumented
102  
103 # XXX setting of parent is not used for external use ...
104 # use subtree (for the other direction) instead
105 663 ▶sub parent {
Subroutine parent: called CC=2 SCAR=6.9
Pod undocumented
106 663 my($self, $parent) = @_;
107 663 ▶ if (defined $parent) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $parent) { } 80 583 663
108 80 $self->{Parent} = $parent;
109 80 $self->modified(1);
110 } else {
111 583 $self->{Parent};
112 }
113 }
114  
115 =head2 reparent
116  
117 $tree->reparent($newparent)
118  
119 Use this method only if there is already a parent. Otherwise, use the
120 parent method.
121  
122 =cut
123  
124 0 ▶sub reparent {
Subroutine reparent: not called CC=3 SCAR=24.8
Pod documented
125 0 my($self, $newparent) = @_;
126 0 my $oldparent = $self->parent;
127 # don't become a child of a descended tree :-)
128 0 ▶ return if $self->is_descendent($newparent);
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_descendent($newparent) 0 0 0
129 0 ▶ return if !$oldparent; # don't reparent root
Branch: 1 branch
BranchTrueFalseTotal
unless $oldparent 0 0 0
130 0 $oldparent->delete_subtree($self);
131 0 $newparent->subtree($self);
132 }
133  
134 =head2 root
135  
136 $root = $tree->root;
137  
138 Return root node of the given $tree.
139  
140 =cut
141  
142 344 ▶sub root {
Subroutine root: called CC=2 SCAR=6.9
Pod documented
143 344 my $self = shift;
144 344 ▶ if ($self->parent) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->parent) { } 184 160 344
145 184 $self->parent->root;
146 } else {
147 160 $self;
148 }
149 }
150  
151 =head2 modified
152  
153 $modfied = $tree->modified
154  
155 Return true if the tree is modified, that is, one of root's #'
156 subtrees are modified.
157  
158 $tree->modified($modified)
159  
160 Set the modified attribute (0 or 1) for the root tree.
161  
162 =cut
163  
164 160 ▶sub modified {
Subroutine modified: called CC=3 SCAR=12.0
Pod documented
165 160 my($self, $flag) = @_;
166 160 my $root = $self->root;
167 160 ▶ if (defined $flag) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $flag) { } 160 0 160
168 160 ▶ $root->{Modified} = ($flag ? 1 : 0);
Branch: 1 branch
BranchTrueFalseTotal
$flag ? : 160 0 160
169 } else {
170 0 $root->{Modified};
171 }
172 }
173  
174 =head2 subtree
175  
176 $root->subtree([$tree1, ...]);
177  
178 With a $tree defined, put the trees as subtrees of $root. Without
179 $tree, return either an array of subtrees (in array context) or a
180 reference to the array of subtrees (in scalar context).
181  
182 The argument can be either GenericTree objects or another scalars, in
183 which case they will be used as the data argument to the constructor
184 of GenericTree.
185  
186 Alias: children.
187  
188 =cut
189  
190 543 ▶sub subtree {
Subroutine subtree: called CC=7 SCAR=19.8
Pod documented
191 543 my $self = shift;
192 543 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 80 463 543
193 80 my @res;
194 80 my $class = ref $self;
195 80 foreach my $subtree (@_) {
196 80 my WE::Util::GenericTree $sub;
197 80 ▶ if (ref $subtree && UNIVERSAL::isa($subtree, __PACKAGE__)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $subtree and UNIVERSAL::isa($subtree, __PACKAGE__)) { } 0 80 80
Condition33%1/3
ref $subtree and UNIVERSAL::isa($subtree, __PACKAGE__)
!ll&&!rl&&r
8000
MC/DC0%0/2
ref $subtree and UNIVERSAL::isa($subtree, __PACKAGE__)
ref $subtree UNIVERSAL::isa($subtree, __PACKAGE__)
Truth table33%1/3
A and B
A= ref $subtreeB= UNIVERSAL::isa($subtree, __PACKAGE__)
ABresult
0X0
100
111
198 0 $sub = $subtree;
199 } else {
200 80 $sub = $class->new($subtree);
201 }
202 80 $sub->parent($self);
203 80 push @{ $self->{Subtrees} }, $sub;
204 80 $self->modified(1);
205 80 push @res, $sub;
206 }
207 80 ▶ wantarray ? @res : $res[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 80 80
208 } else {
209 463 ▶ wantarray ? @{ $self->{Subtrees} } : $self->{Subtrees};
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 264 199 463
210 }
211 }
212  
213 2 *children = \&subtree;
214  
215 0 ▶sub is_descendent {
Subroutine is_descendent: not called CC=4 SCAR=30.0
Pod undocumented
216 0 my($self, $tree) = @_;
217 0 ▶ return 1 if $self eq $tree;
Branch: 1 branch
BranchTrueFalseTotal
if $self eq $tree 0 0 0
218 0 foreach ($self->subtree) {
219 0 my $r = $_->is_descendent($tree);
220 0 ▶ return 1 if $r;
Branch: 1 branch
BranchTrueFalseTotal
if $r 0 0 0
221 }
222 0 0;
223 }
224  
225 0 ▶sub delete_subtree {
Subroutine delete_subtree: not called CC=3 SCAR=24.8
Pod undocumented
226 0 my($self, $subp) = @_;
227 0 my @subtrees = $self->subtree;
228 0 my @newsubtrees;
229 0 foreach (@subtrees) {
230 0 ▶ push @newsubtrees, $_ unless $_ eq $subp;
Branch: 1 branch
BranchTrueFalseTotal
unless $_ eq $subp 0 0 0
231 }
232 0 $self->{Subtrees} = \@newsubtrees;
233 0 $self->modified(1);
234 }
235  
236 =head2 find_by_pathname
237  
238 $tree = $root->find_by_pathname($pathname);
239  
240 Search and return the corresponding $tree (or undef if no such
241 tree exists) for the given $pathname.
242  
243 =cut
244  
245 0 ▶sub find_by_pathname {
Subroutine find_by_pathname: not called CC=4 SCAR=30.0
Pod documented
246 0 my($self, $pathname) = @_;
247 0 ▶ return $self if $self->pathname eq $pathname;
Branch: 1 branch
BranchTrueFalseTotal
if $self->pathname eq $pathname 0 0 0
248 0 foreach ($self->subtree) {
249 0 my $r = $_->find_by_pathname($pathname);
250 0 ▶ return $r if defined $r;
Branch: 1 branch
BranchTrueFalseTotal
if defined $r 0 0 0
251 }
252 0 return undef;
253 }
254  
255 0 ▶sub pathname { # virtual pathname!
Subroutine pathname: not called CC=4 SCAR=30.0
Pod undocumented
256 0 my($self, $separator) = @_;
257 0 ▶ $separator = $self->separator if !defined $separator;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $separator 0 0 0
258 0 my @path = $self->path;
259 0 ▶ if (!defined $path[0] || $path[0] eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $path[0] or $path[0] eq "") 0 0 0
Condition0%0/3
not defined $path[0] or $path[0] eq ""
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $path[0] or $path[0] eq ""
not defined $path[0] $path[0] eq ""
Truth table0%0/3
not(A) or B
A= not defined $path[0]B= $path[0] eq ""
ABresult
1X1
011
000
260 0 shift @path;
261 }
262 0 join($separator, @path);
263 }
264  
265 0 ▶sub path {
Subroutine path: not called CC=3 SCAR=24.8
Pod undocumented
266 0 my($self) = @_;
267 0 my @path;
268 0 ▶ if (!defined $self->parent) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $self->parent) { } 0 0 0
269 0 @path = ($self->id);
270 } else {
271 0 @path = ($self->parent->path, $self->id);
272 }
273 0 ▶ wantarray ? @path : \@path;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
274 }
275  
276 =head2 separator
277  
278 $separator = $tree->separator
279  
280 Return the separator for this tree. Defaults to /.
281  
282 $project->separator($separator);
283  
284 Set the separator for this tree to $separator.
285  
286 =cut
287  
288 0 ▶sub separator {
Subroutine separator: not called CC=2 SCAR=17.9
Pod documented
289 0 my($self, $separator) = @_;
290 0 my $root = $self->root;
291 0 ▶ if (defined $separator) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $separator) { } 0 0 0
292 0 $root->{Separator} = $separator;
293 } else {
294 0 $root->{Separator};
295 }
296 }
297  
298 0 ▶sub level {
Subroutine level: not called CC=2 SCAR=17.9
Pod undocumented
299 0 my $self = shift;
300 0 ▶ if (!defined $self->{Parent}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $self->{'Parent'}) { } 0 0 0
301 0 0;
302 } else {
303 0 $self->{Parent}->level + 1;
304 }
305 }
306  
307 0 ▶sub insert_tree {
Subroutine insert_tree: not called CC=9 SCAR=45.0
Pod undocumented
308 0 my($self, $obj, $type, $pathname) = @_;
309 0 my $tree = $self->find_by_pathname($pathname);
310 0 ▶ if (defined $tree) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $tree) { } 0 0 0
311 0 ▶ if ($type eq '-below') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq '-below') { } 0 0 0
312 0 $tree->subtree($obj);
313 } else {
314 0 my $parent = $tree->parent;
315 0 ▶ die "Can't find parent for $tree" unless $parent;
Branch: 1 branch
BranchTrueFalseTotal
unless $parent 0 0 0
316  
317 0 my $i = 0;
318 SEARCH: {
319 0 foreach my $sub (@{ $parent->{Subtrees} }) {
320 0 ▶ if ($sub eq $tree) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sub eq $tree) 0 0 0
321 0 last SEARCH;
322 }
323 0 $i++;
324 }
325 0 die "Fatal: $tree not found in $parent";
326 }
327  
328 0 my $new_obj = $self->maybe_construct($obj);
329  
330 0 ▶ if ($type eq '-at') {
Branch: 3 branches
BranchTrueFalseTotal
if ($type eq '-at') { } 0 0 0
elsif ($type eq '-after') { } 0 0 0
elsif ($type eq '-before') { } 0 0 0
331 0 $parent->{Subtrees}[$i] = $new_obj;
332 } elsif ($type eq '-after') {
333 0 splice @{ $parent->{Subtrees} }, $i, 0, $new_obj;
334 } elsif ($type eq '-before') {
335 0 splice @{ $parent->{Subtrees} }, $i-1, 0, $new_obj;
336 } else {
337 0 die "Invalid type $type";
338 }
339 }
340 } else {
341 0 die "Can't find pathname $pathname";
342 }
343 }
344  
345 2 1;
346  
347 __END__
« WE_Multisite/Root.pm ↑ Summary WE/Util/HWRights.pm »