Ticket #3811: ENVCANLocation.pm

File ENVCANLocation.pm, 3.0 KB (added by vitaminjoe@…, 17 years ago)

PERL module used to search the XML document

Line 
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
5package ENVCANLocation;
6use strict;
7require Exporter;
8use XML::Parser;
9
10our @ISA = qw(Exporter);
11our @EXPORT = qw(doSearch AddStationIdSearch AddRegionIdSearch AddCitySearch AddProvinceSearch);
12our $VERSION = 0.1;
13
14my @regionidsearches;
15my @stationidsearches;
16my @citysearches;
17my @provincesearches;
18my %currStation;
19my $searchresults;
20
21sub 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
30sub StartDocument {
31    my $expat = shift;
32
33    $expat->{finish} = 0;
34
35}
36
37sub StartTag {
38    my ($expat, $name, %atts) = @_;
39
40    if ($name eq 'station') {
41       $expat->{CurrEntry} = {};
42       $expat->{MatchFound} = 0;
43    }
44}
45
46sub 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
101sub 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
114sub AddCitySearch {
115    my $city = shift;
116    push (@citysearches, $city);
117}
118
119sub AddProvinceSearch {
120    my $province = shift;
121    push (@provincesearches, $province);
122}
123
124sub AddStationIdSearch {
125    my $station_id = shift;
126    push (@stationidsearches, $station_id);
127}
128
129sub AddRegionIdSearch {
130    my $region_id = shift;
131    push (@regionidsearches, $region_id);
132}
133
1341;