2013年7月30日火曜日

Google Readerのstarred.jsonをとりあえず読み込むJava

GoogleReaderで残された「スター付きアイテム」を保存したJSONファイル starred.jsonをとりあえず読み込むJavaのソースです。

JacksonのObjectMapperを使用しています。

getter / setterを定義すれば組み立ててくれるので便利でした。

しかし、これだけ書いたのに、CoffeeScriptに持って行ったら10行かかりませんでした。


import java.io.*;
import java.lang.reflect.Type;
import java.net.URL;
import java.text.DateFormat;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import com.fasterxml.jackson.annotation.*;
import com.fasterxml.jackson.core.*;
import com.fasterxml.jackson.core.io.*;
import com.fasterxml.jackson.core.util.*;
import com.fasterxml.jackson.databind.cfg.BaseSettings;
import com.fasterxml.jackson.databind.cfg.HandlerInstantiator;
import com.fasterxml.jackson.databind.cfg.MapperConfig;
import com.fasterxml.jackson.databind.deser.*;
import com.fasterxml.jackson.databind.*;


class JacksonTest{
public static void main( String[] args) {
StarredByGoogleReader str;
try{
ObjectMapper mapper = new ObjectMapper();

//str = mapper.readValue(new File("ministarred.json"), StarredByGoogleReader.class);
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
str = mapper.readValue(new File("starred-old.json"), StarredByGoogleReader.class);

for (int i = 0; i< str.items.length;i++){
System.out.println(str.items[i].title);
}

}catch(Exception e){
e.printStackTrace();
}
}
}

class StarredByGoogleReader{
String id;
String title;
String author;
long updated;
String direction;
StarredItem[] items;

public String getId() { return id; }
public void setId(String s) { id = s; }
public String getTitle() { return title; }
public void setTitle(String s) { title = s; }
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public long getUpdated() {
return updated;
}
public void setUpdated(long updated) {
this.updated = updated;
}
public String getDirection() {
return direction;
}
public void setDirection(String direction) {
this.direction = direction;
}
public StarredItem[] getItems() {
return items;
}
public void setItems(StarredItem[] items) {
this.items = items;
}


}

