Ticket #3811: ENVCANMapSearch.pm

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

PERL module used to search the ENVCAN-Maps.xml file

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 animated and
3# static maps.
4
5package ENVCANMapSearch;
6use strict;
7require Exporter;
8use base qw(XML::SAX::Base);
9use XML::Parser;
10
11our @ISA = qw(Exporter);
12our @EXPORT = qw(doSearch AddSatSearch AddImageTypeSearch AddSatClassSearch);
13our $VERSION = 0.1;
14
15my @satclasssearches;
16my @satsearches;
17my @imagetypesearches;
18my @anisearches;
19my $searchresults;
20
21sub doSearch {
22
23    my $parser = new XML::Parser( Style => 'Stream' );
24    open(XML, 'ENVCAN-Maps.xml') or die "cannot open file";
25    $parser->parse(*XML);
26    close(XML);
27    return $searchresults;
28}
29
30sub StartDocument {
31
32    my $expat = shift;
33    $expat->{finish} = 0;
34    $searchresults = [];
35}
36
37sub StartTag {
38
39    my ($expat, $name, %atts) = @_;
40    if ($name eq 'entry') {
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    if ($expat->in_element('satellite_class')) {
52        $expat->{CurrEntry}->{satellite_class} = $text;
53        if (!$expat->{MatchFound}) {
54            foreach $search (@satclasssearches) {
55                if ($text =~ m/$search/i) {
56                    $expat->{MatchFound} = 1;
57                    return;
58                }
59            }
60        }
61    }
62
63    elsif ($expat->in_element('satellite')) {
64        $expat->{CurrEntry}->{satellite} = $text;
65        if (!$expat->{MatchFound}) {
66            foreach $search (@satsearches) {
67                if ($text =~ m/$search/i) {
68                    $expat->{MatchFound} = 1;
69                    return;
70                }
71            }
72        }
73    }
74
75    elsif ($expat->in_element('image_type')) {
76        $expat->{CurrEntry}->{image_type} = $text;
77        if (!$expat->{MatchFound}) {
78            foreach $search (@imagetypesearches) {
79                if ($text =~ m/$search/i) {
80                    $expat->{MatchFound} = 1;
81                    return;
82                }
83            }
84        }
85    }
86
87    elsif ($expat->in_element('entry_id')) {
88        $expat->{CurrEntry}->{entry_id} = $text;
89        if (!$expat->{MatchFound}) {
90            foreach $search (@anisearches) {
91                if ($text =~ m/$search/i) {
92                    $expat->{MatchFound} = 1;
93                    return;
94                }
95            }
96        }
97    }
98
99
100    elsif ($expat->in_element('animated_url')) {
101        $expat->{CurrEntry}->{animated_url} = $text;
102        }
103}
104
105sub EndTag {
106
107    my ($expat, $name) = @_;
108    if ($name eq 'entry' && $expat->{MatchFound}) {
109        push (@$searchresults, $expat->{CurrEntry});
110        if ($expat->{finish}) {
111            $expat->finish();
112            return;
113        }
114    }
115
116}
117
118sub AddSatClassSearch {
119    push (@satclasssearches, shift);
120}
121sub AddSatSearch {
122    push (@satsearches, shift);
123}
124sub AddImageTypeSearch {
125    push (@imagetypesearches, shift);
126}
127
128sub AddAniSearch {
129    push (@anisearches, shift);
130}
1311;