NCXMLListParser.m 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. //
  2. // NCXMLListParser.m
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 17/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 "NCXMLListParser.h"
  24. @implementation NCXMLListParser
  25. - (void)initParserWithData:(NSData *)data controlFirstFileOfList:(BOOL)controlFirstFileOfList {
  26. self.list = [NSMutableArray new];
  27. self.controlFirstFileOfList = controlFirstFileOfList;
  28. NSXMLParser *parser = [[NSXMLParser alloc] initWithData:data];
  29. [parser setDelegate:self];
  30. [parser parse];
  31. }
  32. #pragma mark - XML Parser Delegate Methods
  33. - (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict {
  34. if (!self.xmlChars) {
  35. self.xmlChars = [NSMutableString string];
  36. }
  37. [self.xmlChars setString:@""];
  38. }
  39. - (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
  40. if ([elementName isEqualToString:@"d:href"]) {
  41. if ([self.xmlChars hasPrefix:@"http"]) {
  42. NSURL *junk = [NSURL URLWithString:self.xmlChars];
  43. BOOL trailingSlash = [_xmlChars hasSuffix:@"/"];
  44. [self.xmlChars setString:[junk path]];
  45. if (trailingSlash) {
  46. [self.xmlChars appendString:@"/"];
  47. }
  48. }
  49. if ([self.xmlChars length]) {
  50. self.xmlChars = (NSMutableString *)[_xmlChars stringByReplacingOccurrencesOfString:@"//" withString:@"/"];
  51. //Create FileDto
  52. self.currentFile = [OCFileDto new];
  53. self.currentFile.isDirectory = NO;
  54. NSArray *splitedUrl = [self.xmlChars componentsSeparatedByString:@"/"];
  55. if([self.xmlChars hasSuffix:@"/"]) {
  56. //It's a folder
  57. NSInteger fileNameLenght = [((NSString *)[splitedUrl objectAtIndex:[splitedUrl count]-2]) length];
  58. if ( fileNameLenght > 0) {
  59. self.currentFile.filePath = [self.xmlChars substringToIndex:[self.xmlChars length] - (fileNameLenght+1)];
  60. } else {
  61. self.currentFile.filePath = @"/";
  62. }
  63. } else {
  64. //It's a file
  65. NSInteger fileNameLenght = [((NSString *)[splitedUrl objectAtIndex:[splitedUrl count]-1]) length];
  66. if (fileNameLenght > 0) {
  67. self.currentFile.filePath = [self.xmlChars substringToIndex:[self.xmlChars length] - fileNameLenght];
  68. }else {
  69. self.currentFile.filePath = @"/";
  70. }
  71. }
  72. NSArray *foo = [self.xmlChars componentsSeparatedByString: @"/"];
  73. NSString *lastBit;
  74. if([self.xmlChars hasSuffix:@"/"]) {
  75. lastBit = [foo objectAtIndex: [foo count]-2];
  76. lastBit = [NSString stringWithFormat:@"%@/",lastBit];
  77. } else {
  78. lastBit = [foo objectAtIndex: [foo count]-1];
  79. }
  80. if (self.controlFirstFileOfList) {
  81. if (isNotFirstFileOfList) {
  82. self.currentFile.fileName = lastBit;
  83. }
  84. } else {
  85. self.currentFile.fileName = lastBit;
  86. }
  87. self.currentFile.fileName = [self.currentFile.fileName stringByRemovingPercentEncoding];
  88. self.currentFile.filePath = [self.currentFile.filePath stringByRemovingPercentEncoding];
  89. isNotFirstFileOfList = true;
  90. }
  91. } else if ([elementName isEqualToString:@"d:displayname"] && [self.xmlChars length]) {
  92. self.currentFile.displayName = [NSString stringWithString:self.xmlChars];
  93. } else if ([elementName isEqualToString:@"d:getcontenttype"] && [self.xmlChars length]) {
  94. self.currentFile.contentType = [NSString stringWithString:self.xmlChars];
  95. } else if ([elementName isEqualToString:@"d:resourcetype"] && [self.xmlChars length]) {
  96. self.currentFile.resourceType = [NSString stringWithString:self.xmlChars];
  97. } else if ([elementName isEqualToString:@"d:getcontentlength"] && [self.xmlChars length]) {
  98. self.currentFile.size = (long)[self.xmlChars longLongValue];
  99. } else if ([elementName isEqualToString:@"d:getlastmodified"] && [self.xmlChars length]) {
  100. NSDateFormatter *dateFormatter = [NSDateFormatter new];
  101. [dateFormatter setDateFormat:@"EEE, dd MMM y HH:mm:ss zzz"];
  102. [dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"]];
  103. NSDate *date = [dateFormatter dateFromString:[NSString stringWithString:self.xmlChars]];
  104. if (date) {
  105. self.currentFile.date = [date timeIntervalSince1970];
  106. }
  107. } else if ([elementName isEqualToString:@"d:creationdate"] && [self.xmlChars length]) {
  108. NSLog(@"Not yet implemented");
  109. } else if ([elementName isEqualToString:@"d:getetag"] && [self.xmlChars length]) {
  110. self.currentFile.etag = [[self.xmlChars stringByReplacingOccurrencesOfString:@"\"" withString:@""] lowercaseString];
  111. } else if ([elementName isEqualToString:@"d:quota-used-bytes"] && [self.xmlChars length]) {
  112. self.currentFile.quotaUsedBytes = (long)[self.xmlChars longLongValue];
  113. } else if ([elementName isEqualToString:@"d:quota-available-bytes"] && [self.xmlChars length]) {
  114. self.currentFile.quotaAvailableBytes = (long)[self.xmlChars longLongValue];
  115. } else if ([elementName isEqualToString:@"oc:permissions"] && [self.xmlChars length]) {
  116. self.currentFile.permissions = [NSString stringWithString:self.xmlChars];
  117. } else if ([elementName isEqualToString:@"oc:id"] && [self.xmlChars length]) {
  118. self.currentFile.ocId = [NSString stringWithString:self.xmlChars];
  119. } else if ([elementName isEqualToString:@"oc:fileid"] && [self.xmlChars length]) {
  120. self.currentFile.fileId = [NSString stringWithString:self.xmlChars];
  121. } else if ([elementName isEqualToString:@"oc:size"] && [self.xmlChars length]) {
  122. self.currentFile.size = (long)[self.xmlChars longLongValue];
  123. } else if ([elementName isEqualToString:@"oc:favorite"] && [self.xmlChars length]) {
  124. self.currentFile.isFavorite = [self.xmlChars boolValue];
  125. } else if ([elementName isEqualToString:@"nc:is-encrypted"] && [self.xmlChars length]) {
  126. self.currentFile.isEncrypted = [self.xmlChars boolValue];
  127. } else if ([elementName isEqualToString:@"nc:mount-type"] && [self.xmlChars length]) {
  128. self.currentFile.mountType = [NSString stringWithString:self.xmlChars];
  129. } else if ([elementName isEqualToString:@"oc:owner-id"] && [self.xmlChars length]) {
  130. self.currentFile.ownerId = [NSString stringWithString:self.xmlChars];
  131. } else if ([elementName isEqualToString:@"oc:owner-display-name"] && [self.xmlChars length]) {
  132. self.currentFile.ownerDisplayName = [NSString stringWithString:self.xmlChars];
  133. } else if ([elementName isEqualToString:@"oc:comments-unread"] && [self.xmlChars length]) {
  134. self.currentFile.commentsUnread = [self.xmlChars boolValue];
  135. } else if ([elementName isEqualToString:@"nc:has-preview"] && [self.xmlChars length]) {
  136. self.currentFile.hasPreview = [self.xmlChars boolValue];
  137. } else if ([elementName isEqualToString:@"nc:trashbin-filename"] && [self.xmlChars length]) {
  138. self.currentFile.trashbinFileName = [NSString stringWithString:self.xmlChars];
  139. } else if ([elementName isEqualToString:@"nc:trashbin-original-location"] && [self.xmlChars length]) {
  140. self.currentFile.trashbinOriginalLocation = [NSString stringWithString:self.xmlChars];
  141. } else if ([elementName isEqualToString:@"nc:trashbin-deletion-time"] && [self.xmlChars length]) {
  142. self.currentFile.trashbinDeletionTime = (long)[self.xmlChars longLongValue];
  143. } else if ([elementName isEqualToString:@"d:collection"]) {
  144. self.currentFile.isDirectory = true;
  145. } else if ([elementName isEqualToString:@"d:response"]) {
  146. [self.list addObject:self.currentFile];
  147. }
  148. }
  149. - (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
  150. [self.xmlChars appendString:string];
  151. }
  152. - (void)parserDidEndDocument:(NSXMLParser *)parser {
  153. NSLog(@"Finish xml list parse");
  154. }
  155. @end