1 2package Tree::Simple::Visitor::ToNestedArray; 3 4use strict; 5use warnings; 6 7our $VERSION = '0.02'; 8 9use Scalar::Util qw(blessed); 10 11use base qw(Tree::Simple::Visitor); 12 13sub new { 14 my ($_class) = @_; 15 my $class = ref($_class) || $_class; 16 my $visitor = {}; 17 bless($visitor, $class); 18 $visitor->_init(); 19 return $visitor; 20} 21 22sub visit { 23 my ($self, $tree) = @_; 24 (blessed($tree) && $tree->isa("Tree::Simple")) 25 || die "Insufficient Arguments : You must supply a valid Tree::Simple object"; 26 # grab our filter (if we have one) 27 my $filter = $self->getNodeFilter(); 28 my @results; 29 # get the array 30 $self->_buildArray($tree, \@results, $filter); 31 # add the trunk if we need to 32 @results = ( 33 ((defined($filter)) ? 34 $filter->($tree) 35 : 36 $tree->getNodeValue()), 37 [ @results ] 38 ) if $self->includeTrunk(); 39 # set results 40 $self->setResults(\@results); 41} 42 43sub _buildArray { 44 my ($self, $tree, $accumulator, $filter) = @_; 45 foreach my $child ($tree->getAllChildren()) { 46 push @{$accumulator} => (defined($filter) ? $filter->($child) : $child->getNodeValue()); 47 push @{$accumulator} => $self->_buildArray($child, [], $filter) unless $child->isLeaf(); 48 } 49 return $accumulator; 50} 51 521; 53 54__END__ 55 56=head1 NAME 57 58Tree::Simple::Visitor::ToNestedArray - A Visitor for creating nested array trees from Tree::Simple objects. 59 60=head1 SYNOPSIS 61 62 use Tree::Simple::Visitor::ToNestedArray; 63 64 my $visitor = Tree::Simple::Visitor::ToNestedArray->new(); 65 66 # given this Tree::Simple tree 67 my $tree = Tree::Simple->new("Root") 68 ->addChildren( 69 Tree::Simple->new("Child1") 70 ->addChildren( 71 Tree::Simple->new("GrandChild1"), 72 Tree::Simple->new("GrandChild2") 73 ), 74 Tree::Simple->new("Child2"), 75 ); 76 77 # include the trunk (Root) 78 $visitor->includeTrunk(1); 79 80 # visit the tree 81 $tree->accept($visitor); 82 83 my $array_tree = $visitor->getResults(); 84 # this then creates the equivalent nested array tree: 85 # [ 86 # 'Root', [ 87 # 'Child1', [ 88 # 'GrandChild1', 89 # 'GrandChild2' 90 # ], 91 # 'Child2' 92 # ] 93 # ] 94 95 # if you don't include the trunk (Root) then ... 96 $tree->accept($visitor); 97 98 my $array_tree = $visitor->getResults(); 99 # this then creates the following nested array tree: 100 # [ 101 # 'Child1', [ 102 # 'GrandChild1', 103 # 'GrandChild2' 104 # ], 105 # 'Child2' 106 # ] 107 108=head1 DESCRIPTION 109 110Given a tree constructed from a Tree::Simple heirarchy, this Visitor will create the equivalent tree of nested arrays. 111 112=head1 METHODS 113 114=over 4 115 116=item B<new> 117 118There are no arguments to the constructor the object will be in its default state. You can use the C<setNodeFilter> and C<includTrunk> methods to customize its behavior. 119 120=item B<includTrunk ($boolean)> 121 122Setting the C<$boolean> value to true (C<1>) will cause the node value of the tree's root to be included in the nested array output, setting it to false will do the opposite. 123 124=item B<setNodeFilter ($filter_function)> 125 126This 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 placed into the array tree. The C<$filter_function> is passed a Tree::Simple object, and is expected to return the value desired for inclusion into the array tree. 127 128=item B<visit ($tree)> 129 130This 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. 131 132=item B<getResults> 133 134This method will return the array tree constructed. 135 136=back 137 138=head1 BUGS 139 140None that I am aware of. Of course, if you find a bug, let me know, and I will be sure to fix it. 141 142=head1 CODE COVERAGE 143 144See the B<CODE COVERAGE> section in L<Tree::Simple::VisitorFactory> for more inforamtion. 145 146=head1 SEE ALSO 147 148These 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. 149 150=head1 AUTHOR 151 152stevan little, E<lt>stevan@iinteractive.comE<gt> 153 154=head1 COPYRIGHT AND LICENSE 155 156Copyright 2004, 2005 by Infinity Interactive, Inc. 157 158L<http://www.iinteractive.com> 159 160This library is free software; you can redistribute it and/or modify 161it under the same terms as Perl itself. 162 163=cut 164