1 |
#
|
2 |
# TreeMonkey - Climb around the tree as directed by a search string
|
3 |
#
|
4 |
# Interface
|
5 |
# -----------
|
6 |
# new() : A new monkey is born
|
7 |
# find(string) : Tell the monkey to search the tree for the node at the
|
8 |
# location indicated by the string, relative to the current
|
9 |
# tree node.
|
10 |
# goto(TreeNode) : Tell the monkey to go to a particular node in the tree
|
11 |
|
12 |
package ActiveDoc::TreeMonkey;
|
13 |
require 5.001;
|
14 |
use Utilities::ListIterator;
|
15 |
|
16 |
sub new {
|
17 |
my $class=shift;
|
18 |
$self={};
|
19 |
bless $self, $class;
|
20 |
return $self;
|
21 |
}
|
22 |
|
23 |
sub goto {
|
24 |
my $self=shift;
|
25 |
my $node=shift;
|
26 |
|
27 |
$self->{currentnode}=$node;
|
28 |
}
|
29 |
|
30 |
sub find {
|
31 |
my $self=shift;
|
32 |
my $string=shift;
|
33 |
my @words;
|
34 |
my $first;
|
35 |
my $found=0;
|
36 |
my $branch="";
|
37 |
|
38 |
# Split string
|
39 |
($first, @words)=split '/', $string;
|
40 |
|
41 |
# Search towards root for first word or until we reach root
|
42 |
while ( ( $self->{currentnode}->parent() ne '/' ) &&
|
43 |
( $self->{currentnode}->name() ne $first ) ) {
|
44 |
$self->{currentnode}=$self->{currentnode}->parent();
|
45 |
}
|
46 |
|
47 |
# Check the case of just a single word
|
48 |
if (( $#words < 0 )) {
|
49 |
if (( $self->{currentnode}->name() eq $first )) {
|
50 |
$found=1;
|
51 |
}
|
52 |
}
|
53 |
else { #if we have some more words...
|
54 |
$f=1;
|
55 |
$n=0;
|
56 |
while ( ($f!=0) && ( $n < $#words ) ) {
|
57 |
my $branchlist=$self->{currentnode}->nodeList();
|
58 |
$f=$self->_checklist($words[$n],$branchlist);
|
59 |
$n++;
|
60 |
print $n." ".$f."\n";
|
61 |
}
|
62 |
$found=$f;
|
63 |
}
|
64 |
|
65 |
# Now Search the child branches for the pattern
|
66 |
# Now return the object ID of the found node or null
|
67 |
if ( $found==1 ) {
|
68 |
return $self->{currentnode};
|
69 |
}
|
70 |
else {
|
71 |
return "";
|
72 |
}
|
73 |
}
|
74 |
|
75 |
# ------------------------ Support Routines ------------------------------
|
76 |
|
77 |
sub _checklist {
|
78 |
my $self=shift;
|
79 |
my $word=shift;
|
80 |
my $branchlist=shift;
|
81 |
my $bit;
|
82 |
my $branch;
|
83 |
my $found=0;
|
84 |
|
85 |
$bit=ListIterator->new($branchlist);
|
86 |
|
87 |
while ( ($found==0) && ( ! $bit->last() ) ) {
|
88 |
$branch=$bit->next();
|
89 |
if ( $branch->name() eq $word) {
|
90 |
$self->{currentnode}=$branch;
|
91 |
$found=1;
|
92 |
}
|
93 |
} # end while
|
94 |
return $found;
|
95 |
}
|
96 |
|
97 |
|