4 * License : The MIT License
5 * Copyright(c) 2008 olyutorskii
8 package jp.sourceforge.jindolf.archiver;
11 import java.io.IOException;
12 import java.io.InputStream;
13 import java.io.Writer;
14 import java.net.MalformedURLException;
16 import java.net.URISyntaxException;
18 import java.util.Enumeration;
19 import java.util.Iterator;
20 import java.util.LinkedList;
21 import java.util.List;
22 import java.util.regex.Matcher;
23 import java.util.regex.Pattern;
24 import java.util.zip.ZipEntry;
25 import java.util.zip.ZipFile;
26 import jp.sourceforge.jindolf.parser.DecodeException;
27 import jp.sourceforge.jindolf.parser.HtmlParseException;
30 * ZIPアーカイブされた生XHTML情報へのアクセス諸々。
32 public final class ZipUtils{
44 * ZIPファイルに格納された村一覧を抽出する。
46 * @param zipFile ZIPファイル
48 * @throws IOException 入力エラー
50 public static List<VillageData> getVillageDataList(ZipFile zipFile)
52 List<VillageData> result = new LinkedList<VillageData>();
54 List<ZipEntry> logList = getDownloadLogList(zipFile);
55 for(ZipEntry entry : logList){
56 VillageData villageData =
57 getVillageDataFromLogEntry(zipFile, entry);
58 result.add(villageData);
65 * ZIPファイルからダウンロードログファイルの一覧を取得する。
66 * ダウンロードログファイルは必ず「download.log」の名前を持つ。
70 public static List<ZipEntry> getDownloadLogList(ZipFile file){
71 List<ZipEntry> result = new LinkedList<ZipEntry>();
73 Enumeration<? extends ZipEntry> list = file.entries();
74 while(list.hasMoreElements()){
75 ZipEntry entry = list.nextElement();
76 String name = entry.getName();
77 if(name.endsWith("/download.log" )){
86 * ログファイルを表すZIPエントリから村情報を抽出する。
87 * @param zipFile ZIPファイル
88 * @param logEntry ログファイルのZIPエントリ
90 * @throws IOException 入力エラー
92 public static VillageData getVillageDataFromLogEntry(
93 ZipFile zipFile, ZipEntry logEntry)
95 InputStream istream = zipFile.getInputStream(logEntry);
96 List<PeriodResource> list =
97 FileArchive.parseDownloadLog(istream);
100 String baseName = logEntry.getName().replaceAll("/[^/]+$", "/");
101 for(PeriodResource resource : list){
102 modifyResourceUrl(zipFile, baseName, resource);
105 VillageData villageData = new VillageData(list);
110 * ログ記録に書かれたXHTMLファイル名を実際にアクセス可能なURLに修正する。
111 * @param zipFile ZIPファイル
112 * @param entryBase ファイル名のベース
113 * @param resource リソース情報
116 public static PeriodResource modifyResourceUrl(ZipFile zipFile,
118 PeriodResource resource ){
121 URL resUrl = resource.getResourceUrl();
122 URI resUri = resUrl.toURI();
123 File file = new File(resUri);
124 fileName = file.getName();
125 }catch(URISyntaxException e){
126 throw new IllegalArgumentException(e);
129 String zipUri = new File(zipFile.getName()).toURI().toString();
131 String urlText = "jar:" + zipUri + "!/" + entryBase + fileName;
135 zipResource = new URL(urlText);
136 }catch(MalformedURLException e){
137 throw new IllegalArgumentException(e);
140 resource.setResourceUrl(zipResource);
147 * @param zipFile ZIPファイル
150 * @throws IOException 入力エラー
152 public static VillageData getVillageData(ZipFile zipFile, int vid)
154 ZipEntry entry = getDownloadLogEntry(zipFile, vid);
155 VillageData result = getVillageDataFromLogEntry(zipFile, entry);
160 * 村番号から該当するログファイルエントリを取得する。
161 * @param file ZIPファイル
163 * @return ログファイルのZIPエントリ。見つからなければnull。
165 public static ZipEntry getDownloadLogEntry(ZipFile file, int vid){
166 Pattern entryPattern =
167 Pattern.compile("jin_[^_]+_([0-9]+)/download.log$");
169 Enumeration<? extends ZipEntry> list = file.entries();
170 while(list.hasMoreElements()){
171 ZipEntry entry = list.nextElement();
172 String name = entry.getName();
173 Matcher matcher = entryPattern.matcher(name);
175 String vnum = matcher.group(1);
176 if(vid == Integer.parseInt(vnum)) return entry;
183 * ZIPファイル中の指定した村番号の村をXML出力する。
184 * @param zipFile ZIPファイル
187 * @throws IOException 入出力エラー
188 * @throws DecodeException デコードエラー
189 * @throws HtmlParseException パースエラー
191 public static void dumpZipVid(ZipFile zipFile, int vid, Writer writer)
192 throws IOException, DecodeException, HtmlParseException{
193 VillageData data = getVillageData(zipFile, vid);
194 Builder.fillVillageData(data);
195 XmlUtils.dumpVillageData(writer, data);
201 * ZIPファイル中の全村をXML出力する。
202 * @param zipFile ZIPファイル
203 * @throws IOException 入出力エラー
204 * @throws DecodeException デコードエラー
205 * @throws HtmlParseException パースエラー
207 public static void dumpZipAll(ZipFile zipFile)
208 throws IOException, DecodeException, HtmlParseException{
209 List<VillageData> villageDataList;
210 villageDataList = ZipUtils.getVillageDataList(zipFile);
211 Iterator<VillageData> it = villageDataList.iterator();
213 VillageData villageData = it.next();
214 Builder.fillVillageData(villageData);
215 Writer writer = XmlUtils.createFileWriter(villageData);
216 XmlUtils.dumpVillageData(writer, villageData);