2011-06-11
RSS作成
知識が不十分な俺にとって情報の古いRSS作成は少し手間取ってしまいました。
こちらのブログを参考にしています - 技術への名残り「JavaでRSSを作成(ROME利用)」
今回は自分のソースをそのまま書きます
FeedCreator.java
import java.util.ArrayList; import java.util.Date; import java.util.List; import com.sun.syndication.feed.synd.SyndContent; import com.sun.syndication.feed.synd.SyndContentImpl; import com.sun.syndication.feed.synd.SyndEntry; import com.sun.syndication.feed.synd.SyndEntryImpl; import com.sun.syndication.feed.synd.SyndFeed; import com.sun.syndication.feed.synd.SyndFeedImpl; import com.sun.syndication.io.FeedException; import com.sun.syndication.io.SyndFeedOutput; public class FeedCreator { private String feedType; private String title; private String link; private String description; private List<SyndEntry> entries; /** * デフォルトコンストラクタ。「RSS 2.0」形式でFeedを生成します。 */ public FeedCreator() { this("rss_2.0"); } /** * コンストラクタ * * @param feedType * フィードの形式 "rss_1.0" "rss_2.0" "rss_0.9" "rss_0.92" "atom_1.0" * "atom_0.3" */ public FeedCreator(String feedType) { this.feedType = feedType; entries = new ArrayList<SyndEntry>(); } /** * フィードの形式を指定します。 * * @param feedType */ public void setFeedType(String feedType) { this.feedType = feedType; } /** * フィードのタイトルを指定します。 * * @param title */ public void setFeedTitle(String title) { this.title = title; } /** * 配信元サイトのURLを指定します。 * * @param link */ public void setFeedLink(String link) { this.link = link; } /** * フィードの概要を指定します。 * * @param description */ public void setFeedDescription(String description) { this.description = description; } /** * テキスト形式の詳細エントリーを指定します。 * * @param title * @param link * @param value * @param pubDate */ public void setTextEntry(String title, String link, String value, Date pubDate) { SyndEntry entry; SyndContent description; entry = new SyndEntryImpl(); entry.setTitle(title); entry.setLink(link); entry.setPublishedDate(pubDate); description = new SyndContentImpl(); description.setType("text/plain"); description.setValue(value); entry.setDescription(description); entries.add(entry); } /** * 指定された形式でフィードを出力します。 このサンプルでは標準出力に文字列として出力しています。 * * @return */ public String createFeed() { SyndFeed feed = (SyndFeed) new SyndFeedImpl(); feed.setFeedType(feedType); feed.setTitle(title); feed.setLink(link); feed.setDescription(description); feed.setEntries(entries); String feedText = ""; SyndFeedOutput output = new SyndFeedOutput(); try { // サンプルとして文字列として生成。 // ファイルとして出力するならoutputメソッドを利用。 feedText = output.outputString(feed); } catch (FeedException e) { // TODO 自動生成された catch ブロック e.printStackTrace(); } return feedText; } }
RssController.java
import java.io.PrintWriter; import java.util.List; import org.slim3.controller.Controller; import org.slim3.controller.Navigation; import unity.model.api.Game; import unity.model.feed.FeedCreator; import unity.service.ApiService; public class RssController extends Controller { private ApiService as = new ApiService(); @Override public Navigation run() throws Exception { List<Game> findAll = as.findAll(); FeedCreator feed = new FeedCreator("rss_2.0"); feed.setFeedTitle("UnityGames"); feed.setFeedLink("http://unity-games.appspot.com/"); feed.setFeedDescription("Unityゲーム投稿サイト"); for (Game g : findAll) { feed.setTextEntry( g.getGameName(), "http://unity-games.appspot.com/unitygames/game/" + g.getGameId(), g.getExplanation(), g.getEntry()); } String createFeed = feed.createFeed(); response.setContentType("text/xml;charset=UTF-8"); PrintWriter out = response.getWriter(); out.println(createFeed); out.close(); return null; } }
createFeedを少しいじっただけです。他は特に変更していません。
デフォルト設定
よく文字コードはUTF-8、OnGUIなどを始めに手動で変更したり書き加えたりするのですが
面倒くさくなってきたのでテンプレートファイルをいじっちゃいます。
Windowsの場合(手動でパスを変えた場合は各自修正を)
C:\Program Files\Unity\Editor\Data\Resources
Macの場合
/Application/unity/unity.app (右クリックしてパッケージの内容を表示を選択)
Contents/Resources/
にある
NewBehaviourScript.cs
NewBehaviourScript.js
の中身を変更します
今回はそれぞれ
・OnGUI() 付加
しました