blib/lib/Froody/Walker.pm

filter: Statementstmt 93.5% 87 / 93 Branchbran 66.6% 24 / 36 Conditioncond 53.8% 7 / 13 MC/DCmcdc 25.0% 2 / 8 Subroutinesub 92.3% 12 / 13 Podpod 100% 6 / 6 totaltotal 81.6% 138 / 169 CC 27 SCAR 25.5
CC
27
Cov
83%
walk_node
21.9
spec_for_xpath
12.0
walk
3.0
CRAP 12.9
« blib/lib/Froody/Server/Apache.pm ↑ Summary blib/lib/Froody/Pod.pm »
1 =head1 NAME
2  
3 Froody::Walker;
4  
5 =head1 SYNOPSIS
6  
7 my $spec = $froody_method->structure;
8 my $method_name = $froody_method->name;
9
10 # create a new walker that knows about the spec
11 my $walker = Froody::Walker->new($spec, "method name");
12 my $terse = Froody::Walker::Terse->new;
13 my $xml = Froody::Walker::XML->new;
14 $walker->from($terse)->to($xml);
15
16 # walk $source turning it into xml
17 my $xml = $walker->walk($data);
18  
19 =head1 DESCRIPTION
20  
21 Walker classes are constucted with a description of the structure it
22 will generate. That structure is a very specific grammar listing
23 paths into the object that can be built.
24  
25 Setting L<from|/METHODS> and L<to|/METHODS> designates the source and
26 targets for the transformation, respectively. We currently provide
27 two data structures C<Froody::Walker::Terse> and C<Froody::Walker::XML>
28 as transformation engines, which will both work bi-directionally.
29  
30 =cut
31  
32 package Froody::Walker;
33 34 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
34 34 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
35  
36 34 ▶use base 'Class::Accessor::Chained::Fast';
Subroutine BEGIN: called CC=1 SCAR=0.0
37  
38 34 __PACKAGE__->mk_accessors(qw{spec method});
39  
40 34 ▶use Froody::Logger;
Subroutine BEGIN: called CC=1 SCAR=0.0
41 34 ▶use Froody::Error;
Subroutine BEGIN: called CC=1 SCAR=0.0
42  
43 34 ▶use Scalar::Util qw(weaken);
Subroutine BEGIN: called CC=1 SCAR=0.0
44  
45 34 my $logger = get_logger("froody.walker");
46  
47 =head2 METHODS
48  
49 =over
50  
51 =item $self->walk($spec, $data)
52  
53 Walks the structure of data with the source C<Froody::Walker::Driver>,
54 returning a transformed version of the data as per the designation of
55 the target.
56  
57 =cut
58  
59 64 ▶sub walk {
Subroutine walk: called CC=3 SCAR=11.0
Pod documented
60 64 my ($self, $source) = @_;
61  
62 64 $source = $self->from->init_source($source);
63  
64 64 ▶ return $self->to->init_target('') unless $self->toplevels;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->toplevels 1 63 64
65  
66 63 my $result = $self->walk_node($source, ($self->toplevels)[0]);
67  
68 63 ▶ Froody::Error->throw('froody.xml', "walk_node did not return a response")
Branch: 1 branch
BranchTrueFalseTotal
unless defined $result 0 63 63
69 unless defined($result);
70  
71 # hey, at this point, we have multiple top-level elements!
72 63 return $result; # $result->{ $toplevel[0] }; # Terse specific. move to XMLToTerse
73 }
74  
75 =item $self->walk_node($spec, $source, $xpath_key, [ $parent_target ])
76  
77 Walks the data structure this object holds with the specification, starting at
78 the part of the spec indicated by $xpath_key.
79  
80 C<$parent_target> is used for accumulation of results at the current level.
81  
82 =cut
83  
84 182 ▶sub walk_node {
Subroutine walk_node: called CC=20 SCAR=30.8
Pod documented
85 182 my ($self, $source, $xpath_key, $parent_target) = @_;
86 182 ▶ $logger->debug("Walking path '$xpath_key' into source '@{[ $source || '' ]}'");
Condition100%2/2
$source || ''
l!l
1811
MC/DC100%1/1
$source || ''
$source
Truth table100%2/2
A
A= $source
Aresult
11
00
87 182 ▶ my $target = $self->to->init_target($xpath_key, $parent_target)
Branch: 1 branch
BranchTrueFalseTotal
my $target = $self->to->init_target($xpath_key, $parent_target) or "Froody::Error"->throw("froody.xml", "init_target failed to create a target") 182 0 182
88 or Froody::Error->throw('froody.xml', "init_target failed to create a target");
89  
90 # get the part of the spec for where we are now looking at
91 182 ▶ my $global_spec = $self->spec || {};
Condition50%1/2
$self->spec || {}
l!l
1820
MC/DC0%0/1
$self->spec || {}
$self->spec
Truth table50%1/2
A
A= $self->spec
Aresult
11
00
92 182 my $spec = $global_spec->{ $xpath_key };
93  
94 182 $self->from->validate_source($source, $xpath_key);
95  
96 # get text node (simplest case)
97 182 ▶ if (!$spec or $spec->{text}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $spec or $spec->{"text"}) 117 65 182
Condition66%2/3
not $spec or $spec->{"text"}
l!l&&r!l&&!r
011765
MC/DC50%1/2
not $spec or $spec->{"text"}
not $spec $spec->{"text"}
Truth table66%2/3
not(A) or B
A= not $specB= $spec->{"text"}
ABresult
1X1
011
000
98 117 $logger->debug("getting text node");
99 117 my $value = $self->from->read_text($source, $xpath_key);
100 117 ▶ if (defined($value)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) 113 4 117
101 113 $value = "$value";
102 113 $logger->debug(" got '$value'");
103 113 $target = $self->to->write_text($target, $xpath_key, $value);
104 113 ▶ Froody::Error->throw('froody.xml', "write_text did not return a target")
Branch: 1 branch
BranchTrueFalseTotal
unless defined $target 0 113 113
105 unless defined($target);
106 }
107 # if the spec is empty, assume a single text node.
108 117 ▶ return $target unless $spec;
Branch: 1 branch
BranchTrueFalseTotal
unless $spec 0 117 117
109 }
110  
111 # the attributes are easy. We just pass them each on.
112 # TODO: Work out if we need to encode these
113
114 # HANDLE all simple values.
115 182 for my $attr (reverse @{ $spec->{attr} }) {
116 139 $logger->debug("Getting attribute '$attr'");
117 139 my $value = $self->from->read_attribute($source, $xpath_key, $attr);
118 139 ▶ next unless defined($value);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 2 137 139
119 137 $value = "$value";
120 137 $logger->debug(" got $value");
121 137 ▶ $target = $self->to->write_attribute($target, $xpath_key, $attr, $value)
Branch: 1 branch
BranchTrueFalseTotal
$target = $self->to->write_attribute($target, $xpath_key, $attr, $value) or "Froody::Error"->throw("froody.xml", "write_attribute did not return a target") 137 0 137
122 or Froody::Error->throw('froody.xml',"write_attribute did not return a target");
123 }
124  
125 182 for my $element (@{ $spec->{elts} }) {
126 112 $logger->debug("getting element $element");
127 112 ▶ my $local_xpath = $xpath_key ? "$xpath_key/$element" : $element;
Branch: 1 branch
BranchTrueFalseTotal
$xpath_key ? : 112 0 112
128 112 ▶ my @local_source = $self->from->child_sources($source, $xpath_key, $element )
Branch: 1 branch
BranchTrueFalseTotal
my(@local_source) = $self->from->child_sources($source, $xpath_key, $element) or next 83 29 112
129 # if there's no source, we don't make a target - no empty hashes,
130 # xml nodes, etc, etc.
131 or next;
132
133 #warn "Here in elts for $local_xpath";
134 83 ▶ if (@local_source > 1 and !$global_spec->{$local_xpath}{multi}) {
Branch: 1 branch
BranchTrueFalseTotal
if (@local_source > 1 and not $global_spec->{$local_xpath}{"multi"}) 0 83 83
Condition66%2/3
@local_source > 1 and not $global_spec->{$local_xpath}{"multi"}
!ll&&!rl&&r
59240
MC/DC0%0/2
@local_source > 1 and not $global_spec->{$local_xpath}{"multi"}
@local_source > 1 not $global_spec->{$local_xpath}{"multi"}
Truth table66%2/3
A and not(B)
A= @local_source > 1B= not $global_spec->{$local_xpath}{"multi"}
ABresult
0X0
100
111
135 0 Froody::Error->throw('froody.xml',
136 "got multiple entries for path '$local_xpath', but the spec suggests there should be only one");
137 }
138 83 for my $this_source (@local_source) {
139 119 ▶ Froody::Error->throw('froody.xml', "source for path '$local_xpath' is undefined")
Branch: 1 branch
BranchTrueFalseTotal
unless defined $this_source 0 119 119
140 unless defined($this_source);
141 119 $logger->debug("local source '$this_source'");
142 119 my $local_target = $self->walk_node( $this_source, $local_xpath, $target );
143 119 ▶ $target = $self->to->add_child_to_target( $target, $xpath_key,
Branch: 1 branch
BranchTrueFalseTotal
$target = $self->to->add_child_to_target($target, $xpath_key, $element, $local_target) or "Froody::Error"->throw("froody.xml", "add_child_to_target did not return a target") 119 0 119
144 $element, $local_target )
145 or Froody::Error->throw('froody.xml', "add_child_to_target did not return a target");
146 }
147 }
148 182 $logger->debug("done walking path $xpath_key");
149 182 return $target;
150 }
151  
152 =item from (Froody::Walker::Driver)
153  
154 Sets the source driver.
155  
156 =cut
157  
158 678 ▶sub from {
Subroutine from: called CC=2 SCAR=6.9
Pod documented
159 678 my $self = shift;
160
161 678 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 64 614 678
162 64 $self->{from} = shift;
163 64 $self->{from}{walker} = $self;
164 64 weaken( $self->{from}{walker} );
165 64 return $self
166 }
167 614 return $self->{from};
168 }
169  
170 =item to (Froody::Walker::Driver)
171  
172 Sets the target driver.
173  
174 =cut
175  
176 616 ▶sub to {
Subroutine to: called CC=2 SCAR=6.9
Pod documented
177 616 my $self = shift;
178
179 616 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 64 552 616
180 64 $self->{to} = shift;
181 64 $self->{to}{walker} = $self;
182 64 weaken( $self->{to}{walker} );
183 64 return $self;
184 }
185 552 return $self->{to};
186 }
187  
188 =back
189  
190 =head2 Utility methods
191  
192 Small methods that get called a lot from subclasses.
193  
194 =over
195  
196 =item spec_for_xpath( path )
197  
198 Returns the local method spec for the given xpath. Returns the
199 default method spec (text-only node) if there is no spac for that
200 path.
201  
202 =cut
203  
204 0 ▶sub spec_for_xpath {
Subroutine spec_for_xpath: not called CC=3 SCAR=24.8
Pod documented
205 0 my ($self, $xpath) = @_;
206 0 ▶ my $spec = $self->spec->{$xpath} if $self->spec && $xpath;
Branch: 1 branch
BranchTrueFalseTotal
if $self->spec and $xpath 0 0 0
Condition0%0/3
$self->spec and $xpath
!ll&&!rl&&r
000
MC/DC0%0/2
$self->spec and $xpath
$self->spec $xpath
Truth table0%0/3
A and B
A= $self->specB= $xpath
ABresult
0X0
100
111
207 0 return $spec;
208 }
209  
210 =item toplevels()
211  
212 return a list of the top-level node names in the response XML.
213  
214 =cut
215  
216 127 ▶sub toplevels {
Subroutine toplevels: called CC=2 SCAR=7.2
Pod documented
217 127 my $self = shift;
218
219 # look for the toplevel in the spec. If we've got more than one, panic!
220 127 my @toplevel = grep m{^[^/]+$}, keys %{ $self->spec };
221  
222 127 ▶ if (@toplevel > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@toplevel > 1) 0 127 127
223 0 ▶ warn Dumper($self->spec); use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.2
224 0 Froody::Error->throw("froody.xml",
225 "invalid Response spec (multiple toplevel nodes!)")
226 }
227
228 127 $self->{spec}{''} = {
229 elts => \@toplevel,
230 attr => [],
231 text => 0,
232 };
233
234 127 return @toplevel;
235 }
236  
237 =back
238  
239 =head1 BUGS
240  
241 None known.
242  
243 Please report any bugs you find via the CPAN RT system.
244 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Froody>
245  
246 =head1 AUTHOR
247  
248 Copyright Fotango 2005. All rights reserved.
249  
250 Please see the main L<Froody> documentation for details of who has worked
251 on this project.
252  
253 This module is free software; you can redistribute it and/or modify it under
254 the same terms as Perl itself.
255  
256 =head1 SEE ALSO
257  
258 L<Froody>, L<Froody::Response::Terse>
259  
260 =cut
261  
262 34 1;
« blib/lib/Froody/Server/Apache.pm ↑ Summary blib/lib/Froody/Pod.pm »