Desenvolver e Download de Software Open Source

Browse Subversion Repository

Contents of /bathyscaphe/trunk/application/source/manager/Cookie.m

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1178 - (show annotations) (download)
Mon Mar 22 17:52:28 2010 UTC (14 years, 2 months ago) by tsawada2
File size: 11354 byte(s)
Now you can build BathyScaphe.app again (ZANTEI)
1 //:Cookie.m
2 #import "Cookie.h"
3
4
5 //////////////////////////////////////////////////////////////////////
6 ////////////////////// [ 定数やマクロ置換 ] //////////////////////////
7 //////////////////////////////////////////////////////////////////////
8 /* クッキーのオプション名 */
9 #define kCookieOptionPath @"path"
10 #define kCookieOptionDomain @"domain"
11 #define kCookieOptionExpires @"expires"
12 #define kCookieOptionSecure @"secure"
13 /* 内部用 */
14 #define kCookieOptionEnabled @"x-application/CocoMonar enabled"
15 #define kCookieOptionBSEnabled @"x-application/BathyScaphe enabled" // available in BathyScaphe 1.2.2/1.5 and later.
16
17
18
19 @implementation Cookie
20 //////////////////////////////////////////////////////////////////////
21 /////////////////////// [ 初期化・後始末 ] ///////////////////////////
22 //////////////////////////////////////////////////////////////////////
23 /**
24 * 一時オブジェクトの生成。
25 *
26 * @return 一時オブジェクト
27 */
28 + (id) cookie
29 {
30 return [[[[self class] alloc] init] autorelease];
31 }
32
33 /**
34 * 一時オブジェクトの生成。
35 * 文字列表現からインスタンスを生成、初期化。
36 *
37 * @param anyCookies 文字列表現
38 * @return 一時オブジェクト
39 */
40 + (id) cookieWithString : (NSString *) anyCookies
41 {
42 return [[[[self class] alloc] initWithString : anyCookies] autorelease];
43 }
44
45 /**
46 * 一時オブジェクトの生成。
47 * 辞書オブジェクトからインスタンスを生成、初期化。
48 *
49 * @param anyCookies 辞書オブジェクト
50 * @return 一時オブジェクト
51 */
52 + (id) cookieWithDictionary : (NSDictionary *) anyCookies
53 {
54 return [[[[self class] alloc] initWithDictionary : anyCookies] autorelease];
55 }
56
57 - (id) init
58 {
59 if(self = [super init]){
60 [self setIsEnabled : YES];
61 }
62 return self;
63 }
64
65 /**
66 * 指定イニシャライザ。
67 * 文字列表現からインスタンスを生成、初期化。
68 *
69 * @param anyCookies 文字列表現
70 * @return 初期化済みのインスタンス
71 */
72 - (id) initWithString : (NSString *) anyCookies
73 {
74 if(self = [self init]){
75 [self setCookieWithString : anyCookies];
76 }
77 return self;
78 }
79
80 /**
81 * 指定イニシャライザ。
82 * 辞書オブジェクトからインスタンスを生成、初期化。
83 *
84 * @param anyCookies 辞書オブジェクト
85 * @return 初期化済みのインスタンス
86 */
87 - (id) initWithDictionary : (NSDictionary *) dict
88 {
89
90 if(self = [self init]){
91 if(nil == dict)
92 return self;
93
94 [self setCookieWithDictionary : dict];
95 }
96 return self;
97 }
98
99 - (void) dealloc
100 {
101 [m_name release]; //名前
102 [m_value release]; //値
103 [m_path release]; //クッキーが有効であるURL範囲
104 [m_domain release]; //クッキーが有効であるドメイン範囲
105 [m_expires release]; //有効期限
106 [super dealloc];
107 }
108
109 //////////////////////////////////////////////////////////////////////
110 ////////////////////// [ アクセサメソッド ] //////////////////////////
111 //////////////////////////////////////////////////////////////////////
112 /* Accessor for m_name */
113 - (NSString *) name
114 {
115 return m_name;
116 }
117 - (void) setName : (NSString *) aName
118 {
119 [aName retain];
120 [[self name] release];
121 m_name = aName;
122 }
123 /* Accessor for m_value */
124 - (NSString *) value
125 {
126 return m_value;
127 }
128 - (void) setValue : (NSString *) aValue
129 {
130 [aValue retain];
131 [[self value] release];
132 m_value = aValue;
133 }
134 /* Accessor for m_path */
135 - (NSString *) path
136 {
137 return m_path;
138 }
139
140 - (void) setPath : (NSString *) aPath
141 {
142 [aPath retain];
143 [[self path] release];
144 m_path = aPath;
145 }
146 /* Accessor for m_domain */
147 - (NSString *) domain
148 {
149 return m_domain;
150 }
151 - (void) setDomain : (NSString *) aDomain
152 {
153 [aDomain retain];
154 [[self domain] release];
155 m_domain = aDomain;
156 }
157 /* Accessor for m_expires */
158 - (NSString *) expires
159 {
160 return m_expires;
161 }
162 - (void) setExpires : (NSString *) anExpires
163 {
164 [anExpires retain];
165 [[self expires] release];
166 m_expires = anExpires;
167 }
168 /* Accessor for m_secure */
169 - (BOOL) secure
170 {
171 return m_secure;
172 }
173 - (void) setSecure : (BOOL) aSecure
174 {
175 m_secure = aSecure;
176 }
177 /* Accessor for m_isEnabled */
178 - (BOOL) isEnabled
179 {
180 return m_isEnabled;
181 }
182 - (void) setIsEnabled : (BOOL) anIsEnabled
183 {
184 m_isEnabled = anIsEnabled;
185 }
186 //////////////////////////////////////////////////////////////////////
187 //////////////////// [ インスタンスメソッド ] ////////////////////////
188 //////////////////////////////////////////////////////////////////////
189 /**
190 * レシーバのクッキーが有効なURLならYESを返す。
191 *
192 * @param anURL 対象URL
193 * @return クッキーが有効なURLならYES
194 */
195 - (BOOL) isAvalilableURL : (NSURL *) anURL
196 {
197 if(nil == anURL) return NO;
198
199 //pathが指定されていれば、マッチするか検査
200 if(nil == [self path]) return YES;
201 return [[anURL path] hasPrefix : [self path]];
202 }
203
204 /**
205 * 期限切れの場合はYESを返す。
206 * 終了時に破棄される場合にはwhenTerminate = YES
207 *
208 * @param whenTerminate 終了時に破棄される場合はYES
209 * @return 期限切れの場合はYES
210 */
211 - (BOOL) isExpired : (BOOL *) whenTerminate
212 {
213 NSDate *exp_;
214
215 exp_ = [self expiresDate];
216 if(nil == exp_){
217 //終了時に破棄
218 if(whenTerminate != NULL) *whenTerminate = YES;
219 return NO;
220 }
221 NSComparisonResult compare = [exp_ compare:[NSDate date]];
222 return (compare == NSOrderedAscending);
223 // return [exp_ isBeforeDate : [NSDate date]];
224 }
225
226
227 /**
228 * レシーバのを辞書形式で返す。
229 *
230 * @return 辞書オブジェクト
231 */
232 - (NSDictionary *) dictionaryRepresentation
233 {
234 NSMutableDictionary *dict_;
235
236 dict_ = [NSMutableDictionary dictionary];
237 //オプションを保存
238 if([self path] != nil){
239 [dict_ setObject : [self path]
240 forKey : kCookieOptionPath];
241 }
242 if([self domain] != nil){
243 [dict_ setObject : [self domain]
244 forKey : kCookieOptionDomain];
245 }
246 if([self expires] != nil){
247 [dict_ setObject : [self expires]
248 forKey : kCookieOptionExpires];
249 }
250 [dict_ setBool : [self secure]
251 forKey : kCookieOptionSecure];
252 [dict_ setBool : [self isEnabled]
253 forKey : kCookieOptionBSEnabled];
254 //クッキーを保存
255 if([self name] != nil && [self value] != nil){
256 [dict_ setObject : [self value]
257 forKey : [self name]];
258 }
259 return dict_;
260 }
261
262 + (NSDateFormatter *)cookieDateFormatter
263 {
264 static NSDateFormatter *cachedFormatter = nil;
265 if (!cachedFormatter) {
266 NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
267 cachedFormatter = [[NSDateFormatter alloc] init];
268 [cachedFormatter setFormatterBehavior:NSDateFormatterBehavior10_4]; // to make the intent clear
269
270 [cachedFormatter setDateStyle:NSDateFormatterNoStyle];
271 [cachedFormatter setTimeStyle:NSDateFormatterNoStyle];
272 [cachedFormatter setDateFormat:@"EEEE, dd-MMM-yyyy HH:mm:ss 'GMT'"];
273
274 [cachedFormatter setLocale:locale];
275 [cachedFormatter setTimeZone:[NSTimeZone timeZoneWithName:@"GMT"]];
276 [locale release];
277 }
278 return cachedFormatter;
279 }
280
281 //:アクセサ
282 /**
283 * 有効期限を返す。
284 *
285 * @return 有効期限
286 */
287 - (NSDate *)expiresDate
288 {
289 if (![self expires]) return nil;
290 return [[[self class] cookieDateFormatter] dateFromString:[self expires]];
291 }
292
293 //クッキーの設定
294 /**
295 * クッキーを設定。
296 *
297 * @param aValue 値
298 * @param aName 名前
299 */
300 - (void) setCookie : (id ) aValue
301 forName : (NSString *) aName
302 {
303 if(nil == aValue || nil == aName) return;
304 //オプション指定の場合はインスタンス変数に保持
305 if([aName isEqualToString : kCookieOptionSecure]){
306 if(NO == [aValue respondsToSelector : @selector(boolValue)])
307 [self setSecure : NO];
308 [self setSecure : [aValue boolValue]];
309 }else if([aName isEqualToString : kCookieOptionEnabled] || [aName isEqualToString : kCookieOptionBSEnabled]){
310 if(NO == [aValue respondsToSelector : @selector(boolValue)])
311 [self setIsEnabled : YES];
312 [self setIsEnabled : [aValue boolValue]];
313 }else if([aName isEqualToString : kCookieOptionPath]){
314 [self setPath : aValue];
315 }else if([aName isEqualToString : kCookieOptionDomain]){
316 [self setDomain : aValue];
317 }else if([aName isEqualToString : kCookieOptionExpires]){
318 [self setExpires : aValue];
319 }else{
320 [self setName : aName];
321 [self setValue : aValue];
322 }
323 }
324
325 /**
326 * 文字列から変換。
327 * オプションを指定した場合は、それらも反映される。
328 *
329 * ex : @"SPID=XWDtLhNY; expires=1016920836 GMT; path=/"
330 *
331 * @param anyCookies 文字列表現
332 */
333 - (void) setCookieWithString : (NSString *) anyCookies
334 {
335 NSArray *comps_; //組毎を配列オブジェクトに
336 NSEnumerator *iter_; //順次検査
337 NSString *item_; //各組
338
339 if(nil == anyCookies) return;
340 //UTILDebugLog(@"anyCookies = %@", anyCookies);
341 comps_ = [anyCookies componentsSeparatedByString : @";"];
342 if(nil == comps_ || 0 == [comps_ count]) return;
343 //UTILDebugLog(@"comps_ = (%d)", [comps_ count]);
344
345 iter_ = [comps_ objectEnumerator];
346 while(item_ = [iter_ nextObject]){
347 NSArray *pair_; //名前、値
348 NSMutableString *name_, *value_;
349
350 //UTILDebugLog(@"item_ = %@", item_);
351 pair_ = [item_ componentsSeparatedByString : @"="];
352 if(nil == pair_) continue;
353
354 //Secure
355 if(1 == [pair_ count]){
356 NSMutableString *cstr_;
357
358 cstr_ = [NSMutableString stringWithString : [pair_ objectAtIndex : 0]];
359 [cstr_ strip];
360
361 if([cstr_ isEqualToString : kCookieOptionSecure])
362 [self setSecure : YES];
363 continue;
364 }
365 if([pair_ count] != 2) continue;
366
367 name_ = [NSMutableString stringWithString : [pair_ objectAtIndex : 0]];
368 value_ = [NSMutableString stringWithString : [pair_ objectAtIndex : 1]];
369 //先頭、末尾の空白を削除
370 [name_ strip];
371 [value_ strip];
372 //UTILDebugLog(@"name = %@", name_);
373 [self setCookie : value_
374 forName : name_];
375 }
376 }
377
378 /**
379 * 辞書オブジェクトから変換。
380 * オプションを指定した場合は、それらも反映される。
381 *
382 *
383 * @param anyCookies 辞書オブジェクト
384 */
385 - (void) setCookieWithDictionary : (NSDictionary *) anyCookies
386 {
387 NSEnumerator *iter_; //キーを順次検査
388 NSString *key_; //キー
389
390 if(nil == anyCookies) return;
391
392 iter_ = [anyCookies keyEnumerator];
393 while(key_ = [iter_ nextObject]){
394 id value_;
395
396 value_ = [anyCookies objectForKey : key_];
397 if(nil == value_) continue;
398
399 [self setCookie : value_
400 forName : key_];
401 }
402 }
403
404 /**
405 * クッキーを文字列で表現したものを返す。
406 *
407 * @return 文字列表現
408 */
409 - (NSString *) stringValue
410 {
411 if(nil == [self name] || nil == [self value])
412 return nil;
413 return [NSString stringWithFormat : @"%@=%@",
414 [self name],
415 [self value]];
416 }
417
418 /////////////////////////////////////////////////////////////////////
419 /////////////////////////// NSObject ////////////////////////////////
420 /////////////////////////////////////////////////////////////////////
421 - (NSString *) description
422 {
423 return [self stringValue];
424 }
425
426 - (BOOL) isEqual : (id) obj
427 {
428 if([super isEqual : obj])
429 return YES;
430 if(NO == [obj isKindOfClass : [self class]])
431 return NO;
432 if(NO == [[obj name] isEqualToString : [self name]])
433 return NO;
434 if(NO == [[obj path] isEqualToString : [self path]])
435 return NO;
436 return YES;
437 }
438 /////////////////////////////////////////////////////////////////////
439 ////////////////////////// NSCopying ////////////////////////////////
440 /////////////////////////////////////////////////////////////////////
441 - (id) copyWithZone : (NSZone *) zone
442 {
443 Cookie *tmpcopy;
444
445 tmpcopy = [[[self class] allocWithZone : zone] init];
446 [tmpcopy setName : [self name]];
447 [tmpcopy setValue : [self value]];
448 [tmpcopy setPath : [self path]];
449 [tmpcopy setDomain : [self domain]];
450 [tmpcopy setExpires : [self expires]];
451 [tmpcopy setSecure : [self secure]];
452 [tmpcopy setIsEnabled : [self isEnabled]];
453
454 return tmpcopy;
455 }
456 @end

Back to OSDN">Back to OSDN
ViewVC Help
Powered by ViewVC 1.1.26