OSDN Git Service

Merge commit 'cb03a1ed529387c1084e04e6f141eb9d35d095b8'
[jindolf/Jindolf.git] / src / main / java / jp / sfjp / jindolf / data / html / VillageInfoLoader.java
1 /*
2  * village information loader
3  *
4  * License : The MIT License
5  * Copyright(c) 2008 olyutorskii
6  */
7
8 package jp.sfjp.jindolf.data.html;
9
10 import java.io.IOException;
11 import java.util.logging.Level;
12 import java.util.logging.Logger;
13 import jp.osdn.jindolf.parser.HtmlParseException;
14 import jp.osdn.jindolf.parser.HtmlParser;
15 import jp.osdn.jindolf.parser.content.DecodedContent;
16 import jp.sfjp.jindolf.data.Land;
17 import jp.sfjp.jindolf.data.Village;
18 import jp.sfjp.jindolf.net.HtmlSequence;
19 import jp.sfjp.jindolf.net.ServerAccess;
20 import jp.sourceforge.jindolf.corelib.LandDef;
21 import jp.sourceforge.jindolf.corelib.LandState;
22
23 /**
24  * 人狼各国のHTTPサーバから個別の村の村情報をHTMLで取得する。
25  *
26  * <p>村情報には村毎の更新時刻、日程、進行状況などが含まれる。
27  *
28  * <p>各Periodの会話はまだロードされない。
29  */
30 public final class VillageInfoLoader {
31
32     private static final Logger LOGGER = Logger.getAnonymousLogger();
33
34
35     /**
36      * Hidden constructor.
37      */
38     private VillageInfoLoader() {
39         assert false;
40     }
41
42
43     /**
44      * 人狼BBSサーバから
45      * HTMLで記述された各村の村情報ページをダウンロードする。
46      *
47      * <p>村情報ページのURLは各国の状態及び村の進行状況により異なる。
48      *
49      * <p>※ G国HISTORICAL運用移行に伴い、
50      * 2020-02の時点で進行中の村はもはや存在しないため、
51      * 若干の冗長なコードが残存する。
52      *
53      * <p>例: G1000村(エピローグ終了状態)の村情報ページは
54      * <a href="http://www.wolfg.x0.com/index.rb?vid=1000">
55      * http://www.wolfg.x0.com/index.rb?vid=1000</a>
56      *
57      * @param village 村
58      * @return HTML文書
59      * @throws IOException 入出力エラー
60      */
61     private static DecodedContent loadVillageInfo(Village village)
62             throws IOException{
63         Land land = village.getParentLand();
64         ServerAccess server = land.getServerAccess();
65         LandDef landDef = land.getLandDef();
66         LandState landState = landDef.getLandState();
67
68         HtmlSequence html;
69         if(landState == LandState.ACTIVE){
70             html = server.getHTMLBoneHead(village);
71         }else{
72             html = server.getHTMLVillage(village);
73         }
74
75         DecodedContent content = html.getContent();
76
77         return content;
78     }
79
80     /**
81      * 人狼BBSサーバから各村のPeriod一覧情報が含まれたHTML(村情報)を取得し、
82      * 更新時刻や日程、空PeriodのリストをVillageインスタンスに設定する。
83      *
84      * @param village 村
85      * @throws java.io.IOException ネットワーク入出力の異常
86      */
87     public static void updateVillageInfo(Village village)
88             throws IOException{
89         DecodedContent content = loadVillageInfo(village);
90
91         HtmlParser parser = new HtmlParser();
92         VillageInfoHandler handler = new VillageInfoHandler();
93         parser.setBasicHandler   (handler);
94         parser.setSysEventHandler(handler);
95         parser.setTalkHandler    (handler);
96
97         handler.setVillage(village);
98         try{
99             parser.parseAutomatic(content);
100         }catch(HtmlParseException e){
101             LOGGER.log(Level.WARNING, "村の状態が不明", e);
102         }
103
104         parser.reset();
105         handler.reset();
106
107         return;
108     }
109
110 }