OCXMLListParser.m 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. #import "OCXMLListParser.h"
  2. #import "NSString+Encode.h"
  3. @implementation OCXMLListParser
  4. @synthesize searchList=_searchList;
  5. @synthesize currentFile=_currentFile;
  6. /*
  7. * Method that init the parse with the xml data from the server
  8. * @data -> XML webDav data from the owncloud server
  9. */
  10. - (void)initParserWithData: (NSData*)data{
  11. _searchList = [NSMutableArray new];
  12. NSXMLParser *parser = [[NSXMLParser alloc] initWithData:data];
  13. [parser setDelegate:self];
  14. [parser parse];
  15. }
  16. #pragma mark - XML Parser Delegate Methods
  17. /*
  18. * Method that init parse process.
  19. */
  20. - (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict {
  21. if (!_xmlChars) {
  22. _xmlChars = [NSMutableString string];
  23. }
  24. //NSLog(@"_xmlChars: %@", _xmlChars);
  25. [_xmlChars setString:@""];
  26. if ([elementName isEqualToString:@"d:response"]) {
  27. _xmlBucket = [NSMutableDictionary dictionary];
  28. }
  29. }
  30. /*
  31. * Util method to make a NSDate object from a string from xml
  32. * @dateString -> Data string from xml
  33. */
  34. + (NSDate*)parseDateString:(NSString*)dateString {
  35. //Parse the date in all the formats
  36. NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
  37. /*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.*/
  38. [dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"]];
  39. //This is the format for the concret locale used
  40. [dateFormatter setDateFormat:@"EEE, dd MMM y HH:mm:ss zzz"];
  41. NSDate *theDate = nil;
  42. NSError *error = nil;
  43. if (![dateFormatter getObjectValue:&theDate forString:dateString range:nil error:&error]) {
  44. NSLog(@"Date '%@' could not be parsed: %@", dateString, error);
  45. }
  46. return theDate;
  47. }
  48. - (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
  49. // NSLog(@"elementName: %@:%@", elementName,_xmlChars);
  50. if ([elementName isEqualToString:@"d:href"]) {
  51. if ([_xmlChars hasPrefix:@"http"]) {
  52. NSURL *junk = [NSURL URLWithString:_xmlChars];
  53. BOOL trailingSlash = [_xmlChars hasSuffix:@"/"];
  54. [_xmlChars setString:[junk path]];
  55. if (trailingSlash) {
  56. [_xmlChars appendString:@"/"];
  57. }
  58. }
  59. //If has lenght, there are an item
  60. if ([_xmlChars length]) {
  61. //BUG ?? https://github.com/nextcloud/server/issues/6925
  62. _xmlChars = (NSMutableString *)[_xmlChars stringByReplacingOccurrencesOfString:@"//" withString:@"/"];
  63. //Create FileDto
  64. _currentFile = [OCFileDto new];
  65. _currentFile.isDirectory = NO;
  66. [_xmlBucket setObject:[_xmlChars copy] forKey:@"uri"];
  67. NSArray *splitedUrl = [_xmlChars componentsSeparatedByString:@"/"];
  68. //Check if the item is a folder or a file
  69. if([_xmlChars hasSuffix:@"/"]) {
  70. //It's a folder
  71. NSInteger fileNameLenght = [((NSString *)[splitedUrl objectAtIndex:[splitedUrl count]-2]) length];
  72. if ( fileNameLenght > 0) {
  73. //FileDto filepath
  74. _currentFile.filePath = [_xmlChars substringToIndex:[_xmlChars length] - (fileNameLenght+1)];
  75. } else {
  76. _currentFile.filePath = @"/";
  77. }
  78. } else {
  79. //It's a file
  80. NSInteger fileNameLenght = [((NSString *)[splitedUrl objectAtIndex:[splitedUrl count]-1]) length];
  81. if (fileNameLenght > 0) {
  82. _currentFile.filePath = [_xmlChars substringToIndex:[_xmlChars length] - fileNameLenght];
  83. }else {
  84. _currentFile.filePath = @"/";
  85. }
  86. }
  87. NSArray *foo = [_xmlChars componentsSeparatedByString: @"/"];
  88. NSString *lastBit;
  89. if([_xmlChars hasSuffix:@"/"]) {
  90. lastBit = [foo objectAtIndex: [foo count]-2];
  91. lastBit = [NSString stringWithFormat:@"%@/",lastBit];
  92. } else {
  93. lastBit = [foo objectAtIndex: [foo count]-1];
  94. }
  95. [_xmlBucket setObject:lastBit forKey:@"href"];
  96. _currentFile.fileName = lastBit;
  97. self.currentFile.fileName = [self.currentFile.fileName stringByRemovingPercentEncoding];
  98. self.currentFile.filePath = [self.currentFile.filePath stringByRemovingPercentEncoding];
  99. }
  100. } else if ([elementName isEqualToString:@"d:getlastmodified"]) {
  101. //DATE
  102. // 'Thu, 30 Oct 2008 02:52:47 GMT'
  103. // Monday, 12-Jan-98 09:25:56 GMT
  104. // Value: HTTP-date ; defined in section 3.3.1 of RFC2068
  105. if ([_xmlChars length]) {
  106. NSDate *d = [[self class] parseDateString:_xmlChars];
  107. if (d) {
  108. //FildeDto Date
  109. _currentFile.date = [d timeIntervalSince1970];
  110. NSInteger colIdx = [elementName rangeOfString:@":"].location;
  111. [_xmlBucket setObject:d forKey:[elementName substringFromIndex:colIdx + 1]];
  112. }
  113. else {
  114. NSLog(@"Could not parse date string '%@' for '%@'", _xmlChars, elementName);
  115. }
  116. }
  117. } else if ([elementName isEqualToString:@"oc:id"]) {
  118. _currentFile.ocId = _xmlChars;
  119. } else if ([elementName hasSuffix:@":getetag"] && [_xmlChars length]) {
  120. //ETAG
  121. NSLog(@"getetag: %@", _xmlChars);
  122. NSString *stringClean = _xmlChars;
  123. stringClean = [_xmlChars stringByReplacingOccurrencesOfString:@"\"" withString:@""];
  124. _currentFile.etag = [stringClean lowercaseString];
  125. } else if ([elementName hasSuffix:@":getcontenttype"] && [_xmlChars length]) {
  126. //CONTENT TYPE
  127. [_xmlBucket setObject:[_xmlChars copy] forKey:@"contenttype"];
  128. } else if([elementName hasSuffix:@"d:getcontentlength"] && [_xmlChars length]) {
  129. //SIZE
  130. //FileDto current size
  131. _currentFile.size = (long)[_xmlChars longLongValue];
  132. } else if ([elementName isEqualToString:@"oc:permissions"]) {
  133. _currentFile.permissions = _xmlChars;
  134. } else if ([elementName isEqualToString:@"d:collection"]) {
  135. _currentFile.isDirectory = YES;
  136. } else if ([elementName isEqualToString:@"d:response"]) {
  137. //Add to searchList
  138. [_searchList addObject:_currentFile];
  139. _currentFile = [OCFileDto new];
  140. _xmlBucket = nil;
  141. } else if ([elementName isEqualToString:@"oc:favorite"]) {
  142. _currentFile.isFavorite = [_xmlChars boolValue];
  143. } else if ([elementName isEqualToString:@"nc:is-encrypted"]) {
  144. _currentFile.isEncrypted = [_xmlChars boolValue];
  145. }
  146. }
  147. - (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
  148. [_xmlChars appendString:string];
  149. }
  150. - (void)parserDidEndDocument:(NSXMLParser *)parser{
  151. NSLog(@"Finish xml directory list parse");
  152. }
  153. @end