blib/lib/Religion/Islam/PrayMind.pm | |||
---|---|---|---|
Criterion | Covered | Total | % |
statement | 4 | 6 | 66.6 |
branch | n/a | ||
condition | n/a | ||
subroutine | 2 | 2 | 100.0 |
pod | n/a | ||
total | 6 | 8 | 75.0 |
line | stmt | bran | cond | sub | pod | time | code |
---|---|---|---|---|---|---|---|
1 | package Religion::Islam::PrayMind; | ||||||
2 | |||||||
3 | # | ||||||
4 | # Copyright (c) 2002 Tasmin Ahmad | ||||||
5 | # All rights reserved. | ||||||
6 | # | ||||||
7 | # Implements Class to connect and retrieve data from the PrayerMinder server | ||||||
8 | # | ||||||
9 | # This library is free software; you can redistribute it and/or modify | ||||||
10 | # it under the "Artistic License", as described in the accompanying | ||||||
11 | # License.txt file. | ||||||
12 | # | ||||||
13 | # | ||||||
14 | # DISCLAIMER | ||||||
15 | # THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR | ||||||
16 | # IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED | ||||||
17 | # WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. | ||||||
18 | # | ||||||
19 | # Acknowledgement | ||||||
20 | # The following people have contributed to the development of | ||||||
21 | # this tool, in addition to the copyright owner: | ||||||
22 | # | ||||||
23 | # 1. Mr. Tariq Chaudhary | ||||||
24 | # 2. Allied Software Corporation Islamabad, Pakistan; particularly the | ||||||
25 | # following folks: | ||||||
26 | # 2.1 Mr. S. Taimur Hassan | ||||||
27 | # 2.2 Ms. Shaista Rashid | ||||||
28 | # 2.3 Mr. Ijaz Rashid | ||||||
29 | # | ||||||
30 | |||||||
31 | 1 | 1 | 17234 | use IO::Socket; | |||
1 | 59846 | ||||||
1 | 6 | ||||||
32 | 1 | 1 | 2252 | use XML::Parser; | |||
0 | |||||||
0 | |||||||
33 | |||||||
34 | |||||||
35 | use 5.006; | ||||||
36 | use strict; | ||||||
37 | use warnings; | ||||||
38 | |||||||
39 | require Exporter; | ||||||
40 | |||||||
41 | our @ISA = qw(Exporter); | ||||||
42 | |||||||
43 | # Items to export into callers namespace by default. Note: do not export | ||||||
44 | # names by default without a very good reason. Use EXPORT_OK instead. | ||||||
45 | # Do not simply export all your public functions/methods/constants. | ||||||
46 | |||||||
47 | # This allows declaration use Religion::Islam::PrayMind ':all'; | ||||||
48 | # If you do not need this, moving things directly into @EXPORT or @EXPORT_OK | ||||||
49 | # will save memory. | ||||||
50 | our %EXPORT_TAGS = ( 'all' => [ qw( | ||||||
51 | |||||||
52 | ) ] ); | ||||||
53 | |||||||
54 | our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } ); | ||||||
55 | |||||||
56 | our @EXPORT = qw($VERSION | ||||||
57 | eContinentsList eCountriesList eStatesList | ||||||
58 | eCitiesListForState eCitiesListForCountry | ||||||
59 | ePrayerTimes ePrayerTimeTable | ||||||
60 | |||||||
61 | IDS_NO_Error IDS_ERROR_OpenNotCalled IDS_ERROR_ClientIDNotSpecified IDS_ERROR_EmptyString | ||||||
62 | IDS_ERROR_GetListNotCalled IDS_ERROR_GetPrayerTimesNotCalled IDS_ERROR_EmptyList | ||||||
63 | ); | ||||||
64 | |||||||
65 | our $VERSION = '1.01'; | ||||||
66 | |||||||
67 | #define constant values for type of listing requested by caller | ||||||
68 | use constant eContinentsList => 0; | ||||||
69 | use constant eCountriesList => 1; | ||||||
70 | use constant eStatesList => 2; | ||||||
71 | use constant eCitiesListForState => 3; | ||||||
72 | use constant eCitiesListForCountry => 4; | ||||||
73 | use constant eCitiesSearchList => 5; | ||||||
74 | use constant ePrayerTimes => 6; | ||||||
75 | use constant ePrayerTimeTable => 7; | ||||||
76 | |||||||
77 | #define constant values for error codes | ||||||
78 | use constant IDS_NO_Error => 0; | ||||||
79 | use constant IDS_ERROR_OpenNotCalled => 1; | ||||||
80 | use constant IDS_ERROR_ClientIDNotSpecified => 2; | ||||||
81 | use constant IDS_ERROR_EmptyString => 3; | ||||||
82 | use constant IDS_ERROR_GetListNotCalled => 4; | ||||||
83 | use constant IDS_ERROR_GetPrayerTimesNotCalled => 5; | ||||||
84 | use constant IDS_ERROR_EmptyList => 6; | ||||||
85 | |||||||
86 | #define local constant values used to know type of listing obtained | ||||||
87 | use constant eNoData => 0; | ||||||
88 | use constant eLocationsList => 1; | ||||||
89 | use constant ePrayerTimings => 2; | ||||||
90 | |||||||
91 | use constant host => "www.prayerminder.com"; | ||||||
92 | #use constant host => "209.185.200.148"; | ||||||
93 | use constant EOL => "\015\012"; | ||||||
94 | use constant BLANK => EOL x 2; | ||||||
95 | |||||||
96 | #class private global member variable declaration | ||||||
97 | #our ($host, $EOL, $BLANK); | ||||||
98 | |||||||
99 | #Hash to store error codes and their corresponding text | ||||||
100 | our @PMErrors = | ||||||
101 | ( | ||||||
102 | "No Errors", | ||||||
103 | "Open function not called", | ||||||
104 | "Client ID not specified", | ||||||
105 | "Host returned an empty string please check that you have passed correct parameters", | ||||||
106 | "Call GetList function first", | ||||||
107 | "Call GetPrayerTimes function first", | ||||||
108 | "List does not contain elements" | ||||||
109 | ); | ||||||
110 | |||||||
111 | #function that will return class reference | ||||||
112 | |||||||
113 | sub new | ||||||
114 | { | ||||||
115 | my ($class, %args) = @_; | ||||||
116 | my $self = bless \%args, $_[0]; | ||||||
117 | if(!defined($args{ClientID})) | ||||||
118 | { | ||||||
119 | $args{ErrorCode} = IDS_ERROR_ClientIDNotSpecified; | ||||||
120 | } | ||||||
121 | else | ||||||
122 | { | ||||||
123 | $args{ErrorCode} = IDS_NO_Error; | ||||||
124 | $args{_DataType} = eNoData; | ||||||
125 | $args{_TimesData} = | ||||||
126 | { | ||||||
127 | nCityID => 0, | ||||||
128 | nCountryId => 0, | ||||||
129 | nStateID => 0, | ||||||
130 | sCityTitle => '', | ||||||
131 | sCityTimeZone => '', | ||||||
132 | sCityIslamicDate => '', | ||||||
133 | sCityGregorianDate => '', | ||||||
134 | sCityAsrFiqh => '', | ||||||
135 | CityPrayerTimes => {Fajr => "", Shurooq => "", Zuhr => "", AsrHanafi => "", AsrShafei => "", Maghrib => "", Isha => ""} | ||||||
136 | }; | ||||||
137 | $args{_LocationsList} = | ||||||
138 | { | ||||||
139 | LocNames => [], | ||||||
140 | LocIDs => [], | ||||||
141 | nCurrItem => 0 | ||||||
142 | }; | ||||||
143 | } | ||||||
144 | |||||||
145 | $self; | ||||||
146 | } | ||||||
147 | |||||||
148 | # Preloaded methods go here. | ||||||
149 | |||||||
150 | # Autoload methods go after =cut, and are processed by the autosplit program. | ||||||
151 | |||||||
152 | #sends HTTP query to obtain the list required and saves the result in run time structure | ||||||
153 | sub GetList | ||||||
154 | { | ||||||
155 | #store parameters received to functions local variables | ||||||
156 | my ($self, $ListType, $nParentID) = @_; | ||||||
157 | |||||||
158 | my ($document, $nListType); | ||||||
159 | $nListType = eNoData; | ||||||
160 | |||||||
161 | #implementing error when GetList called before making a call to new function | ||||||
162 | if(!defined($self->{ErrorCode})) | ||||||
163 | { | ||||||
164 | $self->{ErrorCode} = IDS_ERROR_OpenNotCalled; | ||||||
165 | } | ||||||
166 | |||||||
167 | if($self->{ErrorCode} != IDS_NO_Error) | ||||||
168 | { | ||||||
169 | return; | ||||||
170 | } | ||||||
171 | |||||||
172 | $document = "/cgi-bin/PMCalc.cgi?F=X&I=" . $self->{ClientID} . "&R="; | ||||||
173 | |||||||
174 | #Get the required list if Open function called before calling it | ||||||
175 | if($self->{ErrorCode} != IDS_ERROR_OpenNotCalled) | ||||||
176 | { | ||||||
177 | |||||||
178 | #now it depends upon $ListType which list to obtain | ||||||
179 | |||||||
180 | #make the query string that will be passed to get continent, countrie and city listing | ||||||
181 | #asked for continents list | ||||||
182 | if($ListType == eContinentsList) | ||||||
183 | { | ||||||
184 | $document = $document . "LCN"; | ||||||
185 | } | ||||||
186 | #asked for countries list | ||||||
187 | elsif($ListType == eCountriesList) | ||||||
188 | { | ||||||
189 | $document = $document . "LCY&CN="; | ||||||
190 | $document = $document . $nParentID; | ||||||
191 | } | ||||||
192 | #asked for cities list | ||||||
193 | elsif($ListType == eCitiesListForCountry) | ||||||
194 | { | ||||||
195 | $document = $document . "LCT&CY="; | ||||||
196 | $document = $document . $nParentID; | ||||||
197 | } | ||||||
198 | #print " in GetList 1 \n"; |
||||||
199 | #establish the connection to the host | ||||||
200 | my ($remote, $StartData, $aline); | ||||||
201 | $remote = IO::Socket::INET->new( Proto => "tcp", | ||||||
202 | PeerAddr => host, | ||||||
203 | PeerPort => "http(80)", | ||||||
204 | ); | ||||||
205 | |||||||
206 | #print " in GetList , doc=" . $document . " \n"; |
||||||
207 | #error message to report if connection is not established with host | ||||||
208 | unless($remote) | ||||||
209 | { | ||||||
210 | die "Cannot connect to http daemon on host " . host; | ||||||
211 | } | ||||||
212 | |||||||
213 | print $remote "GET $document HTTP/1.0" . BLANK; | ||||||
214 | |||||||
215 | $StartData = 0; | ||||||
216 | #$DesiredString = ""; | ||||||
217 | |||||||
218 | #while remote is returning cetain text | ||||||
219 | while(($StartData < 1) && (defined ($aline = <$remote>))) | ||||||
220 | { | ||||||
221 | #print " > " . $aline . " \n"; |
||||||
222 | #check if line is a blank line, after which the XML document follows | ||||||
223 | if($aline eq "\r\n") | ||||||
224 | { | ||||||
225 | $StartData = 1; | ||||||
226 | } | ||||||
227 | } | ||||||
228 | |||||||
229 | # If we got any data, parse it | ||||||
230 | if($StartData == 1) | ||||||
231 | { | ||||||
232 | # Make a call to function that will xml data returned by server | ||||||
233 | # the XML data will be parsed by XML parser and information will be | ||||||
234 | # stored in class member variables | ||||||
235 | ParseXMLFile($self, $remote, eLocationsList); | ||||||
236 | } | ||||||
237 | close $remote; | ||||||
238 | |||||||
239 | #print " *** GetList ErrorCode: " . $self->{ErrorCode} . " ErrorText: " . $self->{ErrorText} . " \n"; |
||||||
240 | |||||||
241 | #you get the required text from host | ||||||
242 | # if($self->{_DataType} != eLocationsList) | ||||||
243 | # { | ||||||
244 | #discard errors previously set | ||||||
245 | #Error Code IDS_ERROR_GetListNotCalled "Call GetList function first"; | ||||||
246 | #Error Code IDS_ERROR_GetPrayerTimesNotCalled "Call GetPrayerTimes function first"; | ||||||
247 | #Error Code IDS_ERROR_EmptyList "Empty List"; | ||||||
248 | # $self->{ErrorCode} = IDS_NO_Error | ||||||
249 | # if($self->{ErrorCode} == IDS_ERROR_GetListNotCalled || $self->{ErrorCode} == IDS_ERROR_GetPrayerTimesNotCalled || | ||||||
250 | # $self->{ErrorCode} == IDS_ERROR_EmptyList); | ||||||
251 | # } | ||||||
252 | |||||||
253 | #check whether some string is returned by host | ||||||
254 | # if($self->{_DataType} != eLocationsList) | ||||||
255 | # { | ||||||
256 | # $self->{ErrorCode} = IDS_ERROR_EmptyString | ||||||
257 | # if($self->{ErrorCode}) == IDS_NO_Error; | ||||||
258 | # } | ||||||
259 | #discard error that was set that empty string returned by host | ||||||
260 | |||||||
261 | # elsif($self->{ErrorCode} == IDS_ERROR_EmptyString) | ||||||
262 | # { | ||||||
263 | # $self->{ErrorCode} = IDS_NO_Error; | ||||||
264 | # } | ||||||
265 | } | ||||||
266 | } | ||||||
267 | |||||||
268 | #functions that are called when asked for listing of continents, countries and cities | ||||||
269 | #returns number of elements in the list retreived by GetList | ||||||
270 | sub GetListSize | ||||||
271 | { | ||||||
272 | #store parameters received to functions local variables | ||||||
273 | my $self = shift; | ||||||
274 | |||||||
275 | #$nListType is set to eLocationsList when asked for continents, countries or city listing | ||||||
276 | if($self->{_DataType} != eLocationsList) | ||||||
277 | { | ||||||
278 | $self->{ErrorCode} = IDS_ERROR_GetListNotCalled; | ||||||
279 | return -1; | ||||||
280 | } | ||||||
281 | else | ||||||
282 | { | ||||||
283 | #check for empty list | ||||||
284 | if($#{$self->{_LocationsList}->{LocNames}} < 0) | ||||||
285 | { | ||||||
286 | $self->{ErrorCode} = IDS_ERROR_EmptyList; | ||||||
287 | } | ||||||
288 | return $#{$self->{_LocationsList}->{LocNames}} + 1; | ||||||
289 | } | ||||||
290 | } | ||||||
291 | |||||||
292 | #returns parent element's ID, to which this list belongs | ||||||
293 | #sub GetParentID | ||||||
294 | #{ | ||||||
295 | # #$nListType is set to eLocationsList when asked for continents, countries or city listing | ||||||
296 | # if($nListType != eLocationsList) | ||||||
297 | # { | ||||||
298 | # $ErrorCode = IDS_ERROR_GetListNotCalled; | ||||||
299 | # return -1; | ||||||
300 | # } | ||||||
301 | # else | ||||||
302 | # { | ||||||
303 | # return $ParentID; | ||||||
304 | # } | ||||||
305 | #} | ||||||
306 | |||||||
307 | #returns ID of the current element from the list | ||||||
308 | sub GetElementID | ||||||
309 | { | ||||||
310 | my $self = shift; | ||||||
311 | |||||||
312 | #$nListType is set to eLocationsList when asked for continents, countries or city listing | ||||||
313 | if($self->{_DataType} != eLocationsList) | ||||||
314 | { | ||||||
315 | $self->{ErrorCode} = IDS_ERROR_GetListNotCalled; | ||||||
316 | return -1; | ||||||
317 | } | ||||||
318 | else | ||||||
319 | { | ||||||
320 | return $self->{_LocationsList}->{LocIDs}->[$self->{_LocationsList}->{nCurrItem}]; | ||||||
321 | } | ||||||
322 | } | ||||||
323 | |||||||
324 | #returns the name of the current element from the list | ||||||
325 | sub GetElementName | ||||||
326 | { | ||||||
327 | my $self = shift; | ||||||
328 | |||||||
329 | #$nListType is set to eLocationsList when asked for continents, countries or city listing | ||||||
330 | if($self->{_DataType} != eLocationsList) | ||||||
331 | { | ||||||
332 | $self->{ErrorCode} = IDS_ERROR_GetListNotCalled; | ||||||
333 | return -1; | ||||||
334 | } | ||||||
335 | else | ||||||
336 | { | ||||||
337 | return $self->{_LocationsList}->{LocNames}->[$self->{_LocationsList}->{nCurrItem}]; | ||||||
338 | } | ||||||
339 | } | ||||||
340 | |||||||
341 | #moves to next element in the list | ||||||
342 | sub NextElement | ||||||
343 | { | ||||||
344 | my $self = shift; | ||||||
345 | |||||||
346 | #$nListType is set to eLocationsList when asked for continents, countries or city listing | ||||||
347 | if($self->{_DataType} != eLocationsList) | ||||||
348 | { | ||||||
349 | $self->{ErrorCode} = IDS_ERROR_GetListNotCalled; | ||||||
350 | return -1; | ||||||
351 | } | ||||||
352 | else | ||||||
353 | { | ||||||
354 | if($self->{_LocationsList}->{nCurrItem} >= $#{$self->{_LocationsList}->{LocNames}}) | ||||||
355 | { | ||||||
356 | return -1; | ||||||
357 | } | ||||||
358 | elsif($self->{_LocationsList}->{nCurrItem} < $#{$self->{_LocationsList}->{LocNames}}) | ||||||
359 | { | ||||||
360 | $self->{_LocationsList}->{nCurrItem}++; | ||||||
361 | return 0; | ||||||
362 | } | ||||||
363 | } | ||||||
364 | } | ||||||
365 | |||||||
366 | #sends HTTP query to obtain prayer times for the required city and saves the | ||||||
367 | #results in run time structure | ||||||
368 | sub GetPrayerTimes | ||||||
369 | { | ||||||
370 | my ($self, $CityID) = @_; | ||||||
371 | |||||||
372 | my($nListType, $document, $remote, $StartData, $aline); | ||||||
373 | $nListType = eNoData; | ||||||
374 | |||||||
375 | #implementing error when GetPrayerTimes called before making a call to Open function | ||||||
376 | unless (host ne "") | ||||||
377 | { | ||||||
378 | $self->{ErrorCode} = IDS_ERROR_OpenNotCalled; | ||||||
379 | } | ||||||
380 | |||||||
381 | $document = "/cgi-bin/PMCalc.cgi?F=X&I=" . $self->{ClientID} . "&R="; | ||||||
382 | |||||||
383 | #Get the required list if Open function called before calling it | ||||||
384 | if($self->{ErrorCode} != IDS_ERROR_OpenNotCalled) | ||||||
385 | { | ||||||
386 | #store parameters received to functions local variables | ||||||
387 | |||||||
388 | #document is query string that will be passed to host | ||||||
389 | $document = $document . "GD&CT="; | ||||||
390 | $document = $document . $CityID; | ||||||
391 | |||||||
392 | #establish the connection to the host | ||||||
393 | $remote = IO::Socket::INET->new( Proto => "tcp", | ||||||
394 | PeerAddr => host, | ||||||
395 | PeerPort => "http(80)", | ||||||
396 | ); | ||||||
397 | |||||||
398 | #report an error if connection to host is not established | ||||||
399 | unless($remote) | ||||||
400 | { | ||||||
401 | die "cannot connect to http daemon on host ". host; | ||||||
402 | } | ||||||
403 | |||||||
404 | print $remote "GET $document HTTP/1.0" . BLANK; | ||||||
405 | |||||||
406 | $StartData = 0; | ||||||
407 | |||||||
408 | #while remote is returning cetain text | ||||||
409 | while(($StartData < 1) && (defined ($aline = <$remote>))) | ||||||
410 | { | ||||||
411 | #print " > " . $aline . "\n"; | ||||||
412 | #check if line is a blank line, after which the XML document follows | ||||||
413 | if($aline eq "\r\n") | ||||||
414 | { | ||||||
415 | $StartData = 1; | ||||||
416 | } | ||||||
417 | } | ||||||
418 | |||||||
419 | #make a call to function that will write response returned by host to file | ||||||
420 | #file will be parsed by XML parser and desired information will be stored in class member variables | ||||||
421 | ParseXMLFile($self, $remote, ePrayerTimings); | ||||||
422 | |||||||
423 | #close the connection established to host | ||||||
424 | close $remote; | ||||||
425 | |||||||
426 | #print " *** GetPrayerTimes ErrorCode: " . $self->{ErrorCode} . " ErrorText: " . $self->{ErrorText} . " \n"; |
||||||
427 | |||||||
428 | # if($self->{_DataType} == ePrayerTimings) | ||||||
429 | # { | ||||||
430 | #discard previously set errors | ||||||
431 | #Error Code IDS_ERROR_GetListNotCalled "Call GetList function first"; | ||||||
432 | #Error Code IDS_ERROR_GetPrayerTimesNotCalled "Call GetPrayerTimes function first"; | ||||||
433 | #Error Code IDS_ERROR_EmptyList "Empty List"; | ||||||
434 | # $self->{ErrorCode} = IDS_NO_Error | ||||||
435 | # if($self->{ErrorCode} == IDS_ERROR_GetListNotCalled || $self->{ErrorCode} == IDS_ERROR_GetPrayerTimesNotCalled || | ||||||
436 | # $self->{ErrorCode} == IDS_ERROR_EmptyList); | ||||||
437 | # } | ||||||
438 | |||||||
439 | #check whether some string is returned by host | ||||||
440 | # if($self->{_DataType} != ePrayerTimings) | ||||||
441 | # { | ||||||
442 | # $self->{ErrorCode} = IDS_ERROR_EmptyString | ||||||
443 | # if($self->{ErrorCode}) == IDS_NO_Error; | ||||||
444 | # } | ||||||
445 | #discard error that was set that empty string returned by host | ||||||
446 | # elsif($self->{ErrorCode} == IDS_ERROR_EmptyString) | ||||||
447 | # { | ||||||
448 | # $self->{ErrorCode} = IDS_NO_Error; | ||||||
449 | # } | ||||||
450 | } | ||||||
451 | } | ||||||
452 | |||||||
453 | #functions that will be called when asked for prayer timings | ||||||
454 | #returns parent element(Country's ID) to which this city belongs | ||||||
455 | sub GetCountryID | ||||||
456 | { | ||||||
457 | my $self = shift; | ||||||
458 | |||||||
459 | #$nListType is set to ePrayerTimings when asked for prayer timings | ||||||
460 | if($self->{_DataType} != ePrayerTimings) | ||||||
461 | { | ||||||
462 | $self->{ErrorCode} = IDS_ERROR_GetPrayerTimesNotCalled; | ||||||
463 | return -1; | ||||||
464 | } | ||||||
465 | else | ||||||
466 | { | ||||||
467 | return $self->{_TimesData}->{nCountryID}; | ||||||
468 | } | ||||||
469 | } | ||||||
470 | |||||||
471 | #returns ID of the city | ||||||
472 | sub CityID | ||||||
473 | { | ||||||
474 | my $self = shift; | ||||||
475 | |||||||
476 | #$nListType is set to ePrayerTimings when asked for prayer timings | ||||||
477 | if($self->{_DataType} != ePrayerTimings) | ||||||
478 | { | ||||||
479 | $self->{ErrorCode} = IDS_ERROR_GetPrayerTimesNotCalled; | ||||||
480 | return -1; | ||||||
481 | } | ||||||
482 | else | ||||||
483 | { | ||||||
484 | return $self->{_TimesData}->{nCityID}; | ||||||
485 | } | ||||||
486 | } | ||||||
487 | |||||||
488 | #returns Title of the city | ||||||
489 | sub CityTitle | ||||||
490 | { | ||||||
491 | my $self = shift; | ||||||
492 | |||||||
493 | #$nListType is set to ePrayerTimings when asked for prayer timings | ||||||
494 | if($self->{_DataType} != ePrayerTimings) | ||||||
495 | { | ||||||
496 | $self->{ErrorCode} = IDS_ERROR_GetPrayerTimesNotCalled; | ||||||
497 | return -1; | ||||||
498 | } | ||||||
499 | else | ||||||
500 | { | ||||||
501 | return $self->{_TimesData}->{sCityTitle}; | ||||||
502 | } | ||||||
503 | } | ||||||
504 | |||||||
505 | #returns time zone of the city | ||||||
506 | sub CityTimeZone | ||||||
507 | { | ||||||
508 | my $self = shift; | ||||||
509 | |||||||
510 | #$nListType is set to ePrayerTimings when asked for prayer timings | ||||||
511 | if($self->{_DataType} != ePrayerTimings) | ||||||
512 | { | ||||||
513 | $self->{ErrorCode} = IDS_ERROR_GetPrayerTimesNotCalled; | ||||||
514 | return -1; | ||||||
515 | } | ||||||
516 | else | ||||||
517 | { | ||||||
518 | return $self->{_TimesData}->{sCityTimeZone}; | ||||||
519 | } | ||||||
520 | } | ||||||
521 | |||||||
522 | #returns Islamic date for city | ||||||
523 | sub CityIslamicDate | ||||||
524 | { | ||||||
525 | my $self = shift; | ||||||
526 | |||||||
527 | #$nListType is set to ePrayerTimings when asked for prayer timings | ||||||
528 | if($self->{_DataType} != ePrayerTimings) | ||||||
529 | { | ||||||
530 | $self->{ErrorCode} = IDS_ERROR_GetPrayerTimesNotCalled; | ||||||
531 | return -1; | ||||||
532 | } | ||||||
533 | else | ||||||
534 | { | ||||||
535 | return $self->{_TimesData}->{sCityIslamicDate}; | ||||||
536 | } | ||||||
537 | } | ||||||
538 | |||||||
539 | #returns Gregorian date for city | ||||||
540 | sub CityGregorianDate | ||||||
541 | { | ||||||
542 | my $self = shift; | ||||||
543 | |||||||
544 | #$nListType is set to ePrayerTimings when asked for prayer timings | ||||||
545 | if($self->{_DataType} != ePrayerTimings) | ||||||
546 | { | ||||||
547 | $self->{ErrorCode} = IDS_ERROR_GetPrayerTimesNotCalled; | ||||||
548 | return -1; | ||||||
549 | } | ||||||
550 | else | ||||||
551 | { | ||||||
552 | return $self->{_TimesData}->{sCityGregorianDate}; | ||||||
553 | } | ||||||
554 | } | ||||||
555 | |||||||
556 | #returns default/official Asr Fiqh for city | ||||||
557 | sub CityAsrFiqh | ||||||
558 | { | ||||||
559 | my $self = shift; | ||||||
560 | |||||||
561 | #$nListType is set to ePrayerTimings when asked for prayer timings | ||||||
562 | if($self->{_DataType} != ePrayerTimings) | ||||||
563 | { | ||||||
564 | $self->{ErrorCode} = IDS_ERROR_GetPrayerTimesNotCalled; | ||||||
565 | return -1; | ||||||
566 | } | ||||||
567 | else | ||||||
568 | { | ||||||
569 | return $self->{_TimesData}->{sCityAsrFiqh}; | ||||||
570 | } | ||||||
571 | } | ||||||
572 | |||||||
573 | #returns requested prayer time for city | ||||||
574 | sub CityPrayerTime | ||||||
575 | { | ||||||
576 | my ($self, $PrayerName) = @_; | ||||||
577 | |||||||
578 | #$nListType is set to ePrayerTimings when asked for prayer timings | ||||||
579 | if($self->{_DataType} != ePrayerTimings) | ||||||
580 | { | ||||||
581 | $self->{ErrorCode} = IDS_ERROR_GetPrayerTimesNotCalled; | ||||||
582 | return -1; | ||||||
583 | } | ||||||
584 | else | ||||||
585 | { | ||||||
586 | return $self->{_TimesData}->{CityPrayerTimes}->{$PrayerName}; | ||||||
587 | } | ||||||
588 | } | ||||||
589 | |||||||
590 | #returns 0 for no error and non zero error code for error | ||||||
591 | sub GetError | ||||||
592 | { | ||||||
593 | my $self = shift; | ||||||
594 | return $self->{ErrorCode}; | ||||||
595 | } | ||||||
596 | |||||||
597 | #returns error string if error function will return non zero | ||||||
598 | sub GetErrorText | ||||||
599 | { | ||||||
600 | my $self = shift; | ||||||
601 | my $err = $self->{ErrorCode}; | ||||||
602 | if(($err >= IDS_NO_Error) && ($err <= IDS_ERROR_EmptyList)) | ||||||
603 | { | ||||||
604 | return $PMErrors[$err]; | ||||||
605 | } | ||||||
606 | else | ||||||
607 | { | ||||||
608 | if(defined($self->{ErrorText})) | ||||||
609 | { | ||||||
610 | return $self->{ErrorText} | ||||||
611 | } | ||||||
612 | else | ||||||
613 | { | ||||||
614 | return "Error text not received from server."; | ||||||
615 | } | ||||||
616 | } | ||||||
617 | } | ||||||
618 | |||||||
619 | #Function that will parse XML file | ||||||
620 | sub ParseXMLFile | ||||||
621 | { | ||||||
622 | #access arguments passed | ||||||
623 | my ($self, $xmlstream, $List) = @_; | ||||||
624 | |||||||
625 | #discard all element names previously stored in an array | ||||||
626 | $#{$self->{_LocationsList}->{LocNames}} = -1; | ||||||
627 | |||||||
628 | #discard all element IDs previously stored in an array | ||||||
629 | $#{$self->{_LocationsList}->{LocIDs}} = -1; | ||||||
630 | $self->{_LocationsList}->{nCurrItem} = 0; | ||||||
631 | |||||||
632 | #declare parser object | ||||||
633 | my $Parser = new XML::Parser(Style=>'Stream'); | ||||||
634 | |||||||
635 | #variable that will hold the text appearing within start and end tag that is country name | ||||||
636 | #my ($csText); | ||||||
637 | |||||||
638 | $Parser->{PMText} = ''; | ||||||
639 | $Parser->{PMListType} = $List; | ||||||
640 | $Parser->{PMObj} = $self; | ||||||
641 | |||||||
642 | #socket having response from the server | ||||||
643 | $Parser->parse($xmlstream); | ||||||
644 | |||||||
645 | $self->{_LocationsList}->{nCurrItem} = 0; | ||||||
646 | |||||||
647 | #Parser calls StartTag sub when it reads opening tag from XML file | ||||||
648 | sub StartTag | ||||||
649 | { | ||||||
650 | #first argument is reference to parser object and second is string containing element type name | ||||||
651 | my ($expat,$elementtype) = @_; | ||||||
652 | |||||||
653 | #print " -- StartTag: elementtype = " . $elementtype . " \n"; |
||||||
654 | if($elementtype eq "PMxml") | ||||||
655 | { | ||||||
656 | my $datatype = %_->{datatype}; | ||||||
657 | #print " -- StartTag: datatype = " . $datatype . " \n"; |
||||||
658 | if($datatype eq "Error") | ||||||
659 | { $expat->{PMObj}->{_DataType} = eNoData; } | ||||||
660 | elsif($datatype eq "Errors") | ||||||
661 | { $expat->{PMObj}->{_DataType} = eNoData; } | ||||||
662 | elsif($datatype eq "PrayerTimes") | ||||||
663 | { $expat->{PMObj}->{_DataType} = ePrayerTimings; } | ||||||
664 | else | ||||||
665 | { $expat->{PMObj}->{_DataType} = eLocationsList; } | ||||||
666 | } | ||||||
667 | } | ||||||
668 | |||||||
669 | #Parser calls Text sub when it sees characters. $_ contains text up to next tag, end tag, | ||||||
670 | # processing instruction, or comment | ||||||
671 | sub Text | ||||||
672 | { | ||||||
673 | my($expat) = @_; | ||||||
674 | |||||||
675 | #print " -- expat = $expat, text= " . $_ . " \n"; |
||||||
676 | #translate new line to single space | ||||||
677 | tr/\n/ /; | ||||||
678 | #discard all leading white space characters | ||||||
679 | s/^\s+//; | ||||||
680 | #discard all trailing white space characters | ||||||
681 | s/\s+$//; | ||||||
682 | |||||||
683 | #return empty string if nothing found as text | ||||||
684 | return if $_ eq ""; | ||||||
685 | |||||||
686 | #if there is found some text within start and end tag then store it to $parabuf | ||||||
687 | $expat->{PMText} = $_; | ||||||
688 | #print "text= " . $_ . " \n"; |
||||||
689 | } | ||||||
690 | |||||||
691 | #Parser calls EndTag sub when it reads end tag from XML file | ||||||
692 | sub EndTag | ||||||
693 | { | ||||||
694 | #first argument is reference to parser object and second is string containing element type name | ||||||
695 | my ($expat,$elementtype) = @_; | ||||||
696 | my $self = $expat->{PMObj}; | ||||||
697 | my $nX = $self->{_LocationsList}->{nCurrItem}; | ||||||
698 | |||||||
699 | #print " -- End: index = " . $nX . ", element type = " . $elementtype . ", value =" . $expat->{PMText} . " \n"; |
||||||
700 | #print " > EndTag: DataType = " . $self->{_DataType} . " \n"; |
||||||
701 | |||||||
702 | #get name and ID from XML file when asked for continents, countries or cities listing | ||||||
703 | if($self->{_DataType} == eLocationsList) | ||||||
704 | { | ||||||
705 | if ($elementtype eq "LocID") | ||||||
706 | { | ||||||
707 | #Storing element ID accessed, to an array | ||||||
708 | $self->{_LocationsList}->{LocIDs}->[$nX] = $expat->{PMText}; | ||||||
709 | } | ||||||
710 | if ($elementtype eq "Name") | ||||||
711 | { | ||||||
712 | #Storing name accessed, to an array | ||||||
713 | $self->{_LocationsList}->{LocNames}->[$nX] = $expat->{PMText}; | ||||||
714 | $self->{_LocationsList}->{nCurrItem} = $nX + 1; | ||||||
715 | } | ||||||
716 | } | ||||||
717 | #asked for prayer times | ||||||
718 | elsif($self->{_DataType} == ePrayerTimings) | ||||||
719 | { | ||||||
720 | #get city local id | ||||||
721 | if ($elementtype eq "LocID") | ||||||
722 | { | ||||||
723 | $self->{_TimesData}->{nCityID} = $expat->{PMText}; | ||||||
724 | } | ||||||
725 | #get city title | ||||||
726 | if ($elementtype eq "LocTitle") | ||||||
727 | { | ||||||
728 | $self->{_TimesData}->{sCityTitle} = $expat->{PMText}; | ||||||
729 | } | ||||||
730 | #get Country ID | ||||||
731 | if ($elementtype eq "CountryID") | ||||||
732 | { | ||||||
733 | $self->{_TimesData}->{nCountryID} = $expat->{PMText}; | ||||||
734 | } | ||||||
735 | #get State ID | ||||||
736 | if ($elementtype eq "StateID") | ||||||
737 | { | ||||||
738 | $self->{_TimesData}->{nStateID} = $expat->{PMText}; | ||||||
739 | } | ||||||
740 | #get city title | ||||||
741 | if ($elementtype eq "LocTitle") | ||||||
742 | { | ||||||
743 | $self->{_TimesData}->{sCityTitle} = $expat->{PMText}; | ||||||
744 | } | ||||||
745 | #get city time zone | ||||||
746 | if ($elementtype eq "LocTimeZone") | ||||||
747 | { | ||||||
748 | $self->{_TimesData}->{sCityTimeZone} = $expat->{PMText}; | ||||||
749 | } | ||||||
750 | #get city islamic date | ||||||
751 | if ($elementtype eq "IDate") | ||||||
752 | { | ||||||
753 | $self->{_TimesData}->{sCityIslamicDate} = $expat->{PMText}; | ||||||
754 | } | ||||||
755 | #get city gregorian date | ||||||
756 | if ($elementtype eq "GDate") | ||||||
757 | { | ||||||
758 | $self->{_TimesData}->{sCityGregorianDate} = $expat->{PMText}; | ||||||
759 | } | ||||||
760 | #get city As Fiqh | ||||||
761 | if ($elementtype eq "AsrFiqh") | ||||||
762 | { | ||||||
763 | $self->{_TimesData}->{sCityAsrFiqh} = $expat->{PMText}; | ||||||
764 | } | ||||||
765 | #get fajr timing | ||||||
766 | if ($elementtype eq "Fajr") | ||||||
767 | { | ||||||
768 | $self->{_TimesData}->{CityPrayerTimes}->{$elementtype} = $expat->{PMText}; | ||||||
769 | } | ||||||
770 | #get Shurooq timing | ||||||
771 | if ($elementtype eq "Shurooq") | ||||||
772 | { | ||||||
773 | $self->{_TimesData}->{CityPrayerTimes}->{$elementtype} = $expat->{PMText}; | ||||||
774 | } | ||||||
775 | #get Zuhr timing | ||||||
776 | if ($elementtype eq "Zuhr") | ||||||
777 | { | ||||||
778 | $self->{_TimesData}->{CityPrayerTimes}->{$elementtype} = $expat->{PMText}; | ||||||
779 | } | ||||||
780 | #get AsrHanafi timing | ||||||
781 | if ($elementtype eq "AsrHanafi") | ||||||
782 | { | ||||||
783 | $self->{_TimesData}->{CityPrayerTimes}->{$elementtype} = $expat->{PMText}; | ||||||
784 | } | ||||||
785 | #get AsrShafei timing | ||||||
786 | if ($elementtype eq "AsrShafei") | ||||||
787 | { | ||||||
788 | $self->{_TimesData}->{CityPrayerTimes}->{$elementtype} = $expat->{PMText}; | ||||||
789 | } | ||||||
790 | #get Maghrib timing | ||||||
791 | if ($elementtype eq "Maghrib") | ||||||
792 | { | ||||||
793 | $self->{_TimesData}->{CityPrayerTimes}->{$elementtype} = $expat->{PMText}; | ||||||
794 | } | ||||||
795 | #get Isha timing | ||||||
796 | if ($elementtype eq "Isha") | ||||||
797 | { | ||||||
798 | $self->{_TimesData}->{CityPrayerTimes}->{$elementtype} = $expat->{PMText}; | ||||||
799 | } | ||||||
800 | } | ||||||
801 | else | ||||||
802 | { | ||||||
803 | #get error code | ||||||
804 | if ($elementtype eq "ErrorCode") | ||||||
805 | { | ||||||
806 | $self->{ErrorCode} = $expat->{PMText}; | ||||||
807 | $self->{ErrorCode} += 500 if $self->{ErrorCode} < 500; | ||||||
808 | } | ||||||
809 | #get error text | ||||||
810 | if ($elementtype eq "ErrorText") | ||||||
811 | { | ||||||
812 | $self->{ErrorText} = $expat->{PMText}; | ||||||
813 | } | ||||||
814 | } | ||||||
815 | $expat->{PMText} = ''; | ||||||
816 | } | ||||||
817 | } | ||||||
818 | |||||||
819 | 1; | ||||||
820 | __END__ |