OSDN Git Service

改行コード指定
[jindolf/JinArchiver.git] / src / main / java / jp / sourceforge / jindolf / archiver / HttpAccess.java
1 /*
2  * downloader
3  *
4  * License : The MIT License
5  * Copyright(c) 2008 olyutorskii
6  */
7
8 package jp.sourceforge.jindolf.archiver;
9
10 import java.io.IOException;
11 import java.io.InputStream;
12 import java.net.URL;
13 import java.nio.charset.Charset;
14 import java.util.LinkedList;
15 import java.util.List;
16 import jp.sourceforge.jindolf.corelib.LandDef;
17 import jp.sourceforge.jindolf.corelib.LandState;
18 import jp.sourceforge.jindolf.corelib.PeriodType;
19 import jp.sourceforge.jindolf.parser.DecodeException;
20 import jp.sourceforge.jindolf.parser.DecodedContent;
21 import jp.sourceforge.jindolf.parser.HtmlAdapter;
22 import jp.sourceforge.jindolf.parser.HtmlParseException;
23 import jp.sourceforge.jindolf.parser.HtmlParser;
24 import jp.sourceforge.jindolf.parser.PageType;
25 import jp.sourceforge.jindolf.parser.SeqRange;
26
27 /**
28  * 人狼HTTPサーバ内のリソース情報を展開する。
29  */
30 public final class HttpAccess{
31
32     /**
33      * 隠しコンストラクタ。
34      */
35     private HttpAccess(){
36         throw new Error();
37     }
38
39
40     /**
41      * 日一覧ページ(エピローグの翌日)のURLを得る。
42      * @param landDef 国指定
43      * @param vid 村番号
44      * @return 一覧ページへのURL
45      * @throws IOException 入力エラー
46      */
47     public static URL getPeriodListURL(LandDef landDef, int vid)
48             throws IOException{
49         StringBuilder urlText = new StringBuilder();
50
51         urlText.append(landDef.getCgiURI().toASCIIString());
52         urlText.append('?').append("vid=").append(vid);
53         if(landDef.getLandState() == LandState.ACTIVE){
54             urlText.append('&').append("meslog=");
55         }
56
57         URL result = new URL(urlText.toString());
58
59         return result;
60     }
61
62     /**
63      * 日ページのロード元情報一覧を得る。
64      * @param landDef 国指定
65      * @param vid 村番号
66      * @return ロード元情報一覧
67      * @throws DecodeException デコードエラー
68      * @throws HtmlParseException パースエラー
69      * @throws IOException 入力エラー
70      */
71     public static List<PeriodResource> loadResourceList(LandDef landDef,
72                                                           int vid)
73             throws DecodeException,
74                    HtmlParseException,
75                    IOException {
76         URL url = getPeriodListURL(landDef, vid);
77
78         Charset charset = landDef.getEncoding();
79         InputStream istream = url.openStream();
80         DecodedContent content = Builder.contentFromStream(charset, istream);
81         istream.close();
82
83         HtmlParser parser = new HtmlParser();
84         PeriodListHandler handler = new PeriodListHandler(landDef, vid);
85         parser.setBasicHandler(handler);
86         parser.setTalkHandler(handler);
87         parser.setSysEventHandler(handler);
88         parser.parseAutomatic(content);
89
90         List<PeriodResource> result = handler.getResourceList();
91
92         return result;
93     }
94
95     /**
96      * 日一覧パース用ハンドラ。
97      */
98     public static class PeriodListHandler extends HtmlAdapter{
99
100         private final LandDef landDef;
101         private final int vid;
102
103         private List<PeriodResource> resourceList = null;
104
105         private int progressDays;
106         private boolean hasDone;
107
108         /**
109          * コンストラクタ。
110          * @param landDef 国指定
111          * @param vid 村番号
112          */
113         public PeriodListHandler(LandDef landDef, int vid){
114             super();
115             this.landDef = landDef;
116             this.vid = vid;
117             return;
118         }
119
120         /**
121          * 日ページのURL文字列を生成する。
122          * @param type 日種類
123          * @param day 日にち
124          * @return URL文字列
125          */
126         public String getURL(PeriodType type, int day){
127             String base = this.landDef.getCgiURI().toASCIIString();
128             base += "?vid=" + this.vid;
129
130             if(this.landDef.getLandId().equals("wolfg")){
131                 base += "&meslog=";
132                 String dnum = "000" + (day - 1);
133                 dnum = dnum.substring(dnum.length() - 3);
134                 switch(type){
135                 case PROLOGUE:
136                     base += "000_ready";
137                     break;
138                 case PROGRESS:
139                     base += dnum;
140                     base += "_progress";
141                     break;
142                 case EPILOGUE:
143                     base += dnum;
144                     base += "_party";
145                     break;
146                 default:
147                     assert false;
148                     return null;
149                 }
150             }else{
151                 base += "&meslog=" + this.vid + "_";
152                 switch(type){
153                 case PROLOGUE:
154                     base += "ready_0";
155                     break;
156                 case PROGRESS:
157                     base += "progress_" + (day - 1);
158                     break;
159                 case EPILOGUE:
160                     base += "party_" + (day - 1);
161                     break;
162                 default:
163                     return null;
164                 }
165             }
166
167             base += "&mes=all";
168
169             return base;
170         }
171
172         /**
173          * PeriodResource一覧を得る。
174          * @return PeriodResource一覧
175          */
176         public List<PeriodResource> getResourceList(){
177             return this.resourceList;
178         }
179
180         /**
181          * {@inheritDoc}
182          * @param content {@inheritDoc}
183          * @throws HtmlParseException {@inheritDoc}
184          */
185         @Override
186         public void startParse(DecodedContent content)
187                 throws HtmlParseException{
188             this.resourceList = new LinkedList<PeriodResource>();
189             this.progressDays = 0;
190             this.hasDone = false;
191             return;
192         }
193
194         /**
195          * {@inheritDoc}
196          * @param type {@inheritDoc}
197          * @throws HtmlParseException {@inheritDoc}
198          */
199         @Override
200         public void pageType(PageType type) throws HtmlParseException{
201             if(type != PageType.PERIOD_PAGE) throw new HtmlParseException();
202             return;
203         }
204
205         /**
206          * {@inheritDoc}
207          * @param content {@inheritDoc}
208          * @param anchorRange {@inheritDoc}
209          * @param periodType {@inheritDoc}
210          * @param day {@inheritDoc}
211          * @throws HtmlParseException {@inheritDoc}
212          */
213         @Override
214         public void periodLink(DecodedContent content,
215                                 SeqRange anchorRange,
216                                 PeriodType periodType,
217                                 int day )
218                 throws HtmlParseException{
219             if(periodType == null){
220                 this.hasDone = true;
221             }else if(periodType == PeriodType.PROGRESS){
222                 this.progressDays = day;
223             }
224             return;
225         }
226
227         /**
228          * {@inheritDoc}
229          * @throws HtmlParseException {@inheritDoc}
230          */
231         @Override
232         public void endParse() throws HtmlParseException{
233             if( ! this.hasDone ) throw new HtmlParseException();
234
235             PeriodResource resource;
236
237             String prologueURI = getURL(PeriodType.PROLOGUE, 0);
238             resource = new PeriodResource(this.landDef,
239                                           this.vid,
240                                           PeriodType.PROLOGUE,
241                                           0,
242                                           prologueURI,
243                                           0L,
244                                           null);
245             this.resourceList.add(resource);
246
247             for(int day = 1; day <= this.progressDays; day++){
248                 String progressURI = getURL(PeriodType.PROGRESS, day);
249                 resource = new PeriodResource(this.landDef,
250                                               this.vid,
251                                               PeriodType.PROGRESS,
252                                               day,
253                                               progressURI,
254                                               0L,
255                                               null);
256                 this.resourceList.add(resource);
257             }
258
259             String epilogueURI = getURL(PeriodType.EPILOGUE,
260                                         this.progressDays + 1);
261             resource = new PeriodResource(this.landDef,
262                                           this.vid,
263                                           PeriodType.EPILOGUE,
264                                           this.progressDays + 1,
265                                           epilogueURI,
266                                           0L,
267                                           null);
268             this.resourceList.add(resource);
269
270             return;
271         }
272
273     }
274
275 }