« blib/lib/SOAP/WSDL/XSD/ComplexType.pm
↑ Summary
blib/lib/SOAP/WSDL/Generator/Template/Plugin/XSD.pm »
| 1 | package SOAP::WSDL::Generator::Visitor::Typemap; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | use Class::Std::Fast::Storable; | ||
| 5 | |||
| 6 | use base qw(SOAP::WSDL::Generator::Visitor); | ||
| 7 | |||
| 8 | our $VERSION = 3.004; | ||
| 9 | |||
| 10 | my %path_of :ATTR(:name<path> :default<[]>); | ||
| 11 | my %typemap_of :ATTR(:name<typemap> :default<()>); | ||
| 12 | my %resolver_of :ATTR(:name<resolver> :default<()>); | ||
| 13 | |||
| 14 | sub START { | ||
| 15 | my ($self, $ident, $arg_ref) = @_; | ||
| 16 | $resolver_of { $ident } = $arg_ref->{ resolver }; | ||
| 17 | } | ||
| 18 | |||
| 19 | sub set_typemap_entry { | ||
| 20 | my ($self, $value) = @_; | ||
| 21 | # warn join( q{/}, @{ $path_of{ ident $self } }) . " => $value"; | ||
| 22 | $typemap_of{ ident $self }->{ | ||
| 23 | join( q{/}, @{ $path_of{ ident $self } } ) | ||
| 24 | } = $value; | ||
| 25 | } | ||
| 26 | |||
| 27 | sub add_element_path { | ||
| 28 | my ($self, $element) = @_; | ||
| 29 | |||
| 30 | # Swapping out this lines against the ones below generates | ||
| 31 | # a namespace-sensitive typemap. | ||
| 32 | # Well almost: Class names are not constructed in a namespace-sensitive | ||
| 33 | # manner, yet - there should be some facility to allow binding a (perl) | ||
| 34 | # prefix to a namespace... | ||
| 35 | |||
| 36 | if (my $ref = $element->get_ref() ) { | ||
| 37 | $element = $self->get_definitions()->first_types()->find_element( | ||
| 38 | $element->expand($ref) ); | ||
| 39 | } | ||
| 40 | my $name = $element->get_name(); | ||
| 41 | |||
| 42 | push @{ $path_of{ ident $self } }, $name; | ||
| 43 | } | ||
| 44 | |||
| 45 | sub process_referenced_type { | ||
| 46 | my ( $self, $ns, $localname ) = @_; | ||
| 47 | |||
| 48 | my $ident = ident $self; | ||
| 49 | |||
| 50 | # get type's class name | ||
| 51 | # Caveat: visits type if it's a referenced type from the | ||
| 52 | # a ? b : c operation. | ||
| 53 | my ($type, $typeclass); | ||
| 54 | $type = $self->get_definitions()->first_types()->find_type( $ns, $localname ); | ||
| 55 | $typeclass = $self->get_resolver()->create_xsd_name($type); | ||
| 56 | |||
| 57 | # set before to allow it to be used from inside _accept | ||
| 58 | $self->set_typemap_entry($typeclass); | ||
| 59 | |||
| 60 | $type->_accept($self) if ($ns ne 'http://www.w3.org/2001/XMLSchema'); | ||
| 61 | |||
| 62 | # set afterwards again (just to be sure...) | ||
| 63 | $self->set_typemap_entry($typeclass); | ||
| 64 | return $self; | ||
| 65 | } | ||
| 66 | |||
| 67 | sub visit_XSD_Element { | ||
| 68 | my ( $self, $ident, $element ) = ( $_[0], ident $_[0], $_[1] ); | ||
| 69 | |||
| 70 | # warn "simpleType " . $element->get_name(); | ||
| 71 | my @path = @{ $path_of{ ${ $self } } }; | ||
| 72 | my $path = join '/', @path; | ||
| 73 | my $parent = $typemap_of{ ${ $self } }->{ $path }; | ||
| 74 | |||
| 75 | # step down in tree | ||
| 76 | $self->add_element_path( $element ); | ||
| 77 | |||
| 78 | # now call all possible variants. | ||
| 79 | # They all just return if no argument is given, | ||
| 80 | # and return $self on success. | ||
| 81 | SWITCH: { | ||
| 82 | my $name = $element->get_name(); | ||
| 83 | |||
| 84 | if ($element->get_type) { | ||
| 85 | $self->process_referenced_type( $element->expand( $element->get_type() ) ); | ||
| 86 | last SWITCH; | ||
| 87 | } | ||
| 88 | |||
| 89 | # atomic simpleType typemap rule: | ||
| 90 | # if we have a parent, use parent's sub-package. | ||
| 91 | # if not, use element package. | ||
| 92 | if ($element->get_simpleType()) { | ||
| 93 | # warn "simpleType " . $element->get_name(); | ||
| 94 | my @path = @{ $path_of{ ${ $self } } }; | ||
| 95 | my $typeclass = $self->get_resolver()->create_subpackage_name($element); | ||
| 96 | $self->set_typemap_entry($typeclass); | ||
| 97 | $typeclass =~s{\.}{::}g; | ||
| 98 | $typeclass =~s{\-}{_}g; | ||
| 99 | last SWITCH; | ||
| 100 | } | ||
| 101 | |||
| 102 | # for atomic and complex types , and ref elements | ||
| 103 | my $typeclass = $self->get_resolver()->create_subpackage_name($element); | ||
| 104 | $self->set_typemap_entry($typeclass); | ||
| 105 | |||
| 106 | if (my $complexType = $element->first_complexType()) { | ||
| 107 | $complexType->_accept($self); | ||
| 108 | last SWITCH; | ||
| 109 | } | ||
| 110 | |||
| 111 | # element ref handling | ||
| 112 | if (my $ref = $element->get_ref()) { | ||
| 113 | $element = $self->get_definitions()->first_types()->find_element( | ||
| 114 | $element->expand($ref) ); | ||
| 115 | # we added a path too much - we should add the path of this | ||
| 116 | # element instead. | ||
| 117 | pop @{ $path_of{$ident} }; | ||
| 118 | $element->_accept($self); | ||
| 119 | # and we must not pop it off now - thus, just return | ||
| 120 | return; | ||
| 121 | } | ||
| 122 | die "Neither type nor ref in element >". $element->get_name ."<. Don't know what to do." | ||
| 123 | }; | ||
| 124 | |||
| 125 | # Safety measure. If someone defines a top-level element with | ||
| 126 | # a normal (not atomic) type, we just override it here | ||
| 127 | if (not defined($parent)) { | ||
| 128 | # for atomic and complex types , and ref elements | ||
| 129 | my $typeclass = $self->get_resolver()->create_xsd_name($element); | ||
| 130 | $self->set_typemap_entry($typeclass); | ||
| 131 | } | ||
| 132 | |||
| 133 | # step up in hierarchy | ||
| 134 | pop @{ $path_of{$ident} }; | ||
| 135 | } | ||
| 136 | |||
| 137 | sub visit_XSD_ComplexType { | ||
| 138 | my ($self, $ident, $type) = ($_[0], ident $_[0], $_[1] ); | ||
| 139 | my $variety = $type->get_variety(); | ||
| 140 | my $derivation = $type->get_derivation(); | ||
| 141 | my $content_model = $type->get_contentModel; | ||
| 142 | return if not $variety; # empty complexType | ||
| 143 | return if ($content_model eq 'simpleContent'); | ||
| 144 | |||
| 145 | if ( grep { $_ eq $variety} qw(all sequence choice) ) | ||
| 146 | { | ||
| 147 | # visit child elements | ||
| 148 | for (@{ $type->get_element() || [] }) { | ||
| 149 | $_->_accept( $self ); | ||
| 150 | } | ||
| 151 | } | ||
| 152 | # Only continue for derived types | ||
| 153 | # Saves a uninitialized warning. | ||
| 154 | return if not $derivation; | ||
| 155 | |||
| 156 | if ($derivation eq 'restriction' ) { | ||
| 157 | # TODO check and probably correct - this includes | ||
| 158 | # all base type's elements in a restriction derivation. | ||
| 159 | # Probably wrong. | ||
| 160 | # | ||
| 161 | # resolve base / get atomic type and run on elements | ||
| 162 | if (my $type_name = $type->get_base()) { | ||
| 163 | my $subtype = $self->get_definitions() | ||
| 164 | ->first_types()->find_type( $type->expand($type_name) ); | ||
| 165 | # visit child elements | ||
| 166 | for (@{ $subtype->get_element() || [] }) { | ||
| 167 | $_->_accept( $self ); | ||
| 168 | } | ||
| 169 | } | ||
| 170 | } | ||
| 171 | elsif ($derivation eq 'extension' ) { | ||
| 172 | # resolve base / get atomic type and run on elements | ||
| 173 | while (my $type_name = $type->get_base()) { | ||
| 174 | $type = $self->get_definitions() | ||
| 175 | ->first_types()->find_type( $type->expand($type_name) ); | ||
| 176 | # visit child elements | ||
| 177 | for (@{ $type->get_element() || [] }) { | ||
| 178 | $_->_accept( $self ); | ||
| 179 | } | ||
| 180 | } | ||
| 181 | } | ||
| 182 | } | ||
| 183 | |||
| 184 | 1; | ||
| 185 | |||
| 186 | __END__ |