Skip to content
This repository
Fetching contributors…

Cannot retrieve contributors at this time

file 1202 lines (1076 sloc) 33.026 kb
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 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202
//
// RichXMLParser.m
// Vienna
//
// Created by Steve on 5/22/05.
// Copyright (c) 2004-2005 Steve Palmer. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#import "RichXMLParser.h"
#import "StringExtensions.h"
#import "ArrayExtensions.h"
#import "XMLTag.h"

@interface FeedItem (Private)
-(void)setTitle:(NSString *)newTitle;
-(void)setDescription:(NSString *)newDescription;
-(void)setAuthor:(NSString *)newAuthor;
-(void)setDate:(NSDate *)newDate;
-(void)setGuid:(NSString *)newGuid;
-(void)setLink:(NSString *)newLink;
-(void)setEnclosure:(NSString *)newEnclosure;
@end

@interface RichXMLParser (Private)
-(void)reset;
-(NSData *)preFlightValidation:(NSData *)xmlData;
-(NSStringEncoding)parseEncodingType:(NSData *)xmlData;
-(BOOL)initRSSFeed:(XMLParser *)feedTree isRDF:(BOOL)isRDF;
-(XMLParser *)channelTree:(XMLParser *)feedTree;
-(BOOL)initRSSFeedHeader:(XMLParser *)feedTree;
-(BOOL)initRSSFeedItems:(XMLParser *)feedTree;
-(BOOL)initAtomFeed:(XMLParser *)feedTree;
-(void)parseSequence:(XMLParser *)seqTree;
-(void)setTitle:(NSString *)newTitle;
-(void)setLink:(NSString *)newLink;
-(void)setDescription:(NSString *)newDescription;
-(void)setLastModified:(NSDate *)newDate;
-(NSString *)stripHTMLTags:(NSString *)htmlString;
-(void)ensureTitle:(FeedItem *)item;
@end

@implementation FeedItem

/* init
* Creates a FeedItem instance
*/
-(id)init
{
if ((self = [super init]) != nil)
{
[self setTitle:@""];
[self setDescription:@""];
[self setAuthor:@""];
[self setGuid:@""];
[self setDate:nil];
[self setLink:@""];
[self setEnclosure:@""];
}
return self;
}

/* setEnclosure
* Set the item title.
*/
-(void)setEnclosure:(NSString *)newEnclosure
{
[newEnclosure retain];
[enclosure release];
enclosure = newEnclosure;
}

/* setTitle
* Set the item title.
*/
-(void)setTitle:(NSString *)newTitle
{
[newTitle retain];
[title release];
title = newTitle;
}

/* setDescription
* Set the item description.
*/
-(void)setDescription:(NSString *)newDescription
{
[newDescription retain];
[description release];
description = newDescription;
}

/* setAuthor
* Set the item author.
*/
-(void)setAuthor:(NSString *)newAuthor
{
[newAuthor retain];
[author release];
author = newAuthor;
}

/* setDate
* Set the item date
*/
-(void)setDate:(NSDate *)newDate
{
[newDate retain];
[date release];
date = newDate;
}

/* setGuid
* Set the item GUID.
*/
-(void)setGuid:(NSString *)newGuid
{
[newGuid retain];
[guid release];
guid = newGuid;
}

/* setLink
* Set the item link.
*/
-(void)setLink:(NSString *)newLink
{
[newLink retain];
[link release];
link = newLink;
}

/* title
* Returns the item title.
*/
-(NSString *)title
{
return title;
}

/* description
* Returns the item description
*/
-(NSString *)description
{
return description;
}

/* author
* Returns the item author
*/
-(NSString *)author
{
return author;
}

/* date
* Returns the item date
*/
-(NSDate *)date
{
return date;
}

/* guid
* Returns the item GUID.
*/
-(NSString *)guid
{
return guid;
}

/* link
* Returns the item link.
*/
-(NSString *)link
{
return link;
}

/* enclosure
* Returns the associated enclosure.
*/
-(NSString *)enclosure
{
return enclosure;
}

/* dealloc
* Clean up when we're released.
*/
-(void)dealloc
{
[guid release];
[title release];
[description release];
[author release];
[date release];
[link release];
[enclosure release];
[super dealloc];
}
@end

@implementation RichXMLParser

/* init
* Creates a RichXMLParser instance.
*/
-(id)init
{
if ((self = [super init]) != nil)
{
[self setTitle:@""];
[self setDescription:@""];
lastModified = nil;
link = nil;
items = nil;
orderArray = nil;
}
return self;
}

