| 1 | ################################################### | ||||||||||||||||||||||||||||
| 2 | ## YahooRESTGeocode.pm | ||||||||||||||||||||||||||||
| 3 | ## Andrew N. Hicox <andrew@hicox.com> | ||||||||||||||||||||||||||||
| 4 | ## Hicox Information Systems Development | ||||||||||||||||||||||||||||
| 5 | ## | ||||||||||||||||||||||||||||
| 6 | ## a built in XML::Parser style for parsing | ||||||||||||||||||||||||||||
| 7 | ## the xml returned from Yahoo REST webservices | ||||||||||||||||||||||||||||
| 8 | ################################################### | ||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | ## Global Stuff ################################### | ||||||||||||||||||||||||||||
| 12 | package XML::Parser::YahooRESTGeocode; | ||||||||||||||||||||||||||||
| 13 | 1 | ▶ | use 5.6.0; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 1 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 1 | $VERSION = 0.2; | |||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | #register with XML::Parser as built in style | ||||||||||||||||||||||||||||
| 19 | 1 | $XML::Parser::Built_In_Styles{YahooRESTGeocode} = 1; | |||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | #host data fields | ||||||||||||||||||||||||||||
| 22 | 1 | my %node_tree = ( | |||||||||||||||||||||||||||
| 23 | 'Result' => ['Latitude','Longitude','Address','City','State','Zip','Country'] | ||||||||||||||||||||||||||||
| 24 | ); | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | ## Init ########################################### | ||||||||||||||||||||||||||||
| 30 | 0 | ▶ | sub Init { | ||||||||||||||||||||||||||
|
Subroutine
Init: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 31 | 0 | my $expat = shift; | |||||||||||||||||||||||||||
| 32 | 0 | $expat->{'Lists'} = {}; | |||||||||||||||||||||||||||
| 33 | 0 | $expat->{'Curlist'} = []; #everything @OPEN used to be | |||||||||||||||||||||||||||
| 34 | 0 | $expat->{'errstr'} = (); #clear out the error string | |||||||||||||||||||||||||||
| 35 | 0 | $expat->{'errmsg'} = (); #clear out the advisory errorrs | |||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | #if there's any arguments corresponding to nodes in 'node_tree', | ||||||||||||||||||||||||||||
| 38 | #evacuate them to '_CDB2IMP_callbacks', they must be code refs | ||||||||||||||||||||||||||||
| 39 | #we'll execute 'em when one of those nodes ends | ||||||||||||||||||||||||||||
| 40 | 0 | foreach (keys %node_tree){ | |||||||||||||||||||||||||||
| 41 | 0 | ▶ | if ((exists($expat->{$_})) && (ref ($expat->{$_}) eq "CODE")){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
exists $expat->{$_} and ref $expat->{$_} eq "CODE"
MC/DC0%0/2
exists $expat->{$_} and ref $expat->{$_} eq "CODE"
exists $expat->{$_}
ref $expat->{$_} eq "CODE"
Truth table0%0/3
A and B
A= exists $expat->{$_}B= ref $expat->{$_} eq "CODE"
| |||||||||||||||||||||||||||||
| 42 | 0 | $expat->{'_YahooREST_callbacks'}->{$_} = $expat->{$_}; | |||||||||||||||||||||||||||
| 43 | 0 | delete($expat->{$_}); | |||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | ## Start ########################################## | ||||||||||||||||||||||||||||
| 53 | 0 | ▶ | sub Start { | ||||||||||||||||||||||||||
|
Subroutine
Start: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 54 | 0 | my ($expat, $element, %p) = @_; | |||||||||||||||||||||||||||
| 55 | 0 | push (@{$expat->{'Curlist'}}, { | |||||||||||||||||||||||||||
| 56 | '_name' => $element, | ||||||||||||||||||||||||||||
| 57 | '_attributes' => \%p | ||||||||||||||||||||||||||||
| 58 | }); | ||||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | ## Char ########################################### | ||||||||||||||||||||||||||||
| 65 | 0 | ▶ | sub Char { | ||||||||||||||||||||||||||
|
Subroutine
Char: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 66 | 0 | my ($expat, $data) = @_; | |||||||||||||||||||||||||||
| 67 | 0 | ▶ | unless ($data =~/^\s+$/){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 68 | 0 | $expat->{'Curlist'}->[$#{$expat->{'Curlist'}}]->{'_data'} .= $data; | |||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | ## Final ########################################## | ||||||||||||||||||||||||||||
| 76 | 0 | ▶ | sub Final { | ||||||||||||||||||||||||||
|
Subroutine
Final: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 77 | 0 | my $expat = shift; | |||||||||||||||||||||||||||
| 78 | #handle error | ||||||||||||||||||||||||||||
| 79 | 0 | ▶ | if ($expat->{'errstr'}){ $XML::Parser::errstr = $expat->{'errstr'}; return (undef); } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 80 | #handle advisory messages | ||||||||||||||||||||||||||||
| 81 | 0 | ▶ | if ($expat->{'errmsg'}){ $XML::Parser::errmsg = $expat->{'errmsg'}; } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 82 | #clean up | ||||||||||||||||||||||||||||
| 83 | 0 | delete $expat->{Curlist}; | |||||||||||||||||||||||||||
| 84 | 0 | my $out = $expat->{Lists}->{'DATA'}; | |||||||||||||||||||||||||||
| 85 | 0 | delete $expat->{Lists}; | |||||||||||||||||||||||||||
| 86 | 0 | return ($out); | |||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | ## gather_data #################################### | ||||||||||||||||||||||||||||
| 91 | 0 | ▶ | sub gather_data { | ||||||||||||||||||||||||||
|
Subroutine
gather_data: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 92 | 0 | my ($expat, $name) = @_; | |||||||||||||||||||||||||||
| 93 | 0 | my (@data) = (); | |||||||||||||||||||||||||||
| 94 | 0 | ▶ | while ($expat->{'Curlist'}->[$#{$expat->{'Curlist'}}]->{'_name'} ne $name){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 95 | 0 | push (@data, pop(@{$expat->{'Curlist'}})); | |||||||||||||||||||||||||||
| 96 | }; | ||||||||||||||||||||||||||||
| 97 | #one more to catch the start tag | ||||||||||||||||||||||||||||
| 98 | 0 | push (@data, pop(@{$expat->{'Curlist'}})); | |||||||||||||||||||||||||||
| 99 | 0 | reverse (@data); | |||||||||||||||||||||||||||
| 100 | 0 | return (\@data); | |||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | ## extract_pcdata ################################# | ||||||||||||||||||||||||||||
| 105 | 0 | ▶ | sub extract_pcdata { | ||||||||||||||||||||||||||
|
Subroutine
extract_pcdata: not called CC=5 SCAR=34.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 106 | 0 | my $data = shift(); | |||||||||||||||||||||||||||
| 107 | 0 | my %out = (); | |||||||||||||||||||||||||||
| 108 | 0 | foreach (@{$data}){ | |||||||||||||||||||||||||||
| 109 | 0 | foreach my $f (@_){ | |||||||||||||||||||||||||||
| 110 | 0 | ▶ | if (($_->{'_name'} eq $f) && ($_->{'_data'} !~/^$/)){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$_->{"_name"} eq $f and not $_->{"_data"} =~ /^$/
MC/DC0%0/2
$_->{"_name"} eq $f and not $_->{"_data"} =~ /^$/
$_->{"_name"} eq $f
not $_->{"_data"} =~ /^$/
Truth table0%0/3
A and not(B)
A= $_->{"_name"} eq $fB= not $_->{"_data"} =~ /^$/
| |||||||||||||||||||||||||||||
| 111 | 0 | $out{$f} = $_->{'_data'}; | |||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||
| 115 | 0 | return (\%out); | |||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | ## End ############################################ | ||||||||||||||||||||||||||||
| 120 | 0 | ▶ | sub End { | ||||||||||||||||||||||||||
|
Subroutine
End: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 121 | 0 | my ($expat, $element) = @_; | |||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | 0 | ▶ | if (exists($node_tree{$element})){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | 0 | my $data = extract_pcdata(gather_data($expat, $element), @{$node_tree{$element}}); | |||||||||||||||||||||||||||
| 126 | 0 | push (@{$expat->{'Lists'}->{'DATA'}->{$element}}, $data); | |||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | #if the user defined some callback to execute, do that | ||||||||||||||||||||||||||||
| 129 | 0 | ▶ | if (exists($expat->{'_YahooREST_callbacks'}->{$element})){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 130 | 0 | &{$expat->{'_YahooREST_callbacks'}->{$element}}($data); | |||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | ## True ########################################### | ||||||||||||||||||||||||||||
| 137 | 1 | 1; | |||||||||||||||||||||||||||