OCXMLSharedParser.m 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. //
  2. // OCXMLSharedParser.m
  3. // OCCommunicationLib
  4. //
  5. // Copyright (C) 2016, ownCloud GmbH. ( http://www.owncloud.org/ )
  6. //
  7. // Permission is hereby granted, free of charge, to any person obtaining a copy
  8. // of this software and associated documentation files (the "Software"), to deal
  9. // in the Software without restriction, including without limitation the rights
  10. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. // copies of the Software, and to permit persons to whom the Software is
  12. // furnished to do so, subject to the following conditions:
  13. // The above copyright notice and this permission notice shall be included in
  14. // all copies or substantial portions of the Software.
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. //
  24. #import "OCXMLSharedParser.h"
  25. #import "OCSharedDto.h"
  26. #define expirationDateFormat @"YYYY-MM-dd HH:mm:ss"
  27. @implementation OCXMLSharedParser
  28. @synthesize shareList=_shareList;
  29. @synthesize currentShared=_currentShared;
  30. /*
  31. * Method that init the parse with the xml data from the server
  32. * @data -> XML webDav data from the owncloud server
  33. */
  34. - (void)initParserWithData: (NSData*)data{
  35. _shareList = [[NSMutableArray alloc]init];
  36. NSXMLParser *parser = [[NSXMLParser alloc] initWithData:data];
  37. [parser setDelegate:self];
  38. [parser parse];
  39. }
  40. #pragma mark - XML Parser Delegate Methods
  41. /*
  42. * Method that init parse process.
  43. */
  44. - (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict {
  45. if (!_xmlChars) {
  46. _xmlChars = [NSMutableString string];
  47. }
  48. //NSLog(@"_xmlChars: %@", _xmlChars);
  49. [_xmlChars setString:@""];
  50. if ([elementName isEqualToString:@"element"]) {
  51. _xmlBucket = [NSMutableDictionary dictionary];
  52. if (_currentShared) {
  53. [_shareList addObject:_currentShared];
  54. }
  55. }
  56. }
  57. /*
  58. * Util method to make a NSDate object from a string from xml
  59. * @dateString -> Data string from xml
  60. */
  61. + (NSDate*)parseDateString:(NSString*)dateString {
  62. //Parse the date in all the formats
  63. NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
  64. /*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.*/
  65. [dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"]];
  66. [dateFormatter setDateFormat:expirationDateFormat];
  67. NSDate *theDate = nil;
  68. NSError *error = nil;
  69. if (![dateFormatter getObjectValue:&theDate forString:dateString range:nil error:&error]) {
  70. NSLog(@"Date '%@' could not be parsed: %@", dateString, error);
  71. }
  72. return theDate;
  73. }
  74. - (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
  75. //NSLog(@"elementName: %@:%@", elementName,_xmlChars);
  76. if ([elementName isEqualToString:@"id"]) {
  77. _currentShared = [[OCSharedDto alloc] init];
  78. _currentShared.idRemoteShared = [_xmlChars intValue];
  79. } else if ([elementName isEqualToString:@"item_type"]) {
  80. if([_xmlChars isEqualToString:@"file"]) {
  81. _currentShared.isDirectory = NO;
  82. } else {
  83. _currentShared.isDirectory = YES;
  84. if (_currentShared.path) {
  85. _currentShared.path = [_currentShared.path stringByAppendingString:@"/"];
  86. }
  87. }
  88. } else if ([elementName isEqualToString:@"item_source"]) {
  89. _currentShared.itemSource = [_xmlChars intValue];
  90. } else if ([elementName isEqualToString:@"parent"]) {
  91. _currentShared.parent = [_xmlChars intValue];
  92. } else if ([elementName isEqualToString:@"share_type"]) {
  93. _currentShared.shareType = [_xmlChars intValue];
  94. } else if ([elementName isEqualToString:@"share_with"]) {
  95. _currentShared.shareWith = _xmlChars;
  96. } else if ([elementName isEqualToString:@"file_source"]) {
  97. _currentShared.fileSource = [_xmlChars intValue];
  98. } else if ([elementName isEqualToString:@"path"]) {
  99. if (_currentShared.isDirectory) {
  100. _currentShared.path = [_xmlChars stringByAppendingString:@"/"];
  101. } else {
  102. _currentShared.path = _xmlChars;
  103. }
  104. } else if ([elementName isEqualToString:@"permissions"]) {
  105. _currentShared.permissions = [_xmlChars intValue];
  106. } else if ([elementName isEqualToString:@"stime"]) {
  107. _currentShared.sharedDate = (long)[_xmlChars longLongValue];
  108. } else if ([elementName isEqualToString:@"expiration"]) {
  109. //Check expiration, is a optional field, sometimes is empty
  110. if (![_xmlChars isEqualToString:@""]) {
  111. NSDate *date = [[self class] parseDateString:_xmlChars];
  112. _currentShared.expirationDate = [date timeIntervalSince1970];
  113. }else{
  114. _currentShared.expirationDate = 0.0;
  115. }
  116. } else if ([elementName isEqualToString:@"token"]) {
  117. _currentShared.token = _xmlChars;
  118. } else if ([elementName isEqualToString:@"storage"]) {
  119. _currentShared.storage = [_xmlChars intValue];
  120. } else if ([elementName isEqualToString:@"mail_send"]) {
  121. _currentShared.mailSend = [_xmlChars intValue];
  122. } else if ([elementName isEqualToString:@"uid_owner"]) {
  123. _currentShared.uidOwner = _xmlChars;
  124. } else if ([elementName isEqualToString:@"uid_file_owner"]) {
  125. _currentShared.uidFileOwner = _xmlChars;
  126. } else if ([elementName isEqualToString:@"share_with_displayname"]) {
  127. _currentShared.shareWithDisplayName = _xmlChars;
  128. } else if ([elementName isEqualToString:@"displayname_owner"]) {
  129. _currentShared.displayNameOwner = _xmlChars;
  130. //Last value on the XML so we add the object to the array and begin again
  131. if (!_currentShared.shareWithDisplayName) {
  132. //To not have a nil we set an empty string
  133. _currentShared.shareWithDisplayName = @"";
  134. }
  135. }
  136. }
  137. - (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
  138. [_xmlChars appendString:string];
  139. }
  140. - (void)parserDidEndDocument:(NSXMLParser *)parser{
  141. //NSLog(@"Finish xml directory list parse");
  142. if (_currentShared) {
  143. [_shareList addObject:_currentShared];
  144. }
  145. }
  146. @end