| 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 complex type object with patched logic to properly: | ||
| 16 | # - Serialize/Deserialize objects with xsi:type | ||
| 17 | # - Properly include namespaces when attributes are inherited from different | ||
| 18 | # namespaces. | ||
| 19 | # - Include built-in XPath search capabilities. | ||
| 20 | # - Add the ability to transform from and to hashes. | ||
| 21 | # This module is based on SOAP::WSDL::XSD::Typelib::ComplexType, with some | ||
| 22 | # overriden methods via inheritance. | ||
| 23 | |||
| 24 | package Google::Ads::SOAP::Typelib::ComplexType; | ||
| 25 | |||
| 26 | use base qw(SOAP::WSDL::XSD::Typelib::ComplexType); | ||
| 27 | |||
| 28 | no warnings qw(redefine); | ||
| 29 | no strict qw(refs); | ||
| 30 | use version; | ||
| 31 | |||
| 32 | # Loading patched packages first. | ||
| 33 | use Google::Ads::Common::XPathSAXParser; | ||
| 34 | |||
| 35 | use Carp; | ||
| 36 | use Data::Dumper; | ||
| 37 | use Scalar::Util qw(blessed); | ||
| 38 | use SOAP::WSDL::Expat::Base; | ||
| 39 | use SOAP::WSDL::Expat::MessageParser; | ||
| 40 | use SOAP::WSDL::XSD::Typelib::ComplexType; | ||
| 41 | |||
| 42 | # Silencing annoying third-party library warnings. | ||
| 43 | $SIG{__WARN__}= sub { | ||
| 44 | warn @_ unless | ||
| 45 | $_[0] =~ /Tie::Hash::FIELDS|Cache::RemovalStrategy|XPath\/ | ||
| 46 | Node\/Element|XMLSchemaSOAP1_2::as_dateTime/; | ||
| 47 | }; | ||
| 48 | |||
| 49 | # Patching of the WSDL library to include xsi:type attribute for | ||
| 50 | # elements that inherit other complex elements like ManualCPC. | ||
| 51 | sub serialize_attr { | ||
| 52 | my ($self, $args) = @_; | ||
| 53 | my $result = q{}; | ||
| 54 | if ($xml_attr_of{${$_[0]}}) { | ||
| 55 | $result = $xml_attr_of{${$_[0]}}->serialize(); | ||
| 56 | } | ||
| 57 | |||
| 58 | # PATCH to include xsi:type when necessary. | ||
| 59 | if ($args->{xsitype}) { | ||
| 60 | $result = $result . " xsi:type=\"$args->{xsitype}\" "; | ||
| 61 | } | ||
| 62 | # END OF PATCH | ||
| 63 | |||
| 64 | if ($args->{xsitypens}) { | ||
| 65 | $result = $result . " xmlns:$args->{xsitypens}->{name}=\"" . | ||
| 66 | "$args->{xsitypens}->{value}\" "; | ||
| 67 | } | ||
| 68 | |||
| 69 | return $result; | ||
| 70 | } | ||
| 71 | |||
| 72 | |||
| 73 | # Redefining complex type factory method to allow subclasses to be passed to | ||
| 74 | # attribute setters, so for example a | ||
| 75 | # set_operations(\@{ARRAY_OF_SUBCLASSES_OF_OPERATION}) can be performed. | ||
| 76 | sub _factory { | ||
| 77 | my $class = shift; | ||
| 78 | |||
| 79 | $ELEMENTS_FROM->{$class} = shift; | ||
| 80 | $ATTRIBUTES_OF->{$class} = shift; | ||
| 81 | $CLASSES_OF->{$class} = shift; | ||
| 82 | $NAMES_OF->{$class} = shift; | ||
| 83 | |||
| 84 | while (my ($name, $attribute_ref) = each %{$ATTRIBUTES_OF->{$class}}) { | ||
| 85 | my $type = $CLASSES_OF->{$class}->{$name} or | ||
| 86 | croak "No class given for $name"; | ||
| 87 | $type->isa('UNIVERSAL') or eval "require $type" or croak $@; | ||
| 88 | my $is_list = $type->isa('SOAP::WSDL::XSD::Typelib::Builtin::list'); | ||
| 89 | my $method_name = $name; | ||
| 90 | $method_name =~s{[\.\-]}{_}xmsg; | ||
| 91 | *{"$class\::set_$method_name"} = sub { | ||
| 92 | if (not $#_) { | ||
| 93 | delete $attribute_ref->{${$_[0]}}; | ||
| 94 | return; | ||
| 95 | }; | ||
| 96 | my $is_ref = ref $_[1]; | ||
| 97 | $attribute_ref->{${$_[0]}} = ($is_ref)? | ||
| 98 | ($is_ref eq 'ARRAY')? | ||
| 99 | $is_list? | ||
| 100 | $type->new({value => $_[1]}): | ||
| 101 | [map { | ||
| 102 | ref $_? | ||
| 103 | ref $_ eq 'HASH'? | ||
| 104 | # PATCH Call custom hash to object subroutine | ||
| 105 | # that correctly handles xsi_type. | ||
| 106 | _hash_to_object($type, $_): | ||
| 107 | # An isa type comparison is needed to check | ||
| 108 | # for the right type. | ||
| 109 | $_->isa($type)? | ||
| 110 | # END OF PATCH | ||
| 111 | $_ : croak "cannot use " . ref($_) . | ||
| 112 | " reference as value for" . | ||
| 113 | " $name - $type required" | ||
| 114 | : $type->new({value => $_}) | ||
| 115 | } @{$_[1]}]: | ||
| 116 | $is_ref eq 'HASH'? | ||
| 117 | # PATCH Call custom hash to object subroutine that correctly | ||
| 118 | # handles xsi_type. | ||
| 119 | do { | ||
| 120 | _hash_to_object($type, $_[1]); | ||
| 121 | }: | ||
| 122 | # END OF PATCH | ||
| 123 | blessed $_[1] && $_[1]->isa($type)? | ||
| 124 | $_[1]: | ||
| 125 | die croak "cannot use $is_ref reference as value for " . | ||
| 126 | "$name - $type required": | ||
| 127 | defined $_[1] ? $type->new({value => $_[1]}) : (); | ||
| 128 | return; | ||
| 129 | }; | ||
| 130 | |||
| 131 | *{"$class\::add_$method_name"} = sub { | ||
| 132 | warn "attempting to add empty value to " . ref $_[0] | ||
| 133 | if not defined $_[1]; | ||
| 134 | |||
| 135 | if (not exists $attribute_ref->{${$_[0]}}) { | ||
| 136 | $attribute_ref->{${$_[0]}} = $_[1]; | ||
| 137 | return; | ||
| 138 | } | ||
| 139 | |||
| 140 | if (not ref $attribute_ref->{${$_[0]}} eq 'ARRAY') { | ||
| 141 | $attribute_ref->{${$_[0]}} = [$attribute_ref->{${$_[0]}}, $_[1]]; | ||
| 142 | return; | ||
| 143 | } | ||
| 144 | |||
| 145 | push @{$attribute_ref->{${$_[0]}}}, $_[1]; | ||
| 146 | return; | ||
| 147 | }; | ||
| 148 | } | ||
| 149 | |||
| 150 | *{"$class\::new"} = sub { | ||
| 151 | my $self = bless \(my $o = Class::Std::Fast::ID()), $_[0]; | ||
| 152 | |||
| 153 | if (exists $_[1]->{xmlattr}) { | ||
| 154 | $self->attr(delete $_[1]->{xmlattr}); | ||
| 155 | } | ||
| 156 | |||
| 157 | # Iterate over keys of arguments and call set appropriate field in class | ||
| 158 | map {($ATTRIBUTES_OF->{$class}->{$_})? | ||
| 159 | do { | ||
| 160 | my $method = "set_$_"; | ||
| 161 | $method =~s{[\.\-]}{_}xmsg; | ||
| 162 | $self->$method($_[1]->{$_}); | ||
| 163 | }: | ||
| 164 | # PATCH Ignoring xsi_type as a regular attribute of a given HASH since | ||
| 165 | # is treated specially later. | ||
| 166 | $_ =~ m{ \A | ||
| 167 | xmlns|xsi_type | ||
| 168 | }xms?(): | ||
| 169 | do { | ||
| 170 | croak "Unknown field $_ in $class.\nValid fields are:\n" . | ||
| 171 | join(', ', @{$ELEMENTS_FROM->{$class}}) . "\n" . | ||
| 172 | "Structure given:\n" . Dumper ($_[1]) | ||
| 173 | }; | ||
| 174 | # END PATCH | ||
| 175 | } keys %{$_[1]}; | ||
| 176 | return $self; | ||
| 177 | }; | ||
| 178 | |||
| 179 | *{"$class\::_serialize"} = sub { | ||
| 180 | my $ident = ${$_[0]}; | ||
| 181 | my $option_ref = $_[1]; | ||
| 182 | |||
| 183 | return \join q{} , map { | ||
| 184 | my $element = $ATTRIBUTES_OF->{$class}->{$_}->{$ident}; | ||
| 185 | |||
| 186 | if (defined $element) { | ||
| 187 | $element = [$element] if not ref $element eq 'ARRAY'; | ||
| 188 | my $name = $NAMES_OF->{$class}->{$_} || $_; | ||
| 189 | my $target_namespace = $_[0]->get_xmlns(); | ||
| 190 | map { | ||
| 191 | if ($_->isa('SOAP::WSDL::XSD::Typelib::Element')) { | ||
| 192 | ($target_namespace ne $_->get_xmlns())? | ||
| 193 | $_->serialize({name => $name, qualified => 1}): | ||
| 194 | $_->serialize({name => $name}); | ||
| 195 | } else { | ||
| 196 | if (!defined $ELEMENT_FORM_QUALIFIED_OF->{$class} or | ||
| 197 | $ELEMENT_FORM_QUALIFIED_OF->{$class}) { | ||
| 198 | if (exists $option_ref->{xmlns_stack} && | ||
| 199 | (scalar @{$option_ref->{xmlns_stack}} >= 2) && | ||
| 200 | ($option_ref->{xmlns_stack}->[-1] ne | ||
| 201 | $option_ref->{xmlns_stack}->[-2])) { | ||
| 202 | join q{}, $_->start_tag({ | ||
| 203 | name => $name, | ||
| 204 | xmlns => $option_ref->{xmlns_stack}->[-1], | ||
| 205 | %{$option_ref} | ||
| 206 | }), | ||
| 207 | $_->serialize($option_ref), | ||
| 208 | $_->end_tag({name => $name , %{$option_ref}}); | ||
| 209 | } else { | ||
| 210 | # PATCH Determine if xsi:type is required. | ||
| 211 | my $refname = ref($_); | ||
| 212 | my $classname = $CLASSES_OF->{$class}->{$name}; | ||
| 213 | if ($classname && $classname ne ref($_)) { | ||
| 214 | my $xsitypens = {}; | ||
| 215 | if ($option_ref->{xmlns_stack}->[-1] ne $_->get_xmlns()){ | ||
| 216 | $xsitypens->{name} = "xns"; | ||
| 217 | $xsitypens->{value} = $_->get_xmlns(); | ||
| 218 | $option_ref->{xsitypens} = $xsitypens; | ||
| 219 | } | ||
| 220 | my $package_name = ref($_); | ||
| 221 | $package_name =~ /^.*::(.*)$/; | ||
| 222 | my $xsi_type = $1; | ||
| 223 | $option_ref->{xsitype} = | ||
| 224 | ($xsitypens->{name}?$xsitypens->{name} . ":" : "") . | ||
| 225 | "$xsi_type"; | ||
| 226 | } else { | ||
| 227 | delete $option_ref->{xsitype}; | ||
| 228 | } | ||
| 229 | |||
| 230 | # Checks to see if namespace is required because it is an | ||
| 231 | # inherited attribute on a different namespace. | ||
| 232 | my $class_isa = $class . "::ISA"; | ||
| 233 | my @class_parents = @$class_isa; | ||
| 234 | my $requires_namespace = 0; | ||
| 235 | foreach my $parent (@class_parents) { | ||
| 236 | my %parent_elements = | ||
| 237 | map { $_ => 1 } @{$ELEMENTS_FROM->{$parent}}; | ||
| 238 | my $parent_has_element = exists($parent_elements{$name}); | ||
| 239 | |||
| 240 | if ($parent_has_element) { | ||
| 241 | my $parent_xns; | ||
| 242 | eval "\$parent_xns = " . $parent. "::get_xmlns()"; | ||
| 243 | if ($parent_xns ne $option_ref->{xmlns_stack}->[-1]) { | ||
| 244 | $requires_namespace = 1; | ||
| 245 | } | ||
| 246 | } | ||
| 247 | } | ||
| 248 | |||
| 249 | if ($requires_namespace) { | ||
| 250 | join q{}, $_->start_tag({name => $name, | ||
| 251 | xmlns => $_->get_xmlns(), | ||
| 252 | %{$option_ref}}), | ||
| 253 | $_->serialize($option_ref), | ||
| 254 | $_->end_tag({name => $name , %{$option_ref}}); | ||
| 255 | } else { | ||
| 256 | join q{}, $_->start_tag({name => $name, %{$option_ref}}), | ||
| 257 | $_->serialize($option_ref), | ||
| 258 | $_->end_tag({name => $name , %{$option_ref}}); | ||
| 259 | } | ||
| 260 | # END PATCH | ||
| 261 | } | ||
| 262 | } else { | ||
| 263 | my $set_xmlns = delete $option_ref->{xmlns}; | ||
| 264 | |||
| 265 | join q{}, | ||
| 266 | $_->start_tag({ | ||
| 267 | name => $name, | ||
| 268 | %{$option_ref}, | ||
| 269 | (!defined $set_xmlns)?(xmlns => ""):() | ||
| 270 | }), | ||
| 271 | $_->serialize({%{$option_ref}, xmlns => ""}), | ||
| 272 | $_->end_tag({name => $name , %{$option_ref}}); | ||
| 273 | } | ||
| 274 | } | ||
| 275 | } @{$element} | ||
| 276 | } else { | ||
| 277 | q{}; | ||
| 278 | } | ||
| 279 | } (@{$ELEMENTS_FROM->{$class}}); | ||
| 280 | }; | ||
| 281 | |||
| 282 | if (!$class->isa('SOAP::WSDL::XSD::Typelib::AttributeSet')) { | ||
| 283 | *{"$class\::serialize"} = | ||
| 284 | \&SOAP::WSDL::XSD::Typelib::ComplexType::__serialize_complex; | ||
| 285 | }; | ||
| 286 | } | ||
| 287 | |||
| 288 | # Added to support hash to object serialization. | ||
| 289 | # A special xsi_type attribute name has been reserved to specify subtype of | ||
| 290 | # the object been passed when using hashes. | ||
| 291 | # PATCH This entire method was added to the class. | ||
| 292 | sub _hash_to_object { | ||
| 293 | my ($type, $hash) = @_; | ||
| 294 | |||
| 295 | if ($hash->{"xsi_type"}) { | ||
| 296 | my $base_type = $type; | ||
| 297 | my $xsi_type = $hash->{"xsi_type"}; | ||
| 298 | $type = substr($type, 0, rindex($type, "::") + 2) . $xsi_type; | ||
| 299 | eval("require $type"); | ||
| 300 | die croak "xsi_type $xsi_type not found" if $@; | ||
| 301 | my $instance = $type->new($hash); | ||
| 302 | die croak "xsi_type $xsi_type must inherit from " . "$type" | ||
| 303 | if not $instance->isa($base_type); | ||
| 304 | return $instance; | ||
| 305 | } else { | ||
| 306 | return $type->new($hash); | ||
| 307 | } | ||
| 308 | } | ||
| 309 | # END PATCH | ||
| 310 | |||
| 311 | # Redefining as_hash_ref method to correctly map all object properties to a | ||
| 312 | # hash structure. | ||
| 313 | sub as_hash_ref { | ||
| 314 | my $self = $_[0]; | ||
| 315 | my $attributes_ref = $self->__get_object_attributes($self); | ||
| 316 | |||
| 317 | my $hash_of_ref = {}; | ||
| 318 | if ($_[0]->isa('SOAP::WSDL::XSD::Typelib::Builtin::anySimpleType')) { | ||
| 319 | $hash_of_ref->{value} = $_[0]->get_value(); | ||
| 320 | } else { | ||
| 321 | foreach my $attribute (keys %{$attributes_ref}) { | ||
| 322 | next if not defined $attributes_ref->{$attribute}->{${$_[0]}}; | ||
| 323 | my $value = $attributes_ref->{$attribute}->{${$_[0]}}; | ||
| 324 | # PATCH normalizing the attribute name | ||
| 325 | $attribute =~ s/__/./g; | ||
| 326 | # END PATCH | ||
| 327 | $hash_of_ref->{$attribute} = blessed $value | ||
| 328 | ? $value->isa('SOAP::WSDL::XSD::Typelib::Builtin::anySimpleType') | ||
| 329 | ? $value->get_value() | ||
| 330 | # PATCH returning the value no need to recurse | ||
| 331 | : $value | ||
| 332 | # END PATCH | ||
| 333 | : ref $value eq 'ARRAY' | ||
| 334 | ? [map { | ||
| 335 | $_->isa('SOAP::WSDL::XSD::Typelib::Builtin::anySimpleType') | ||
| 336 | ? $_->get_value() | ||
| 337 | # PATCH returning the object no need to recurse | ||
| 338 | : $_ | ||
| 339 | # END PATCH | ||
| 340 | } @{$value}] | ||
| 341 | : die "Neither blessed obj nor list ref"; | ||
| 342 | }; | ||
| 343 | } | ||
| 344 | |||
| 345 | no warnings "once"; | ||
| 346 | return $hash_of_ref if $_[1] or $AS_HASH_REF_WITHOUT_ATTRIBUTES; | ||
| 347 | |||
| 348 | if (exists $xml_attr_of{${$_[0]}}) { | ||
| 349 | $hash_of_ref->{xmlattr} = $xml_attr_of{${$_[0]}}->as_hash_ref(); | ||
| 350 | } | ||
| 351 | |||
| 352 | return $hash_of_ref; | ||
| 353 | } | ||
| 354 | |||
| 355 | # PATCH To retrieve object attributes mapping including inherited. | ||
| 356 | sub __get_object_attributes { | ||
| 357 | my $self = shift; | ||
| 358 | my $object = shift; | ||
| 359 | my @types = (ref $object); | ||
| 360 | my %attributes; | ||
| 361 | |||
| 362 | while (my $type = pop(@types)) { | ||
| 363 | eval("require $type"); | ||
| 364 | my $type_bases_name = $type . "::ISA"; | ||
| 365 | push @types, @$type_bases_name; | ||
| 366 | my $attributes_ref = $ATTRIBUTES_OF->{$type}; | ||
| 367 | for my $key (keys %$attributes_ref) { | ||
| 368 | my $value = $attributes_ref->{$key}; | ||
| 369 | if (not exists $attributes{$key}) { | ||
| 370 | $attributes{$key} = $value; | ||
| 371 | } | ||
| 372 | } | ||
| 373 | } | ||
| 374 | return \%attributes; | ||
| 375 | } | ||
| 376 | # END PATCH | ||
| 377 | |||
| 378 | # PATCH To retrieve attributes xml names including inherited. | ||
| 379 | sub __get_object_names { | ||
| 380 | my $object = $_[1]; | ||
| 381 | my @types = (ref $object); | ||
| 382 | my %names; | ||
| 383 | |||
| 384 | while (my $type = pop(@types)) { | ||
| 385 | eval("require $type"); | ||
| 386 | my $type_bases_name = $type . "::ISA"; | ||
| 387 | push @types, @$type_bases_name; | ||
| 388 | my $names_ref = $NAMES_OF{$type}; | ||
| 389 | for my $key (keys %$names_ref) { | ||
| 390 | my $value = $names_ref->{$key}; | ||
| 391 | if (not exists $names{$key}) { | ||
| 392 | $names{$key} = $value; | ||
| 393 | } | ||
| 394 | } | ||
| 395 | } | ||
| 396 | return \%names; | ||
| 397 | } | ||
| 398 | # END PATCH | ||
| 399 | |||
| 400 | # PATCH Method for the client to find objects in the tree based on an a partial | ||
| 401 | # support of XPath expressions. | ||
| 402 | sub find { | ||
| 403 | my ($self, $xpath_expr) = @_; | ||
| 404 | |||
| 405 | my $parser_node = | ||
| 406 | Google::Ads::Common::XPathSAXParser::get_node_from_object($self); | ||
| 407 | |||
| 408 | my @return_list = (); | ||
| 409 | if (defined $parser_node) { | ||
| 410 | my $node_set = $parser_node->find($xpath_expr); | ||
| 411 | foreach my $node ($node_set->get_nodelist()) { | ||
| 412 | my $soap_object = | ||
| 413 | Google::Ads::Common::XPathSAXParser::get_object_from_node($node); | ||
| 414 | if (defined $soap_object) { | ||
| 415 | push @return_list, $soap_object; | ||
| 416 | } | ||
| 417 | } | ||
| 418 | } | ||
| 419 | |||
| 420 | return \@return_list; | ||
| 421 | } | ||
| 422 | # END PATCH | ||
| 423 | |||
| 424 | # PATCH Setting an alias of find -> valueof for backwards compatibility with | ||
| 425 | # the old version of the client library. | ||
| 426 | no warnings "once"; | ||
| 427 | *Google::Ads::SOAP::ComplexType::valueof = | ||
| 428 | \&Google::Ads::SOAP::ComplexType::find; | ||
| 429 | # END PATCH | ||
| 430 | |||
| 431 | # PATCH Overloading hash casting routine for ComplexType, so all complex types | ||
| 432 | # can behave as hashes. | ||
| 433 | use overload ( | ||
| 434 | '%{}' => 'as_hash_ref', | ||
| 435 | fallback => 1, | ||
| 436 | ); | ||
| 437 | # END PATCH |