/* reset
* Reset to remove existing feed info.
*/
-(void)reset
{
[title release];
[description release];
[lastModified release];
[link release];
[items release];
title = nil;
description = nil;
lastModified = nil;
link = nil;
items = nil;
}

/* parseRichXML
* Given an XML feed in xmlData, parses the feed as either an RSS or an Atom feed.
* The actual parsed items can subsequently be accessed through the interface.
*/
-(BOOL)parseRichXML:(NSData *)xmlData
{
BOOL success = NO;
NS_DURING
NSData * parsedXmlData = [self preFlightValidation:xmlData];
if (parsedXmlData && [self setData:parsedXmlData])
{
XMLParser * subtree;

// If this RSS?
if ((subtree = [self treeByName:@"rss"]) != nil)
success = [self initRSSFeed:subtree isRDF:NO];

// If this RSS:RDF?
else if ((subtree = [self treeByName:@"rdf:RDF"]) != nil)
success = [self initRSSFeed:subtree isRDF:YES];

// Atom?
else if ((subtree = [self treeByName:@"feed"]) != nil)
success = [self initAtomFeed:subtree];
}
NS_HANDLER
success = NO;
NS_ENDHANDLER
return success;
}

/* extractFeeds
* Given a block of XML data, determine whether this is HTML format and, if so,
* extract all RSS links in the data. Returns YES if we found any feeds, or NO if
* this was not HTML.
*/
+(BOOL)extractFeeds:(NSData *)xmlData toArray:(NSMutableArray *)linkArray
{
BOOL success = NO;
NS_DURING
NSArray * arrayOfTags = [XMLTag parserFromData:xmlData];
if (arrayOfTags != nil)
{
for (XMLTag * tag in arrayOfTags)
{
NSString * tagName = [tag name];

if ([tagName isEqualToString:@"rss"] || [tagName isEqualToString:@"rdf:rdf"] || [tagName isEqualToString:@"feed"])
{
success = NO;
break;
}
if ([tagName isEqualToString:@"link"])
{
NSDictionary * tagAttributes = [tag attributes];
NSString * linkType = [tagAttributes objectForKey:@"type"];

// We're looking for the link tag. Specifically we're looking for the one which
// has application/rss+xml or atom+xml type. There may be more than one which is why we're
// going to be returning an array.
if ([linkType isEqualToString:@"application/rss+xml"])
{
NSString * href = [tagAttributes objectForKey:@"href"];
if (href != nil)
[linkArray addObject:href];
}
else if ([linkType isEqualToString:@"application/atom+xml"])
{
NSString * href = [tagAttributes objectForKey:@"href"];
if (href != nil)
[linkArray addObject:href];
}
}
if ([tagName isEqualToString:@"/head"])
break;
success = [linkArray count] > 0;
}
}
NS_HANDLER
success = NO;
NS_ENDHANDLER
return success;
}

