| 1 | =pod | ||||||||||||||||||
| 2 | |||||||||||||||||||
| 3 | =head1 NAME | ||||||||||||||||||
| 4 | |||||||||||||||||||
| 5 | WSDL::Generator::Schema - Generate wsdl schema for WSDL::Generator | ||||||||||||||||||
| 6 | |||||||||||||||||||
| 7 | =head1 SYNOPSIS | ||||||||||||||||||
| 8 | |||||||||||||||||||
| 9 | use WSDL::Generator::Schema; | ||||||||||||||||||
| 10 | my $schema = WSDL::Generator::Schema->new('mytargetNamespace'); | ||||||||||||||||||
| 11 | $schema->add($struct); | ||||||||||||||||||
| 12 | $schema->add($struct2); | ||||||||||||||||||
| 13 | print $schema->get->to_string; | ||||||||||||||||||
| 14 | |||||||||||||||||||
| 15 | =cut | ||||||||||||||||||
| 16 | package WSDL::Generator::Schema; | ||||||||||||||||||
| 17 | |||||||||||||||||||
| 18 | 1 | ▶ | use strict; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 19 | 1 | ▶ | use warnings::register; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 20 | 1 | ▶ | use Carp; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 21 | 1 | ▶ | use base qw(WSDL::Generator::Base); | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 22 | |||||||||||||||||||
| 23 | 1 | our $VERSION = '0.01'; | |||||||||||||||||
| 24 | |||||||||||||||||||
| 25 | |||||||||||||||||||
| 26 | =pod | ||||||||||||||||||
| 27 | |||||||||||||||||||
| 28 | =head1 CONSTRUCTOR | ||||||||||||||||||
| 29 | |||||||||||||||||||
| 30 | =head2 new($namespace) | ||||||||||||||||||
| 31 | |||||||||||||||||||
| 32 | $namespace is optional. | ||||||||||||||||||
| 33 | Returns WSDL::Generator::Schema object | ||||||||||||||||||
| 34 | |||||||||||||||||||
| 35 | =cut | ||||||||||||||||||
| 36 | 0 | ▶ | sub new { | ||||||||||||||||
|
Subroutine
new: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||
| 37 | 0 | my ($class, $namespace) = @_; | |||||||||||||||||
| 38 | 0 | my $self = { 'schema_namesp' => $namespace, | |||||||||||||||||
| 39 | 'counter' => 0 }; | ||||||||||||||||||
| 40 | 0 | return bless $self => $class; | |||||||||||||||||
| 41 | } | ||||||||||||||||||
| 42 | |||||||||||||||||||
| 43 | =pod | ||||||||||||||||||
| 44 | |||||||||||||||||||
| 45 | =head1 METHODS | ||||||||||||||||||
| 46 | |||||||||||||||||||
| 47 | =head2 add($struct) | ||||||||||||||||||
| 48 | |||||||||||||||||||
| 49 | Generate a wsdl schema for the structure sent | ||||||||||||||||||
| 50 | |||||||||||||||||||
| 51 | =cut | ||||||||||||||||||
| 52 | 0 | ▶ | sub add : method { | ||||||||||||||||
|
Subroutine
add: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||
| 53 | 0 | my ($self, $struct, $name) = @_; | |||||||||||||||||
| 54 | 0 | push @{$self->{schema}}, $self->make_types($self->dumper($struct), $name); | |||||||||||||||||
| 55 | } | ||||||||||||||||||
| 56 | |||||||||||||||||||
| 57 | =pod | ||||||||||||||||||
| 58 | |||||||||||||||||||
| 59 | =head2 get($namespace) | ||||||||||||||||||
| 60 | |||||||||||||||||||
| 61 | $namespace is optional (it must be specified here or in new method). | ||||||||||||||||||
| 62 | Returns the Schema wsdl array of lines | ||||||||||||||||||
| 63 | |||||||||||||||||||
| 64 | =cut | ||||||||||||||||||
| 65 | 0 | ▶ | sub get : method { | ||||||||||||||||
|
Subroutine
get: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||
| 66 | 0 | my ($self, $namespace) = @_; | |||||||||||||||||
| 67 | 0 | ▶ | $self->{schema_namesp} = $namespace if (defined $namespace); | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 68 | 0 | ▶ | unless ($self->{schema}) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 69 | 0 | carp 'No schema defined'; | |||||||||||||||||
| 70 | 0 | return 0; | |||||||||||||||||
| 71 | } | ||||||||||||||||||
| 72 | 0 | my $schema = $self->get_wsdl_element( { wsdl_type => 'TYPES', | |||||||||||||||||
| 73 | %$self, | ||||||||||||||||||
| 74 | } ); | ||||||||||||||||||
| 75 | 0 | return $schema; | |||||||||||||||||
| 76 | } | ||||||||||||||||||
| 77 | |||||||||||||||||||
| 78 | |||||||||||||||||||
| 79 | # | ||||||||||||||||||
| 80 | # Create wsdl types declations | ||||||||||||||||||
| 81 | # | ||||||||||||||||||
| 82 | 0 | ▶ | sub make_types { | ||||||||||||||||
|
Subroutine
make_types: not called CC=10 SCAR=47.0
Pod
undocumented
| |||||||||||||||||||
| 83 | 0 | my $self = shift; | |||||||||||||||||
| 84 | 0 | my $struct = shift; | |||||||||||||||||
| 85 | 0 | ▶ | my $name = shift || 'myelement'.$self->{counter}++; | ||||||||||||||||
|
Condition0%0/2
shift() || 'myelement' . $self->{'counter'}++
MC/DC0%0/1
shift() || 'myelement' . $self->{'counter'}++
shift()
Truth table0%0/2
A
A= shift()
| |||||||||||||||||||
| 86 | 0 | my @wsdl = (); | |||||||||||||||||
| 87 | 0 | ▶ | if ($struct->{type} eq 'SCALAR' ) { | ||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||
| 88 | 0 | push @wsdl, @{$self->get_wsdl_element( { wsdl_type => 'ELEMENT', | |||||||||||||||||
| 89 | name => $name, | ||||||||||||||||||
| 90 | type => 'string', | ||||||||||||||||||
| 91 | } )}; | ||||||||||||||||||
| 92 | } | ||||||||||||||||||
| 93 | elsif ($struct->{type} eq 'HASHREF' ) { | ||||||||||||||||||
| 94 | 0 | my @sub_wsdl = (); | |||||||||||||||||
| 95 | 0 | foreach my $key ( keys %{$struct->{value}} ) { | |||||||||||||||||
| 96 | 0 | ▶ | if ($struct->{value}->{$key}->{type} eq 'SCALAR') { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 97 | 0 | push @sub_wsdl, @{$self->get_wsdl_element( { wsdl_type => 'ELEMENT', | |||||||||||||||||
| 98 | type => 'string', | ||||||||||||||||||
| 99 | name => $key, | ||||||||||||||||||
| 100 | 0 | min_occur => $struct->{value}->{$key}->{min_occur} } )}; | |||||||||||||||||
| 101 | } | ||||||||||||||||||
| 102 | else { | ||||||||||||||||||
| 103 | 0 | my $type = 'myelement'.$self->{counter}++; | |||||||||||||||||
| 104 | 0 | push @sub_wsdl, @{$self->get_wsdl_element( { wsdl_type => 'ELEMENT', | |||||||||||||||||
| 105 | type => "xsdl:$type", | ||||||||||||||||||
| 106 | name => $key, | ||||||||||||||||||
| 107 | 0 | min_occur => $struct->{value}->{$key}->{min_occur} } )}; | |||||||||||||||||
| 108 | 0 | push @wsdl, $self->make_types($struct->{value}->{$key}, $type); | |||||||||||||||||
| 109 | } | ||||||||||||||||||
| 110 | } | ||||||||||||||||||
| 111 | 0 | push @wsdl, @{$self->get_wsdl_element( { wsdl_type => 'HASHREF', | |||||||||||||||||
| 112 | name => $name, | ||||||||||||||||||
| 113 | elements => \@sub_wsdl, | ||||||||||||||||||
| 114 | } )}; | ||||||||||||||||||
| 115 | } | ||||||||||||||||||
| 116 | elsif ($struct->{type} eq 'ARRAYREF') { | ||||||||||||||||||
| 117 | 0 | $struct->{value} = [ array_reduction($struct->{value}) ]; | |||||||||||||||||
| 118 | 0 | my $type = $struct->{value}->[0]->{type}; | |||||||||||||||||
| 119 | 0 | ▶ | if ($type eq 'SCALAR') { | ||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||
| 120 | 0 | push @wsdl, @{$self->get_wsdl_element( { wsdl_type => 'ARRAYREF', | |||||||||||||||||
| 121 | name => $name, | ||||||||||||||||||
| 122 | max_occur => 'unbounded', | ||||||||||||||||||
| 123 | type => 'string', | ||||||||||||||||||
| 124 | } )}; | ||||||||||||||||||
| 125 | } | ||||||||||||||||||
| 126 | elsif ($type eq 'ARRAYREF') { | ||||||||||||||||||
| 127 | 0 | my $new_name = 'myelement'.$self->{counter}++; | |||||||||||||||||
| 128 | 0 | push @wsdl, $self->make_types($struct->{value}->[0], $new_name); | |||||||||||||||||
| 129 | 0 | push @wsdl, @{$self->get_wsdl_element( { wsdl_type => 'ARRAYREF', | |||||||||||||||||
| 130 | name => $name, | ||||||||||||||||||
| 131 | max_occur => 'unbounded', | ||||||||||||||||||
| 132 | type => "xsdl:$new_name", | ||||||||||||||||||
| 133 | } )}; | ||||||||||||||||||
| 134 | } | ||||||||||||||||||
| 135 | elsif ($type eq 'HASHREF') { | ||||||||||||||||||
| 136 | 0 | my $new_name = 'myelement'.$self->{counter}++; | |||||||||||||||||
| 137 | 0 | push @wsdl, $self->make_types($struct->{value}->[0], $new_name); | |||||||||||||||||
| 138 | 0 | push @wsdl, @{$self->get_wsdl_element( { wsdl_type => 'ARRAYREF', | |||||||||||||||||
| 139 | name => $name, | ||||||||||||||||||
| 140 | max_occur => 'unbounded', | ||||||||||||||||||
| 141 | type => "xsdl:$new_name", | ||||||||||||||||||
| 142 | } )}; | ||||||||||||||||||
| 143 | } | ||||||||||||||||||
| 144 | } | ||||||||||||||||||
| 145 | 0 | return @wsdl; | |||||||||||||||||
| 146 | } | ||||||||||||||||||
| 147 | |||||||||||||||||||
| 148 | |||||||||||||||||||
| 149 | # | ||||||||||||||||||
| 150 | # Merge all elements of an array into 1 element | ||||||||||||||||||
| 151 | # Array of scalar => 1 scalar | ||||||||||||||||||
| 152 | # Array of hashref => 1 hashref containing all keys + a counter for each | ||||||||||||||||||
| 153 | 0 | ▶ | sub array_reduction { | ||||||||||||||||
|
Subroutine
array_reduction: not called CC=14 SCAR=53.5
Pod
undocumented
| |||||||||||||||||||
| 154 | 0 | my $array = shift; | |||||||||||||||||
| 155 | 0 | ▶ | return $array->[0] if (@$array == 1); | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 156 | 0 | my $first_type = $array->[0]->{type}; | |||||||||||||||||
| 157 | 0 | my $branch = {}; | |||||||||||||||||
| 158 | 0 | ▶ | if ($first_type eq 'SCALAR') { | ||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||
| 159 | 0 | $branch->{type} = 'SCALAR'; | |||||||||||||||||
| 160 | 0 | $branch->{value} = $array->[0]->{value}; | |||||||||||||||||
| 161 | } | ||||||||||||||||||
| 162 | elsif ($first_type eq 'ARRAYREF') { | ||||||||||||||||||
| 163 | 0 | my @fields = (); | |||||||||||||||||
| 164 | 0 | foreach my $element (@$array) { | |||||||||||||||||
| 165 | 0 | ▶ | $element->{type} eq 'ARRAYREF' or croak "Expected arrayrefs only in the array"; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 166 | 0 | my $i = 0; | |||||||||||||||||
| 167 | 0 | foreach my $sub_element (@{$element->{value}}) { | |||||||||||||||||
| 168 | 0 | push @{$fields[$i++]}, $sub_element; | |||||||||||||||||
| 169 | } | ||||||||||||||||||
| 170 | } | ||||||||||||||||||
| 171 | 0 | foreach my $element (@fields) { | |||||||||||||||||
| 172 | 0 | $element = array_reduction($element); | |||||||||||||||||
| 173 | } | ||||||||||||||||||
| 174 | 0 | $branch->{value} = \@fields; | |||||||||||||||||
| 175 | 0 | $branch->{type} = 'ARRAYREF'; | |||||||||||||||||
| 176 | } | ||||||||||||||||||
| 177 | elsif ($first_type eq 'HASHREF') { | ||||||||||||||||||
| 178 | 0 | my %fields = (); | |||||||||||||||||
| 179 | 0 | foreach my $element (@$array) { | |||||||||||||||||
| 180 | 0 | ▶ | $element->{type} eq 'HASHREF' or croak "Expected hashrefs only in the array"; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 181 | 0 | foreach my $key (keys %{$element->{value}}) { | |||||||||||||||||
| 182 | 0 | push @{$fields{$key}}, $element->{value}->{$key}; | |||||||||||||||||
| 183 | } | ||||||||||||||||||
| 184 | } | ||||||||||||||||||
| 185 | # Calculates min_occur | ||||||||||||||||||
| 186 | 0 | foreach my $key (keys %fields) { | |||||||||||||||||
| 187 | 0 | ▶ | my $min_occur = (@{$fields{$key}} == scalar @$array) ? 1 : 0; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 188 | 0 | $fields{$key} = array_reduction($fields{$key}); | |||||||||||||||||
| 189 | 0 | $fields{$key}->{min_occur} = $min_occur; | |||||||||||||||||
| 190 | } | ||||||||||||||||||
| 191 | 0 | $branch->{value} = \%fields; | |||||||||||||||||
| 192 | 0 | $branch->{type} = 'HASHREF'; | |||||||||||||||||
| 193 | } | ||||||||||||||||||
| 194 | 0 | return $branch; | |||||||||||||||||
| 195 | } | ||||||||||||||||||
| 196 | |||||||||||||||||||
| 197 | |||||||||||||||||||
| 198 | |||||||||||||||||||
| 199 | |||||||||||||||||||
| 200 | 1 | 1; | |||||||||||||||||
| 201 | |||||||||||||||||||
| 202 | =pod | ||||||||||||||||||
| 203 | |||||||||||||||||||
| 204 | =head1 SEE ALSO | ||||||||||||||||||
| 205 | |||||||||||||||||||
| 206 | WSDL::Generator | ||||||||||||||||||
| 207 | |||||||||||||||||||
| 208 | =head1 AUTHOR | ||||||||||||||||||
| 209 | |||||||||||||||||||
| 210 | "Pierre Denis" <pdenis@fotango.com> | ||||||||||||||||||
| 211 | |||||||||||||||||||
| 212 | =head1 COPYRIGHT | ||||||||||||||||||
| 213 | |||||||||||||||||||
| 214 | Copyright (C) 2001, Fotango Ltd - All rights reserved. | ||||||||||||||||||
| 215 | This is free software. This software may be modified and/or distributed under the same terms as Perl itself. | ||||||||||||||||||
| 216 | |||||||||||||||||||
| 217 | =cut | ||||||||||||||||||