| 1 | ################################################################################ | ||||||||||
| 2 | # Copyright 2008 Amazon Technologies, Inc. | ||||||||||
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); | ||||||||||
| 4 | # | ||||||||||
| 5 | # You may not use this file except in compliance with the License. | ||||||||||
| 6 | # You may obtain a copy of the License at: http://aws.amazon.com/apache2.0 | ||||||||||
| 7 | # This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR | ||||||||||
| 8 | # CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||||||||||
| 9 | # specific language governing permissions and limitations under the License. | ||||||||||
| 10 | ################################################################################ | ||||||||||
| 11 | # __ _ _ ___ | ||||||||||
| 12 | # ( )( \/\/ )/ __) | ||||||||||
| 13 | # /__\ \ / \__ \ | ||||||||||
| 14 | # (_)(_) \/\/ (___/ | ||||||||||
| 15 | # | ||||||||||
| 16 | # Amazon SQS Perl Library | ||||||||||
| 17 | # API Version: 2009-02-01 | ||||||||||
| 18 | # Generated: Thu Apr 09 01:13:11 PDT 2009 | ||||||||||
| 19 | # | ||||||||||
| 20 | # Amazon::SQS::Model - base class for all model classes | ||||||||||
| 21 | # | ||||||||||
| 22 | package Amazon::SQS::Model; | ||||||||||
| 23 | |||||||||||
| 24 | 2 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 25 | 2 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 26 | |||||||||||
| 27 | 2 | ▶ | use Amazon::SQS::Constants qw(:all); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 28 | 2 | ▶ | use List::Util qw(pairs); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 29 | 2 | ▶ | use Module::Load qw(load); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 30 | |||||||||||
| 31 | # | ||||||||||
| 32 | # XML fragment representation of this object | ||||||||||
| 33 | # Note, name of the root determined by caller | ||||||||||
| 34 | # This fragment returns inner fields representation only | ||||||||||
| 35 | 0 | ▶ | sub _toXMLFragment { | ||||||||
|
Subroutine
_toXMLFragment: not called CC=8 SCAR=42.8
| |||||||||||
| 36 | 0 | my ($self) = @_; | |||||||||
| 37 | 0 | my $xml = $EMPTY; | |||||||||
| 38 | |||||||||||
| 39 | 0 | foreach my $p ( pairs %{ $self->{_fields} } ) { | |||||||||
| 40 | 0 | my ( $fieldName, $field ) = @{$p}; | |||||||||
| 41 | |||||||||||
| 42 | 0 | my $fieldValue = $field->{FieldValue}; | |||||||||
| 43 | |||||||||||
| 44 | 0 | ▶ | if ( defined $fieldValue ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 45 | 0 | my $fieldType = $field->{FieldType}; | |||||||||
| 46 | |||||||||||
| 47 | 0 | ▶ | if ( _isArrayRef($fieldType) ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 48 | 0 | ▶ | if ( _isComplexType( $fieldType->[0] ) ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 49 | 0 | for my $item ( @{$fieldValue} ) { | |||||||||
| 50 | 0 | $xml .= "<$fieldName>"; | |||||||||
| 51 | 0 | $xml .= $item->_toXMLFragment(); | |||||||||
| 52 | 0 | $xml .= "</$fieldName>"; | |||||||||
| 53 | } | ||||||||||
| 54 | } | ||||||||||
| 55 | else { | ||||||||||
| 56 | 0 | for my $item ($fieldValue) { | |||||||||
| 57 | 0 | $xml .= "<$fieldName>"; | |||||||||
| 58 | 0 | $xml .= $self->_escapeXML($item); | |||||||||
| 59 | 0 | $xml .= "</$fieldName>"; | |||||||||
| 60 | } | ||||||||||
| 61 | } | ||||||||||
| 62 | } | ||||||||||
| 63 | else { | ||||||||||
| 64 | 0 | ▶ | if ( _isComplexType($fieldType) ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 65 | 0 | $xml .= "<$fieldName>"; | |||||||||
| 66 | 0 | $xml .= $fieldValue->_toXMLFragment(); | |||||||||
| 67 | 0 | $xml .= "</$fieldName>"; | |||||||||
| 68 | } | ||||||||||
| 69 | else { | ||||||||||
| 70 | 0 | $xml .= "<$fieldName>"; | |||||||||
| 71 | 0 | $xml .= $self->_escapeXML($fieldValue); | |||||||||
| 72 | 0 | $xml .= "</$fieldName>"; | |||||||||
| 73 | } | ||||||||||
| 74 | } | ||||||||||
| 75 | } | ||||||||||
| 76 | } | ||||||||||
| 77 | |||||||||||
| 78 | 0 | return $xml; | |||||||||
| 79 | } | ||||||||||
| 80 | |||||||||||
| 81 | # | ||||||||||
| 82 | # Escape special XML characters | ||||||||||
| 83 | # return string with escaped XML characters | ||||||||||
| 84 | # | ||||||||||
| 85 | 0 | ▶ | sub _escapeXML { | ||||||||
|
Subroutine
_escapeXML: not called CC=1 SCAR=6.9
| |||||||||||
| 86 | 0 | my ( $self, $str ) = @_; | |||||||||
| 87 | |||||||||||
| 88 | 0 | $str =~ s/&/&/xsmg; | |||||||||
| 89 | 0 | $str =~ s/</</xsmg; | |||||||||
| 90 | 0 | $str =~ s/>/>/xsmg; | |||||||||
| 91 | 0 | $str =~ s/'/'/xsmg; | |||||||||
| 92 | 0 | $str =~ s/"/"/xsmg; | |||||||||
| 93 | |||||||||||
| 94 | 0 | return $str; | |||||||||
| 95 | } | ||||||||||
| 96 | |||||||||||
| 97 | # | ||||||||||
| 98 | # Determines if field is complex type | ||||||||||
| 99 | # | ||||||||||
| 100 | 7 | ▶ | sub _isComplexType { | ||||||||
|
Subroutine
_isComplexType: called CC=1 SCAR=0.0
| |||||||||||
| 101 | 7 | my ( $self, $fieldType ) = @_; | |||||||||
| 102 | |||||||||||
| 103 | 7 | return $fieldType =~ m/^Amazon::SQS::Model::/xsm; | |||||||||
| 104 | } | ||||||||||
| 105 | |||||||||||
| 106 | # | ||||||||||
| 107 | # Checks whether passed variable is an associative array | ||||||||||
| 108 | # | ||||||||||
| 109 | 0 | ▶ | sub _isHashRef { | ||||||||
|
Subroutine
_isHashRef: not called CC=1 SCAR=6.9
| |||||||||||
| 110 | 0 | my ( $self, $var ) = @_; | |||||||||
| 111 | |||||||||||
| 112 | 0 | return ref $var eq 'HASH'; | |||||||||
| 113 | } | ||||||||||
| 114 | |||||||||||
| 115 | # | ||||||||||
| 116 | # Checks whether passed variable is an array reference | ||||||||||
| 117 | # | ||||||||||
| 118 | 9 | ▶ | sub _isArrayRef { | ||||||||
|
Subroutine
_isArrayRef: called CC=1 SCAR=0.0
| |||||||||||
| 119 | 9 | my ( $self, $var ) = @_; | |||||||||
| 120 | |||||||||||
| 121 | 9 | return ref $var eq 'ARRAY'; | |||||||||
| 122 | } | ||||||||||
| 123 | |||||||||||
| 124 | # | ||||||||||
| 125 | # Construct from hash ref | ||||||||||
| 126 | # | ||||||||||
| 127 | 1 | ▶ | sub _fromHashRef { | ||||||||
|
Subroutine
_fromHashRef: called CC=15 SCAR=40.7
| |||||||||||
| 128 | 1 | my ( $self, $array ) = @_; | |||||||||
| 129 | |||||||||||
| 130 | 1 | foreach my $p ( pairs %{ $self->{_fields} } ) { | |||||||||
| 131 | 7 | my ( $fieldName, $field ) = @{$p}; | |||||||||
| 132 | |||||||||||
| 133 | 7 | my $fieldType = $field->{FieldType}; | |||||||||
| 134 | |||||||||||
| 135 | 7 | ▶ | if ( $self->_isArrayRef($fieldType) ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 136 | |||||||||||
| 137 | 2 | ▶ | if ( $self->_isComplexType( $fieldType->[0] ) ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 138 | 0 | my $elements = $array->{$fieldName}; | |||||||||
| 139 | |||||||||||
| 140 | 0 | ▶ | if ( !$self->_isArrayRef($elements) ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 141 | 0 | ▶ | $elements = defined $elements ? [$elements] : []; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 142 | } | ||||||||||
| 143 | |||||||||||
| 144 | 0 | ▶ | if ( scalar @{$elements} >= 1 ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 145 | 0 | my $package = $fieldType->[0]; | |||||||||
| 146 | 0 | load $package; | |||||||||
| 147 | |||||||||||
| 148 | 0 | my $list = $self->{_fields}->{$fieldName}->{FieldValue}; | |||||||||
| 149 | |||||||||||
| 150 | 0 | for my $element ( @{$elements} ) { | |||||||||
| 151 | 0 | push @{$list}, $fieldType->[0]->new($element); | |||||||||
| 152 | } | ||||||||||
| 153 | |||||||||||
| 154 | 0 | $self->{_fields}->{$fieldName}->{FieldValue} = $list; | |||||||||
| 155 | } | ||||||||||
| 156 | } | ||||||||||
| 157 | else { | ||||||||||
| 158 | 2 | my $elements = $array->{$fieldName}; | |||||||||
| 159 | |||||||||||
| 160 | 2 | ▶ | if ( !$self->_isArrayRef($elements) ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 161 | 2 | ▶ | $elements = defined $elements ? [$elements] : []; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 162 | } | ||||||||||
| 163 | |||||||||||
| 164 | 2 | ▶ | if ( scalar @{$elements} >= 1 ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 165 | 0 | my $list = $self->{_fields}->{$fieldName}->{FieldValue}; | |||||||||
| 166 | |||||||||||
| 167 | 0 | for my $element ( @{$elements} ) { | |||||||||
| 168 | 0 | push @{$list}, $element; | |||||||||
| 169 | } | ||||||||||
| 170 | |||||||||||
| 171 | 0 | $self->{_fields}->{$fieldName}->{FieldValue} = $list; | |||||||||
| 172 | } | ||||||||||
| 173 | } | ||||||||||
| 174 | } | ||||||||||
| 175 | else { | ||||||||||
| 176 | 5 | ▶ | if ( $self->_isComplexType($fieldType) ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 177 | 0 | my $element = $array->{$fieldName}; | |||||||||
| 178 | |||||||||||
| 179 | 0 | ▶ | if ( defined $element ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 180 | 0 | my $package = $fieldType; | |||||||||
| 181 | 0 | load $package; | |||||||||
| 182 | |||||||||||
| 183 | 0 | $self->{_fields}->{$fieldName}->{FieldValue} = $fieldType->new($element); | |||||||||
| 184 | } | ||||||||||
| 185 | } | ||||||||||
| 186 | else { | ||||||||||
| 187 | 5 | my $element = $array->{$fieldName}; | |||||||||
| 188 | 5 | ▶ | if ( defined $element ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 189 | 3 | $self->{_fields}->{$fieldName}->{FieldValue} = $element; | |||||||||
| 190 | } | ||||||||||
| 191 | } | ||||||||||
| 192 | } | ||||||||||
| 193 | } | ||||||||||
| 194 | |||||||||||
| 195 | 1 | return; | |||||||||
| 196 | } | ||||||||||
| 197 | |||||||||||
| 198 | 2 | 1; | |||||||||