/* preFlightValidation
* Try and sanitise the XML data before the XML parser gets a chance to reject it. This
* should address the most common bad-feed errors until we can change the parser to one
* that provides us more control.
*/
-(NSData *)preFlightValidation:(NSData *)xmlData
{
NSUInteger count = [xmlData length];
const unsigned char * srcPtr = [xmlData bytes];
const unsigned char * srcEndPtr = srcPtr + count;

// We'll create another data stream with the converted characters
NSMutableData * newXmlData = [NSMutableData dataWithLength:count];
char * destPtr = [newXmlData mutableBytes];
NSUInteger destCapacity = count;
NSUInteger destSize = count;
NSUInteger destIndex = 0;

// Determine XML encoding and BOM
NSStringEncoding encodedType;

if ( (count > 2 && srcPtr[0] == 0xFE && srcPtr[1] == 0xFF) ||
(count > 2 && srcPtr[0] == 0xFF && srcPtr[1] == 0xFE) )
{
// Copy Unicode UTF-16 big/little-endian BOM.
destPtr[destIndex++] = srcPtr[0];
destPtr[destIndex++] = srcPtr[1];
srcPtr += 2;

char* encodingNameStr = "UTF-16";
CFStringRef encodingName = CFStringCreateWithBytes(kCFAllocatorDefault, (unsigned char *)encodingNameStr, strlen(encodingNameStr), kCFStringEncodingISOLatin1, false);
encodedType = CFStringConvertIANACharSetNameToEncoding(encodingName);
CFRelease(encodingName);
}

else if (count > 3 && srcPtr[0] == 0xEF && srcPtr[1] == 0xBB && srcPtr[2] == 0xBF)
{
// Copy Unicode UTF-8 little-endian BOM.
destPtr[destIndex++] = srcPtr[0];
destPtr[destIndex++] = srcPtr[1];
destPtr[destIndex++] = srcPtr[2];
srcPtr += 3;

char* encodingNameStr = "UTF-8";
CFStringRef encodingName = CFStringCreateWithBytes(kCFAllocatorDefault, (unsigned char *)encodingNameStr, strlen(encodingNameStr), kCFStringEncodingISOLatin1, false);
encodedType = CFStringConvertIANACharSetNameToEncoding(encodingName);
CFRelease(encodingName);
}

else
{
// Lets see if we have any better luck parsing the XML
encodedType = [self parseEncodingType:xmlData];
}

while (srcPtr < srcEndPtr)
{
unsigned char ch = *srcPtr++;
if (ch >= 0xC0 && ch <= 0xFD && srcPtr < srcEndPtr && *srcPtr >= 0x80 && *srcPtr <= 0xBF)
{
// Copy UTF-8 lead bytes unchanged. The parser can cope with
// these fine.
destPtr[destIndex++] = ch;
while (srcPtr < srcEndPtr && (*srcPtr & 0x80))
destPtr[destIndex++] = *srcPtr++;
}
else if (ch > 0x7F && encodedType == NSUTF8StringEncoding)
{
// Other characters with their high bits set are not valid UTF-8.
// But regardless of the encoding scheme, their entity equivalents
// are. So convert them into a hex entity character code.
if (destSize + 5 > destCapacity)
{
[newXmlData setLength:destCapacity += 256];
destPtr = [newXmlData mutableBytes];
}
destPtr[destIndex++] = '&';
destPtr[destIndex++] = '#';
destPtr[destIndex++] = 'x';
destPtr[destIndex++] = "0123456789ABCDEF"[(ch / 16)];
destPtr[destIndex++] = "0123456789ABCDEF"[(ch % 16)];
destPtr[destIndex++] = ';';
destSize += 5;
}
else if (ch == '&' && srcPtr < srcEndPtr && *srcPtr != '#')
{
// Some feeds use a '&' outside of its intended use as an entity
// delimiter. So if '&' is followed by a non-alphanumeric, make it
// into its entity equivalent.
const unsigned char * srcTmpPtr = srcPtr;
while (srcTmpPtr < srcEndPtr && isalpha(*srcTmpPtr))
++srcTmpPtr;
if (srcTmpPtr < srcEndPtr && *srcTmpPtr == ';')
destPtr[destIndex++] = '&';
else
{
if (destSize + 4 > destCapacity)
{
[newXmlData setLength:destCapacity += 256];
destPtr = [newXmlData mutableBytes];
}
destPtr[destIndex++] = '&';
destPtr[destIndex++] = 'a';
destPtr[destIndex++] = 'm';
destPtr[destIndex++] = 'p';
destPtr[destIndex++] = ';';
destSize += 4;
}
}
else
destPtr[destIndex++] = ch;
}
NSAssert(destIndex == destSize, @"Did not copy all data bytes to destination buffer");
[newXmlData setLength:destIndex];

// Make sure that the last valid character of the feed is '>' otherwise it was truncated. The
// CFXML parser annoyingly crashes if it is given a truncated feed.
while (--destIndex > 0 && (destPtr[destIndex] == '\0' || isspace(destPtr[destIndex])));
return (destPtr[destIndex] == '>') ? newXmlData : nil;
}

