2 package Tree::Simple::Visitor::LoadDirectoryTree;
10 use Scalar::Util qw(blessed);
12 use base qw(Tree::Simple::Visitor);
16 my $class = ref($_class) || $_class;
18 bless($visitor, $class);
25 $self->{sort_function} = undef;
26 $self->SUPER::_init();
29 # pre-built sort functions
30 sub SORT_FILES_FIRST {
32 my ($path, $left, $right) = @_;
33 $left = File::Spec->catdir($path, $left);
34 $right = File::Spec->catdir($path, $right);
35 return ((-d $left && -f $right) ? 1 : # file beats directory
36 (-d $right && -f $left) ? -1 : # file beats directory
37 (lc($left) cmp lc($right))) # otherwise just sort 'em
43 my ($path, $left, $right) = @_;
44 $left = File::Spec->catdir($path, $left);
45 $right = File::Spec->catdir($path, $right);
46 return ((-d $left && -f $right) ? -1 : # directory beats file
47 (-d $right && -f $left) ? 1 : # directory beats file
48 (lc($left) cmp lc($right))) # otherwise just sort 'em
53 my ($self, $sort_function) = @_;
54 (defined($sort_function) && ref($sort_function) eq "CODE")
55 || die "Insufficient Arguments : sort function argument must be a subroutine reference";
56 $self->{sort_function} = $sort_function;
60 my ($self, $tree) = @_;
61 (blessed($tree) && $tree->isa("Tree::Simple"))
62 || die "Insufficient Arguments : You must supply a valid Tree::Simple object";
64 ($tree->isLeaf()) || die "Illegal Operation : The tree must be a leaf node to load a directory";
65 # check that our directory is valid
66 my $root_dir = $tree->getNodeValue();
67 (-e $root_dir && -d $root_dir)
68 || die "Incorrect Type : The tree's node value must be a valid directory";
69 # and load it recursively
70 $self->_recursiveLoad($tree, $root_dir);
74 my ($self, $t, $path) = @_;
75 # get a node filter if we have one
76 my $filter = $self->getNodeFilter();
78 # get the contents of the directory
79 opendir(DIR, $path) || die "IO Error : Could not open directory : $!";
80 # avoid the . and .. symbolic links
81 my @dir_contents = grep {
82 $_ ne File::Spec->curdir() && $_ ne File::Spec->updir()
86 # sort them if we need to with full paths
87 @dir_contents = sort {
88 $self->{sort_function}->($path, $a, $b)
89 } @dir_contents if $self->{sort_function};
92 foreach my $item (@dir_contents) {
93 # filter based on the item name
94 $filter->($item) || next if defined($filter);
95 # get the full path for checking
96 # the item type and recursion
97 my $full_path = File::Spec->catdir($path, $item);
99 my $new_tree = $t->new($item);
100 $t->addChild($new_tree);
101 $self->_recursiveLoad($new_tree, $full_path);
103 elsif (-f $full_path) {
104 $t->addChild($t->new($item));
115 Tree::Simple::Visitor::LoadDirectoryTree - A Visitor for loading the contents of a directory into a Tree::Simple object
119 use Tree::Simple::Visitor::LoadDirectoryTree;
121 # create a Tree::Simple object whose
122 # node is path to a directory
123 my $tree = Tree::Simple->new("./");
125 # create an instance of our visitor
126 my $visitor = Tree::Simple::Visitor::LoadDirectoryTree->new();
128 # set the directory sorting style
129 $visitor->setSortStyle($visitor->SORT_FILES_FIRST);
131 # create node filter to filter
132 # out certain files and directories
133 $visitor->setNodeFilter(sub {
135 return 0 if $item =~ /CVS/;
139 # pass the visitor to a Tree::Simple object
140 $tree->accept($visitor);
142 # the tree now mirrors the structure of the directory
146 This visitor can be used to load a directory tree into a Tree::Simple hierarchy.
154 There are no arguments to the constructor the object will be in its default state. You can use the C<setNodeFilter> and C<setSortStyle> methods to customize its behavior.
156 =item B<setNodeFilter ($filter_function)>
158 This method accepts a CODE reference as its C<$filter_function> argument and throws an exception if it is not a code reference. This code reference is used to filter the tree nodes as they are created. The function is given the current directory or file being added to the tree, and it is expected to return either true (C<1>) of false (C<0>) to determine if that directory should be traversed or file added to the tree.
160 =item B<setSortStyle ($sort_function)>
162 This method accepts a CODE reference as its C<$sort_function> argument and throws an exception if it is not a code reference. This function is used to sort the individual levels of the directory tree right before it is added to the tree being built. The function is passed the the current path, followed by the two items being sorted. The reason for passing the path in is so that sorting operations can be performed on the entire path if desired.
164 Two pre-built functions are supplied and described below.
168 =item B<SORT_FILES_FIRST>
170 This sorting function will sort files before directories, so that files are sorted alphabetically first in the list followed by directories sorted alphabetically. Here is example of how that would look:
180 =item B<SORT_DIRS_FIRST>
182 This sorting function will sort directories before files, so that directories are sorted alphabetically first in the list followed by files sorted alphabetically. Here is example of how that would look:
194 =item B<visit ($tree)>
196 This is the method that is used by Tree::Simple's C<accept> method. It can also be used on its own, it requires the C<$tree> argument to be a Tree::Simple object (or derived from a Tree::Simple object), and will throw and exception otherwise.
198 The node value of the C<$tree> argument (gotten by calling C<getNodeValue>) is considered the root directory from which we begin our traversal. We use File::Spec to keep our paths cross-platform, but it is expected that you will feed in a valid path for your OS. If the path either does not exist, or is not a directory, then an exception is thrown.
200 The C<$tree> argument which is passed to C<visit> must be a leaf node. This is because this Visitor will create all the sub-nodes for this tree. If the tree is not a leaf, an exception is thrown. We do not require the tree to be a root though, and this Visitor will not affect any nodes above the C<$tree> argument.
206 None that I am aware of. Of course, if you find a bug, let me know, and I will be sure to fix it.
210 See the B<CODE COVERAGE> section in L<Tree::Simple::VisitorFactory> for more inforamtion.
214 These Visitor classes are all subclasses of B<Tree::Simple::Visitor>, which can be found in the B<Tree::Simple> module, you should refer to that module for more information.
218 stevan little, E<lt>stevan@iinteractive.comE<gt>
220 =head1 COPYRIGHT AND LICENSE
222 Copyright 2004, 2005 by Infinity Interactive, Inc.
224 L<http://www.iinteractive.com>
226 This library is free software; you can redistribute it and/or modify
227 it under the same terms as Perl itself.