Add built local::lib
[catagits/Gitalist.git] / local-lib5 / lib / perl5 / Tree / Simple / Visitor / FromNestedArray.pm
1
2 package Tree::Simple::Visitor::FromNestedArray;
3
4 use strict;
5 use warnings;
6
7 our $VERSION = '0.02';
8
9 use Scalar::Util qw(blessed);
10
11 use base qw(Tree::Simple::Visitor);
12
13 sub 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
22 sub _init {
23     my ($self) = @_;
24     $self->{array_tree} = undef;
25     $self->SUPER::_init();
26 }
27
28 sub setArrayTree {
29     my ($self, $array_tree) = @_;
30     (defined($array_tree) && ref($array_tree) eq 'ARRAY')
31         || die "Insufficient Arguments : You must supply a valid ARRAY reference"; 
32     # validate the tree ...
33     # it must not be empty
34     (scalar @{$array_tree} != 0)
35         || die "Insufficient Arguments : The array tree provided is empty";
36     # it's first element must not be an array
37     (ref($array_tree->[0]) ne 'ARRAY')
38         || die "Incorrect Object Type : The first value in the array tree is an array reference";
39     # and it must be a single rooted tree
40     (ref($array_tree->[1]) eq 'ARRAY')
41         || die "Incorrect Object Type : The second value in the array tree must be an array reference"
42             if defined($array_tree->[1]);
43     $self->{array_tree} = $array_tree;    
44 }
45
46 sub visit {
47         my ($self, $tree) = @_;
48         (blessed($tree) && $tree->isa("Tree::Simple"))
49                 || die "Insufficient Arguments : You must supply a valid Tree::Simple object"; 
50         $self->_buildTree(
51                 $tree,
52                 # our array tree 
53                 $self->{array_tree}, 
54                 # get a node filter if we have one
55                 $self->getNodeFilter(), 
56                 # pass the value of includeTrunk too
57                 $self->includeTrunk()
58                 );                                    
59 }
60
61 sub _buildTree {
62     my ($self, $tree, $array, $node_filter, $include_trunk) = @_;
63     my $i = 0;
64     while ($i < scalar @{$array}) {
65         my $node = $array->[$i];
66         # check to make sure we have a well formed tree
67         (ref($node) ne 'ARRAY') 
68             || die "Incorrect Object Type : The node value should never be an array reference";
69         # filter the node if nessecary
70         $node = $node_filter->($node) if defined($node_filter);
71         # create the new tree
72         my $new_tree;
73         if ($include_trunk) {
74             $tree->setNodeValue($node);
75             $new_tree = $tree;
76         }       
77         else {
78             $new_tree = Tree::Simple->new($node);
79             $tree->addChild($new_tree);
80         }
81         # increment the index value
82         $i++;
83         # NOTE:
84         # the value of include trunk is only 
85         # passed in the recursion, so that 
86         # the trunk/root can be populated, 
87         # we have no more need for it after 
88         # that time.
89         $self->_buildTree($new_tree, $array->[$i++], $node_filter) 
90             if ref($array->[$i]) eq 'ARRAY';
91     }
92 }
93
94 1;
95
96 __END__
97
98 =head1 NAME
99
100 Tree::Simple::Visitor::FromNestedArray - A Visitor for creating Tree::Simple objects from nested array trees.
101
102 =head1 SYNOPSIS
103
104   use Tree::Simple::Visitor::FromNestedArray;
105
106   my $visitor = Tree::Simple::Visitor::FromNestedArray->new();
107
108   # given this nested array tree
109   my $array_tree = [ 
110                     'Root', [ 
111                         'Child1', [
112                                 'GrandChild1',
113                                 'GrandChild2'
114                                 ],
115                         'Child2'
116                         ]
117                     ];
118   # set the array tree we 
119   # are going to convert
120   $visitor->setArrayTree($array_tree);            
121
122   $tree->accept($visitor);
123
124   # this then creates the equivalent Tree::Simple object:
125   # Tree::Simple->new("Root")
126   #     ->addChildren(
127   #         Tree::Simple->new("Child1")
128   #             ->addChildren(
129   #                 Tree::Simple->new("GrandChild1"),                
130   #                 Tree::Simple->new("GrandChild2")
131   #             ),
132   #         Tree::Simple->new("Child2"),
133   #     );
134
135 =head1 DESCRIPTION 
136
137 Given a tree constructed from nested arrays, this Visitor will create the equivalent Tree::Simple heirarchy. 
138
139 =head1 METHODS
140
141 =over 4
142
143 =item B<new>
144
145 There are no arguments to the constructor the object will be in its default state. You can use the C<setNodeFilter>, C<includTrunk> and C<setArrayTree> methods to customize its behavior.
146
147 =item B<includTrunk ($boolean)>
148
149 Setting the C<$boolean> value to true (C<1>) will cause the node value of the C<$tree> object passed into C<visit> to be set with the root value found in the C<$array_tree>. Setting it to false (C<0>), or not setting it, will result in the first value in the C<$array_tree> creating a new node level. 
150
151 =item B<setNodeFilter ($filter_function)>
152
153 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 C<$filter_function> is passed the node value extracted from the array prior to it being inserted into the tree being built. The C<$filter_function> is expected to return the value desired for inclusion into the tree.
154
155 =item B<setArrayTree ($array_tree)>
156
157 This method is used to set the C<$array_tree> that our Tree::Simple heirarchy will be constructed from. It must be in the following form:
158
159   [ 
160     'Root', [ 
161         'Child1', [
162               'GrandChild1',
163               'GrandChild2'
164               ],
165         'Child2'
166       ]
167   ]
168
169 Basically each element in the array is considered a node, unless it is an array reference, in which case it is interpreted as containing the children of the node created from the previous element in the array. 
170
171 The tree is validated prior being accepted, if it fails validation an execption will be thrown. The rules are as follows;
172
173 =over 4
174
175 =item The array tree must not be empty.
176
177 It makes not sense to create a tree out of nothing, so it is assumed that this is a sign of something wrong.
178
179 =item All nodes of the array tree must not be array references.
180
181 The root node is validated against this in this function, but all subsequent nodes are checked as the tree is built. Any nodes found to be array references are rejected and an exception is thrown. If you desire your node values to be array references, you can use the node filtering mechanism to acheive this as the node is filtered I<after> it is validated.
182
183 =item The array tree must be a single rooted tree.
184
185 If there is a second element in the array tree, it is assumed to be the children of the root, and therefore must be in the form of an array reference. 
186
187 =back
188
189 =item B<visit ($tree)>
190
191 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.
192
193 =back
194
195 =head1 BUGS
196
197 None that I am aware of. Of course, if you find a bug, let me know, and I will be sure to fix it. 
198
199 =head1 CODE COVERAGE
200
201 See the B<CODE COVERAGE> section in L<Tree::Simple::VisitorFactory> for more inforamtion.
202
203 =head1 SEE ALSO
204
205 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.
206
207 =head1 AUTHOR
208
209 stevan little, E<lt>stevan@iinteractive.comE<gt>
210
211 =head1 COPYRIGHT AND LICENSE
212
213 Copyright 2004, 2005 by Infinity Interactive, Inc.
214
215 L<http://www.iinteractive.com>
216
217 This library is free software; you can redistribute it and/or modify
218 it under the same terms as Perl itself. 
219
220 =cut