2 package Tree::Simple::Visitor::FromNestedArray;
9 use Scalar::Util qw(blessed);
11 use base qw(Tree::Simple::Visitor);
15 my $class = ref($_class) || $_class;
17 bless($visitor, $class);
24 $self->{array_tree} = undef;
25 $self->SUPER::_init();
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;
47 my ($self, $tree) = @_;
48 (blessed($tree) && $tree->isa("Tree::Simple"))
49 || die "Insufficient Arguments : You must supply a valid Tree::Simple object";
54 # get a node filter if we have one
55 $self->getNodeFilter(),
56 # pass the value of includeTrunk too
62 my ($self, $tree, $array, $node_filter, $include_trunk) = @_;
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);
74 $tree->setNodeValue($node);
78 $new_tree = Tree::Simple->new($node);
79 $tree->addChild($new_tree);
81 # increment the index value
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
89 $self->_buildTree($new_tree, $array->[$i++], $node_filter)
90 if ref($array->[$i]) eq 'ARRAY';
100 Tree::Simple::Visitor::FromNestedArray - A Visitor for creating Tree::Simple objects from nested array trees.
104 use Tree::Simple::Visitor::FromNestedArray;
106 my $visitor = Tree::Simple::Visitor::FromNestedArray->new();
108 # given this nested array tree
118 # set the array tree we
119 # are going to convert
120 $visitor->setArrayTree($array_tree);
122 $tree->accept($visitor);
124 # this then creates the equivalent Tree::Simple object:
125 # Tree::Simple->new("Root")
127 # Tree::Simple->new("Child1")
129 # Tree::Simple->new("GrandChild1"),
130 # Tree::Simple->new("GrandChild2")
132 # Tree::Simple->new("Child2"),
137 Given a tree constructed from nested arrays, this Visitor will create the equivalent Tree::Simple heirarchy.
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.
147 =item B<includTrunk ($boolean)>
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.
151 =item B<setNodeFilter ($filter_function)>
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.
155 =item B<setArrayTree ($array_tree)>
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:
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.
171 The tree is validated prior being accepted, if it fails validation an execption will be thrown. The rules are as follows;
175 =item The array tree must not be empty.
177 It makes not sense to create a tree out of nothing, so it is assumed that this is a sign of something wrong.
179 =item All nodes of the array tree must not be array references.
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.
183 =item The array tree must be a single rooted tree.
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.
189 =item B<visit ($tree)>
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.
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.
201 See the B<CODE COVERAGE> section in L<Tree::Simple::VisitorFactory> for more inforamtion.
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.
209 stevan little, E<lt>stevan@iinteractive.comE<gt>
211 =head1 COPYRIGHT AND LICENSE
213 Copyright 2004, 2005 by Infinity Interactive, Inc.
215 L<http://www.iinteractive.com>
217 This library is free software; you can redistribute it and/or modify
218 it under the same terms as Perl itself.