00001 # This code is mostly the work of Lucien Dunning (ldunning@gmail.com). It was
00002 # used to provide search functionality for the Environment Canada animated and
00003 # static maps.
00004
00005 package ENVCANMapSearch;
00006 use strict;
00007 require Exporter;
00008 use base qw(XML::SAX::Base);
00009 use XML::Parser;
00010
00011 our @ISA = qw(Exporter);
00012 our @EXPORT = qw(doSearch AddSatSearch AddImageTypeSearch AddSatClassSearch);
00013 our $VERSION = 0.1;
00014
00015 my @satclasssearches;
00016 my @satsearches;
00017 my @imagetypesearches;
00018 my @anisearches;
00019 my $searchresults;
00020
00021 sub doSearch {
00022
00023 my $parser = new XML::Parser( Style => 'Stream' );
00024 open(XML, 'ENVCAN-Maps.xml') or die "cannot open file";
00025 $parser->parse(*XML);
00026 close(XML);
00027 return $searchresults;
00028 }
00029
00030 sub StartDocument {
00031
00032 my $expat = shift;
00033 $expat->{finish} = 0;
00034 $searchresults = [];
00035 }
00036
00037 sub StartTag {
00038
00039 my ($expat, $name, %atts) = @_;
00040 if ($name eq 'entry') {
00041 $expat->{CurrEntry} = {};
00042 $expat->{MatchFound} = 0;
00043 }
00044 }
00045
00046 sub Text {
00047
00048 my $expat = shift;
00049 my $text = $expat->{Text};
00050 my $search;
00051 if ($expat->in_element('satellite_class')) {
00052 $expat->{CurrEntry}->{satellite_class} = $text;
00053 if (!$expat->{MatchFound}) {
00054 foreach $search (@satclasssearches) {
00055 if ($text =~ m/$search/i) {
00056 $expat->{MatchFound} = 1;
00057 return;
00058 }
00059 }
00060 }
00061 }
00062
00063 elsif ($expat->in_element('satellite')) {
00064 $expat->{CurrEntry}->{satellite} = $text;
00065 if (!$expat->{MatchFound}) {
00066 foreach $search (@satsearches) {
00067 if ($text =~ m/$search/i) {
00068 $expat->{MatchFound} = 1;
00069 return;
00070 }
00071 }
00072 }
00073 }
00074
00075 elsif ($expat->in_element('image_type')) {
00076 $expat->{CurrEntry}->{image_type} = $text;
00077 if (!$expat->{MatchFound}) {
00078 foreach $search (@imagetypesearches) {
00079 if ($text =~ m/$search/i) {
00080 $expat->{MatchFound} = 1;
00081 return;
00082 }
00083 }
00084 }
00085 }
00086
00087 elsif ($expat->in_element('entry_id')) {
00088 $expat->{CurrEntry}->{entry_id} = $text;
00089 if (!$expat->{MatchFound}) {
00090 foreach $search (@anisearches) {
00091 if ($text =~ m/$search/i) {
00092 $expat->{MatchFound} = 1;
00093 return;
00094 }
00095 }
00096 }
00097 }
00098
00099
00100 elsif ($expat->in_element('animated_url')) {
00101 $expat->{CurrEntry}->{animated_url} = $text;
00102 }
00103 }
00104
00105 sub EndTag {
00106
00107 my ($expat, $name) = @_;
00108 if ($name eq 'entry' && $expat->{MatchFound}) {
00109 push (@$searchresults, $expat->{CurrEntry});
00110 if ($expat->{finish}) {
00111 $expat->finish();
00112 return;
00113 }
00114 }
00115
00116 }
00117
00118 sub AddSatClassSearch {
00119 push (@satclasssearches, shift);
00120 }
00121 sub AddSatSearch {
00122 push (@satsearches, shift);
00123 }
00124 sub AddImageTypeSearch {
00125 push (@imagetypesearches, shift);
00126 }
00127
00128 sub AddAniSearch {
00129 push (@anisearches, shift);
00130 }
00131 1;