/* parseEncodingType
* Parse off the encoding field.
*/
-(NSStringEncoding)parseEncodingType:(NSData *)xmlData
{
NSStringEncoding encodingType = NSUTF8StringEncoding;
const char * textPtr = [xmlData bytes];
const char * textEndPtr = textPtr + [xmlData length];

while (textPtr < textEndPtr && *textPtr != '<')
++textPtr;

// Scan for the encoding attribute name up until the closing tag
const char * encodingAttribute = "encoding=";
const char * encodingAttributePtr = encodingAttribute;
while (textPtr < textEndPtr && *encodingAttributePtr != '\0' && *textPtr != '>')
{
if (*textPtr == *encodingAttributePtr)
++encodingAttributePtr;
else
encodingAttributePtr = encodingAttribute;
++textPtr;
}

// If we found it, parse off the encoding type name
if (*encodingAttributePtr == '\0')
{
if (textPtr < textEndPtr && *textPtr == '"')
++textPtr;

// We need to special case UTF-8 as CFStringConvertIANACharSetNameToEncoding
// doesn't recognise it.
const char * encodingNamePtr = textPtr;
const char * utf8EncodingName = "UTF-8";
const char * utf8EncodingNamePtr = utf8EncodingName;
while (textPtr < textEndPtr && *textPtr != '"')
{
if (toupper(*textPtr) == *utf8EncodingNamePtr)
++utf8EncodingNamePtr;
else
utf8EncodingNamePtr = utf8EncodingName;
++textPtr;
}

// Now extract the encoding name if it wasn't UTF-8
if (*utf8EncodingNamePtr != '\0')
{
CFStringRef encodingName = CFStringCreateWithBytes(kCFAllocatorDefault, (unsigned char *)encodingNamePtr, textPtr - encodingNamePtr, kCFStringEncodingISOLatin1, false);
encodingType = CFStringConvertIANACharSetNameToEncoding(encodingName);
CFRelease(encodingName);
}
}
return encodingType;
}

/* initRSSFeed
* Prime the feed with header and items from an RSS feed
*/
-(BOOL)initRSSFeed:(XMLParser *)feedTree isRDF:(BOOL)isRDF
{
BOOL success = [self initRSSFeedHeader:[self channelTree:feedTree]];
if (success)
{
if (isRDF)
success = [self initRSSFeedItems:feedTree];
else
success = [self initRSSFeedItems:[self channelTree:feedTree]];
}
return success;
}

/* channelTree
* Return the root of the RSS feed's channel.
*/
-(XMLParser *)channelTree:(XMLParser *)feedTree
{
XMLParser * channelTree = [feedTree treeByName:@"channel"];
if (channelTree == nil)
channelTree = [feedTree treeByName:@"rss:channel"];
return channelTree;
}

/* initRSSFeedHeader
* Parse an RSS feed header items.
*/
-(BOOL)initRSSFeedHeader:(XMLParser *)feedTree
{
BOOL success = YES;

// Iterate through the channel items
CFIndex count = [feedTree countOfChildren];
CFIndex index;

for (index = 0; index < count; ++index)
{
XMLParser * subTree = [feedTree treeByIndex:index];
NSString * nodeName = [subTree nodeName];

// Parse title
if ([nodeName isEqualToString:@"title"])
{
[self setTitle:[[subTree valueOfElement] stringByUnescapingExtendedCharacters]];
continue;
}

// Parse items group which dictates the sequence of the articles.
if ([nodeName isEqualToString:@"items"])
{
XMLParser * seqTree = [subTree treeByName:@"rdf:Seq"];
if (seqTree != nil)
[self parseSequence:seqTree];
}

// Parse description
if ([nodeName isEqualToString:@"description"])
{
[self setDescription:[subTree valueOfElement]];
continue;
}

// Parse link
if ([nodeName isEqualToString:@"link"])
{
[self setLink:[[subTree valueOfElement] stringByUnescapingExtendedCharacters]];
continue;
}

// Parse the date when this feed was last updated
if ([nodeName isEqualToString:@"lastBuildDate"])
{
NSString * dateString = [subTree valueOfElement];
[self setLastModified:[XMLParser parseXMLDate:dateString]];
continue;
}

// Parse item date
if ([nodeName isEqualToString:@"dc:date"])
{
NSString * dateString = [subTree valueOfElement];
[self setLastModified:[XMLParser parseXMLDate:dateString]];
continue;
}

// Parse item date
if ([nodeName isEqualToString:@"pubDate"])
{
NSString * dateString = [subTree valueOfElement];
[self setLastModified:[XMLParser parseXMLDate:dateString]];
continue;
}
}
return success;
}

/* parseSequence
* Parses an RDF sequence and initialises orderArray with the appropriate sequence.
* The RSS parser will then use this to order the actual items appropriately.
*/
-(void)parseSequence:(XMLParser *)seqTree
{
CFIndex count = [seqTree countOfChildren];
CFIndex index;

[orderArray release];
orderArray = [[NSMutableArray alloc] initWithCapacity:count];
for (index = 0; index < count; ++index)
{
XMLParser * subTree = [seqTree treeByIndex:index];
if ([[subTree nodeName] isEqualToString:@"rdf:li"])
{
NSString * resourceString = [subTree valueOfAttribute:@"rdf:resource"];
if (resourceString == nil)
resourceString = [subTree valueOfAttribute:@"resource"];
if (resourceString != nil)
[orderArray addObject:resourceString];
}
}
}

