#import "Controller.h"
@implementation Controller
- (IBAction)getContents:(id)sender
{
// 指定したURL
NSURL *url = [NSURL URLWithString: [selUrl stringValue]];
BOOL flag;
int i,j;
// メモリを確保してインスタンスを生成し、初期化する
NSMutableAttributedString *astr = [[NSMutableAttributedString alloc] init];
// 取り出す項目と、それぞれの文字列の色の設定
NSArray *key = [NSArray arrayWithObjects:@"title",@"description",@"link",nil];
NSArray *color = [NSArray arrayWithObjects:[NSColor magentaColor],[NSColor grayColor],[NSColor purpleColor],nil];
NSAttributedString *strCR = [[[NSAttributedString alloc] initWithString:@"\n"] autorelease];
// メイン画面を初期化(表示をクリア)
[[display textStorage]setAttributedString:astr];
// 指定したURLからデータを読み込んで、XMLパーサオブジェクトを返す。
NSXMLParser *parser = [[NSXMLParser alloc] initWithContentsOfURL:url];
// デリゲートをセット
[parser setDelegate:self];
// XMLパーサオブジェクトを解析
flag = [parser parse];
// 読み込みに失敗すればエラーメッセージ出力
if ( flag == NO ) {
[display setEditable:YES];
[display insertText:@"Cannot read this RSS in ver0.56"];
[display setEditable:NO];
} else {
for ( i = 0; i < [ary count]; i++ ) {
NSDictionary *tmpdict = [ary objectAtIndex:i];
for ( j = 0; j < [key count]; j++ ) {
NSString *strKey = [key objectAtIndex:j]; // キー
NSColor *color1 = [color objectAtIndex:j]; // 色
NSDictionary *dic = [NSDictionary dictionaryWithObject:color1 forKey:NSForegroundColorAttributeName];
NSString *contents = [tmpdict objectForKey:strKey];
NSAttributedString *temp;
if ( [strKey isEqualToString:@"link"] ) {
// クリックしてブラウザが開くようにしている
NSDictionary *attrs = [NSDictionary dictionaryWithObject:contents forKey:NSLinkAttributeName];
temp = [[NSAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@\n",contents] attributes:attrs];
} else {
temp = [[NSAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@\n",contents] attributes:dic];
}
[astr appendAttributedString:temp];
[temp release];
}
[astr appendAttributedString:strCR];
}
// メイン画面に出力
[display setEditable:YES];
[display insertText:astr];
}
// メモリの解放
[astr release];
[parser release];
// カーソルを先頭に持ってくる
[display setSelectedRange:NSMakeRange(0,0)];
[display insertText:@""];
[display setEditable:NO];
}
// ---------------------------
// パース実行
//----------------------------
// Parse開始
- (void)parserDidStartDocument:(NSXMLParser *)parser
{
//NSLog(@"start");
[spin startAnimation:self];
// 初期化
dict = [NSMutableDictionary dictionary];
mustr = [NSMutableString string];
ary = [NSMutableArray array];
itemflag = false;
}
// 1項目取り出し開始 (タグの開始)
-(void)parser:(NSXMLParser *)parser
didStartElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI
qualifiedName:(NSString *)qName
attributes:(NSDictionary *)attributeDict
{
// ヘッダ部分を抜くため