forked from jerrykrinock/ClassesObjC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSSRSS.m
executable file
·667 lines (416 loc) · 16.6 KB
/
SSRSS.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
/*
BSD License
Copyright (c) 2002, Brent Simmons
All rights reserved.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* Neither the name of ranchero.com or Brent Simmons nor the names of its
contributors may be used to endorse or promote products derived
from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*
RSS.m
A class for reading RSS feeds.
Created by Brent Simmons on Wed Apr 17 2002.
Copyright (c) 2002 Brent Simmons. All rights reserved.
*/
#import "SSRSS.h"
@implementation SSRSS
#define titleKey @"title"
#define linkKey @"link"
#define descriptionKey @"description"
/*Public interface*/
- (SSRSS *) initWithTitle: (NSString *) title andDescription: (NSString *) description {
/*
Create an empty feed. Useful for synthetic feeds.
*/
NSMutableDictionary *header;
flRdf = NO;
header = [NSMutableDictionary dictionaryWithCapacity: 2];
[header setObject: title forKey: titleKey];
[header setObject: description forKey: descriptionKey];
headerItems = (NSDictionary *) [header copy];
newsItems = [[NSMutableArray alloc] initWithCapacity: 0];
version = [[NSString alloc] initWithString: @"synthetic"];
return (self);
} /*initWithTitle*/
- (SSRSS *) initWithData:(NSData *) rssData normalize:(BOOL)fl {
if ((self = [super init])) {
CFXMLTreeRef tree = NULL ;
flRdf = NO;
normalize = fl;
if (rssData) {
NS_DURING
tree = CFXMLTreeCreateFromData (kCFAllocatorDefault, (CFDataRef)rssData,
NULL, kCFXMLParserSkipWhitespace, kCFXMLNodeCurrentVersion);
NS_HANDLER
NSLog(@"System raised exception while parsing RSS data.") ;
tree = nil ;
// See http://lists.apple.com/archives/Objc-language/2008/Sep/msg00133.html ...
[super dealloc] ;
self = nil ;
NS_ENDHANDLER
}
if (tree) {
[self createheaderdictionary: tree];
[self createitemsarray: tree];
[self setversionstring: tree];
CFXMLNodeRef node = CFXMLTreeGetNode (tree);
CFXMLDocumentInfo* documentInfo ;
documentInfo = (CFXMLDocumentInfo*)CFXMLNodeGetInfoPtr(node) ;
encoding = documentInfo->encoding ;
CFRelease (tree);
}
else {
NSString* dataString = [[NSString alloc] initWithData:rssData encoding:NSASCIIStringEncoding] ;
// We used ASCII because if the string is corrupt UTF8 may fail
[dataString release] ;
NSString* filePath = [NSString stringWithFormat:
@"%@/Desktop/GoogleBad.%@.%i.data",
NSHomeDirectory(),
NSUserName(),
[[NSDate date] timeIntervalSinceReferenceDate]] ;
NSString* msg = @"Could not parse data from Google containing some bookmarks.\n\n Some of your Google Bookmarks may be missing.\n\nFile containing bad data was written to your desktop." ;
[rssData writeToFile:filePath atomically:NO] ;
NSRunCriticalAlertPanel(nil, msg, nil, nil, nil) ;
// See http://lists.apple.com/archives/Objc-language/2008/Sep/msg00133.html ...
[super dealloc] ;
self = nil ;
}
}
else {
self = nil ;
}
return self ;
}
- (NSDictionary *) headerItems {
return (headerItems);
} /*headerItems*/
- (NSMutableArray *) newsItems {
return (newsItems);
} /*newsItems*/
- (NSString *) version {
return (version);
} /*version*/
- (CFStringEncoding) encoding {
return (encoding);
} /*encoding*/
- (void) dealloc {
[headerItems release] ;
[newsItems release] ;
[version release] ;
[super dealloc] ;
} /*dealloc*/
/*Private methods. Don't call these: they may change.*/
- (void) createheaderdictionary: (CFXMLTreeRef) tree {
CFXMLTreeRef channelTree, childTree;
CFXMLNodeRef childNode;
int childCount, i;
NSString *childName;
NSMutableDictionary *headerItemsMutable;
channelTree = [self getchanneltree: tree];
if (channelTree == nil) {
NSException *exception = [NSException exceptionWithName: @"RSSCreateHeaderDictionaryFailed"
reason: @"Couldn't find the channel tree." userInfo: nil];
[exception raise];
} /*if*/
childCount = CFTreeGetChildCount (channelTree);
headerItemsMutable = [NSMutableDictionary dictionaryWithCapacity: childCount];
for (i = 0; i < childCount; i++) {
childTree = CFTreeGetChildAtIndex (channelTree, i);
childNode = CFXMLTreeGetNode (childTree);
childName = (NSString *) CFXMLNodeGetString (childNode);
if ([childName hasPrefix: @"rss:"])
childName = [childName substringFromIndex: 4];
if ([childName isEqualToString: @"item"])
break;
if ([childName isEqualTo: @"image"])
[self flattenimagechildren: childTree into: headerItemsMutable];
[headerItemsMutable setObject: [self getelementvalue: childTree] forKey: childName];
} /*for*/
headerItems = [headerItemsMutable copy];
} /*initheaderdictionary*/
- (void) createitemsarray: (CFXMLTreeRef) tree {
CFXMLTreeRef channelTree, childTree, itemTree;
CFXMLNodeRef childNode, itemNode;
NSString *childName;
NSString *itemName, *itemValue;
int childCount, itemChildCount, i, j;
NSMutableDictionary *itemDictionaryMutable;
NSMutableArray *itemsArrayMutable;
if (flRdf)
channelTree = [self getnamedtree: tree name: @"rdf:RDF"];
else
channelTree = [self getchanneltree: tree];
if (channelTree == nil) {
NSException *exception = [NSException exceptionWithName: @"RSSCreateItemsArrayFailed"
reason: @"Couldn't find the news items." userInfo: nil];
[exception raise];
} /*if*/
childCount = CFTreeGetChildCount (channelTree);
itemsArrayMutable = [NSMutableArray arrayWithCapacity: childCount];
for (i = 0; i < childCount; i++) {
childTree = CFTreeGetChildAtIndex (channelTree, i);
childNode = CFXMLTreeGetNode (childTree);
childName = (NSString *) CFXMLNodeGetString (childNode);
if ([childName hasPrefix: @"rss:"])
childName = [childName substringFromIndex: 4];
if (![childName isEqualToString: @"item"])
continue;
itemChildCount = CFTreeGetChildCount (childTree);
itemDictionaryMutable = [NSMutableDictionary dictionaryWithCapacity: itemChildCount];
for (j = 0; j < itemChildCount; j++) {
itemTree = CFTreeGetChildAtIndex (childTree, j);
itemNode = CFXMLTreeGetNode (itemTree);
itemName = (NSString *) CFXMLNodeGetString (itemNode);
if ([itemName hasPrefix: @"rss:"])
itemName = [itemName substringFromIndex: 4];
itemValue = [self getelementvalue: itemTree];
// We never see this in Google Bookmarks
//if ([itemName isEqualToString: @"source"])
// [self flattensourceattributes: itemNode into: itemDictionaryMutable];
// By Jerry: If an item is repeated (such as Google Bookmarks' label, should concatenate)
#define GOOGLE_LABEL_NAME @"smh:bkmk_label"
if ([itemName isEqualToString:GOOGLE_LABEL_NAME]) {
NSString* existingLabels = [itemDictionaryMutable objectForKey:GOOGLE_LABEL_NAME] ;
if (existingLabels) {
itemValue = [NSString stringWithFormat:
@"%@, %@", existingLabels, itemValue] ;
}
}
[itemDictionaryMutable setObject: itemValue forKey: itemName];
} /*for*/
if (normalize)
[self normalizeRSSItem: itemDictionaryMutable];
[itemsArrayMutable addObject: itemDictionaryMutable];
} /*for*/
newsItems = [itemsArrayMutable copy];
} /*createitemsarray*/
- (void) setversionstring: (CFXMLTreeRef) tree {
CFXMLTreeRef rssTree;
const CFXMLElementInfo *elementInfo;
CFXMLNodeRef node;
if (flRdf) {
version = [[NSString alloc] initWithString: @"rdf"];
return;
} /*if*/
rssTree = [self getnamedtree: tree name: @"rss"];
node = CFXMLTreeGetNode (rssTree);
elementInfo = CFXMLNodeGetInfoPtr (node);
version = [[NSString alloc] initWithString: [(NSDictionary *) (*elementInfo).attributes objectForKey: @"version"]];
} /*setversionstring*/
- (void) flattenimagechildren: (CFXMLTreeRef) tree into: (NSMutableDictionary *) dictionary {
int childCount = CFTreeGetChildCount (tree);
int i = 0;
CFXMLTreeRef childTree;
CFXMLNodeRef childNode;
NSString *childName, *childValue, *keyName;
if (childCount < 1)
return;
for (i = 0; i < childCount; i++) {
childTree = CFTreeGetChildAtIndex (tree, i);
childNode = CFXMLTreeGetNode (childTree);
childName = (NSString *) CFXMLNodeGetString (childNode);
if ([childName hasPrefix: @"rss:"])
childName = [childName substringFromIndex: 4];
childValue = [self getelementvalue: childTree];
keyName = [NSString stringWithFormat: @"image%@", childName];
[dictionary setObject: childValue forKey: keyName];
} /*for*/
} /*flattenimagechildren*/
- (void) flattensourceattributes: (CFXMLNodeRef) node into: (NSMutableDictionary *) dictionary {
const CFXMLElementInfo *elementInfo;
NSString *sourceHomeUrl, *sourceRssUrl;
elementInfo = CFXMLNodeGetInfoPtr (node);
sourceHomeUrl = [(NSDictionary *) (*elementInfo).attributes objectForKey: @"homeUrl"];
sourceRssUrl = [(NSDictionary *) (*elementInfo).attributes objectForKey: @"url"];
if (sourceHomeUrl != nil)
[dictionary setObject: sourceHomeUrl forKey: @"sourceHomeUrl"];
if (sourceRssUrl != nil)
[dictionary setObject: sourceRssUrl forKey: @"sourceRssUrl"];
} /*flattensourceattributes*/
- (CFXMLTreeRef) getchanneltree: (CFXMLTreeRef) tree {
CFXMLTreeRef rssTree, channelTree;
rssTree = [self getnamedtree: tree name: @"rss"];
if (rssTree == nil) { /*It might be "rdf:RDF" instead, a 1.0 or greater feed.*/
rssTree = [self getnamedtree: tree name: @"rdf:RDF"];
if (rssTree != nil)
flRdf = YES; /*This info will be needed later when creating the items array.*/
} /*if*/
if (rssTree == nil)
return (nil);
channelTree = [self getnamedtree: rssTree name: @"channel"];
if (channelTree == nil)
channelTree = [self getnamedtree: rssTree name: @"rss:channel"];
return (channelTree);
} /*getchanneltree*/
- (CFXMLTreeRef) getnamedtree: (CFXMLTreeRef) currentTree name: (NSString *) name {
int childCount, index;
CFXMLNodeRef xmlNode;
CFXMLTreeRef xmlTreeNode;
NSString *itemName;
childCount = CFTreeGetChildCount (currentTree);
for (index = childCount - 1; index >= 0; index--) {
xmlTreeNode = CFTreeGetChildAtIndex (currentTree, index);
xmlNode = CFXMLTreeGetNode (xmlTreeNode);
itemName = (NSString *) CFXMLNodeGetString (xmlNode);
if ([itemName isEqualToString: name])
return (xmlTreeNode);
} /*for*/
return (nil);
} /*getnamedtree*/
- (void) normalizeRSSItem: (NSMutableDictionary *) rssItem {
/*
Make sure item, link, and description are present and have
reasonable values. Description and link may be "".
Also trim white space, remove HTML when appropriate.
*/
NSString *description, *link, *title;
BOOL nilDescription = NO;
/*Description*/
description = [rssItem objectForKey: descriptionKey];
if (description == nil) {
description = @"";
nilDescription = YES;
} /*if*/
description = [description trimWhiteSpace];
if ([description isEqualTo: @""])
nilDescription = YES;
[rssItem setObject: description forKey: descriptionKey];
/*Link*/
link = [rssItem objectForKey: linkKey];
if ([NSString stringIsEmpty: link]) {
/*Try to get a URL from the description.*/
if (!nilDescription) {
NSArray *stringComponents = [description componentsSeparatedByString: @"href=\""];
if ([stringComponents count] > 1) {
link = [stringComponents objectAtIndex: 1];
stringComponents = [link componentsSeparatedByString: @"\""];
link = [stringComponents objectAtIndex: 0];
} /*if*/
} /*if*/
} /*if*/
if (link == nil)
link = @"";
link = [link trimWhiteSpace];
[rssItem setObject: link forKey: linkKey];
/*Title*/
title = [rssItem objectForKey: titleKey];
if (title != nil) {
title = [title stripHTML];
title = [title trimWhiteSpace];
} /*if*/
if ([NSString stringIsEmpty: title]) {
/*Grab a title from the description.*/
if (!nilDescription) {
NSArray *stringComponents = [description componentsSeparatedByString: @">"];
if ([stringComponents count] > 1) {
title = [stringComponents objectAtIndex: 1];
stringComponents = [title componentsSeparatedByString: @"<"];
title = [stringComponents objectAtIndex: 0];
title = [title stripHTML];
title = [title trimWhiteSpace];
} /*if*/
if ([NSString stringIsEmpty: title]) { /*use first part of description*/
NSString *shortTitle = [[[description stripHTML] trimWhiteSpace] ellipsizeAfterNWords: 5];
shortTitle = [shortTitle trimWhiteSpace];
title = [NSString stringWithFormat: @"%@...", shortTitle];
} /*else*/
} /*if*/
title = [title stripHTML];
title = [title trimWhiteSpace];
if ([NSString stringIsEmpty: title])
title = @"Untitled";
} /*if*/
[rssItem setObject: title forKey: titleKey];
/*dangerousmeta case: super-long title with no description*/
if ((nilDescription) && ([title length] > 50)) {
NSString *shortTitle = [[[title stripHTML] trimWhiteSpace] ellipsizeAfterNWords: 7];
description = [[title copy] autorelease];
[rssItem setObject: description forKey: descriptionKey];
title = [NSString stringWithFormat: @"%@...", shortTitle];
[rssItem setObject: title forKey: titleKey];
} /*if*/
{ /*deal with entities*/
const char *tempcstring;
NSAttributedString *s = nil;
NSString *convertedTitle = nil;
NSArray *stringComponents;
stringComponents = [title componentsSeparatedByString: @"&"];
if ([stringComponents count] > 1) {
stringComponents = [title componentsSeparatedByString: @";"];
if ([stringComponents count] > 1) {
int len;
tempcstring = [title UTF8String];
len = strlen (tempcstring);
if (len > 0) {
s = [[NSAttributedString alloc]
initWithHTML: [NSData dataWithBytes: tempcstring length: strlen (tempcstring)]
documentAttributes: (NSDictionary **) NULL];
convertedTitle = [s string];
[s autorelease];
convertedTitle = [convertedTitle stripHTML];
convertedTitle = [convertedTitle trimWhiteSpace];
} /*if*/
if ([NSString stringIsEmpty: convertedTitle])
convertedTitle = @"Untitled";
[rssItem setObject: convertedTitle forKey: @"convertedTitle"];
} /*if*/
} /*if*/
} /*deal with entities*/
} /*normalizeRSSItem*/
- (NSString *) getelementvalue: (CFXMLTreeRef) tree {
CFXMLNodeRef node;
CFXMLTreeRef itemTree;
int childCount, ix;
NSMutableString *valueMutable;
NSString *value;
NSString *name;
childCount = CFTreeGetChildCount (tree);
valueMutable = [[NSMutableString alloc] init];
for (ix = 0; ix < childCount; ix++) {
itemTree = CFTreeGetChildAtIndex (tree, ix);
node = CFXMLTreeGetNode (itemTree);
name = (NSString *) CFXMLNodeGetString (node);
if (name != nil) {
if (CFXMLNodeGetTypeCode (node) == kCFXMLNodeTypeEntityReference) {
if ([name isEqualTo: @"lt"])
name = @"<";
else if ([name isEqualTo: @"gt"])
name = @">";
else if ([name isEqualTo: @"quot"])
name = @"\"";
else if ([name isEqualTo: @"amp"])
name = @"&";
else if ([name isEqualTo: @"rsquo"])
name = [NSString stringWithFormat:@"%C", 8217];
else if ([name isEqualTo: @"lsquo"])
name = [NSString stringWithFormat:@"%C", 8216];
else if ([name isEqualTo: @"apos"])
name = @"'";
else
name = [NSString stringWithFormat: @"&%@;", name];
} /*if*/
[valueMutable appendString: name];
} /*if*/
} /*for*/
value = [valueMutable copy];
[valueMutable autorelease];
return ([value autorelease]);
} /*getelementvalue*/
@end