mirror of
https://github.com/zhigang1992/RestKit.git
synced 2026-04-22 20:18:53 +08:00
Oops … RKPathMatcher merge didn't actually have RKPathMatcher .h and .m in it … Thanks @dmthomas for the heads up.
This commit is contained in:
103
Code/Support/RKPathMatcher.h
Executable file
103
Code/Support/RKPathMatcher.h
Executable file
@@ -0,0 +1,103 @@
|
||||
//
|
||||
// RKPathMatcher.h
|
||||
// RestKit
|
||||
//
|
||||
// Created by Greg Combs on 9/2/11.
|
||||
// Copyright (c) 2011 RestKit. All rights reserved.
|
||||
//
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
@class SOCPattern;
|
||||
|
||||
/**
|
||||
This class performs pattern matching and parameter parsing of strings, usually resource paths.
|
||||
It provides much of the necessary tools to map a given resource path to local objects (the inverse
|
||||
of RKRouter's function). This makes it easier to implement RKManagedObjectCache, and generate fetched
|
||||
requests from a given resource path. There are two means of instantiating and using a matcher object
|
||||
in order to provide more flexibility in implementations, and to improve efficiency by eliminating
|
||||
repetitive and costly pattern initializations.
|
||||
|
||||
@see RKManagedObjectCache
|
||||
@see RKMakePathWithObject
|
||||
@see RKRouter
|
||||
*/
|
||||
@interface RKPathMatcher : NSObject {
|
||||
@private
|
||||
SOCPattern *socPattern_;
|
||||
NSString *sourcePath_;
|
||||
NSString *rootPath_;
|
||||
NSDictionary *queryParameters_;
|
||||
}
|
||||
@property (retain,readonly) NSDictionary *queryParameters;
|
||||
|
||||
/**
|
||||
Creates an RKPathMatcher starting from a resource path string. This method should be followed by
|
||||
matchesPattern:tokenizeQueryStrings:parsedArguments:
|
||||
|
||||
@param pathString The string to evaluate and parse, such as /districts/tx/upper/?apikey=GC5512354
|
||||
@return An instantiated RKPathMatcher without an established pattern.
|
||||
*/
|
||||
+(RKPathMatcher *)matcherWithPath:(NSString *)pathString;
|
||||
|
||||
/**
|
||||
Determines if the path string matches the provided pattern, and yields a dictionary with the resulting
|
||||
matched key/value pairs. Use of this method should be preceded by matcherWithPath:
|
||||
Pattern strings should include encoded parameter keys, delimited by a single colon at the
|
||||
beginning of the key name.
|
||||
|
||||
*NOTE* - Numerous colon-encoded parameter keys can be joined in a long pattern, but each key must be
|
||||
separated by at least one unmapped character. For instance, /:key1:key2:key3/ is invalid, wheras
|
||||
/:key1/:key2/:key3/ is acceptable.
|
||||
|
||||
@param patternString The pattern to use for evaluating, such as /:entityName/:stateID/:chamber/
|
||||
@param shouldTokenize If YES, any query parameters will be tokenized and inserted into the parsed argument dictionary.
|
||||
@param arguments A pointer to a dictionary that contains the key/values from the pattern (and parameter) matching.
|
||||
@return A boolean indicating if the path string successfully matched the pattern.
|
||||
*/
|
||||
- (BOOL)matchesPattern:(NSString *)patternString tokenizeQueryStrings:(BOOL)shouldTokenize parsedArguments:(NSDictionary **)arguments;
|
||||
|
||||
/**
|
||||
Creates an RKPathMatcher starting from a pattern string. This method should be followed by
|
||||
matchesPath:tokenizeQueryStrings:parsedArguments: Patterns should include encoded parameter keys,
|
||||
delimited by a single colon at the beginning of the key name.
|
||||
|
||||
*NOTE* - Numerous colon-encoded parameter keys can be joined in a long pattern, but each key must be
|
||||
separated by at least one unmapped character. For instance, /:key1:key2:key3/ is invalid, wheras
|
||||
/:key1/:key2/:key3/ is acceptable.
|
||||
|
||||
@param patternString The pattern to use for evaluating, such as /:entityName/:stateID/:chamber/
|
||||
@return An instantiated RKPathMatcher with an established pattern.
|
||||
*/
|
||||
+(RKPathMatcher *)matcherWithPattern:(NSString *)patternString;
|
||||
|
||||
/**
|
||||
Determines if the provided resource path string matches a pattern, and yields a dictionary with the resulting
|
||||
matched key/value pairs. Use of this method should be preceded by matcherWithPattern:
|
||||
|
||||
@param pathString The string to evaluate and parse, such as /districts/tx/upper/?apikey=GC5512354
|
||||
@param shouldTokenize If YES, any query parameters will be tokenized and inserted into the parsed argument dictionary.
|
||||
@param arguments A pointer to a dictionary that contains the key/values from the pattern (and parameter) matching.
|
||||
@return A boolean indicating if the path string successfully matched the pattern.
|
||||
*/
|
||||
- (BOOL)matchesPath:(NSString *)pathString tokenizeQueryStrings:(BOOL)shouldTokenize parsedArguments:(NSDictionary **)arguments;
|
||||
|
||||
/**
|
||||
This generates a resource path by interpolating the properties of the 'object' argument, assuming the existence
|
||||
of a previously specified pattern established via matcherWithPattern:. Otherwise, this method is identical in
|
||||
function to RKMakePathWithObject (in fact it is a shortcut for this method).
|
||||
|
||||
For example, given an 'article' object with an 'articleID' property value of 12345 ...
|
||||
|
||||
RKPathMatcher *matcher = [RKPathMatcher matcherWithPattern:@"/articles/:articleID"];
|
||||
NSString *resourcePath = [matcher pathFromObject:article];
|
||||
|
||||
... will produce a 'resourcePath' containing the string "/articles/12345"
|
||||
|
||||
@param object The object containing the properties to interpolate.
|
||||
@return A string with the object's interpolated property values inserted into the receiver's established pattern.
|
||||
@see RKMakePathWithObject
|
||||
@see RKRouter
|
||||
*/
|
||||
- (NSString *)pathFromObject:(id)object;
|
||||
|
||||
@end
|
||||
121
Code/Support/RKPathMatcher.m
Executable file
121
Code/Support/RKPathMatcher.m
Executable file
@@ -0,0 +1,121 @@
|
||||
//
|
||||
// RKPathMatcher.m
|
||||
// RestKit
|
||||
//
|
||||
// Created by Greg Combs on 9/2/11.
|
||||
// Copyright (c) 2011 RestKit. All rights reserved.
|
||||
//
|
||||
|
||||
#import "RKPathMatcher.h"
|
||||
#import "SOCKit.h"
|
||||
#import "NSString+RestKit.h"
|
||||
#import "NSDictionary+RKAdditions.h"
|
||||
#import "RKLog.h"
|
||||
|
||||
BOOL RKPathUsesParentheticalParameters(NSString *path) {
|
||||
NSCharacterSet *parens = [NSCharacterSet characterSetWithCharactersInString:@"()"];
|
||||
NSArray *parenComponents = [path componentsSeparatedByCharactersInSet:parens];
|
||||
return (parenComponents != NULL && [parenComponents count] > 1);
|
||||
}
|
||||
|
||||
NSString *RKPathPatternFindAndReplaceParensWithColons(NSString *pattern) {
|
||||
if (RKPathUsesParentheticalParameters(pattern)) {
|
||||
RKLogWarning(@"Use of encapsulating parentheses for pattern parameters is deprecated. Use a single colon instead. For example, instead of /group/(role)/(user) you should use /group/:role/:user");
|
||||
NSString *noTrailingParen = [pattern stringByReplacingOccurrencesOfString:@")" withString:@""];
|
||||
pattern = [noTrailingParen stringByReplacingOccurrencesOfString:@"(" withString:@":"];
|
||||
}
|
||||
return pattern;
|
||||
}
|
||||
|
||||
@interface RKPathMatcher()
|
||||
@property (nonatomic,retain) SOCPattern *socPattern;
|
||||
@property (nonatomic,copy) NSString *sourcePath;
|
||||
@property (nonatomic,copy) NSString *rootPath;
|
||||
@property (retain,readwrite) NSDictionary *queryParameters;
|
||||
@end
|
||||
|
||||
@implementation RKPathMatcher
|
||||
@synthesize socPattern=socPattern_;
|
||||
@synthesize sourcePath=sourcePath_;
|
||||
@synthesize rootPath=rootPath_;
|
||||
@synthesize queryParameters=queryParameters_;
|
||||
|
||||
- (void)dealloc {
|
||||
self.socPattern = nil;
|
||||
self.sourcePath = nil;
|
||||
self.rootPath = nil;
|
||||
self.queryParameters = nil;
|
||||
[super dealloc];
|
||||
}
|
||||
|
||||
+(RKPathMatcher *)matcherWithPattern:(NSString *)patternString {
|
||||
NSAssert(patternString != NULL, @"Pattern string must not be empty in order to perform pattern matching.");
|
||||
patternString = RKPathPatternFindAndReplaceParensWithColons(patternString);
|
||||
RKPathMatcher *matcher = [[[RKPathMatcher alloc] init] autorelease];
|
||||
matcher.socPattern = [SOCPattern patternWithString:patternString];
|
||||
return matcher;
|
||||
}
|
||||
|
||||
+(RKPathMatcher *)matcherWithPath:(NSString *)pathString {
|
||||
RKPathMatcher *matcher = [[[RKPathMatcher alloc] init] autorelease];
|
||||
matcher.sourcePath = pathString;
|
||||
matcher.rootPath = pathString;
|
||||
return matcher;
|
||||
}
|
||||
|
||||
- (BOOL)matches {
|
||||
NSAssert( (self.socPattern != NULL && self.rootPath != NULL) , @"Matcher is insufficiently configured. Before attempting pattern matching, you must provide a path string and a pattern to match it against.");
|
||||
return [self.socPattern stringMatches:self.rootPath];
|
||||
}
|
||||
|
||||
- (BOOL)bifurcateSourcePathFromQueryParameters {
|
||||
NSArray *components = [self.sourcePath componentsSeparatedByString:@"?"];
|
||||
if ([components count] > 1) {
|
||||
self.rootPath = [components objectAtIndex:0];
|
||||
self.queryParameters = [[components objectAtIndex:1] queryParametersUsingEncoding:NSASCIIStringEncoding];
|
||||
return YES;
|
||||
}
|
||||
return NO;
|
||||
}
|
||||
|
||||
- (BOOL)itMatchesAndHasParsedArguments:(NSDictionary **)arguments tokenizeQueryStrings:(BOOL)shouldTokenize {
|
||||
NSAssert(self.socPattern != NULL, @"Matcher has no established pattern. Instantiate it using matcherWithPattern: before attempting a pattern match.");
|
||||
NSMutableDictionary *argumentsCollection = [NSMutableDictionary dictionary];
|
||||
if ([self bifurcateSourcePathFromQueryParameters]) {
|
||||
if (shouldTokenize) {
|
||||
[argumentsCollection addEntriesFromDictionary:self.queryParameters];
|
||||
}
|
||||
}
|
||||
if (![self matches])
|
||||
return NO;
|
||||
if (!arguments) {
|
||||
RKLogWarning(@"The parsed arguments dictionary reference is nil.");
|
||||
return YES;
|
||||
}
|
||||
NSDictionary *extracted = [self.socPattern extractParameterKeyValuesFromSourceString:self.rootPath];
|
||||
if (extracted)
|
||||
[argumentsCollection addEntriesFromDictionary:[extracted removePercentEscapesFromKeysAndObjects]];
|
||||
*arguments = argumentsCollection;
|
||||
return YES;
|
||||
}
|
||||
|
||||
- (BOOL)matchesPattern:(NSString *)patternString tokenizeQueryStrings:(BOOL)shouldTokenize parsedArguments:(NSDictionary **)arguments {
|
||||
NSAssert(patternString != NULL, @"Pattern string must not be empty in order to perform patterm matching.");
|
||||
patternString = RKPathPatternFindAndReplaceParensWithColons(patternString);
|
||||
self.socPattern = [SOCPattern patternWithString:patternString];
|
||||
return [self itMatchesAndHasParsedArguments:arguments tokenizeQueryStrings:shouldTokenize];
|
||||
}
|
||||
|
||||
- (BOOL)matchesPath:(NSString *)sourceString tokenizeQueryStrings:(BOOL)shouldTokenize parsedArguments:(NSDictionary **)arguments {
|
||||
self.sourcePath = sourceString;
|
||||
self.rootPath = sourceString;
|
||||
return [self itMatchesAndHasParsedArguments:arguments tokenizeQueryStrings:shouldTokenize];
|
||||
}
|
||||
|
||||
- (NSString *)pathFromObject:(id)object {
|
||||
NSAssert(self.socPattern != NULL, @"Matcher has no established pattern. Instantiate it using matcherWithPattern: before calling pathFromObject:");
|
||||
NSAssert(object != NULL, @"Object provided is invalid; cannot create a path from a NULL object");
|
||||
return [self.socPattern stringFromObject:object];
|
||||
}
|
||||
|
||||
@end
|
||||
Reference in New Issue
Block a user