/* initRSSFeedItems
* Parse the items from an RSS feed
*/
-(BOOL)initRSSFeedItems:(XMLParser *)feedTree
{
BOOL success = YES;

// Iterate through the channel items
CFIndex count = [feedTree countOfChildren];
CFIndex index;

// Allocate an items array
NSAssert(items == nil, @"initRSSFeedItems called more than once per initialisation");
items = [[NSMutableArray alloc] initWithCapacity:count];

for (index = 0; index < count; ++index)
{
XMLParser * subTree = [feedTree treeByIndex:index];
NSString * nodeName = [subTree nodeName];

// Parse a single item to construct a FeedItem object which is appended to
// the items array we maintain.
if ([nodeName isEqualToString:@"item"])
{
FeedItem * newItem = [[FeedItem alloc] init];
CFIndex itemCount = [subTree countOfChildren];
NSMutableString * articleBody = nil;
BOOL hasDetailedContent = NO;
BOOL hasLink = NO;
CFIndex itemIndex;

// Check for rdf:about so we can identify this item in the orderArray.
NSString * itemIdentifier = [subTree valueOfAttribute:@"rdf:about"];

for (itemIndex = 0; itemIndex < itemCount; ++itemIndex)
{
XMLParser * subItemTree = [subTree treeByIndex:itemIndex];
NSString * itemNodeName = [subItemTree nodeName];

// Parse item title
if ([itemNodeName isEqualToString:@"title"])
{
NSString * newTitle = [[subItemTree valueOfElement] stringByUnescapingExtendedCharacters];
[newItem setTitle:[self stripHTMLTags:newTitle]];
continue;
}

// Parse item description
if ([itemNodeName isEqualToString:@"description"] && !hasDetailedContent)
{
[articleBody release];
articleBody = [[[NSMutableString alloc] initWithString:[subItemTree valueOfElement]] retain];
// FIX WARNING
// articleBody = [[subItemTree valueOfElement] retain];
continue;
}

// Parse GUID. The GUID may optionally have a permaLink attribute
// in which case this is also the article link unless overridden by
// an explicit link tag.
if ([itemNodeName isEqualToString:@"guid"])
{
NSString * permaLink = [subItemTree valueOfAttribute:@"isPermaLink"];
if (permaLink && [permaLink isEqualToString:@"true"] && !hasLink)
[newItem setLink:[subItemTree valueOfElement]];
[newItem setGuid:[subItemTree valueOfElement]];
continue;
}

// Parse detailed item description. This overrides the existing
// description for this item.
if ([itemNodeName isEqualToString:@"content:encoded"])
{
[articleBody release];
articleBody = [[[NSMutableString alloc] initWithString:[subItemTree valueOfElement]] retain];
// FIX WARNING
//articleBody = [[subItemTree valueOfElement] retain];
hasDetailedContent = YES;
continue;
}

// Parse item author
if ([itemNodeName isEqualToString:@"author"])
{
[newItem setAuthor:[subItemTree valueOfElement]];
continue;
}

// Parse item author
if ([itemNodeName isEqualToString:@"dc:creator"])
{
[newItem setAuthor:[subItemTree valueOfElement]];
continue;
}

// Parse item date
if ([itemNodeName isEqualToString:@"dc:date"])
{
NSString * dateString = [subItemTree valueOfElement];
[newItem setDate:[XMLParser parseXMLDate:dateString]];
continue;
}

// Parse item link
if ([itemNodeName isEqualToString:@"link"])
{
[newItem setLink:[[subItemTree valueOfElement] stringByUnescapingExtendedCharacters]];
hasLink = YES;
continue;
}

// Parse item date
if ([itemNodeName isEqualToString:@"pubDate"])
{
NSString * dateString = [subItemTree valueOfElement];
[newItem setDate:[XMLParser parseXMLDate:dateString]];
continue;
}

// Parse associated enclosure
if ([itemNodeName isEqualToString:@"enclosure"])
{
if ([subItemTree valueOfAttribute:@"url"])
[newItem setEnclosure:[subItemTree valueOfAttribute:@"url"]];
continue;
}

}

// If no link, set it to the feed link if there is one
if (!hasLink && [self link])
[newItem setLink:[self link]];

// Do relative IMG tag fixup
[articleBody fixupRelativeImgTags:[self link]];
[newItem setDescription:SafeString(articleBody)];
[articleBody release];

// Derive any missing title
[self ensureTitle:newItem];

// Add this item in the proper location in the array
NSUInteger indexOfItem = (orderArray && itemIdentifier) ? [orderArray indexOfStringInArray:itemIdentifier] : NSNotFound;
if (indexOfItem == NSNotFound || indexOfItem >= [items count])
[items addObject:newItem];
else
[items insertObject:newItem atIndex:indexOfItem];
[newItem release];
}
}

// Now scan the array and set the article date if it is missing. We'll use the
// last modified date of the feed and set each article to be 1 second older than the
// previous one. So the array is effectively newest first.
NSDate * itemDate = [self lastModified];
if (itemDate == nil)
itemDate = [NSDate date];
for (FeedItem * anItem in items)
{
if ([anItem date] == nil)
[anItem setDate:itemDate];
itemDate = [itemDate dateByAddingTimeInterval:-1.0];
}
return success;
}

