2010年10月5日火曜日

Objective-C + libxml2 を使ってフィードを取り込みたいんだ (4)

Xcode 3.2.4 and iOS SDK 4.1 で試してる。
古いバージョンとか新しいバージョンのこと考えてない。

さて、続きを書こう。少し間があいたので前回は何を書いてたのか忘れた。
軽く見なおしたら「C言語の関数からMethod呼び出せるって不思議」ってことみたいなのでその続きになるっぽい。

今回はそうすると、実際にパースするところを書いていけばいいか。

流れとしてはこんな感じ。

  1. [Objective-C] connection:didReceiveData: でデータを受信(分割して取得される)
  2. [C] startElementSAX でタグの始まりを判断
  3. [C] charactersFoundSAX でタグ間の文字データを取得(タグの終わりまで繰り返される)
  4. [C] endElementSAX でタグの終わりを判断
  5. [C] 2.に戻る
  6. [Objective-C] 1.に戻る
  7. [Objective-C] connectionDidFinishLoading: でデータ受信の完了


「結局どこで文字列を取得できてるんだよ!」ってことが知りたいわけだが、それは4.のタグの終わりを判断するところ。
ここでバッファに貯めたタグ間の文字列を取得するためにゴニョゴニョしてる。

  1. //  RSSImporter.h  
  2.   
  3. #import <Foundation/Foundation.h>  
  4. #import <libxml/tree.h>  
  5.   
  6. @interface RSSImporter : NSObject {  
  7. @private  
  8.  xmlParserCtxtPtr context;  
  9.  // ここ追加  
  10.  BOOL isItem; // 記事情報か  
  11.  BOOL isBuffering; // バッファー中か  
  12.  NSMutableData *characterBuffer; // バッファする箱  
  13. }  
  14.   
  15. // ここ追加  
  16. @property BOOL isItem;  
  17. @property BOOL isBuffering;  
  18.   
  19. - (void)fromC;  
  20. - (void)fromC:(const char *)localname;  
  21.   
  22. // ここ追加  
  23. - (void)appendCharacters:(const char *)characters length:(NSInteger)length;  
  24. - (void)finishAppendCharacters:(NSString *)element;  
  25.   
  26. @end  


  1. // RSSImporter.m の該当箇所を編集すること   
  2.   
  3. //  
  4. // 要素開始の通知を受けたときの処理  
  5. //  
  6. static void startElementSAX(  
  7.        void *context,  
  8.        const xmlChar *localname,  
  9.        const xmlChar *prefix,  
  10.        const xmlChar *URI,  
  11.        int nb_namespaces,  
  12.        const xmlChar **namespaces,  
  13.        int nb_attributes, int nb_defaulted,  
  14.        const xmlChar **atrributes) {  
  15.  // void *context は Objective-C との橋渡し。  
  16.  // xmlCreatePushParserCtxt()の第2引数でselfとして渡されたのが、contextとして渡ってきた。  
  17.  // 型を合わせることで、Objective-Cのオブジェクトとして扱えるようにしている。  
  18.  RSSImporter *paserImporter = (RSSImporter *)context;  
  19.   
  20.  // ここ追加  
  21.  // タグの開始(要素の開始)を判断する。例えば<title>xxx</title>なら<title>を判断してくれる。  
  22.  // バッファし始めることをここで宣言(paserImporter.isBuffering=YES)する。  
  23.  if (paserImporter.isItem == YES) {  
  24.   if (strncmp((const char*)localname, "title"sizeof("title")) == 0) {  
  25.    paserImporter.isBuffering = YES;  
  26.   }  
  27.   else if (strncmp((const char*)localname, "link"sizeof("link")) == 0) {  
  28.    paserImporter.isBuffering = YES;  
  29.   }  
  30.   else if (strncmp((const char*)localname, "description"sizeof("description")) == 0) {  
  31.    //paserImporter.isBuffering = YES; // 今回はコメントにして記事本文は読み込まないようにしとく  
  32.   }  
  33.   else if (strncmp((const char*)localname, "pubDate"sizeof("pubDate")) == 0) {  
  34.    paserImporter.isBuffering = YES;  
  35.   }  
  36.     
  37.  } else {  
  38.   if (strncmp((const char*)localname, "item"sizeof("item")) == 0) {  
  39.    paserImporter.isItem = YES;  
  40.   }  
  41.   else {  
  42.    paserImporter.isItem = NO;  
  43.   }  
  44.  }  
  45. }  
  46.   
  47. //  
  48. // 要素終了の通知を受けたときの処理  
  49. //  
  50. static void endElementSAX(  
  51.         void *context,  
  52.         const xmlChar *localname,  
  53.         const xmlChar *prefix,  
  54.         const xmlChar *URI) {  
  55.  RSSImporter *paserImporter = (RSSImporter *)context;  
  56.    
  57.  // ここ追加  
  58.  // タグの終了(要素の終了)を判断する。例えば<title>xxx</title>なら</title>を判断してくれる。  
  59.  // バッファを終了する。  
  60.  // 例えば<title>あいうえお</title>なら、Objective-C側で溜めた文字列「あいうえお」  
  61.  // を確定させて次のバッファを溜められるようにしている。  
  62.  if (paserImporter.isBuffering == YES) {  
  63.   if (strncmp((const char*)localname, "title"sizeof("title")) == 0) {  
  64.    [paserImporter finishAppendCharacters:@"title"];  
  65.   }  
  66.   else if (strncmp((const char*)localname, "link"sizeof("link")) == 0) {  
  67.    [paserImporter finishAppendCharacters:@"link"];  
  68.   }  
  69.   else if (strncmp((const char*)localname, "description"sizeof("description")) == 0) {  
  70.    [paserImporter finishAppendCharacters:@"description"];  
  71.   }  
  72.   else if (strncmp((const char*)localname, "pubDate"sizeof("pubDate")) == 0) {  
  73.    [paserImporter finishAppendCharacters:@"pubDate"];  
  74.   }  
  75.  }  
  76.  paserImporter.isBuffering = NO;  
  77. }  
  78.   
  79. //  
  80. // 要素間のテキストの通知を受けたときの処理  
  81. //  
  82. static void charactersFoundSAX(  
  83.           void *context,  
  84.           const xmlChar *characters,  
  85.           int length) {  
  86.  RSSImporter *paserImporter = (RSSImporter *)context;  
  87.   
  88.  // ここ追加  
  89.  // 通知で受け取る文字列は、一回だけじゃなくて連続してくる。(短ければ1回で受け取れる)  
  90.  // それらをその都度Objective-C側にバッファを溜めていく。  
  91.  // 例えば<title>あいうえお</title>なら、startElementSAXからendElementSAXの間  
  92.  // で断続して文字列「あいうえお」を取得することになる。  
  93.  if (paserImporter.isBuffering == YES) {  
  94.   [paserImporter appendCharacters:(const char *)characters length:length];  
  95.  }  
  96. }  


  1. // RSSImporter.m の該当箇所を編集すること   
  2.   
  3. //  
  4. // クラスエクステンションを利用することで、  
  5. // プロパティとメソッドへのアクセスをプライベートからに制限できる  
  6. //  
  7. @interface RSSImporter ()  
  8. // これ追加  
  9. @property (nonatomic, retain) NSMutableData *characterBuffer;  
  10. @end  



  1. // RSSImporter.m の該当箇所を編集すること   
  2.   
  3. @implementation RSSImporter  
  4.   
  5. // ここ追加  
  6. @synthesize isItem; // 記事情報か  
  7. @synthesize isBuffering; // バッファー中か  
  8. @synthesize characterBuffer; // バッファする箱  
  9.   
  10. /** 
  11.  */  
  12. - (id)init {  
  13.    
  14.  self = [super init];  
  15.  if (self != nil) {  
  16.   
  17.   // ここ追加  
  18.   self.isBuffering = NO;  
  19.   self.characterBuffer = [NSMutableData data];  
  20.     
  21.   // パーサ用のコンテクストを作る  
  22.   if (!context) {  
  23.    context = xmlCreatePushParserCtxt(&simpleSAXHandlerStruct, self, NULL, 0, NULL);  
  24.   }  
  25.     
  26.   NSString *feedURL = @"http://eyesrobe.blogspot.com/feeds/posts/default?alt=rss";  
  27.   NSURLRequest *requestURL = [NSURLRequest requestWithURL:[NSURL URLWithString:feedURL]];  
  28.   NSURLConnection *urlConnection = [[[NSURLConnection alloc] initWithRequest:requestURL delegate:self] autorelease];  
  29.   if (urlConnection == nil) {  
  30.    NSLog(@"error");  
  31.   }  
  32.  }  
  33.    
  34.  return self;  
  35. }  
  36.   
  37. /** 
  38.  ここ追加 
  39.  パースされて送られてきた文字を、バッファに溜める。 
  40.  NSMutableDataなcharacterBufferに送られてきた文字をがんがん入れていく。 
  41.  Cの関数から呼ばれる不思議 
  42.  */  
  43. - (void)appendCharacters:(const char *)characters length:(NSInteger)length {  
  44.  [characterBuffer appendBytes:characters length:length];  
  45. }  
  46.   
  47. /** 
  48.  ここ追加 
  49.  溜めたバッファを NSString に変換する 
  50.  Cの関数から呼ばれる不思議 
  51.  */  
  52. - (void)finishAppendCharacters:(NSString *)element {  
  53.   
  54.  NSString *currentString = [[[NSString alloc] initWithData:self.characterBuffer encoding:NSUTF8StringEncoding] autorelease];  
  55.  NSLog(@"#%@ : %@", element, currentString);  
  56.   [characterBuffer setLength:0];  
  57. }  
  58.   
  59. /** 
  60.  メモリ解放 
  61.  */  
  62. - (void)dealloc {  
  63.  [characterBuffer release]; // ここ追加  
  64.  [super dealloc];  
  65. }  


