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 | |
---|
5 | package ENVCANMapSearch; |
---|
6 | use strict; |
---|
7 | require Exporter; |
---|
8 | use base qw(XML::SAX::Base); |
---|
9 | use XML::Parser; |
---|
10 | |
---|
11 | our @ISA = qw(Exporter); |
---|
12 | our @EXPORT = qw(doSearch AddSatSearch AddImageTypeSearch AddSatClassSearch); |
---|
13 | our $VERSION = 0.1; |
---|
14 | |
---|
15 | my @satclasssearches; |
---|
16 | my @satsearches; |
---|
17 | my @imagetypesearches; |
---|
18 | my @anisearches; |
---|
19 | my $searchresults; |
---|
20 | |
---|
21 | sub 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 | |
---|
30 | sub StartDocument { |
---|
31 | |
---|
32 | my $expat = shift; |
---|
33 | $expat->{finish} = 0; |
---|
34 | $searchresults = []; |
---|
35 | } |
---|
36 | |
---|
37 | sub StartTag { |
---|
38 | |
---|
39 | my ($expat, $name, %atts) = @_; |
---|
40 | if ($name eq 'entry') { |
---|
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 | 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 | |
---|
105 | sub 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 | |
---|
118 | sub AddSatClassSearch { |
---|
119 | push (@satclasssearches, shift); |
---|
120 | } |
---|
121 | sub AddSatSearch { |
---|
122 | push (@satsearches, shift); |
---|
123 | } |
---|
124 | sub AddImageTypeSearch { |
---|
125 | push (@imagetypesearches, shift); |
---|
126 | } |
---|
127 | |
---|
128 | sub AddAniSearch { |
---|
129 | push (@anisearches, shift); |
---|
130 | } |
---|
131 | 1; |
---|