/* initAtomFeed
* Prime the feed with header and items from an Atom feed
*/
-(BOOL)initAtomFeed:(XMLParser *)feedTree
{
BOOL success = YES;

// Allocate an items array
NSAssert(items == nil, @"initAtomFeed called more than once per initialisation");
items = [[NSMutableArray alloc] initWithCapacity:10];

// Look for feed attributes we need to process
NSString * linkBase = [[feedTree valueOfAttribute:@"xml:base"] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
if (linkBase == nil)
linkBase = [[self link] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL * linkBaseURL = (linkBase != nil) ? [NSURL URLWithString:linkBase] : nil;

// Iterate through the atom items
NSString * defaultAuthor = @"";
CFIndex count = [feedTree countOfChildren];
CFIndex index;

for (index = 0; index < count; ++index)
{
XMLParser * subTree = [feedTree treeByIndex:index];
NSString * nodeName = [subTree nodeName];

// Parse title
if ([nodeName isEqualToString:@"title"])
{
[self setTitle:[[[subTree valueOfElement] stringByUnescapingExtendedCharacters] summaryTextFromHTML]];
continue;
}

// Parse description]
if ([nodeName isEqualToString:@"subtitle"])
{
[self setDescription:[subTree valueOfElement]];
continue;
}

// Parse description
if ([nodeName isEqualToString:@"tagline"])
{
[self setDescription:[subTree valueOfElement]];
continue;
}

// Parse link
if ([nodeName isEqualToString:@"link"])
{
if ([subTree valueOfAttribute:@"rel"] == nil || [[subTree valueOfAttribute:@"rel"] isEqualToString:@"alternate"])
{
NSString * theLink = [[subTree valueOfAttribute:@"href"] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
if (theLink != nil)
{
if ((linkBaseURL != nil) && ![theLink hasPrefix:@"http://"])
{
NSURL * theLinkURL = [NSURL URLWithString:theLink relativeToURL:linkBaseURL];
[self setLink:(theLinkURL != nil) ? [theLinkURL absoluteString] : theLink];
}
else
[self setLink:theLink];
}
}
continue;
}

// Parse author at the feed level. This is the default for any entry
// that doesn't have an explicit author.
if ([nodeName isEqualToString:@"author"])
{
XMLParser * emailTree = [subTree treeByName:@"name"];
if (emailTree != nil)
defaultAuthor = [emailTree valueOfElement];
continue;
}

// Parse the date when this feed was last updated
if ([nodeName isEqualToString:@"updated"])
{
NSString * dateString = [subTree valueOfElement];
[self setLastModified:[XMLParser parseXMLDate:dateString]];
continue;
}

// Parse the date when this feed was last updated
if ([nodeName isEqualToString:@"modified"])
{
NSString * dateString = [subTree valueOfElement];
[self setLastModified:[XMLParser parseXMLDate:dateString]];
continue;
}

// Parse a single item to construct a FeedItem object which is appended to
// the items array we maintain.
if ([nodeName isEqualToString:@"entry"])
{
FeedItem * newItem = [[FeedItem alloc] init];
[newItem setAuthor:defaultAuthor];
CFIndex itemCount = [subTree countOfChildren];
NSMutableString * articleBody = nil;
CFIndex itemIndex;

// Look for the xml:base attribute, and use absolute url or stack relative url
NSString * entryBase = [[subTree valueOfAttribute:@"xml:base"] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
if (entryBase == nil)
entryBase = linkBase;
NSURL * entryBaseURL = (entryBase != nil) ? [NSURL URLWithString:entryBase] : nil;
if ((entryBaseURL != nil) && (linkBaseURL != nil) && ([entryBaseURL scheme] == nil))
{
entryBaseURL = [NSURL URLWithString:entryBase relativeToURL:linkBaseURL];
if (entryBaseURL != nil)
entryBase = [entryBaseURL absoluteString];
}

for (itemIndex = 0; itemIndex < itemCount; ++itemIndex)
{
XMLParser * subItemTree = [subTree treeByIndex:itemIndex];
NSString * itemNodeName = [subItemTree nodeName];

// Parse item title
if ([itemNodeName isEqualToString:@"title"])
{
NSString * newTitle = [[subItemTree valueOfElement] stringByUnescapingExtendedCharacters];
NSString * titleType = [subItemTree valueOfAttribute:@"type"];

if ([titleType isEqualToString:@"html"] || [titleType isEqualToString:@"xhtml"])
newTitle = [self stripHTMLTags:newTitle];

[newItem setTitle:newTitle];
continue;
}

// Parse item description
if ([itemNodeName isEqualToString:@"content"])
{
[articleBody release];
articleBody = [[[NSMutableString alloc] initWithString:[subItemTree valueOfElement]] retain];
// FIX WARNING
// articleBody = [[subItemTree valueOfElement] retain];
continue;
}

// Parse item description
if ([itemNodeName isEqualToString:@"summary"])
{
[articleBody release];
articleBody = [[[NSMutableString alloc] initWithString:[subItemTree valueOfElement]] retain];
// FIX WARNING
// articleBody = [[subItemTree valueOfElement] retain];
continue;
}

// Parse item author
if ([itemNodeName isEqualToString:@"author"])
{
NSString * authorName = [[subItemTree treeByName:@"name"] valueOfElement];
if (authorName == nil)
authorName = [[subItemTree treeByName:@"email"] valueOfElement];
if (authorName != nil)
[newItem setAuthor:authorName];
continue;
}

// Parse item link
if ([itemNodeName isEqualToString:@"link"])
{
if ([[subItemTree valueOfAttribute:@"rel"] isEqualToString:@"enclosure"])
{
NSString * theLink = [[subItemTree valueOfAttribute:@"href"] stringByUnescapingExtendedCharacters];
if (theLink != nil)
{
if ((entryBaseURL != nil) && ([[NSURL URLWithString:theLink] scheme] == nil))
{
NSURL * theLinkURL = [NSURL URLWithString:theLink relativeToURL:entryBaseURL];
[newItem setEnclosure:(theLinkURL != nil) ? [theLinkURL absoluteString] : theLink];
}
else
[newItem setEnclosure:theLink];
}
}
else
{
if ([subItemTree valueOfAttribute:@"rel"] == nil || [[subItemTree valueOfAttribute:@"rel"] isEqualToString:@"alternate"])
{
NSString * theLink = [[subItemTree valueOfAttribute:@"href"] stringByUnescapingExtendedCharacters];
if (theLink != nil)
{
if ((entryBaseURL != nil) && ([[NSURL URLWithString:theLink] scheme] == nil))
{
NSURL * theLinkURL = [NSURL URLWithString:theLink relativeToURL:entryBaseURL];
[newItem setLink:(theLinkURL != nil) ? [theLinkURL absoluteString] : theLink];
}
else
[newItem setLink:theLink];
}
}
continue;
}
}

// Parse item id
if ([itemNodeName isEqualToString:@"id"])
{
[newItem setGuid:[subItemTree valueOfElement]];
continue;
}

// Parse item date
if ([itemNodeName isEqualToString:@"modified"])
{
NSString * dateString = [subItemTree valueOfElement];
NSDate * newDate = [XMLParser parseXMLDate:dateString];
if ([newItem date] == nil || [newDate isGreaterThan:[newItem date]])
[newItem setDate:newDate];
continue;
}

// Parse item date
if ([itemNodeName isEqualToString:@"created"])
{
NSString * dateString = [subItemTree valueOfElement];
NSDate * newDate = [XMLParser parseXMLDate:dateString];
if ([newItem date] == nil || [newDate isGreaterThan:[newItem date]])
[newItem setDate:newDate];
continue;
}

// Parse item date
if ([itemNodeName isEqualToString:@"updated"])
{
NSString * dateString = [subItemTree valueOfElement];
NSDate * newDate = [XMLParser parseXMLDate:dateString];
if ([newItem date] == nil || [newDate isGreaterThan:[newItem date]])
[newItem setDate:newDate];
continue;
}
}

// Do relative IMG tag fixup
[articleBody fixupRelativeImgTags:entryBase];
[newItem setDescription:SafeString(articleBody)];
[articleBody release];

// Derive any missing title
[self ensureTitle:newItem];
[items addObject:newItem];
[newItem release];
}
}

return success;
}

/* setTitle
* Set this feed's title string.
*/
-(void)setTitle:(NSString *)newTitle
{
[newTitle retain];
[title release];
title = newTitle;
}

/* setDescription
* Set this feed's description string.
*/
-(void)setDescription:(NSString *)newDescription
{
[newDescription retain];
[description release];
description = newDescription;
}

/* setLink
* Sets this feed's link
*/
-(void)setLink:(NSString *)newLink
{
[newLink retain];
[link release];
link = newLink;
}

/* setLastModified
* Set the date when this feed was last updated.
*/
-(void)setLastModified:(NSDate *)newDate
{
[newDate retain];
[lastModified release];
lastModified = newDate;
}

/* title
* Return the title string.
*/
-(NSString *)title
{
return title;
}

/* description
* Return the description string.
*/
-(NSString *)description
{
return description;
}

/* link
* Returns the URL of this feed
*/
-(NSString *)link
{
return link;
}

/* items
* Returns the array of items.
*/
-(NSArray *)items
{
return items;
}

/* lastModified
* Returns the feed's last update
*/
-(NSDate *)lastModified
{
return lastModified;
}

/* stripHTMLTags
* Strip off HTML tags from title strings. This code takes stricter approach to
* HTML removal because some feeds use HTML tags in the title which are actually part
* of the title rather than presentation data. So we only remove tags which:
*
* 1. Have a corresponding </tag> instruction.
*/
-(NSString *)stripHTMLTags:(NSString *)htmlString
{
NSMutableString * rawString = [[NSMutableString alloc] initWithString:htmlString];
NSUInteger openTagStartIndex = 0;

while ((openTagStartIndex = [rawString indexOfCharacterInString:'<' afterIndex:openTagStartIndex]) != NSNotFound)
{
NSUInteger openTagEndIndex;
if ((openTagEndIndex = [rawString indexOfCharacterInString:'>' afterIndex:openTagStartIndex]) != NSNotFound)
{
NSString * tagName = [[rawString substringWithRange:NSMakeRange(openTagStartIndex + 1, openTagEndIndex - openTagStartIndex - 1)] lowercaseString];
NSString * closingTag = [NSString stringWithFormat:@"</%@>", [tagName firstWord]];
NSRange openingTagRange = NSMakeRange(openTagStartIndex, openTagEndIndex - openTagStartIndex + 1);
NSRange closingTagRange = [rawString rangeOfString:closingTag options:NSLiteralSearch|NSCaseInsensitiveSearch range:NSMakeRange(openTagEndIndex, [rawString length] - openTagEndIndex)];

if ([tagName isEqualToString:@"br"] || [tagName isEqualToString:@"br /"])
{
[rawString deleteCharactersInRange:openingTagRange];
continue;
}
else if (closingTagRange.location != NSNotFound)
{
[rawString deleteCharactersInRange:closingTagRange];
[rawString deleteCharactersInRange:openingTagRange];
continue;
}
}
++openTagStartIndex;
}
return [rawString autorelease];
}

/* ensureTitle
* Make sure we have a title and synthesize one from the description if we don't.
*/
-(void)ensureTitle:(FeedItem *)item
{
if (![item title] || [[item title] isBlank])
{
NSString * newTitle = [[[item description] titleTextFromHTML] stringByUnescapingExtendedCharacters];
if ([newTitle isBlank])
newTitle = NSLocalizedString(@"(No title)", nil);
[item setTitle:newTitle];
}
}

/* dealloc
* Clean up afterwards.
*/
-(void)dealloc
{
[orderArray release];
[title release];
[description release];
[lastModified release];
[link release];
[items release];
[super dealloc];
}
@end
Something went wrong with that request. Please try again.