Sunday, May 08, 2011

Google Spreadsheets を Androidから扱う (5) DataModelの定義

Google-API-Java-Clientを用いて,Google Docsにアクセスするためには,GData-Java-Clientとは異なり,自分でデータモデルを定義する必要があります。

First, you need to invest in writing a custom data model for the Google API. Please read the JavaDoc for the new XML data model in google-api-java-client.

参照元 (What if I have a larde code base that uses gdata-java-client)

ここでは,次のfeedを表すデータモデルを具体的に定義してみましょう。feed は
QueryResponses
に掲載されているデータを利用しました。



  http://www.example.com/feed/1234.1/posts/full
  2005-09-16T00:42:06Z
  Books and Romance with Jo and Liz
  
  
  
  
  
    Elizabeth Bennet
    liz@gmail.com
  
  Example Generator Engine
  2
  0
  
    http://www.example.com/feed/1234.1/posts/full/4521614025009481151
    2005-01-09T08:00:00Z
    2005-01-09T08:00:00Z
    
    This is the title of entry 1009
    
      
This is the entry body of entry 1009
Elizabeth Bennet liz@gmail.com
http://www.example.com/feed/1234.1/posts/full/3067545004648931569 2005-01-07T08:00:00Z 2005-01-07T08:02:00Z This is the title of entry 1007
This is the entry body of entry 1007
Elizabeth Bennet liz@gmail.com

まず,root要素 feed について定義していきます。

public class GoogleDocFeed {
    // TBD
}

feed要素の属性にある名前空間に関係した属性,"xmlns:atom","xmlns:openSearch","xmlns:gd" は今回扱いません。
一方,属性 "gd:etag" は今後使うことも考慮して,データモデルを定義します。
import com.google.api.client.util.Key;

public class GoogleDocFeed {
    @Key("gd:etag")
    public string gd_etag;
    // TBD
}

つぎに,<id>http://www.example.com/feed/1234.1/posts/full</id> のように,属性を持たず,かつ内部に子要素を含まない要素の
データモデルを定義していきます。

    @Key  
    public string id;      // for http://www.example.com/feed/1234.1/posts/full

    @Key  
    public string updated; // for 2005-09-16T00:42:06Z

つぎに,link要素を定義していきます。feed要素にはlink要素を子要素として複数持つことができますので,


と定義します。class Link は以下のように定義します。


次に,author要素を定義します。
public class Author {
    /* for 
     *  
     *    Elizabeth Bennet
     *    liz@gmail.com
     *  
     */

    @Key
    public String name;

    @Key
    public String email;
}

その他に,属性と要素の内容にテキストコンテンツが与えられているtitle要素を定義します。

JavaDoc Package com.google.api.client.googleapis.xml.atom
に,
The optional value parameter of this @Key annotation specifies the XPath name to use to represent the field. For example, an XML attribute a has an XPath name of @a, an XML element <a> has an XPath name of a, and an XML text content has an XPath name of text().
とありますように,@Key("text()") と指定して,テキストコンテンツの内容を表します。

public class Title {
    /* for 
     *   This is the title of entry 1007
     */

    @Key("@type")
    public String type;

    @Key("text()")
    public String context;
}

他の要素も同様にして,上記で与えられている feed 要素を定義するデータモデル GoogleDocFeed が定義できます。


import com.google.api.client.util.Key;

public class Category {

    /*  
     *  
     */

    @Key("@scheme")
    public String scheme;

    @Key("@term")
    public String term;

}

public class Content {

    /*
     *  
     *    
This is the entry body of entry 1007
*
*/ @Key("@type") public String type; @Key("text()") public String content; } public class Link { /* * */ @Key("@rel") public String rel; @Key("@type") public String type; @Key("@href") public String href; } public class Author { /* for * * Elizabeth Bennet * liz@gmail.com * */ @Key public String name; @Key public String email; } public class Title { /* for * This is the title of entry 1007 */ @Key("@type") public String type; @Key("text()") public String context; } public class GoogleDocEntry { @Key public string id; @Key public string published; @Key public string updated; @Key public Category category; @Key public Title title; @Key public Content content; @Key("link") public List links; @Key public Author author; } public class GoogleDocFeed { @Key("gd:etag") public string gd_etag; @Key public string id; // for http://www.example.com/feed/1234.1/posts/full @Key public string updated; // for 2005-09-16T00:42:06Z @Key("link") public List links; @Key public Author author; @Key public Generator generator; @Key("openSearch:totalResults") public int totalResults; @Key("openSearch:startIndex") public int startIndex; @Key("entry") public List entries; }


今回は,具体的なコンテントをベースにしてデータモデルを定義しましたが,
schema は用意されていますので(例
Google Spreadsheets API Reference Guide (v3.0)
),それを参考にして各自でデータモデルを定義できます。


No comments: