4 * License : The MIT License
5 * Copyright(c) 2008 olyutorskii
8 package jp.sourceforge.jindolf.archiver;
10 import java.io.IOException;
11 import java.io.InputStream;
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;
28 * 人狼HTTPサーバ内のリソース情報を展開する。
30 public final class HttpAccess{
41 * 日一覧ページ(エピローグの翌日)のURLを得る。
45 * @throws IOException 入力エラー
47 public static URL getPeriodListURL(LandDef landDef, int vid)
49 StringBuilder urlText = new StringBuilder();
51 urlText.append(landDef.getCgiURI().toASCIIString());
52 urlText.append('?').append("vid=").append(vid);
53 if(landDef.getLandState() == LandState.ACTIVE){
54 urlText.append('&').append("meslog=");
57 URL result = new URL(urlText.toString());
67 * @throws DecodeException デコードエラー
68 * @throws HtmlParseException パースエラー
69 * @throws IOException 入力エラー
71 public static List<PeriodResource> loadResourceList(LandDef landDef,
73 throws DecodeException,
76 URL url = getPeriodListURL(landDef, vid);
78 Charset charset = landDef.getEncoding();
79 InputStream istream = url.openStream();
80 DecodedContent content = Builder.contentFromStream(charset, istream);
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);
90 List<PeriodResource> result = handler.getResourceList();
98 public static class PeriodListHandler extends HtmlAdapter{
100 private final LandDef landDef;
101 private final int vid;
103 private List<PeriodResource> resourceList = null;
105 private int progressDays;
106 private boolean hasDone;
113 public PeriodListHandler(LandDef landDef, int vid){
115 this.landDef = landDef;
126 public String getURL(PeriodType type, int day){
127 String base = this.landDef.getCgiURI().toASCIIString();
128 base += "?vid=" + this.vid;
130 if(this.landDef.getLandId().equals("wolfg")){
132 String dnum = "000" + (day - 1);
133 dnum = dnum.substring(dnum.length() - 3);
151 base += "&meslog=" + this.vid + "_";
157 base += "progress_" + (day - 1);
160 base += "party_" + (day - 1);
173 * PeriodResource一覧を得る。
174 * @return PeriodResource一覧
176 public List<PeriodResource> getResourceList(){
177 return this.resourceList;
182 * @param content {@inheritDoc}
183 * @throws HtmlParseException {@inheritDoc}
186 public void startParse(DecodedContent content)
187 throws HtmlParseException{
188 this.resourceList = new LinkedList<PeriodResource>();
189 this.progressDays = 0;
190 this.hasDone = false;
196 * @param type {@inheritDoc}
197 * @throws HtmlParseException {@inheritDoc}
200 public void pageType(PageType type) throws HtmlParseException{
201 if(type != PageType.PERIOD_PAGE) throw new HtmlParseException();
207 * @param content {@inheritDoc}
208 * @param anchorRange {@inheritDoc}
209 * @param periodType {@inheritDoc}
210 * @param day {@inheritDoc}
211 * @throws HtmlParseException {@inheritDoc}
214 public void periodLink(DecodedContent content,
215 SeqRange anchorRange,
216 PeriodType periodType,
218 throws HtmlParseException{
219 if(periodType == null){
221 }else if(periodType == PeriodType.PROGRESS){
222 this.progressDays = day;
229 * @throws HtmlParseException {@inheritDoc}
232 public void endParse() throws HtmlParseException{
233 if( ! this.hasDone ) throw new HtmlParseException();
235 PeriodResource resource;
237 String prologueURI = getURL(PeriodType.PROLOGUE, 0);
238 resource = new PeriodResource(this.landDef,
245 this.resourceList.add(resource);
247 for(int day = 1; day <= this.progressDays; day++){
248 String progressURI = getURL(PeriodType.PROGRESS, day);
249 resource = new PeriodResource(this.landDef,
256 this.resourceList.add(resource);
259 String epilogueURI = getURL(PeriodType.EPILOGUE,
260 this.progressDays + 1);
261 resource = new PeriodResource(this.landDef,
264 this.progressDays + 1,
268 this.resourceList.add(resource);