123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247 |
- //
- // OCXMLSharedParser.m
- // OCCommunicationLib
- //
- // Copyright (C) 2016, ownCloud GmbH. ( http://www.owncloud.org/ )
- //
- // Permission is hereby granted, free of charge, to any person obtaining a copy
- // of this software and associated documentation files (the "Software"), to deal
- // in the Software without restriction, including without limitation the rights
- // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- // copies of the Software, and to permit persons to whom the Software is
- // furnished to do so, subject to the following conditions:
- // The above copyright notice and this permission notice shall be included in
- // all copies or substantial portions of the Software.
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- // THE SOFTWARE.
- //
- //
- #import "OCXMLSharedParser.h"
- #import "OCSharedDto.h"
- #define expirationDateFormat @"YYYY-MM-dd HH:mm:ss"
- @implementation OCXMLSharedParser
- @synthesize shareList=_shareList;
- @synthesize currentShared=_currentShared;
- /*
- * Method that init the parse with the xml data from the server
- * @data -> XML webDav data from the owncloud server
- */
- - (void)initParserWithData: (NSData*)data{
-
- _shareList = [[NSMutableArray alloc]init];
-
- NSXMLParser *parser = [[NSXMLParser alloc] initWithData:data];
- [parser setDelegate:self];
- [parser parse];
-
- }
- #pragma mark - XML Parser Delegate Methods
- /*
- * Method that init parse process.
- */
- - (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict {
-
- if (!_xmlChars) {
- _xmlChars = [NSMutableString string];
- }
-
- //NSLog(@"_xmlChars: %@", _xmlChars);
-
- [_xmlChars setString:@""];
-
- if ([elementName isEqualToString:@"element"]) {
- _xmlBucket = [NSMutableDictionary dictionary];
-
- if (_currentShared) {
- [_shareList addObject:_currentShared];
- }
- }
- }
- /*
- * Util method to make a NSDate object from a string from xml
- * @dateString -> Data string from xml
- */
- + (NSDate*)parseDateString:(NSString*)dateString {
- //Parse the date in all the formats
- NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
- /*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.*/
- [dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"]];
- [dateFormatter setDateFormat:expirationDateFormat];
-
- NSDate *theDate = nil;
- NSError *error = nil;
- if (![dateFormatter getObjectValue:&theDate forString:dateString range:nil error:&error]) {
- NSLog(@"Date '%@' could not be parsed: %@", dateString, error);
- }
-
- return theDate;
- }
- - (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
-
- //NSLog(@"elementName: %@:%@", elementName,_xmlChars);
-
- if ([elementName isEqualToString:@"id"]) {
-
- _currentShared = [[OCSharedDto alloc] init];
- _currentShared.idRemoteShared = [_xmlChars intValue];
-
- } else if ([elementName isEqualToString:@"item_type"]) {
-
- if([_xmlChars isEqualToString:@"file"]) {
- _currentShared.isDirectory = NO;
- } else {
- _currentShared.isDirectory = YES;
- if (_currentShared.path) {
- _currentShared.path = [_currentShared.path stringByAppendingString:@"/"];
- }
- }
- } else if ([elementName isEqualToString:@"item_source"]) {
-
- _currentShared.itemSource = [_xmlChars intValue];
-
- } else if ([elementName isEqualToString:@"parent"]) {
-
- _currentShared.parent = [_xmlChars intValue];
-
- } else if ([elementName isEqualToString:@"share_type"]) {
-
- _currentShared.shareType = [_xmlChars intValue];
-
- } else if ([elementName isEqualToString:@"share_with"]) {
-
- _currentShared.shareWith = _xmlChars;
-
- } else if ([elementName isEqualToString:@"file_source"]) {
-
- _currentShared.fileSource = [_xmlChars intValue];
-
- } else if ([elementName isEqualToString:@"path"]) {
-
- if (_currentShared.isDirectory) {
- _currentShared.path = [_xmlChars stringByAppendingString:@"/"];
- } else {
- _currentShared.path = _xmlChars;
- }
-
- } else if ([elementName isEqualToString:@"permissions"]) {
-
- _currentShared.permissions = [_xmlChars intValue];
-
- } else if ([elementName isEqualToString:@"stime"]) {
-
- _currentShared.sharedDate = (long)[_xmlChars longLongValue];
-
-
- } else if ([elementName isEqualToString:@"expiration"]) {
-
- //Check expiration, is a optional field, sometimes is empty
- if (![_xmlChars isEqualToString:@""]) {
- NSDate *date = [[self class] parseDateString:_xmlChars];
- _currentShared.expirationDate = [date timeIntervalSince1970];
- }else{
- _currentShared.expirationDate = 0.0;
- }
-
- } else if ([elementName isEqualToString:@"token"]) {
-
- _currentShared.token = _xmlChars;
-
- } else if ([elementName isEqualToString:@"storage"]) {
-
- _currentShared.storage = [_xmlChars intValue];
-
- } else if ([elementName isEqualToString:@"mail_send"]) {
-
- _currentShared.mailSend = [_xmlChars intValue];
-
- } else if ([elementName isEqualToString:@"uid_owner"]) {
-
- _currentShared.uidOwner = _xmlChars;
-
- } else if ([elementName isEqualToString:@"uid_file_owner"]) {
-
- _currentShared.uidFileOwner = _xmlChars;
-
- } else if ([elementName isEqualToString:@"share_with_displayname"]) {
-
- _currentShared.shareWithDisplayName = _xmlChars;
-
- } else if ([elementName isEqualToString:@"displayname_owner"]) {
-
- _currentShared.displayNameOwner = _xmlChars;
-
- //Last value on the XML so we add the object to the array and begin again
- if (!_currentShared.shareWithDisplayName) {
- //To not have a nil we set an empty string
- _currentShared.shareWithDisplayName = @"";
- }
-
- } else if ([elementName isEqualToString:@"hide_download"]) {
-
- _currentShared.hideDownload = [_xmlChars boolValue];
-
- } else if ([elementName isEqualToString:@"note"]) {
-
- _currentShared.note = _xmlChars;
-
- } else if ([elementName isEqualToString:@"label"]) {
-
- _currentShared.label = _xmlChars;
-
- } else if ([elementName isEqualToString:@"displayname_file_owner"]) {
-
- _currentShared.displayNameFileOwner = _xmlChars;
-
- } else if ([elementName isEqualToString:@"url"]) {
-
- _currentShared.url = _xmlChars;
-
- } else if ([elementName isEqualToString:@"mimetype"]) {
-
- _currentShared.mimeType = _xmlChars;
-
- } else if ([elementName isEqualToString:@"storage_id"]) {
-
- _currentShared.storageID = _xmlChars;
-
- } else if ([elementName isEqualToString:@"file_parent"]) {
-
- _currentShared.fileParent = _xmlChars;
-
- } else if ([elementName isEqualToString:@"file_target"]) {
-
- _currentShared.fileTarget = _xmlChars;
- }
- }
- - (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
- [_xmlChars appendString:string];
- }
- - (void)parserDidEndDocument:(NSXMLParser *)parser{
- NSLog(@"Finish xml shared parse");
- if (_currentShared) {
- [_shareList addObject:_currentShared];
- }
- }
- @end
|