で実行すると、こんな感じのログが出ると。

2010-10-05 14:54:57.747 Foo[1285:207] 受信中(データは分割されて受信される)
2010-10-05 14:54:57.749 Foo[1285:207] #pubDate : Thu, 30 Sep 2010 09:27:00 +0000
2010-10-05 14:54:57.750 Foo[1285:207] #title : Objective-C + libxml2 を使ってフィードを取り込みたいんだ (3)
2010-10-05 14:54:57.752 Foo[1285:207] #link : http://eyesrobe.blogspot.com/2010/09/objective-c-libxml2-3.html
2010-10-05 14:54:57.753 Foo[1285:207] #pubDate : Thu, 30 Sep 2010 07:27:00 +0000
2010-10-05 14:54:57.754 Foo[1285:207] #title : Objective-C + libxml2 を使ってフィードを取り込みたいんだ (2)
2010-10-05 14:54:57.781 Foo[1285:207] 受信中(データは分割されて受信される)
2010-10-05 14:54:57.782 Foo[1285:207] #link : http://eyesrobe.blogspot.com/2010/09/objective-c-libxml2-2.html
2010-10-05 14:54:57.783 Foo[1285:207] #pubDate : Thu, 30 Sep 2010 02:58:00 +0000
2010-10-05 14:54:57.784 Foo[1285:207] #title : Objective-C + libxml2 を使ってフィードを取り込みたいんだ (1)
2010-10-05 14:54:57.786 Foo[1285:207] 受信中(データは分割されて受信される)
2010-10-05 14:54:57.787 Foo[1285:207] #link : http://eyesrobe.blogspot.com/2010/09/objective-c-libxml2-1.html
2010-10-05 14:54:57.846 Foo[1285:207] 受信完了

0 件のコメント:

コメントを投稿