| 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 visitor for generation of typemaps based on | ||
| 16 | # SOAP::WSDL::Generator::Visitor::Typemap, with some overriden methods via | ||
| 17 | # inheritance. | ||
| 18 | |||
| 19 | package Google::Ads::SOAP::Generator::TypemapVisitor; | ||
| 20 | |||
| 21 | use base qw(SOAP::WSDL::Generator::Visitor::Typemap); | ||
| 22 | |||
| 23 | use Class::Std::Fast::Storable; | ||
| 24 | |||
| 25 | sub set_typemap_entry { | ||
| 26 | my ($self, $value) = @_; | ||
| 27 | my $path = join(q{/}, @{$self->get_path()}); | ||
| 28 | my $tm = $self->get_typemap(); | ||
| 29 | if ($tm->{$path} && $path =~ m/Fault\/detail\/ApiExceptionFault/) { | ||
| 30 | return; | ||
| 31 | } | ||
| 32 | $tm->{$path} = $value; | ||
| 33 | } | ||
| 34 | |||
| 35 | sub visit_XSD_Element { | ||
| 36 | my ( $self, $ident, $element ) = ( $_[0], ident $_[0], $_[1] ); | ||
| 37 | |||
| 38 | my @path = @{ $self->get_path() }; | ||
| 39 | my $path = join '/', @path; | ||
| 40 | my $parent = $self->get_typemap()->{ $path }; | ||
| 41 | |||
| 42 | # PATCH breaking cycles | ||
| 43 | if (scalar(@path) > 30) { | ||
| 44 | return; | ||
| 45 | } | ||
| 46 | # END PATCH | ||
| 47 | |||
| 48 | $self->SUPER::visit_XSD_Element($_[1]); | ||
| 49 | } | ||
| 50 | |||
| 51 | sub visit_XSD_ComplexType { | ||
| 52 | my ($self, $ident, $type) = ($_[0], ident $_[0], $_[1]); | ||
| 53 | |||
| 54 | my $variety = $type->get_variety(); | ||
| 55 | my $derivation = $type->get_derivation(); | ||
| 56 | my $content_model = $type->get_contentModel(); | ||
| 57 | |||
| 58 | return if not $variety or ($content_model eq "simpleContent"); | ||
| 59 | if (grep {$_ eq $variety} qw(all sequence choice)) { | ||
| 60 | # Recursively going to visit child element since the type variety is | ||
| 61 | # either all sequence choice. | ||
| 62 | if (my $type_name = $type->get_base()) { | ||
| 63 | my $subtype = $self->get_definitions()->first_types()->find_type( | ||
| 64 | $type->expand($type_name)); | ||
| 65 | for (@{$subtype->get_element() || []}) { | ||
| 66 | $_->_accept($self); | ||
| 67 | } | ||
| 68 | } | ||
| 69 | |||
| 70 | for (@{$type->get_element() || []}) { | ||
| 71 | $_->_accept($self); | ||
| 72 | } | ||
| 73 | } | ||
| 74 | |||
| 75 | # PATCH - We need to also check if the complex type has derivations and | ||
| 76 | # include type path for all the types that derived from it. | ||
| 77 | my $last_path_elem = pop(@{$self->get_path()}); | ||
| 78 | my $def_types = $self->get_definitions()->first_types(); | ||
| 79 | my $schema = @{$def_types->get_schema()}[1]; | ||
| 80 | my @types = @{$schema->get_type()}; | ||
| 81 | my $base_type = $type->get_name(); | ||
| 82 | |||
| 83 | if (@{$self->get_path()}[0] && | ||
| 84 | @{$self->get_path()}[0] eq "ApiExceptionFault") { | ||
| 85 | @{$self->get_path()}[0] = "Fault/detail/ApiExceptionFault"; | ||
| 86 | } | ||
| 87 | |||
| 88 | if (defined $base_type) { | ||
| 89 | my $schemas = | ||
| 90 | @{$self->get_definitions()->get_types()}[0]->get_schema; | ||
| 91 | SCHEMA: foreach my $my_schema (@{$schemas}) { | ||
| 92 | next SCHEMA if ($my_schema->isa("SOAP::WSDL::XSD::Schema::Builtin")); | ||
| 93 | my @types = @{$my_schema->get_type()}; | ||
| 94 | TYPE: foreach my $type (@types) { | ||
| 95 | if ($type->isa("SOAP::WSDL::XSD::ComplexType")) { | ||
| 96 | my $type_name = $type->get_name(); | ||
| 97 | my $base = $type->get_base(); | ||
| 98 | next TYPE if !$base; | ||
| 99 | $base =~ s{ .*: }{}xms; | ||
| 100 | if ($base eq $base_type) { | ||
| 101 | # Checking for infinite cycles if the type has already been mapped | ||
| 102 | # before we skip to the next one. | ||
| 103 | foreach my $path_elem (@{$self->get_path()}) { | ||
| 104 | next TYPE if $path_elem eq $last_path_elem . "[$type_name]"; | ||
| 105 | } | ||
| 106 | |||
| 107 | # In this case we generate a new path that includes the type name | ||
| 108 | # E.G. /elem1/elem2[type] | ||
| 109 | if ($last_path_elem =~ m/\[[^\]]+\]/) { | ||
| 110 | $last_path_elem =~ s/\[[^\]]+\]/[${type_name}]/; | ||
| 111 | push(@{$self->get_path()}, $last_path_elem); | ||
| 112 | } else{ | ||
| 113 | push(@{$self->get_path()}, $last_path_elem . "[$type_name]"); | ||
| 114 | } | ||
| 115 | my $typeclass = $self->get_resolver()->create_xsd_name($type); | ||
| 116 | |||
| 117 | # Setting current typemap class before to allow it to be used from | ||
| 118 | # inside _accept. | ||
| 119 | $self->set_typemap_entry($typeclass); | ||
| 120 | $type->_accept($self); | ||
| 121 | |||
| 122 | # Setting it afterwards again since accept could have touch it. | ||
| 123 | $self->set_typemap_entry($typeclass); | ||
| 124 | pop(@{$self->get_path()}); | ||
| 125 | } | ||
| 126 | } | ||
| 127 | } | ||
| 128 | } | ||
| 129 | } | ||
| 130 | push(@{$self->get_path()}, $last_path_elem); | ||
| 131 | # END OF PATCH. | ||
| 132 | |||
| 133 | return if (!$derivation); | ||
| 134 | |||
| 135 | if ($derivation eq "restriction") { | ||
| 136 | # Resolving the base, getting atomic type and runnning on elements. | ||
| 137 | if (my $type_name = $type->get_base()) { | ||
| 138 | my $subtype = $self->get_definitions()->first_types()->find_type( | ||
| 139 | $type->expand($type_name)); | ||
| 140 | for (@{$subtype->get_element() || []}) { | ||
| 141 | $_->_accept($self); | ||
| 142 | } | ||
| 143 | } | ||
| 144 | } elsif ($derivation eq "extension") { | ||
| 145 | # Resolving the base, getting atomic type and runnning on elements. | ||
| 146 | while (my $type_name = $type->get_base()) { | ||
| 147 | $type = $self->get_definitions()->first_types()->find_type( | ||
| 148 | $type->expand($type_name)); | ||
| 149 | for (@{$type->get_element() || []}) { | ||
| 150 | $_->_accept($self); | ||
| 151 | } | ||
| 152 | } | ||
| 153 | } | ||
| 154 | } | ||
| 155 | |||
| 156 | return 1; |