-
Notifications
You must be signed in to change notification settings - Fork 3
/
MPURLRequestParameter.m
158 lines (118 loc) · 4.29 KB
/
MPURLRequestParameter.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
//
// MPURLParameter.m
// MPOAuthConnection
//
// Created by Karl Adam on 08.12.05.
// Copyright 2008 matrixPointer. All rights reserved.
//
#import "MPURLRequestParameter.h"
#import "NSString+URLEscapingAdditions.h"
@implementation MPURLRequestParameter
+ (NSArray *)parametersFromString:(NSString *)inString {
NSMutableArray *foundParameters = [NSMutableArray arrayWithCapacity:10];
NSScanner *parameterScanner = [[NSScanner alloc] initWithString:inString];
NSString *name = nil;
NSString *value = nil;
MPURLRequestParameter *currentParameter = nil;
while (![parameterScanner isAtEnd]) {
name = nil;
value = nil;
[parameterScanner scanUpToString:@"=" intoString:&name];
[parameterScanner scanString:@"=" intoString:NULL];
[parameterScanner scanUpToString:@"&" intoString:&value];
[parameterScanner scanString:@"&" intoString:NULL];
currentParameter = [[MPURLRequestParameter alloc] init];
currentParameter.name = name;
currentParameter.value = [value stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
[foundParameters addObject:currentParameter];
[currentParameter release];
}
[parameterScanner release];
return foundParameters;
}
+ (NSArray *)parametersFromDictionary:(NSDictionary *)inDictionary {
NSMutableArray *parameterArray = [[NSMutableArray alloc] init];
MPURLRequestParameter *aURLParameter = nil;
for (NSString *aKey in [inDictionary allKeys]) {
aURLParameter = [[MPURLRequestParameter alloc] init];
aURLParameter.name = aKey;
aURLParameter.value = [inDictionary objectForKey:aKey];
[parameterArray addObject:aURLParameter];
[aURLParameter release];
}
return [parameterArray autorelease];
}
+ (NSDictionary *)parameterDictionaryFromString:(NSString *)inString {
NSMutableDictionary *foundParameters = [NSMutableDictionary dictionaryWithCapacity:10];
if (inString) {
NSScanner *parameterScanner = [[NSScanner alloc] initWithString:inString];
NSString *name = nil;
NSString *value = nil;
while (![parameterScanner isAtEnd]) {
name = nil;
value = nil;
[parameterScanner scanUpToString:@"=" intoString:&name];
[parameterScanner scanString:@"=" intoString:NULL];
[parameterScanner scanUpToString:@"&" intoString:&value];
[parameterScanner scanString:@"&" intoString:NULL];
[foundParameters setObject:[value stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding] forKey:name];
}
[parameterScanner release];
}
return foundParameters;
}
+ (NSString *)parameterStringForParameters:(NSArray *)inParameters {
NSMutableString *queryString = [[NSMutableString alloc] init];
int i = 0;
int parameterCount = [inParameters count];
MPURLRequestParameter *aParameter = nil;
for (; i < parameterCount; i++) {
aParameter = [inParameters objectAtIndex:i];
[queryString appendString:[aParameter URLEncodedParameterString]];
if (i < parameterCount - 1) {
[queryString appendString:@"&"];
}
}
return [queryString autorelease];
}
+ (NSString *)parameterStringForDictionary:(NSDictionary *)inParameterDictionary {
NSArray *parameters = [self parametersFromDictionary:inParameterDictionary];
NSString *queryString = [self parameterStringForParameters:parameters];
return queryString;
}
#pragma mark -
- (id)init {
if (self = [super init]) {
}
return self;
}
- (id)initWithName:(NSString *)inName andValue:(NSString *)inValue {
if (self = [super init]) {
self.name = inName;
self.value = inValue;
}
return self;
}
- (oneway void)dealloc {
self.name = nil;
self.value = nil;
[super dealloc];
}
@synthesize name = _name;
@synthesize value = _value;
#pragma mark -
- (NSString *)URLEncodedParameterString {
return [NSString stringWithFormat:@"%@=%@", [self.name stringByAddingURIPercentEscapesUsingEncoding:NSUTF8StringEncoding], self.value ? [self.value stringByAddingURIPercentEscapesUsingEncoding:NSUTF8StringEncoding] : @""];
}
#pragma mark -
- (NSComparisonResult)compare:(id)inObject {
NSComparisonResult result = [self.name compare:[(MPURLRequestParameter *)inObject name]];
if (result == NSOrderedSame) {
result = [self.value compare:[(MPURLRequestParameter *)inObject value]];
}
return result;
}
- (NSString *)description {
return [NSString stringWithFormat:@"<%@: %p %@>", NSStringFromClass([self class]), self, [self URLEncodedParameterString]];
}
@end