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.Writer;
12 import java.util.Collections;
13 import java.util.LinkedList;
14 import java.util.List;
15 import jp.sourceforge.jindolf.corelib.DisclosureType;
16 import jp.sourceforge.jindolf.corelib.LandDef;
17 import jp.sourceforge.jindolf.corelib.PeriodType;
18 import jp.sourceforge.jindolf.corelib.PreDefAvatar;
24 public class VillageData{
26 private final List<PeriodResource> resourceList;
28 private final LandDef landDef;
29 private final int villageId;
30 private final String baseUri;
32 private String fullName = "";
33 private int commitHour = -1;
34 private int commitMinute = -1;
35 private String graveIconUri;
37 private final List<AvatarData> avatarList = new LinkedList<>();
38 private int undefAvatarNo = 1;
40 private final List<PeriodData> periodList = new LinkedList<>();
45 * @param resourceList PeriodResource並び
47 public VillageData(List<PeriodResource> resourceList){
50 validatePeriodResource(resourceList);
52 this.resourceList = new LinkedList<>(resourceList);
54 PeriodResource resource1st = this.resourceList.get(0);
55 this.landDef = resource1st.getLandDef();
56 this.villageId = resource1st.getVillageId();
57 this.baseUri = getBaseUri(this.resourceList);
64 * PeriodResourceの組が正当かチェックする。
66 * <li>全て同じ国に属していなければならない
67 * <li>全て同じ村に属していなければならない
68 * <li>日付は0から始まる連続した数値でなければならない
69 * <li>プロローグで始まらなければならない
70 * <li>エピローグで終わらなければならない
71 * <li>進行日はプロローグとエピローグに挟まれていなければならない
73 * @param list PeriodResource並び
74 * @throws IllegalArgumentException 引数が正当でない
76 public static void validatePeriodResource(List<PeriodResource> list)
77 throws IllegalArgumentException{
78 LandDef landDef = null;
81 PeriodType periodType = null;
83 for(PeriodResource resource : list){
85 landDef = resource.getLandDef();
86 }else if(resource.getLandDef() != landDef){
87 throw new IllegalArgumentException();
91 villageId = resource.getVillageId();
92 }else if(resource.getVillageId() != villageId){
93 throw new IllegalArgumentException();
97 lastDay = resource.getDay();
98 if(lastDay != 0) throw new IllegalArgumentException();
100 if(resource.getDay() != lastDay + 1){
101 throw new IllegalArgumentException();
103 lastDay = resource.getDay();
106 if(periodType == null){
107 periodType = resource.getPeriodType();
108 if(periodType != PeriodType.PROLOGUE){
109 throw new IllegalArgumentException();
111 if(lastDay != 0) throw new IllegalArgumentException();
112 }else if(periodType == PeriodType.PROLOGUE){
113 periodType = resource.getPeriodType();
114 if(periodType != PeriodType.PROGRESS){
115 throw new IllegalArgumentException();
117 }else if(periodType == PeriodType.PROGRESS){
118 periodType = resource.getPeriodType();
119 }else if(periodType == PeriodType.EPILOGUE){
120 throw new IllegalArgumentException();
124 if(lastDay < 0) throw new IllegalArgumentException();
125 if(periodType != PeriodType.EPILOGUE){
126 throw new IllegalArgumentException();
133 * 全PeriodResourceから、共通するベースURIを抽出する。
134 * @param list PeriodResource並び
136 * @throws IllegalArgumentException ベースURIが一致していない
138 public static String getBaseUri(List<PeriodResource> list)
139 throws IllegalArgumentException{
140 String result = null;
142 for(PeriodResource resource : list){
143 String urlText = resource.getOrigUrlText();
144 urlText = urlText.replaceAll("[^/]*$", "");
148 if( ! result.equals(urlText) ){
149 throw new IllegalArgumentException();
161 public LandDef getLandDef(){
169 public int getVillageId(){
170 return this.villageId;
177 public String getBaseUri(){
185 public String getFullName(){
186 return this.fullName;
191 * @param fullName 村フルネーム
193 public void setFullName(String fullName){
194 this.fullName = fullName;
202 public int getCommitHour(){
203 return this.commitHour;
208 * @param commitHour 更新時
210 public void setCommitHour(int commitHour){
211 this.commitHour = commitHour;
219 public int getCommitMinute(){
220 return this.commitMinute;
225 * @param commitMinute 更新分
227 public void setCommitMinute(int commitMinute){
228 this.commitMinute = commitMinute;
234 * @return 墓アイコンURI文字列
236 public String getGraveIconUri(){
237 if(this.graveIconUri == null){
238 return this.landDef.getTombFaceIconURI().toASCIIString();
240 return this.graveIconUri;
245 * @param graveIconUri 墓アイコンURI文字列
247 public void setGraveIconUri(String graveIconUri){
248 this.graveIconUri = graveIconUri;
253 * 全Periodの開示状況から総合開示状況を算出する。
256 public DisclosureType getDisclosureType(){
257 DisclosureType result = DisclosureType.COMPLETE;
259 for(PeriodData period : this.periodList){
260 DisclosureType type = period.getDisclosureType();
263 return DisclosureType.HOT;
265 result = DisclosureType.UNCOMPLETE;
277 * @param period Periodモデル
279 public void addPeriodData(PeriodData period){
280 this.periodList.add(period);
285 * PeriodResourcenar並びを取得する。
286 * @return PeriodResource並び
288 public List<PeriodResource> getPeriodResourceList(){
289 return Collections.unmodifiableList(this.resourceList);
295 * Avatarのフルネーム、短縮名、識別子が設定される。
296 * @param avfullName Avatarのフルネーム
297 * @return 新規Avatarモデル
299 public AvatarData createAvatar(String avfullName){
300 AvatarData avatar = new AvatarData();
302 avatar.setFullName(avfullName);
304 String[] token = avfullName.split("\\s");
305 String shortName = token[token.length - 1];
306 avatar.setShortName(shortName);
308 String avatarId = "ukavatar" + this.undefAvatarNo;
309 this.undefAvatarNo++;
310 avatar.setAvatarId(avatarId);
316 * AvatarフルネームからAvatarを得る。
317 * まだこの村にいないAvatarならAvatar一覧に登録される。
318 * @param seq Avatarフルネーム
321 public AvatarData getAvatarData(CharSequence seq){
322 for(AvatarData avatar : this.avatarList){
323 String avfullName = avatar.getFullName();
324 if(avfullName.contentEquals(seq)){
329 PreDefAvatar predefAvatar =
330 AvatarData.getPreDefAvatar(seq);
331 if(predefAvatar != null){
332 AvatarData avatar = new AvatarData(predefAvatar);
333 this.avatarList.add(avatar);
337 AvatarData avatar = createAvatar(seq.toString());
338 this.avatarList.add(avatar);
344 * avatarList要素のXML出力。
346 * @throws IOException 出力エラー
348 public void dumpAvatarList(Writer writer) throws IOException{
349 writer.append("<avatarList>").append("\n\n");
351 for(AvatarData avatar : this.avatarList){
352 avatar.dumpXml(writer);
356 writer.append("</avatarList>").append('\n');
364 * @throws IOException 出力エラー
366 public void dumpPeriodList(Writer writer) throws IOException{
367 for(PeriodData period : this.periodList){
368 period.dumpXml(writer);
377 * @throws IOException 出力エラー
379 public void dumpXml(Writer writer) throws IOException{
380 writer.append("<village\n");
382 XmlUtils.indent(writer, 1);
383 XmlUtils.dumpNameSpaceDecl(writer);
386 XmlUtils.indent(writer, 1);
387 XmlUtils.dumpSiNameSpaceDecl(writer);
390 XmlUtils.indent(writer, 1);
391 XmlUtils.dumpSchemeLocation(writer);
394 XmlUtils.indent(writer, 1);
395 XmlUtils.attrOut(writer, "xml:lang", "ja-JP");
398 XmlUtils.indent(writer, 1);
399 XmlUtils.attrOut(writer, "xml:base", this.baseUri);
402 XmlUtils.indent(writer, 1);
403 XmlUtils.attrOut(writer, "fullName", this.fullName);
406 XmlUtils.attrOut(writer, "vid", Integer.toString(this.villageId));
409 XmlUtils.indent(writer, 1);
410 XmlUtils.timeAttrOut(writer,
412 this.commitHour, this.commitMinute);
415 XmlUtils.indent(writer, 1);
416 XmlUtils.attrOut(writer, "state", "gameover");
418 DisclosureType type = getDisclosureType();
419 if(type != DisclosureType.COMPLETE){
421 XmlUtils.attrOut(writer, "disclosure", type.getXmlName());
425 if(this.landDef.isValidVillageId(this.villageId)){
431 XmlUtils.attrOut(writer, "isValid", isValid);
434 XmlUtils.indent(writer, 1);
435 XmlUtils.attrOut(writer, "landName", this.landDef.getLandName());
438 XmlUtils.attrOut(writer, "formalName", this.landDef.getFormalName());
441 XmlUtils.indent(writer, 1);
442 XmlUtils.attrOut(writer, "landId", this.landDef.getLandId());
445 XmlUtils.attrOut(writer, "landPrefix", this.landDef.getLandPrefix());
448 XmlUtils.indent(writer, 1);
449 String locale = this.landDef.getLocale().toString();
450 locale = locale.replaceAll("_", "-");
451 XmlUtils.attrOut(writer, "locale", locale);
454 XmlUtils.attrOut(writer,
455 "origencoding", this.landDef.getEncoding().name());
458 XmlUtils.attrOut(writer,
459 "timezone", this.landDef.getTimeZone().getID());
462 XmlUtils.indent(writer, 1);
463 XmlUtils.attrOut(writer, "graveIconURI", getGraveIconUri());
466 XmlUtils.indent(writer, 1);
467 XmlUtils.attrOut(writer, "generator", JinArchiver.GENERATOR);
470 writer.append(">").append('\n');
473 dumpAvatarList(writer);
476 dumpPeriodList(writer);
478 writer.append("</village>").append("\n");