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 weather
00003 # source scripts.
00004
00005 package ENVCANLocation;
00006 use strict;
00007 require Exporter;
00008 use XML::Parser;
00009
00010 our @ISA = qw(Exporter);
00011 our @EXPORT = qw(doSearch AddStationIdSearch AddRegionIdSearch AddCitySearch AddProvinceSearch);
00012 our $VERSION = 0.1;
00013
00014 my @regionidsearches;
00015 my @stationidsearches;
00016 my @citysearches;
00017 my @provincesearches;
00018 my %currStation;
00019 my $searchresults;
00020
00021 sub doSearch {
00022
00023 my $parser = new XML::Parser( Style => 'Stream' );
00024 open(XML, 'ENVCAN-Stations.xml') or die "cannot open file\n";
00025 $parser->parse(*XML);
00026 close(XML);
00027 return $searchresults;
00028 }
00029
00030 sub StartDocument {
00031 my $expat = shift;
00032
00033 $expat->{finish} = 0;
00034
00035 }
00036
00037 sub StartTag {
00038 my ($expat, $name, %atts) = @_;
00039
00040 if ($name eq 'station') {
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
00052 if ($expat->in_element('station_id')) {
00053 $expat->{CurrEntry}->{station_id} = $text;
00054 if (!$expat->{MatchFound}) {
00055 foreach $search (@stationidsearches) {
00056 if ($text =~ m/$search/i) {
00057 $expat->{MatchFound} = 1;
00058 return;
00059 }
00060 }
00061 }
00062 }
00063
00064 if ($expat->in_element('region_id')) {
00065 $expat->{CurrEntry}->{region_id} = $text;
00066 if (!$expat->{MatchFound}) {
00067 foreach $search (@regionidsearches) {
00068 if ($text =~ m/$search/i) {
00069 $expat->{MatchFound} = 1;
00070 return;
00071 }
00072 }
00073 }
00074 }
00075
00076 if ($expat->in_element('province')) {
00077 $expat->{CurrEntry}->{province} = $text;
00078 if (!$expat->{MatchFound}) {
00079 foreach $search (@provincesearches) {
00080 if ($text =~ m/$search/i) {
00081 $expat->{MatchFound} = 1;
00082 return;
00083 }
00084 }
00085 }
00086 }
00087
00088 if ($expat->in_element('city')) {
00089 $expat->{CurrEntry}->{city} = $text;
00090 if (!$expat->{MatchFound}) {
00091 foreach $search (@citysearches) {
00092 if ($text =~ m/$search/i) {
00093 $expat->{MatchFound} = 1;
00094 return;
00095 }
00096 }
00097 }
00098 }
00099 }
00100
00101 sub EndTag {
00102 my ($expat, $name) = @_;
00103
00104 if ($name eq 'station' && $expat->{MatchFound}) {
00105 push (@$searchresults, $expat->{CurrEntry});
00106 if ($expat->{finish}) {
00107 $expat->finish();
00108 return;
00109 }
00110 }
00111
00112 }
00113
00114 sub AddCitySearch {
00115 my $city = shift;
00116 push (@citysearches, $city);
00117 }
00118
00119 sub AddProvinceSearch {
00120 my $province = shift;
00121 push (@provincesearches, $province);
00122 }
00123
00124 sub AddStationIdSearch {
00125 my $station_id = shift;
00126 push (@stationidsearches, $station_id);
00127 }
00128
00129 sub AddRegionIdSearch {
00130 my $region_id = shift;
00131 push (@regionidsearches, $region_id);
00132 }
00133
00134 1;