lib/Google/Ads/SOAP/Deserializer/MessageParser.pm

filter: Statementstmt 100% 9 / 9 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 3 / 3 Podpod n/a totaltotal 100% 12 / 12 CC 1 SCAR 0.0
CC
1
Cov
100%
BEGIN
1.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« lib/Google/Ads/Common/ReportDownloadHandler.pm ↑ Summary
1 # Copyright 2012, Google Inc. All Rights Reserved.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14 #
15 # Custom MessageParser based on SOAP::WSDL::Expat::MessageParser, with some
16 # overriden methods via inheritance.
17  
18 package Google::Ads::SOAP::Deserializer::MessageParser;
19  
20 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 2 ▶use base qw(SOAP::WSDL::Expat::MessageParser);
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 use Google::Ads::Common::LibXmlParser;
25 use Google::Ads::Common::XPathSAXParser;
26  
27 use Carp;
28 use Scalar::Util qw(blessed);
29 use SOAP::WSDL::Expat::Base;
30 use SOAP::WSDL::Expat::MessageParser;
31 use SOAP::WSDL::XSD::Typelib::ComplexType;
32  
33 # PATCH Overriding the SAX Parser initialization to use ours.
34 sub parse_string {
35 my ($self, $xml, $client) = @_;
36 # The variable 'soap_legacy' in the client is a boolean value indicating if
37 # the legacy SOAP library should be used.
38 my $soap_parser =
39 ($client->get_soap_legacy())
40 ? Google::Ads::Common::XPathSAXParser->new()
41 : Google::Ads::Common::LibXmlParser->new();
42 my $parser = $self->_initialize($soap_parser, $client);
43 $parser->parse($xml);
44 if ($@) {
45 $client->get_die_on_faults() ? die($@) : warn($@);
46 }
47 delete $self->{parser};
48 return $self->{data};
49 }
50 # END PATCH
51  
52 sub _initialize {
53 my ($self, $parser, $client) = @_;
54  
55 # Removing potential old results.
56 delete $self->{data};
57 delete $self->{header};
58 my $characters;
59 my $current = undef;
60  
61 # Setting up variables for depth-first tree traversal.
62 my $list = [];
63 my $path = [];
64 my $skip = 0;
65 my $depth = 0;
66  
67 # Executing sanity checks of main SOAP response headers.
68 my %content_check = $self->{strict}
69 ? (
70 0 => sub {
71 die "Bad top node $_[1]" if $_[1] ne "Envelope";
72 die "Bad namespace for SOAP envelope: " . $_[0]->recognized_string()
73 if $_[0]->namespace() ne "http://schemas.xmlsoap.org/soap/envelope/";
74 $depth++;
75 return;
76 },
77 1 => sub {
78 $depth++;
79 if ($_[1] eq "Body") {
80 if (exists $self->{data}) {
81 $self->{header} = $self->{data};
82 delete $self->{data};
83 $list = [];
84 $path = [];
85 undef $current;
86 }
87 }
88 return;
89 })
90 : (
91 0 => sub {
92 $depth++;
93 },
94 1 => sub {
95 $depth++;
96 });
97  
98 # Using "globals" for speed.
99 # PATCH Added global variables to check if a method package exists at
100 # runtime.
101 my ($_prefix, $_add_method, $_add_method_package, $_set_method,
102 $_set_method_package, $_class, $_leaf)
103 = ();
104 # END OF PATCH
105 my $char_handler = sub {
106 # Returning if not a leaf.
107 return if (!$_leaf);
108 $characters .= $_[1];
109 return;
110 };
111 $parser->set_handlers({
112 Start => sub {
113 # PATCH Added more input coming from the parser
114 my ($parser, $element, $attrs, $node) = @_;
115 # END PATCH
116  
117 $_leaf = 1;
118  
119 return &{$content_check{$depth}} if exists $content_check{$depth};
120  
121 # Resolving class of this element.
122 my $typemap = $self->{class_resolver}->get_typemap();
123 my $name = "";
124  
125 # PATCH Checking if the xsi:type attribute is set hence generating a
126 # different path to look in the typemap.
127 if (not $attrs->{"type"}) {
128 $name = $_[1];
129 } else {
130 my $attr_type = $attrs->{"type"};
131 $attr_type =~ s/(.*:)?(.*)/$2/;
132 $name = $_[1] . "[$attr_type]";
133 }
134 # END PATCH
135  
136 # Adding one more entry to the path
137 push @{$path}, $name;
138  
139 # Skipping the element if is marked __SKIP__.
140 return if $skip;
141  
142 $_class = $typemap->{join("/", @{$path})};
143  
144 # PATCH Checking and retreating if the path is not in the typemap.
145 if (!defined $_class) {
146 $name =~ s/\[.+\]//;
147 $path->[-1] = $name;
148  
149 $_class = $typemap->{join("/", @{$path})};
150 }
151 # END PATCH
152  
153 if (!defined($_class) and $self->{strict}) {
154 die "Cannot resolve class for " . $name . " path " .
155 join("/", @{$path}) . " via " . $self->{class_resolver};
156 }
157 if (!defined($_class) or ($_class eq "__SKIP__")) {
158 $skip = join("/", @{$path});
159 $_[0]->setHandlers(Char => undef);
160 return;
161 }
162  
163 # Stepping down, adding $current to the list element of the current
164 # branch being visited.
165 push @$list, $current;
166  
167 # Cleaning up current. Mainly to help profilers find the real hot spots.
168 undef $current;
169  
170 $characters = q{};
171  
172 no warnings "once";
173 $current =
174 pop @{$SOAP::WSDL::Expat::MessageParser::OBJECT_CACHE_REF->{$_class}};
175 if (not defined $current) {
176 my $o = Class::Std::Fast::ID();
177 $current = bless \$o, $_class;
178 }
179  
180 # PATCH Creating a double link between the SOAP Object and the parser
181 # node, so it can be later use for XPath searches.
182 ($client->get_soap_legacy())
183 ? Google::Ads::Common::XPathSAXParser::link_object_to_node($current,
184 $node)
185 : Google::Ads::Common::LibXmlParser::link_object_to_node($current,
186 $node);
187 # END PATCH
188  
189 # Setting attributes if there are any.
190 if ($attrs && $current->can("attr")) {
191 $current->attr($attrs);
192 }
193 $depth++;
194 return;
195 },
196 Char => $char_handler,
197 End => sub {
198 # End of the element stepping up in the current branch path.
199 pop @{$path};
200  
201 # Checking if element need to be skipped __SKIP__.
202 if ($skip) {
203 return if $skip ne join "/", @{$path}, $_[1];
204 $skip = 0;
205 $_[0]->setHandlers(Char => $char_handler);
206 return;
207 }
208 $depth--;
209  
210 # Setting character values only if a leaf.
211 if ($_leaf) {
212 $SOAP::WSDL::XSD::Typelib::Builtin::anySimpleType::___value->
213 {$$current} = $characters
214 if defined $characters && defined $current;
215 }
216  
217 $characters = q{};
218 $_leaf = 0;
219  
220 # Finishing if at the top of the tree of elements, no more parents.
221 if (not defined $list->[-1]) {
222 $self->{data} = $current if (not exists $self->{data});
223 return;
224 }
225  
226 # Method to be called in the parent to add the current object to it.
227 $_add_method = "add_$_[1]";
228  
229 # Fixing up XML names for Perl names.
230 $_add_method =~ s{\.}{__}xg;
231 $_add_method =~ s{\-}{_}xg;
232  
233 # PATCH Adding the element if the method to add is defined in the
234 # parent.
235 eval('use ' . ref($list->[-1]));
236 eval { $list->[-1]->$_add_method($current); };
237 if ($@) {
238 warn("Couldn't find a setter $_add_method for object of type " .
239 ref($current) . " in object of type " .
240 ref($list->[-1]) . " method " . $_add_method);
241 }
242 # END PATCH
243  
244 #Stepping up in the current object hierarchy.
245 $current = pop @$list;
246 return;
247 }
248 });
249 return $parser;
250 }
251  
252 return 1;
« lib/Google/Ads/Common/ReportDownloadHandler.pm ↑ Summary