4 * License : The MIT License
5 * Copyright(c) 2008 olyutorskii
8 package jp.sfjp.jindolf.data;
10 import java.awt.image.BufferedImage;
11 import java.io.IOException;
12 import java.net.MalformedURLException;
14 import java.net.URISyntaxException;
16 import java.util.Collections;
17 import java.util.LinkedList;
18 import java.util.List;
19 import java.util.SortedSet;
20 import java.util.TreeSet;
21 import java.util.logging.Level;
22 import java.util.logging.Logger;
23 import jp.sfjp.jindolf.net.HtmlSequence;
24 import jp.sfjp.jindolf.net.ServerAccess;
25 import jp.sourceforge.jindolf.corelib.LandDef;
26 import jp.sourceforge.jindolf.corelib.LandState;
27 import jp.sourceforge.jindolf.corelib.VillageState;
28 import jp.sourceforge.jindolf.parser.DecodedContent;
29 import jp.sourceforge.jindolf.parser.HtmlAdapter;
30 import jp.sourceforge.jindolf.parser.HtmlParseException;
31 import jp.sourceforge.jindolf.parser.HtmlParser;
32 import jp.sourceforge.jindolf.parser.PageType;
33 import jp.sourceforge.jindolf.parser.SeqRange;
41 private static final String ID_VANILLAWOLF = "wolf";
43 private static final Logger LOGGER = Logger.getAnonymousLogger();
46 private final LandDef landDef;
47 private final ServerAccess serverAccess;
48 private final HtmlParser parser = new HtmlParser();
49 private final VillageListHandler handler = new VillageListHandler();
51 private final List<Village> villageList = new LinkedList<>();
57 * @throws java.lang.IllegalArgumentException 不正な国定義
59 public Land(LandDef landDef) throws IllegalArgumentException{
62 this.landDef = landDef;
66 url = this.landDef.getCgiURI().toURL();
67 }catch(MalformedURLException e){
68 throw new IllegalArgumentException(e);
70 this.serverAccess = new ServerAccess(url, this.landDef.getEncoding());
72 this.parser.setBasicHandler(this.handler);
80 * クエリーの書式例:「{@literal a=b&c=d&e=f}」この場合キーcの値はd
82 * @param allQuery クエリー
85 public static String getValueFromCGIQueries(String key,
89 String[] queries = allQuery.split("\\Q&\\E");
91 for(String pair : queries){
92 if(pair == null) continue;
93 String[] namevalue = pair.split("\\Q=\\E");
94 if(namevalue == null) continue;
95 if(namevalue.length != 2) continue;
96 String name = namevalue[0];
97 String value = namevalue[1];
98 if(name == null) continue;
99 if( name.equals(key) ){
101 if(result == null) continue;
102 if(result.length() <= 0) continue;
111 * AタグのHREF属性値からクエリー部を抽出する。
112 * 「{@literal &}」は「{@literal &}」に解釈される。
113 * @param hrefValue HREF属性値
116 public static String getRawQueryFromHREF(CharSequence hrefValue){
117 if(hrefValue == null) return null;
119 // HTML 4.01 B.2.2 rule
120 String pureHREF = hrefValue.toString().replace("&", "&");
124 uri = new URI(pureHREF);
125 }catch(URISyntaxException e){
133 String rawQuery = uri.getRawQuery();
139 * AタグのHREF属性値から村IDを得る。
140 * @param hrefValue HREF値
141 * @return village 村ID
143 public static String getVillageIDFromHREF(CharSequence hrefValue){
144 String rawQuery = getRawQueryFromHREF(hrefValue);
145 if(rawQuery == null) return null;
147 String villageID = getValueFromCGIQueries("vid", rawQuery);
148 if(villageID == null) return null;
149 if(villageID.length() <= 0) return null;
158 public LandDef getLandDef(){
164 * @return ServerAccessインスタンス
166 public ServerAccess getServerAccess(){
167 return this.serverAccess;
171 * 指定されたインデックス位置の村を返す。
172 * @param index 0から始まるインデックス値
175 public Village getVillage(int index){
176 if(index < 0) return null;
177 if(index >= getVillageCount()) return null;
179 Village result = this.villageList.get(index);
187 public int getVillageCount(){
188 int result = this.villageList.size();
196 // TODO インスタンス変数でいいはず。
197 public List<Village> getVillageList(){
198 return Collections.unmodifiableList(this.villageList);
202 * 絶対または相対URLの指すパーマネントなイメージ画像をダウンロードする。
203 * ※ A,B,D 国の顔アイコンは絶対パスらしい…。
204 * @param imageURL 画像URL文字列
207 public BufferedImage downloadImage(String imageURL){
208 ServerAccess server = getServerAccess();
211 image = server.downloadImage(imageURL);
212 }catch(IOException e){
213 LOGGER.log(Level.WARNING,
214 "イメージ[" + imageURL + "]"
226 public BufferedImage getGraveIconImage(){
227 URI uri = getLandDef().getTombFaceIconURI();
228 BufferedImage result = downloadImage(uri.toASCIIString());
234 * @return 墓アイコンイメージ(大)
236 public BufferedImage getGraveBodyImage(){
237 URI uri = getLandDef().getTombBodyIconURI();
238 BufferedImage result = downloadImage(uri.toASCIIString());
244 * リスト元情報は国のトップページと村一覧ページ。
245 * 古国の場合は村一覧にアクセスせずトップページのみ。
246 * 古国以外で村建てをやめた国はトップページにアクセスしない。
247 * 村リストはVillageの実装に従いソートされる。重複する村は排除。
250 * @throws java.io.IOException ネットワーク入出力の異常
252 public SortedSet<Village> downloadVillageList() throws IOException {
253 LandDef thisLand = getLandDef();
254 LandState state = thisLand.getLandState();
255 boolean isVanillaWolf = thisLand.getLandId().equals(ID_VANILLAWOLF);
257 ServerAccess server = getServerAccess();
259 // たまに同じ村が複数回出現するので注意!
260 SortedSet<Village> result = new TreeSet<>();
263 if(state.equals(LandState.ACTIVE) || isVanillaWolf){
264 HtmlSequence html = server.getHTMLTopPage();
265 DecodedContent content = html.getContent();
267 this.parser.parseAutomatic(content);
268 }catch(HtmlParseException e){
269 LOGGER.log(Level.WARNING, "トップページを認識できない", e);
271 List<Village> list = this.handler.getVillageList();
278 if( ! isVanillaWolf ){
279 HtmlSequence html = server.getHTMLLandList();
280 DecodedContent content = html.getContent();
282 this.parser.parseAutomatic(content);
283 }catch(HtmlParseException e){
284 LOGGER.log(Level.WARNING, "村一覧ページを認識できない", e);
286 List<Village> list = this.handler.getVillageList();
293 this.handler.reset();
300 * @param vset ソート済みの村一覧
302 public void updateVillageList(SortedSet<Village> vset){
303 // TODO 村リスト更新のイベントリスナがあると便利か?
304 this.villageList.clear();
305 this.villageList.addAll(vset);
314 public String toString(){
315 return getLandDef().getLandName();
321 private class VillageListHandler extends HtmlAdapter{
323 private List<Village> villageList = null;
328 public VillageListHandler(){
335 * 再度パースを行うまで呼んではいけない。
337 * @throws IllegalStateException パース前に呼び出された。
338 * あるいはパース後すでにリセットされている。
340 public List<Village> getVillageList() throws IllegalStateException{
341 if(this.villageList == null){
342 throw new IllegalStateException("パースが必要です。");
345 List<Village> result = this.villageList;
355 this.villageList = null;
362 * @param content {@inheritDoc}
363 * @throws HtmlParseException {@inheritDoc}
366 public void startParse(DecodedContent content)
367 throws HtmlParseException{
369 this.villageList = new LinkedList<>();
375 * 自動判定の結果がトップページでも村一覧ページでもなければ
377 * @param type {@inheritDoc}
378 * @throws HtmlParseException {@inheritDoc} 意図しないページが来た。
381 public void pageType(PageType type) throws HtmlParseException{
382 if( type != PageType.VILLAGELIST_PAGE
383 && type != PageType.TOP_PAGE ){
384 throw new HtmlParseException(
385 "トップページか村一覧ページが必要です。");
392 * @param content {@inheritDoc}
393 * @param anchorRange {@inheritDoc}
394 * @param villageRange {@inheritDoc}
395 * @param hour {@inheritDoc}
396 * @param minute {@inheritDoc}
397 * @param villageState {@inheritDoc}
398 * @throws HtmlParseException {@inheritDoc}
401 public void villageRecord(DecodedContent content,
402 SeqRange anchorRange,
403 SeqRange villageRange,
404 int hour, int minute,
405 VillageState villageState)
406 throws HtmlParseException{
407 LandDef landdef = getLandDef();
408 LandState landState = landdef.getLandState();
410 CharSequence href = anchorRange.sliceSequence(content);
411 String villageID = getVillageIDFromHREF(href);
412 if( villageID == null
413 || villageID.length() <= 0 ){
415 "認識できないURL[" + href + "]に遭遇しました。");
419 CharSequence fullVillageName =
420 villageRange.sliceSequence(content);
422 // TODO 既に出来ているかもしれないVillageを再度作るのは無駄?
423 Village village = new Village(Land.this,
425 fullVillageName.toString() );
427 if(landState == LandState.HISTORICAL){
428 village.setState(VillageState.GAMEOVER);
430 village.setState(villageState);
433 this.villageList.add(village);