1 | # This code is mostly the work of Lucien Dunning (ldunning@gmail.com). It was |
---|
2 | # used to provide search functionality for the Environment Canada weather |
---|
3 | # source scripts. |
---|
4 | |
---|
5 | package ENVCANLocation; |
---|
6 | use strict; |
---|
7 | require Exporter; |
---|
8 | use XML::Parser; |
---|
9 | |
---|
10 | our @ISA = qw(Exporter); |
---|
11 | our @EXPORT = qw(doSearch AddStationIdSearch AddRegionIdSearch AddCitySearch AddProvinceSearch); |
---|
12 | our $VERSION = 0.1; |
---|
13 | |
---|
14 | my @regionidsearches; |
---|
15 | my @stationidsearches; |
---|
16 | my @citysearches; |
---|
17 | my @provincesearches; |
---|
18 | my %currStation; |
---|
19 | my $searchresults; |
---|
20 | |
---|
21 | sub doSearch { |
---|
22 | |
---|
23 | my $parser = new XML::Parser( Style => 'Stream' ); |
---|
24 | open(XML, 'ENVCAN-Stations.xml') or die "cannot open file\n"; |
---|
25 | $parser->parse(*XML); |
---|
26 | close(XML); |
---|
27 | return $searchresults; |
---|
28 | } |
---|
29 | |
---|
30 | sub StartDocument { |
---|
31 | my $expat = shift; |
---|
32 | |
---|
33 | $expat->{finish} = 0; |
---|
34 | |
---|
35 | } |
---|
36 | |
---|
37 | sub StartTag { |
---|
38 | my ($expat, $name, %atts) = @_; |
---|
39 | |
---|
40 | if ($name eq 'station') { |
---|
41 | $expat->{CurrEntry} = {}; |
---|
42 | $expat->{MatchFound} = 0; |
---|
43 | } |
---|
44 | } |
---|
45 | |
---|
46 | sub Text { |
---|
47 | |
---|
48 | my $expat = shift; |
---|
49 | my $text = $expat->{Text}; |
---|
50 | my $search; |
---|
51 | |
---|
52 | if ($expat->in_element('station_id')) { |
---|
53 | $expat->{CurrEntry}->{station_id} = $text; |
---|
54 | if (!$expat->{MatchFound}) { |
---|
55 | foreach $search (@stationidsearches) { |
---|
56 | if ($text =~ m/$search/i) { |
---|
57 | $expat->{MatchFound} = 1; |
---|
58 | return; |
---|
59 | } |
---|
60 | } |
---|
61 | } |
---|
62 | } |
---|
63 | |
---|
64 | if ($expat->in_element('region_id')) { |
---|
65 | $expat->{CurrEntry}->{region_id} = $text; |
---|
66 | if (!$expat->{MatchFound}) { |
---|
67 | foreach $search (@regionidsearches) { |
---|
68 | if ($text =~ m/$search/i) { |
---|
69 | $expat->{MatchFound} = 1; |
---|
70 | return; |
---|
71 | } |
---|
72 | } |
---|
73 | } |
---|
74 | } |
---|
75 | |
---|
76 | if ($expat->in_element('province')) { |
---|
77 | $expat->{CurrEntry}->{province} = $text; |
---|
78 | if (!$expat->{MatchFound}) { |
---|
79 | foreach $search (@provincesearches) { |
---|
80 | if ($text =~ m/$search/i) { |
---|
81 | $expat->{MatchFound} = 1; |
---|
82 | return; |
---|
83 | } |
---|
84 | } |
---|
85 | } |
---|
86 | } |
---|
87 | |
---|
88 | if ($expat->in_element('city')) { |
---|
89 | $expat->{CurrEntry}->{city} = $text; |
---|
90 | if (!$expat->{MatchFound}) { |
---|
91 | foreach $search (@citysearches) { |
---|
92 | if ($text =~ m/$search/i) { |
---|
93 | $expat->{MatchFound} = 1; |
---|
94 | return; |
---|
95 | } |
---|
96 | } |
---|
97 | } |
---|
98 | } |
---|
99 | } |
---|
100 | |
---|
101 | sub EndTag { |
---|
102 | my ($expat, $name) = @_; |
---|
103 | |
---|
104 | if ($name eq 'station' && $expat->{MatchFound}) { |
---|
105 | push (@$searchresults, $expat->{CurrEntry}); |
---|
106 | if ($expat->{finish}) { |
---|
107 | $expat->finish(); |
---|
108 | return; |
---|
109 | } |
---|
110 | } |
---|
111 | |
---|
112 | } |
---|
113 | |
---|
114 | sub AddCitySearch { |
---|
115 | my $city = shift; |
---|
116 | push (@citysearches, $city); |
---|
117 | } |
---|
118 | |
---|
119 | sub AddProvinceSearch { |
---|
120 | my $province = shift; |
---|
121 | push (@provincesearches, $province); |
---|
122 | } |
---|
123 | |
---|
124 | sub AddStationIdSearch { |
---|
125 | my $station_id = shift; |
---|
126 | push (@stationidsearches, $station_id); |
---|
127 | } |
---|
128 | |
---|
129 | sub AddRegionIdSearch { |
---|
130 | my $region_id = shift; |
---|
131 | push (@regionidsearches, $region_id); |
---|
132 | } |
---|
133 | |
---|
134 | 1; |
---|