123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254 |
- #import "OCXMLParser.h"
- #import "NSString+Encode.h"
- NSString *OCCWebDAVContentTypeKey = @"contenttype";
- NSString *OCCWebDAVETagKey = @"etag";
- NSString *OCCWebDAVHREFKey = @"href";
- NSString *OCCWebDAVURIKey = @"uri";
- @implementation OCXMLParser
- @synthesize directoryList=_directoryList;
- @synthesize currentFile=_currentFile;
- - (void)initParserWithData: (NSData*)data{
-
- _directoryList = [[NSMutableArray alloc]init];
-
- NSXMLParser *parser = [[NSXMLParser alloc] initWithData:data];
- [parser setDelegate:self];
- [parser parse];
-
- }
- #pragma mark - XML Parser Delegate Methods
- - (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict {
-
- if (!_xmlChars) {
- _xmlChars = [NSMutableString string];
- }
-
-
-
- [_xmlChars setString:@""];
- if ([elementName isEqualToString:@"d:response"]) {
- _xmlBucket = [NSMutableDictionary dictionary];
- }
- }
- + (NSDate*)parseDateString:(NSString*)dateString {
-
- NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
-
- [dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"]];
-
- [dateFormatter setDateFormat:@"EEE, dd MMM y HH:mm:ss zzz"];
-
- 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 {
-
-
-
- if ([elementName isEqualToString:@"d:href"]) {
-
- if ([_xmlChars hasPrefix:@"http"]) {
- NSURL *junk = [NSURL URLWithString:_xmlChars];
- BOOL trailingSlash = [_xmlChars hasSuffix:@"/"];
- [_xmlChars setString:[junk path]];
- if (trailingSlash) {
- [_xmlChars appendString:@"/"];
- }
- }
-
-
- if ([_xmlChars length]) {
-
- _currentFile = [[OCFileDto alloc] init];
- _currentFile.isDirectory = NO;
- [_xmlBucket setObject:[_xmlChars copy] forKey:OCCWebDAVURIKey];
-
- NSArray *splitedUrl = [_xmlChars componentsSeparatedByString:@"/"];
-
-
- if([_xmlChars hasSuffix:@"/"]) {
-
- NSInteger fileNameLenght = [((NSString *)[splitedUrl objectAtIndex:[splitedUrl count]-2]) length];
-
- if ( fileNameLenght > 0) {
-
- _currentFile.filePath = [_xmlChars substringToIndex:[_xmlChars length] - (fileNameLenght+1)];
- } else {
- _currentFile.filePath = @"/";
- }
- } else {
-
- NSInteger fileNameLenght = [((NSString *)[splitedUrl objectAtIndex:[splitedUrl count]-1]) length];
- if (fileNameLenght > 0) {
- _currentFile.filePath = [_xmlChars substringToIndex:[_xmlChars length] - fileNameLenght];
- }else {
- _currentFile.filePath = @"/";
- }
- }
-
-
-
- NSArray *foo = [_xmlChars componentsSeparatedByString: @"/"];
- NSString *lastBit;
-
- if([_xmlChars hasSuffix:@"/"]) {
- lastBit = [foo objectAtIndex: [foo count]-2];
- lastBit = [NSString stringWithFormat:@"%@/",lastBit];
- } else {
- lastBit = [foo objectAtIndex: [foo count]-1];
- }
-
-
-
- if (isNotFirstFileOfList == YES) {
- [_xmlBucket setObject:lastBit forKey:OCCWebDAVHREFKey];
- _currentFile.fileName = lastBit;
- }
-
- self.currentFile.fileName = [self.currentFile.fileName stringByRemovingPercentEncoding];
- self.currentFile.filePath = [self.currentFile.filePath stringByRemovingPercentEncoding];
-
- isNotFirstFileOfList = YES;
- }
- } else if ([elementName isEqualToString:@"d:getlastmodified"]) {
-
-
-
-
-
- if ([_xmlChars length]) {
- NSDate *d = [[self class] parseDateString:_xmlChars];
-
- if (d) {
-
- _currentFile.date = [d timeIntervalSince1970];
- NSInteger colIdx = [elementName rangeOfString:@":"].location;
- [_xmlBucket setObject:d forKey:[elementName substringFromIndex:colIdx + 1]];
- }
-
- else {
- NSLog(@"Could not parse date string '%@' for '%@'", _xmlChars, elementName);
- }
- }
-
- } else if ([elementName isEqualToString:@"oc:id"]) {
- _currentFile.ocId = _xmlChars;
-
- } else if ([elementName hasSuffix:@":getetag"] && [_xmlChars length]) {
-
-
-
-
- NSString *stringClean = _xmlChars;
- stringClean = [_xmlChars stringByReplacingOccurrencesOfString:@"\"" withString:@""];
-
- _currentFile.etag = [stringClean lowercaseString];
-
- } else if ([elementName hasSuffix:@":getcontenttype"] && [_xmlChars length]) {
-
- [_xmlBucket setObject:[_xmlChars copy] forKey:OCCWebDAVContentTypeKey];
-
- } else if([elementName hasSuffix:@"d:getcontentlength"] && [_xmlChars length]) {
-
-
- _currentFile.size = (long)[_xmlChars longLongValue];
-
- } else if ([elementName isEqualToString:@"oc:permissions"]) {
- _currentFile.permissions = _xmlChars;
-
- } else if ([elementName isEqualToString:@"d:collection"]) {
- _currentFile.isDirectory = YES;
-
- } else if ([elementName isEqualToString:@"d:response"]) {
-
-
-
- [_directoryList addObject:_currentFile];
- _currentFile = [[OCFileDto alloc] init];
- _xmlBucket = nil;
- } else if ([elementName isEqualToString:@"oc:favorite"]) {
- _currentFile.isFavorite = [_xmlChars boolValue];
- } else if ([elementName isEqualToString:@"x1:share-permissions"]) {
- _currentFile.permissions = _xmlChars;
- } else if ([elementName isEqualToString:@"nc:is-encrypted"]) {
- _currentFile.isEncrypted = [_xmlChars boolValue];
- }
- }
- - (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
- [_xmlChars appendString:string];
- }
- - (void)parserDidEndDocument:(NSXMLParser *)parser{
-
- NSLog(@"Finish xml directory list parse");
- }
- @end
|