views:

21

answers:

0

I want to change the feed source...

the code is:

//
//  BlogRssParser.h
//  RssFun
//
//  Created by Imthiaz Rafiq on 8/15/09.
//  Copyright 2009 __MyCompanyName__. All rights reserved.
//

#import <Foundation/Foundation.h>

@class BlogRss;

@protocol BlogRssParserDelegate;

@interface BlogRssParser : NSObject {
    BlogRss * _currentItem;
    NSMutableString * _currentItemValue;
    NSMutableArray * _rssItems;
    id<BlogRssParserDelegate> _delegate;
    NSOperationQueue *_retrieverQueue;
    NSURL * _feedURL;
}


@property(nonatomic, retain) BlogRss * currentItem;
@property(nonatomic, retain) NSMutableString * currentItemValue;
@property(readonly) NSMutableArray * rssItems;

@property(nonatomic, assign) id<BlogRssParserDelegate> delegate;
@property(nonatomic, retain) NSOperationQueue *retrieverQueue;
@property (readwrite,retain) NSURL * feedURL; 


- (void)startProcess:(NSURL *)aURL;
- (void)impostaurl:(NSURL *)aURL; 

@end

@protocol BlogRssParserDelegate <NSObject>

-(void)processCompleted;
-(void)processHasErrors;

@end

AND

//
//  BlogRssParser.m
//  RssFun
//
//  Created by Imthiaz Rafiq on 8/15/09.
//  Copyright 2009 __MyCompanyName__. All rights reserved.
//

#import "BlogRssParser.h"
#import "BlogRss.h"

@implementation BlogRssParser

@synthesize currentItem = _currentItem;
@synthesize currentItemValue = _currentItemValue;
@synthesize rssItems = _rssItems;
@synthesize delegate = _delegate;
@synthesize retrieverQueue = _retrieverQueue;
@synthesize feedURL = _feedURL; 


- (void)impostaurl:(NSURL *)aURL{
    _feedURL = aURL;
}


- (id)init{
    if(![super init]){
        return nil;
    }
    _rssItems = [[NSMutableArray alloc]init];
    return self;
}

- (NSOperationQueue *)retrieverQueue {
    if(nil == _retrieverQueue) {
          //_retrieverQueue =[[[NSOperationQueue alloc] init] autorelease];
        _retrieverQueue = [[NSOperationQueue alloc] init];
        _retrieverQueue.maxConcurrentOperationCount = 1;
    }
    return _retrieverQueue;
}

- (void)startProcess:(NSURL *)aURL{
        _feedURL = aURL;
    SEL method = @selector(fetchAndParseRss);
    [[self rssItems] removeAllObjects];
    NSInvocationOperation *op = [[NSInvocationOperation alloc] initWithTarget:self 
                                                                     selector:method 
                                                                       object:nil];
    [self.retrieverQueue addOperation:op];
    [op release];
}

