Bladeren bron

API search

Marino Faggiana 8 jaren geleden
bovenliggende
commit
a4c870269c

+ 1 - 1
Libraries external/OCCommunicationLib/OCCommunicationLib/OCWebDavClient/OCWebDAVClient.m

@@ -288,7 +288,7 @@ NSString const *OCWebDAVModificationDateKey	= @"modificationdate";
     
     NSMutableURLRequest *request = [self requestWithMethod:_requestMethod path:path parameters:nil];
     
-    NSString *body = [NSString stringWithFormat:@"<?xml version=\"1.0\"?><d:searchrequest xmlns:d=\"DAV:\" xmlns:oc=\"http://owncloud.org/ns\"><d:basicsearch><d:select><d:prop><oc:fileid/><d:getcontenttype/><d:getetag/><oc:size/></d:prop></d:select><d:from><d:scope><d:href>/files/%@</d:href><d:depth>infinity</d:depth></d:scope></d:from><d:where><d:like><d:prop><d:displayname/></d:prop><d:literal>%%%@%%</d:literal></d:like></d:where><d:orderby/></d:basicsearch></d:searchrequest>", user, fileName];
+    NSString *body = [NSString stringWithFormat:@"<?xml version=\"1.0\"?><d:searchrequest xmlns:d=\"DAV:\" xmlns:oc=\"http://owncloud.org/ns\"><d:basicsearch><d:select><d:prop><oc:fileid/><d:getcontenttype/><d:getetag/><d:displayname/><d:creationdate/><oc:size/></d:prop></d:select><d:from><d:scope><d:href>/files/%@</d:href><d:depth>infinity</d:depth></d:scope></d:from><d:where><d:like><d:prop><d:displayname/></d:prop><d:literal>%%%@%%</d:literal></d:like></d:where><d:orderby/></d:basicsearch></d:searchrequest>", user, fileName];
     
     [request setHTTPBody:[body dataUsingEncoding:NSUTF8StringEncoding]];
     [request setValue:@"text/xml" forHTTPHeaderField:@"Content-Type"];

+ 22 - 0
Libraries external/OCCommunicationLib/OCCommunicationLib/OCWebDavClient/Parsers/OCXMLSearchParser.h

@@ -0,0 +1,22 @@
+
+
+#import <Foundation/Foundation.h>
+#import "OCFileDto.h"
+
+
+@interface OCXMLSearchParser : NSObject <NSXMLParserDelegate>{
+    
+    NSMutableString *_xmlChars;
+    NSMutableDictionary *_xmlBucket;
+    NSMutableArray *_directoryList;
+    OCFileDto *_currentFile;
+    BOOL isNotFirstFileOfList;
+    
+}
+
+@property(nonatomic,strong) NSMutableArray *directoryList;
+@property(nonatomic,strong) OCFileDto *currentFile;
+
+- (void)initParserWithData: (NSData*)data;
+
+@end

+ 228 - 0
Libraries external/OCCommunicationLib/OCCommunicationLib/OCWebDavClient/Parsers/OCXMLSearchParser.m

