NCXMLCommentsParser.m 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. //
  2. // NCXMLCommentsParser.m
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 08/08/19.
  6. // Copyright © 2018 Marino Faggiana. All rights reserved.
  7. //
  8. // Author Marino Faggiana <marino.faggiana@nextcloud.com>
  9. //
  10. // This program is free software: you can redistribute it and/or modify
  11. // it under the terms of the GNU General Public License as published by
  12. // the Free Software Foundation, either version 3 of the License, or
  13. // (at your option) any later version.
  14. //
  15. // This program is distributed in the hope that it will be useful,
  16. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. // GNU General Public License for more details.
  19. //
  20. // You should have received a copy of the GNU General Public License
  21. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. //
  23. #import "NCXMLCommentsParser.h"
  24. @implementation NCXMLCommentsParser
  25. - (void)initParserWithData: (NSData*)data{
  26. self.list = [NSMutableArray new];
  27. NSXMLParser *parser = [[NSXMLParser alloc] initWithData:data];
  28. [parser setDelegate:self];
  29. [parser parse];
  30. }
  31. #pragma mark - XML Parser Delegate Methods
  32. - (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict {
  33. if (!self.xmlChars) {
  34. self.xmlChars = [NSMutableString string];
  35. }
  36. [self.xmlChars setString:@""];
  37. }
  38. - (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
  39. if ([elementName isEqualToString:@"d:href"]) {
  40. self.currentComment = [NCComments new];
  41. } else if ([elementName isEqualToString:@"oc:id"]) {
  42. self.currentComment.messageID = [NSString stringWithString:self.xmlChars];
  43. } else if ([elementName isEqualToString:@"oc:verb"]) {
  44. self.currentComment.verb = [NSString stringWithString:self.xmlChars];
  45. } else if ([elementName isEqualToString:@"oc:actorType"]) {
  46. self.currentComment.actorType = [NSString stringWithString:self.xmlChars];
  47. } else if ([elementName isEqualToString:@"oc:actorId"]) {
  48. self.currentComment.actorId = [NSString stringWithString:self.xmlChars];
  49. } else if ([elementName isEqualToString:@"oc:creationDateTime"]) {
  50. if ([self.xmlChars length]) {
  51. NSDateFormatter *dateFormatter = [NSDateFormatter new];
  52. [dateFormatter setDateFormat:@"EEE, dd MMM y HH:mm:ss zzz"];
  53. [dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"]];
  54. self.currentComment.creationDateTime = [dateFormatter dateFromString:[NSString stringWithString:self.xmlChars]];
  55. }
  56. } else if ([elementName isEqualToString:@"oc:objectType"]) {
  57. self.currentComment.objectType = [NSString stringWithString:self.xmlChars];
  58. } else if ([elementName isEqualToString:@"oc:objectId"]) {
  59. self.currentComment.objectId = [NSString stringWithString:self.xmlChars];
  60. } else if ([elementName isEqualToString:@"oc:isUnread"]) {
  61. self.currentComment.isUnread = [self.xmlChars boolValue];
  62. } else if ([elementName isEqualToString:@"oc:message"]) {
  63. self.currentComment.message = [NSString stringWithString:self.xmlChars];
  64. } else if ([elementName isEqualToString:@"oc:actorDisplayName"]) {
  65. self.currentComment.actorDisplayName = [NSString stringWithString:self.xmlChars];
  66. } else if ([elementName isEqualToString:@"d:status"]) {
  67. self.status = [NSString stringWithString:self.xmlChars];
  68. } else if ([elementName isEqualToString:@"d:response"]) {
  69. if ([self.status containsString:@"200"]) {
  70. [self.list addObject:self.currentComment];
  71. }
  72. }
  73. }
  74. - (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
  75. [self.xmlChars appendString:string];
  76. }
  77. - (void)parserDidEndDocument:(NSXMLParser *)parser {
  78. NSLog(@"Finish xml comments parse");
  79. }
  80. @end