OCXMLNotificationsParser.m 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. //
  2. // OCXMLNotificationsParser.m
  3. // ownCloud iOS library
  4. //
  5. // Created by Marino Faggiana on 23/01/17.
  6. // Copyright © 2017 ownCloud. All rights reserved.
  7. //
  8. #import "OCXMLNotificationsParser.h"
  9. @implementation OCXMLNotificationsParser
  10. @synthesize notificationsList =_notificationsList;
  11. @synthesize currentNotifications =_currentNotifications;
  12. /*
  13. * Method that init the parse with the xml data from the server
  14. * @data -> XML webDav data from the owncloud server
  15. */
  16. - (void)initParserWithData: (NSData*)data{
  17. _notificationsList = [[NSMutableArray alloc]init];
  18. NSXMLParser *parser = [[NSXMLParser alloc] initWithData:data];
  19. [parser setDelegate:self];
  20. [parser parse];
  21. }
  22. /*
  23. * Method that init parse process.
  24. */
  25. - (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict {
  26. if (!_xmlChars) {
  27. _xmlChars = [NSMutableString string];
  28. }
  29. //NSLog(@"_xmlChars: %@", _xmlChars);
  30. [_xmlChars setString:@""];
  31. if ([elementName isEqualToString:@"d:response"]) {
  32. _xmlBucket = [NSMutableDictionary dictionary];
  33. }
  34. }
  35. /*
  36. * Util method to make a NSDate object from a string from xml
  37. * @dateString -> Data string from xml
  38. */
  39. + (NSDate*)parseDateString:(NSString*)dateString {
  40. //Parse the date in all the formats
  41. NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
  42. /*In most cases the best locale to choose is "en_US_POSIX", a locale that's specifically designed to yield US English results regardless of both user and system preferences. "en_US_POSIX" is also invariant in time (if the US, at some point in the future, changes the way it formats dates, "en_US" will change to reflect the new behaviour, but "en_US_POSIX" will not). It will behave consistently for all users.*/
  43. [dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"]];
  44. //This is the format for the concret locale used
  45. [dateFormatter setDateFormat:@"EEE, dd MMM y HH:mm:ss zzz"];
  46. NSDate *theDate = nil;
  47. NSError *error = nil;
  48. if (![dateFormatter getObjectValue:&theDate forString:dateString range:nil error:&error]) {
  49. NSLog(@"Date '%@' could not be parsed: %@", dateString, error);
  50. }
  51. return theDate;
  52. }
  53. // Decode a percent escape encoded string.
  54. - (NSString*) decodeFromPercentEscapeString:(NSString *) string {
  55. return (__bridge NSString *) CFURLCreateStringByReplacingPercentEscapesUsingEncoding(NULL,
  56. (__bridge CFStringRef) string,
  57. CFSTR(""),
  58. kCFStringEncodingUTF8);
  59. }
  60. @end