« blib/lib/Google/Ads/SOAP/Typelib/ComplexType.pm
↑ Summary
blib/lib/Google/Ads/Common/ReportUtils.pm »
| 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 | use base qw(SOAP::WSDL::Expat::MessageParser); | ||
| 21 | |||
| 22 | use Google::Ads::Common::XPathSAXParser; | ||
| 23 | |||
| 24 | use Carp; | ||
| 25 | use Scalar::Util qw(blessed); | ||
| 26 | use SOAP::WSDL::Expat::Base; | ||
| 27 | use SOAP::WSDL::Expat::MessageParser; | ||
| 28 | use SOAP::WSDL::XSD::Typelib::ComplexType; | ||
| 29 | |||
| 30 | # PATCH Overriding the SAX Parser initialization to use ours. | ||
| 31 | sub parse_string { | ||
| 32 | my $xml = $_[1]; | ||
| 33 | my $parser = $_[0]->_initialize(Google::Ads::Common::XPathSAXParser->new()); | ||
| 34 | eval { | ||
| 35 | $parser->parse($xml); | ||
| 36 | }; | ||
| 37 | croak($@) if $@; | ||
| 38 | delete $_[0]->{parser}; | ||
| 39 | return $_[0]->{data}; | ||
| 40 | } | ||
| 41 | # END PATCH | ||
| 42 | |||
| 43 | sub _initialize { | ||
| 44 | my ($self, $parser) = @_; | ||
| 45 | |||
| 46 | # Removing potential old results. | ||
| 47 | delete $self->{data}; | ||
| 48 | delete $self->{header}; | ||
| 49 | my $characters; | ||
| 50 | my $current = undef; | ||
| 51 | |||
| 52 | # Setting up variables for depth-first tree traversal. | ||
| 53 | my $list = []; | ||
| 54 | my $path = []; | ||
| 55 | my $skip = 0; | ||
| 56 | my $depth = 0; | ||
| 57 | |||
| 58 | # Executing sanity checks of main SOAP response headers. | ||
| 59 | my %content_check = $self->{strict}? | ||
| 60 | ( | ||
| 61 | 0 => sub { | ||
| 62 | die "Bad top node $_[1]" if $_[1] ne "Envelope"; | ||
| 63 | die "Bad namespace for SOAP envelope: " . $_[0]->recognized_string() | ||
| 64 | if $_[0]->namespace() ne | ||
| 65 | "http://schemas.xmlsoap.org/soap/envelope/"; | ||
| 66 | $depth++; | ||
| 67 | return; | ||
| 68 | }, | ||
| 69 | 1 => sub { | ||
| 70 | $depth++; | ||
| 71 | if ($_[1] eq "Body") { | ||
| 72 | if (exists $self->{data}) { | ||
| 73 | $self->{header} = $self->{data}; | ||
| 74 | delete $self->{data}; | ||
| 75 | $list = []; | ||
| 76 | $path = []; | ||
| 77 | undef $current; | ||
| 78 | } | ||
| 79 | } | ||
| 80 | return; | ||
| 81 | } | ||
| 82 | ): | ||
| 83 | ( | ||
| 84 | 0 => sub { | ||
| 85 | $depth++; | ||
| 86 | }, | ||
| 87 | 1 => sub { | ||
| 88 | $depth++; | ||
| 89 | } | ||
| 90 | ); | ||
| 91 | |||
| 92 | # Using "globals" for speed. | ||
| 93 | # PATCH Added global variables to check if a method package exists at | ||
| 94 | # runtime. | ||
| 95 | my ($_prefix, $_add_method, $_add_method_package, $_set_method, | ||
| 96 | $_set_method_package, $_class, $_leaf) = (); | ||
| 97 | # END OF PATCH | ||
| 98 | my $char_handler = sub { | ||
| 99 | # Returning if not a leaf. | ||
| 100 | return if (!$_leaf); | ||
| 101 | $characters .= $_[1]; | ||
| 102 | return; | ||
| 103 | }; | ||
| 104 | $parser->set_handlers({ | ||
| 105 | Start => sub { | ||
| 106 | # PATCH Added more input coming from the SAX parser | ||
| 107 | my ($parser, $element, $attrs, $node) = @_; | ||
| 108 | # END PATCH | ||
| 109 | |||
| 110 | $_leaf = 1; | ||
| 111 | |||
| 112 | return &{$content_check{$depth}} if exists $content_check{$depth}; | ||
| 113 | |||
| 114 | # Resolving class of this element. | ||
| 115 | my $typemap = $self->{class_resolver}->get_typemap(); | ||
| 116 | my $name = ""; | ||
| 117 | |||
| 118 | # PATCH Checking if the xsi:type attribute is set hence generating a | ||
| 119 | # different path to look in the typemap. | ||
| 120 | if (not $attrs->{"type"}) { | ||
| 121 | $name = $_[1]; | ||
| 122 | } else { | ||
| 123 | my $attr_type = $attrs->{"type"}; | ||
| 124 | $attr_type =~ s/(.*:)?(.*)/$2/; | ||
| 125 | $name = $_[1] . "[$attr_type]"; | ||
| 126 | } | ||
| 127 | # END PATCH | ||
| 128 | |||
| 129 | # Adding one more entry to the path | ||
| 130 | push @{$path}, $name; | ||
| 131 | |||
| 132 | # Skipping the element if is marked __SKIP__. | ||
| 133 | return if $skip; | ||
| 134 | |||
| 135 | $_class = $typemap->{join("/", @{$path}) }; | ||
| 136 | |||
| 137 | if (!defined($_class) and $self->{strict}) { | ||
| 138 | die "Cannot resolve class for " . $name . " path " . | ||
| 139 | join("/", @{$path}) . " via " . $self->{class_resolver}; | ||
| 140 | } | ||
| 141 | if (!defined($_class) or ($_class eq "__SKIP__")) { | ||
| 142 | $skip = join("/", @{$path}); | ||
| 143 | $_[0]->setHandlers(Char => undef); | ||
| 144 | return; | ||
| 145 | } | ||
| 146 | |||
| 147 | # Stepping down, adding $current to the list element of the current | ||
| 148 | # branch being visited. | ||
| 149 | push @$list, $current; | ||
| 150 | |||
| 151 | # Cleaning up current. Mainly to help profilers find the real hot spots. | ||
| 152 | undef $current; | ||
| 153 | |||
| 154 | $characters = q{}; | ||
| 155 | |||
| 156 | no warnings "once"; | ||
| 157 | $current = pop @{$OBJECT_CACHE_REF->{$_class}}; | ||
| 158 | if (not defined $current) { | ||
| 159 | my $o = Class::Std::Fast::ID(); | ||
| 160 | $current = bless \$o, $_class; | ||
| 161 | } | ||
| 162 | |||
| 163 | # PATCH Creating a double link between the SOAP Object and the parser | ||
| 164 | # node, so it can be later use for XPath searches. | ||
| 165 | Google::Ads::Common::XPathSAXParser::link_object_to_node($current, $node); | ||
| 166 | # END PATCH | ||
| 167 | |||
| 168 | # Setting attributes if there are any. | ||
| 169 | if ($attrs && $current->can("attr")) { | ||
| 170 | $current->attr($attrs); | ||
| 171 | } | ||
| 172 | $depth++; | ||
| 173 | return; | ||
| 174 | }, | ||
| 175 | Char => $char_handler, | ||
| 176 | End => sub { | ||
| 177 | # End of the element stepping up in the current branch path. | ||
| 178 | pop @{$path}; | ||
| 179 | |||
| 180 | # Checking if element need to be skipped __SKIP__. | ||
| 181 | if ($skip) { | ||
| 182 | return if $skip ne join "/", @{$path}, $_[1]; | ||
| 183 | $skip = 0; | ||
| 184 | $_[0]->setHandlers(Char => $char_handler); | ||
| 185 | return; | ||
| 186 | } | ||
| 187 | $depth--; | ||
| 188 | |||
| 189 | # Setting character values only if a leaf. | ||
| 190 | if ($_leaf) { | ||
| 191 | $SOAP::WSDL::XSD::Typelib::Builtin::anySimpleType::___value | ||
| 192 | ->{$$current} = $characters | ||
| 193 | if defined $characters && defined $current; | ||
| 194 | } | ||
| 195 | |||
| 196 | $characters = q{}; | ||
| 197 | $_leaf = 0; | ||
| 198 | |||
| 199 | # Finishing if at the top of the tree of elements, no more parents. | ||
| 200 | if (not defined $list->[-1]) { | ||
| 201 | $self->{data} = $current if (not exists $self->{data}); | ||
| 202 | return; | ||
| 203 | } | ||
| 204 | |||
| 205 | # Method to be called in the parent to add the current object to it. | ||
| 206 | $_add_method = "add_$_[1]"; | ||
| 207 | |||
| 208 | # Fixing up XML names for Perl names. | ||
| 209 | $_add_method =~ s{\.}{__}xg; | ||
| 210 | $_add_method =~ s{\-}{_}xg; | ||
| 211 | |||
| 212 | # PATCH Adding the element if the method to add is defined in the | ||
| 213 | # parent. | ||
| 214 | eval('use ' . ref($list->[-1])); | ||
| 215 | eval { | ||
| 216 | $list->[-1]->$_add_method($current); | ||
| 217 | }; | ||
| 218 | if ($@) { | ||
| 219 | warn ("Couldn't find a setter $_add_method for object of type " . | ||
| 220 | ref($current) . " in object of type " . ref($list->[-1]) . | ||
| 221 | " method " . $_add_method); | ||
| 222 | } | ||
| 223 | # END PATCH | ||
| 224 | |||
| 225 | #Stepping up in the current object hierarchy. | ||
| 226 | $current = pop @$list; | ||
| 227 | return; | ||
| 228 | } | ||
| 229 | }); | ||
| 230 | return $parser; | ||
| 231 | } | ||
| 232 | |||
| 233 | return 1; |