-(BOOL)fetchAndParseRss{
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;

    //To suppress the leak in NSXMLParser
    [[NSURLCache sharedURLCache] setMemoryCapacity:0];
    [[NSURLCache sharedURLCache] setDiskCapacity:0];

    NSURL *url = _feedURL;
    BOOL success = NO;
    NSXMLParser *parser = [[NSXMLParser alloc] initWithContentsOfURL:url];
    [parser setDelegate:self];
    [parser setShouldProcessNamespaces:YES];
    [parser setShouldReportNamespacePrefixes:YES];
    [parser setShouldResolveExternalEntities:NO];
    success = [parser parse];
    [parser release];
    [pool drain];
    return success;
}

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI 
 qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict{
    if(nil != qualifiedName){
        elementName = qualifiedName;
    }
    if ([elementName isEqualToString:@"item"]) {
        self.currentItem = [[[BlogRss alloc]init]autorelease];
    }else if ([elementName isEqualToString:@"thumbnail"]) {
        self.currentItem.mediaUrl = [attributeDict valueForKey:@"url"];
    }else if ([elementName isEqualToString:@"permalink"]) {
        self.currentItem.originalurl = [attributeDict valueForKey:@"url"];
    } else if([elementName isEqualToString:@"title"] || 
              [elementName isEqualToString:@"description"] ||
              [elementName isEqualToString:@"link"] ||
              [elementName isEqualToString:@"guid"] ||
              [elementName isEqualToString:@"pubDate"]) {
        self.currentItemValue = [NSMutableString string];
    } else {
        self.currentItemValue = nil;
    }   
}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
    if(nil != qName){
        elementName = qName;
    }
    if([elementName isEqualToString:@"title"]){
        self.currentItem.title = self.currentItemValue;
    }else if([elementName isEqualToString:@"description"]){
        self.currentItem.description = self.currentItemValue;

        //FORMATTO LA STRINGA
        NSString *descrizione;

        descrizione = self.currentItem.description;
        // TOLGO I TAG HTML
        NSScanner *theScanner;
        NSString *text = nil;

        theScanner = [NSScanner scannerWithString:descrizione];

        while (![theScanner isAtEnd]) {

            // find start of tag
            [theScanner scanUpToString:@"<" intoString:NULL] ; 

            // find end of tag
            [theScanner scanUpToString:@">" intoString:&text] ;

            // replace the found tag with a space
            //(you can filter multi-spaces out later if you wish)
            descrizione = [descrizione stringByReplacingOccurrencesOfString:[ NSString stringWithFormat:@"%@>", text] withString:@""];

        } 



        descrizione = [[descrizione componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]] componentsJoinedByString:@""];



        NSCharacterSet *whitespaces = [NSCharacterSet whitespaceCharacterSet];
        NSPredicate *noEmptyStrings = [NSPredicate predicateWithFormat:@"SELF != ''"];

        NSArray *parts = [descrizione componentsSeparatedByCharactersInSet:whitespaces];
        NSArray *filteredArray = [parts filteredArrayUsingPredicate:noEmptyStrings];
        descrizione = [filteredArray componentsJoinedByString:@" "];

        //FORMATTO LA STRINGA
        self.currentItem.description=descrizione;

    }else if([elementName isEqualToString:@"link"]){
        self.currentItem.linkUrl = self.currentItemValue;
    }else if([elementName isEqualToString:@"guid"]){
        self.currentItem.guidUrl = self.currentItemValue;
    }else if([elementName isEqualToString:@"pubDate"]){
        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        [formatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss'Z'"];
        self.currentItem.pubDate = [formatter dateFromString:self.currentItemValue];
        [formatter release];
    }else if([elementName isEqualToString:@"item"]){
        [[self rssItems] addObject:self.currentItem];
    }
}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
    if(nil != self.currentItemValue){
        [self.currentItemValue appendString:string];
    }
}


- (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock
{
    NSString *someString = [[NSString alloc] initWithData:CDATABlock encoding:NSUTF8StringEncoding];
    if(nil != self.currentItemValue){
        [self.currentItemValue appendString:someString];
    }
}

- (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError{
    if(parseError.code != NSXMLParserDelegateAbortedParseError) {
        [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
        [(id)[self delegate] performSelectorOnMainThread:@selector(processHasErrors)
         withObject:nil
         waitUntilDone:NO];
    }
}



- (void)parserDidEndDocument:(NSXMLParser *)parser {
    [(id)[self delegate] performSelectorOnMainThread:@selector(processCompleted)
     withObject:nil
     waitUntilDone:NO];
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
}


-(void)dealloc{
    self.currentItem = nil;
    self.currentItemValue = nil;
    self.delegate = nil;

    [_rssItems release];
    [super dealloc];
}

@end

I CALL IT with a button in the detailview

- (IBAction)impostaword:(id)sender{
        NSURL *url = [NSURL URLWithString:@"http://example"];
    [[self rssParser]startProcess:url];
}

the erros is: Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '* -[NSCFString addOperation:]: unrecognized selector sent to instance