@@ -0,0 +1,228 @@
+
+
+#import "OCXMLSearchParser.h"
+#import "NSString+Encode.h"
+
+@implementation OCXMLSearchParser
+
+@synthesize directoryList=_directoryList;
+@synthesize currentFile=_currentFile;
+
+/*
+ * Method that init the parse with the xml data from the server
+ * @data -> XML webDav data from the owncloud server
+ */
+- (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
+
+
+/*
+ * 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:@"d:response"]) {
+        _xmlBucket = [NSMutableDictionary dictionary];
+    }
+}
+
+/*
+ * 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"]];
+    //This is the format for the concret locale used
+    [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 {
+    
+    // NSLog(@"elementName: %@:%@", elementName,_xmlChars);
+    
+    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 has lenght, there are an item
+        if ([_xmlChars length]) {
+            //Create FileDto
+            _currentFile = [[OCFileDto alloc] init];
+             _currentFile.isDirectory = NO;
+            [_xmlBucket setObject:[_xmlChars copy] forKey:@"uri"];
+            
+            NSArray *splitedUrl = [_xmlChars componentsSeparatedByString:@"/"];
+            
+            //Check if the item is a folder or a file
+            if([_xmlChars hasSuffix:@"/"]) {
+                //It's a folder
+                NSInteger fileNameLenght = [((NSString *)[splitedUrl objectAtIndex:[splitedUrl count]-2]) length];
+                
+                if ( fileNameLenght > 0) {
+                    //FileDto filepath
+                    _currentFile.filePath = [_xmlChars substringToIndex:[_xmlChars length] - (fileNameLenght+1)];
+                } else {
+                    _currentFile.filePath = @"/";
+                }
+            } else {
+                //It's a file
+                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];
+        }
+        
+        //NSString *lastBit = [_xmlChars substringFromIndex:_uriLength];
+        //NSLog(@"lastBit:- %@",lastBit);
+        if (isNotFirstFileOfList == YES) {
+            [_xmlBucket setObject:lastBit forKey:@"href"];
+            _currentFile.fileName = lastBit;
+        }
+            
+        NSString *decodedFileName = [self decodeFromPercentEscapeString:self.currentFile.fileName];
+        NSString *decodedFilePath = [self decodeFromPercentEscapeString:self.currentFile.filePath];
+            
+        self.currentFile.fileName = [decodedFileName encodeString:NSUTF8StringEncoding];
+        self.currentFile.filePath = [decodedFilePath encodeString:NSUTF8StringEncoding];
+            
+        isNotFirstFileOfList = YES;
+
+//        //NSLog(@"1 _xmlBucked :- %@",_xmlBucket);
+     }
+    } else if ([elementName isEqualToString:@"d:getlastmodified"]) {
+        //DATE
+        // 'Thu, 30 Oct 2008 02:52:47 GMT'
+        // Monday, 12-Jan-98 09:25:56 GMT
+        // Value: HTTP-date  ; defined in section 3.3.1 of RFC2068
+        
+        if ([_xmlChars length]) {
+            NSDate *d = [[self class] parseDateString:_xmlChars];
+            
+            if (d) {
+                //FildeDto Date
+                _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]) {
+        //ETAG
+        NSLog(@"getetag: %@", _xmlChars);
+        
+        NSString *stringClean = _xmlChars;
+        stringClean = [_xmlChars stringByReplacingOccurrencesOfString:@"\"" withString:@""];
+        
+        _currentFile.etag = [stringClean lowercaseString];
+        
+    } else if ([elementName hasSuffix:@":getcontenttype"] && [_xmlChars length]) {
+        //CONTENT TYPE
+        [_xmlBucket setObject:[_xmlChars copy] forKey:@"contenttype"];
+        
+    } else if([elementName hasSuffix:@"d:getcontentlength"] && [_xmlChars length]) {
+        //SIZE
+        //FileDto current size
+        _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"]) {
+        //NSLog(@"2 _xmlBucked :- %@",_xmlBucket);
+        
+        //Add to directoryList
+        [_directoryList addObject:_currentFile];
+        _currentFile = [[OCFileDto alloc] init];
+
+        _xmlBucket = nil;
+    } else if ([elementName isEqualToString:@"d:quota-used-bytes"]) {
+        _currentFile.quotaUsed = (double)[_xmlChars doubleValue];
+    } else if ([elementName isEqualToString:@"d:quota-available-bytes"]) {
+        _currentFile.quotaAvailable = (double)[_xmlChars doubleValue];
+    } else if ([elementName isEqualToString:@"oc:favorite"]) {
+        _currentFile.isFavorite = [_xmlChars boolValue];
+    }
+}
+
+- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
+    [_xmlChars appendString:string];
+}
+
+- (void)parserDidEndDocument:(NSXMLParser *)parser{
+    
+    NSLog(@"Finish xml directory list parse");
+}
+
+// Decode a percent escape encoded string.
+- (NSString*) decodeFromPercentEscapeString:(NSString *) string {
+    return (__bridge NSString *) CFURLCreateStringByReplacingPercentEscapesUsingEncoding(NULL,
+                                                                                         (__bridge CFStringRef) string,
+                                                                                         CFSTR(""),
+                                                                                         kCFStringEncodingUTF8);
+}
+
+
+
+@end

+ 6 - 0
Libraries external/OCCommunicationLib/ownCloud iOS library.xcodeproj/project.pbxproj

@@ -52,6 +52,7 @@
 		F704980E1E369EF6008F5BB6 /* OCNotificationsAction.m in Sources */ = {isa = PBXBuildFile; fileRef = F704980A1E369EF6008F5BB6 /* OCNotificationsAction.m */; };
 		F704980F1E369EF6008F5BB6 /* OCRichObjectStrings.m in Sources */ = {isa = PBXBuildFile; fileRef = F704980C1E369EF6008F5BB6 /* OCRichObjectStrings.m */; };
 		F73C00351E56098300EEEFA7 /* OCUserProfile.m in Sources */ = {isa = PBXBuildFile; fileRef = F73C00341E56098300EEEFA7 /* OCUserProfile.m */; };
+		F754B7BE1E60D66B0027F21B /* OCXMLSearchParser.m in Sources */ = {isa = PBXBuildFile; fileRef = F754B7BD1E60D66B0027F21B /* OCXMLSearchParser.m */; };
 /* End PBXBuildFile section */
 
 /* Begin PBXContainerItemProxy section */
@@ -147,6 +148,8 @@
 		F704980C1E369EF6008F5BB6 /* OCRichObjectStrings.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = OCRichObjectStrings.m; sourceTree = "<group>"; };
 		F73C00331E56098300EEEFA7 /* OCUserProfile.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = OCUserProfile.h; sourceTree = "<group>"; };
 		F73C00341E56098300EEEFA7 /* OCUserProfile.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = OCUserProfile.m; sourceTree = "<group>"; };
+		F754B7BC1E60D66B0027F21B /* OCXMLSearchParser.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = OCXMLSearchParser.h; path = OCWebDavClient/Parsers/OCXMLSearchParser.h; sourceTree = "<group>"; };
+		F754B7BD1E60D66B0027F21B /* OCXMLSearchParser.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = OCXMLSearchParser.m; path = OCWebDavClient/Parsers/OCXMLSearchParser.m; sourceTree = "<group>"; };
 /* End PBXFileReference section */
 
 /* Begin PBXFrameworksBuildPhase section */
@@ -178,6 +181,8 @@
 			children = (
 				131C67B7187C3E360009030E /* OCXMLSharedParser.h */,
 				131C67B8187C3E360009030E /* OCXMLSharedParser.m */,
+				F754B7BC1E60D66B0027F21B /* OCXMLSearchParser.h */,
+				F754B7BD1E60D66B0027F21B /* OCXMLSearchParser.m */,
 				13AA863E187C3B9700A10927 /* OCXMLParser.h */,
 				13AA863F187C3B9700A10927 /* OCXMLParser.m */,
 				1345E45E1884213400153F14 /* OCXMLShareByLinkParser.h */,
@@ -451,6 +456,7 @@
 				F73C00351E56098300EEEFA7 /* OCUserProfile.m in Sources */,
 				EA7CC934183E150000B6A4B4 /* OCCommunication.m in Sources */,
 				F704980E1E369EF6008F5BB6 /* OCNotificationsAction.m in Sources */,
+				F754B7BE1E60D66B0027F21B /* OCXMLSearchParser.m in Sources */,
 				131C67B9187C3E360009030E /* OCXMLSharedParser.m in Sources */,
 				13AA8640187C3B9700A10927 /* OCXMLParser.m in Sources */,
 				EA7CC912183E146000B6A4B4 /* UtilsFramework.m in Sources */,