class Canonical{
String href;

public String getHref() {
return href;
}

public void setHref(String href) {
this.href = href;
}

}
class Alternate{
String href;
String type;
public String getHref() {
return href;
}
public void setHref(String href) {
this.href = href;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}

}
class Content{
String direction;
String content;
public String getDirection() {
return direction;
}
public void setDirection(String direction) {
this.direction = direction;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}

}
class Origin{
String streamId;
String title;
String htmlUrl;
public String getStreamId() {
return streamId;
}
public void setStreamId(String streamId) {
this.streamId = streamId;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getHtmlUrl() {
return htmlUrl;
}
public void setHtmlUrl(String htmlUrl) {
this.htmlUrl = htmlUrl;
}

}
class StarredItem{
boolean isReadStateLocked;
long crawlTimeMsec;
long timestampUsec;
String id;
String[] categories;
String title;
long published;
long updated;

Canonical[] canonical;
Alternate[] alternate;
Content content;
Content summary;
String author;
String[] comments;
String[] annotations;
Origin origin;

public long getCrawlTimeMsec() {
return crawlTimeMsec;
}
public void setCrawlTimeMsec(long crawlTimeMsec) {
this.crawlTimeMsec = crawlTimeMsec;
}
public long getTimestampUsec() {
return timestampUsec;
}
public void setTimestampUsec(long timestampUsec) {
this.timestampUsec = timestampUsec;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String[] getCategories() {
return categories;
}
public void setCategories(String[] categories) {
this.categories = categories;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public long getPublished() {
return published;
}
public void setPublished(long published) {
this.published = published;
}
public long getUpdated() {
return updated;
}
public void setUpdated(long updated) {
this.updated = updated;
}


public Canonical[] getCanonical() {
return canonical;
}
public void setCanonical(Canonical[] canonical) {
this.canonical = canonical;
}
public Alternate[] getAlternate() {
return alternate;
}
public void setAlternate(Alternate[] alternate) {
this.alternate = alternate;
}
public Content getContent() {
return content;
}
public void setContent(Content content) {
this.content = content;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String[] getComments() {
return comments;
}
public void setComments(String[] comments) {
this.comments = comments;
}
public String[] getAnnotations() {
return annotations;
}
public void setAnnotations(String[] annotations) {
this.annotations = annotations;
}
public Origin getOrigin() {
return origin;
}
public void setOrigin(Origin origin) {
this.origin = origin;
}
public Content getSummary(){
return summary;
}
public void setSummary(Content summary){ this.summary = summary; }


}

jQueryイベントハンドラ

keydown(または他のキーボード押下イベント)で、どのキーが押されたかを知りたい

Eventオブジェクトの、whichを使う。

ちなむと、
A65
...
Z90

2013年7月29日月曜日

starred.jsonをとりあえず読み込むcoffeescript

先のplay framework上で、Google Readerのスターを読み込むためのcoffee scriptです。

Jacksonでごりごり書いているのとは違う大変さがありましたが終わってみると楽だなあという印象。

$ ->
 $.getJSON "/assets/starred.json", (data) ->
  $.each data.items, (index, item) ->
   $("#gr").append "<li><a href='" + item.alternate[0].href + "'>" + item.title + "</a></li>"

#システムの都合上、li, aタグは全角を使っています。

javascriptの呼び出し側に、id=gr属性を設定した、リスト要素が必要です。



JavaScriptへの変換はこちら。要jQueryです。


$.getJSON "/assets/starred.json", (data) ->

 →publicフォルダに配置したファイルはplay framework上では/assetsディレクトリとして見えています。

また、

    "alternate" : [ {
      "href" : "http://eruchee.blogspot.com",
      "type" : "text/html"
    } ],

これは配列を表しているので、

item.alternate[0].href の[0]をきちんと書かないとundefinedとなるというところで延々悩みました。JSONを知らない故のつまずきでした。

2013年7月21日日曜日

IntelliJ IDEA

Java あるいは Android向けのIDE

play frameworkのチュートリアル動画を見た時に、便利に見えたので、
これまでeclipseを使っていましたが、こちらに行ってみようかなあという感触もあります。


IntelliJ IDEA
Community版は無償(オープンソース)です。

日本語化

日本語化パッチ

導入方法

解凍して得られるresource_jp.jarファイルを IntelliJ IDEA のlibフォルダに格納します。

ただし、日本語のフォントを有効にしておかないと、日本語の部分が□□□□□となってしまうようです。
  1. [File]→[Settings]→左側のツリーから[Appearance]を選択します。
  2. 右側設定項目の UIOptions の中から、Override default fonts by (not recommended) に
    チェックを入れます。
  3. 日本語が利用できるフォントを選択します。

ビルドパスの追加


Eclipseからお引越しをしてきて、非常にわかりづらかった部分でした。設定の入り口が違うので、探すのが困難でした。

  1. 「ファイル」メニュー→「プロジェクト構造」 の順にクリックします。
  2. 左側の一覧から「SDK」を選択します。
  3. Classpath / Sourcepath など、必要なパスを編集します。





Play Framework

Play Framework

英語版

動画を見ると結構あっさりWebアプリケーションが作れるんだなあという印象。

サーバもDBも内包しているので構築も簡単。

$ を通常のプロンプト
[アプリケーション名] $ をplay frameworkのプロンプトとします。
 →例) [application] $

コンソール / 開発モードでのサーバの起動


コンソールの起動

$ cd application_path
$ play

開発モードでのサーバの起動

[application] $ run

または、

$ cd application_path
$ play run

運用モードでのサーバの起動

$ play start