OSDN Git Service

Merge commit 'cbf79f1f735efd8eacdb6f10beb9c2705225ac30'
[jindolf/JinArchiver.git] / src / main / java / jp / sourceforge / jindolf / archiver / HttpAccess.java
index 51e40f1..309f726 100644 (file)
-/*\r
- * downloader\r
- *\r
- * Copyright(c) 2008 olyutorskii\r
- */\r
-\r
-package jp.sourceforge.jindolf.archiver;\r
-\r
-import java.io.IOException;\r
-import java.io.InputStream;\r
-import java.net.URL;\r
-import java.nio.charset.Charset;\r
-import java.util.LinkedList;\r
-import java.util.List;\r
-import jp.sourceforge.jindolf.corelib.LandDef;\r
-import jp.sourceforge.jindolf.corelib.LandState;\r
-import jp.sourceforge.jindolf.corelib.PeriodType;\r
-import jp.sourceforge.jindolf.parser.DecodeException;\r
-import jp.sourceforge.jindolf.parser.DecodedContent;\r
-import jp.sourceforge.jindolf.parser.HtmlAdapter;\r
-import jp.sourceforge.jindolf.parser.HtmlParseException;\r
-import jp.sourceforge.jindolf.parser.HtmlParser;\r
-import jp.sourceforge.jindolf.parser.PageType;\r
-import jp.sourceforge.jindolf.parser.SeqRange;\r
-\r
-/**\r
- * 人狼HTTPサーバ内のリソース情報を展開する。\r
- */\r
-public final class HttpAccess{\r
-\r
-    /**\r
-     * 日一覧ページ(エピローグの翌日)のURLを得る。\r
-     * @param landDef 国指定\r
-     * @param vid 村番号\r
-     * @return 一覧ページへのURL\r
-     * @throws IOException 入力エラー\r
-     */\r
-    public static URL getPeriodListURL(LandDef landDef, int vid)\r
-            throws IOException{\r
-        StringBuilder urlText = new StringBuilder();\r
-\r
-        urlText.append(landDef.getCgiURI().toASCIIString());\r
-        urlText.append('?').append("vid=").append(vid);\r
-        if(landDef.getLandState() == LandState.ACTIVE){\r
-            urlText.append('&').append("meslog=");\r
-        }\r
-\r
-        URL result = new URL(urlText.toString());\r
-\r
-        return result;\r
-    }\r
-\r
-    /**\r
-     * 日ページのロード元情報一覧を得る。\r
-     * @param landDef 国指定\r
-     * @param vid 村番号\r
-     * @return ロード元情報一覧\r
-     * @throws DecodeException デコードエラー\r
-     * @throws HtmlParseException パースエラー\r
-     * @throws IOException 入力エラー\r
-     */\r
-    public static List<PeriodResource> loadResourceList(LandDef landDef,\r
-                                                          int vid)\r
-            throws DecodeException,\r
-                   HtmlParseException,\r
-                   IOException {\r
-        URL url = getPeriodListURL(landDef, vid);\r
-\r
-        Charset charset = landDef.getEncoding();\r
-        InputStream istream = url.openStream();\r
-        DecodedContent content = Builder.contentFromStream(charset, istream);\r
-        istream.close();\r
-\r
-        HtmlParser parser = new HtmlParser();\r
-        PeriodListHandler handler = new PeriodListHandler(landDef, vid);\r
-        parser.setBasicHandler(handler);\r
-        parser.setTalkHandler(handler);\r
-        parser.setSysEventHandler(handler);\r
-        parser.parseAutomatic(content);\r
-\r
-        List<PeriodResource> result = handler.getResourceList();\r
-\r
-        return result;\r
-    }\r
-\r
-    /**\r
-     * 隠しコンストラクタ。\r
-     */\r
-    private HttpAccess(){\r
-        throw new Error();\r
-    }\r
-\r
-    /**\r
-     * 日一覧パース用ハンドラ。\r
-     */\r
-    public static class PeriodListHandler extends HtmlAdapter{\r
-\r
-        private final LandDef landDef;\r
-        private final int vid;\r
-\r
-        private List<PeriodResource> resourceList = null;\r
-\r
-        private int progressDays;\r
-        private boolean hasDone;\r
-\r
-        /**\r
-         * コンストラクタ。\r
-         * @param landDef 国指定\r
-         * @param vid 村番号\r
-         */\r
-        public PeriodListHandler(LandDef landDef, int vid){\r
-            super();\r
-            this.landDef = landDef;\r
-            this.vid = vid;\r
-            return;\r
-        }\r
-\r
-        /**\r
-         * 日ページのURL文字列を生成する。\r
-         * @param type 日種類\r
-         * @param day 日にち\r
-         * @return URL文字列\r
-         */\r
-        public String getURL(PeriodType type, int day){\r
-            String base = this.landDef.getCgiURI().toASCIIString();\r
-            base += "?vid=" + this.vid;\r
-\r
-            if(this.landDef.getLandId().equals("wolfg")){\r
-                base += "&meslog=";\r
-                String dnum = "000" + (day - 1);\r
-                dnum = dnum.substring(dnum.length() - 3);\r
-                switch(type){\r
-                case PROLOGUE:\r
-                    base += "000_ready";\r
-                    break;\r
-                case PROGRESS:\r
-                    base += dnum;\r
-                    base += "_progress";\r
-                    break;\r
-                case EPILOGUE:\r
-                    base += dnum;\r
-                    base += "_party";\r
-                    break;\r
-                default:\r
-                    assert false;\r
-                    return null;\r
-                }\r
-            }else{\r
-                base += "&meslog=" + this.vid + "_";\r
-                switch(type){\r
-                case PROLOGUE:\r
-                    base += "ready_0";\r
-                    break;\r
-                case PROGRESS:\r
-                    base += "progress_" + (day - 1);\r
-                    break;\r
-                case EPILOGUE:\r
-                    base += "party_" + (day - 1);\r
-                    break;\r
-                default:\r
-                    return null;\r
-                }\r
-            }\r
-\r
-            base += "&mes=all";\r
-\r
-            return base;\r
-        }\r
-\r
-        /**\r
-         * PeriodResource一覧を得る。\r
-         * @return PeriodResource一覧\r
-         */\r
-        public List<PeriodResource> getResourceList(){\r
-            return this.resourceList;\r
-        }\r
-\r
-        /**\r
-         * {@inheritDoc}\r
-         * @param content {@inheritDoc}\r
-         * @throws HtmlParseException {@inheritDoc}\r
-         */\r
-        @Override\r
-        public void startParse(DecodedContent content)\r
-                throws HtmlParseException{\r
-            this.resourceList = new LinkedList<PeriodResource>();\r
-            this.progressDays = 0;\r
-            this.hasDone = false;\r
-            return;\r
-        }\r
-\r
-        /**\r
-         * {@inheritDoc}\r
-         * @param type {@inheritDoc}\r
-         * @throws HtmlParseException {@inheritDoc}\r
-         */\r
-        @Override\r
-        public void pageType(PageType type) throws HtmlParseException{\r
-            if(type != PageType.PERIOD_PAGE) throw new HtmlParseException();\r
-            return;\r
-        }\r
-\r
-        /**\r
-         * {@inheritDoc}\r
-         * @param content {@inheritDoc}\r
-         * @param anchorRange {@inheritDoc}\r
-         * @param periodType {@inheritDoc}\r
-         * @param day {@inheritDoc}\r
-         * @throws HtmlParseException {@inheritDoc}\r
-         */\r
-        @Override\r
-        public void periodLink(DecodedContent content,\r
-                                SeqRange anchorRange,\r
-                                PeriodType periodType,\r
-                                int day )\r
-                throws HtmlParseException{\r
-            if(periodType == null){\r
-                this.hasDone = true;\r
-            }else if(periodType == PeriodType.PROGRESS){\r
-                this.progressDays = day;\r
-            }\r
-            return;\r
-        }\r
-\r
-        /**\r
-         * {@inheritDoc}\r
-         * @throws HtmlParseException {@inheritDoc}\r
-         */\r
-        @Override\r
-        public void endParse() throws HtmlParseException{\r
-            if( ! this.hasDone ) throw new HtmlParseException();\r
-\r
-            PeriodResource resource;\r
-\r
-            String prologueURI = getURL(PeriodType.PROLOGUE, 0);\r
-            resource = new PeriodResource(this.landDef,\r
-                                          this.vid,\r
-                                          PeriodType.PROLOGUE,\r
-                                          0,\r
-                                          prologueURI,\r
-                                          0L,\r
-                                          null);\r
-            this.resourceList.add(resource);\r
-\r
-            for(int day = 1; day <= this.progressDays; day++){\r
-                String progressURI = getURL(PeriodType.PROGRESS, day);\r
-                resource = new PeriodResource(this.landDef,\r
-                                              this.vid,\r
-                                              PeriodType.PROGRESS,\r
-                                              day,\r
-                                              progressURI,\r
-                                              0L,\r
-                                              null);\r
-                this.resourceList.add(resource);\r
-            }\r
-\r
-            String epilogueURI = getURL(PeriodType.EPILOGUE,\r
-                                        this.progressDays + 1);\r
-            resource = new PeriodResource(this.landDef,\r
-                                          this.vid,\r
-                                          PeriodType.EPILOGUE,\r
-                                          this.progressDays + 1,\r
-                                          epilogueURI,\r
-                                          0L,\r
-                                          null);\r
-            this.resourceList.add(resource);\r
-\r
-            return;\r
-        }\r
-\r
-    }\r
-\r
-}\r
+/*
+ * downloader
+ *
+ * License : The MIT License
+ * Copyright(c) 2008 olyutorskii
+ */
+
+package jp.sourceforge.jindolf.archiver;
+
+import io.bitbucket.olyutorskii.jiocema.DecodeBreakException;
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.URL;
+import java.nio.charset.Charset;
+import java.util.LinkedList;
+import java.util.List;
+import jp.osdn.jindolf.parser.HtmlAdapter;
+import jp.osdn.jindolf.parser.HtmlParseException;
+import jp.osdn.jindolf.parser.HtmlParser;
+import jp.osdn.jindolf.parser.PageType;
+import jp.osdn.jindolf.parser.SeqRange;
+import jp.osdn.jindolf.parser.content.DecodedContent;
+import jp.sourceforge.jindolf.corelib.LandDef;
+import jp.sourceforge.jindolf.corelib.LandState;
+import jp.sourceforge.jindolf.corelib.PeriodType;
+
+/**
+ * 人狼HTTPサーバ内のリソース情報を展開する。
+ */
+public final class HttpAccess{
+
+    /**
+     * 隠しコンストラクタ。
+     */
+    private HttpAccess(){
+        assert false;
+        throw new AssertionError();
+    }
+
+
+    /**
+     * 日一覧ページ(エピローグの翌日)のURLを得る。
+     * @param landDef 国指定
+     * @param vid 村番号
+     * @return 一覧ページへのURL
+     * @throws IOException 入力エラー
+     */
+    public static URL getPeriodListURL(LandDef landDef, int vid)
+            throws IOException{
+        StringBuilder urlText = new StringBuilder();
+
+        urlText.append(landDef.getCgiURI().toASCIIString());
+        urlText.append('?').append("vid=").append(vid);
+        if(landDef.getLandState() == LandState.ACTIVE){
+            urlText.append('&').append("meslog=");
+        }
+
+        URL result = new URL(urlText.toString());
+
+        return result;
+    }
+
+    /**
+     * 日ページのロード元情報一覧を得る。
+     * @param landDef 国指定
+     * @param vid 村番号
+     * @return ロード元情報一覧
+     * @throws DecodeBreakException デコードエラー
+     * @throws HtmlParseException パースエラー
+     * @throws IOException 入力エラー
+     */
+    public static List<PeriodResource> loadResourceList(LandDef landDef,
+                                                          int vid)
+            throws DecodeBreakException,
+                   HtmlParseException,
+                   IOException {
+        URL url = getPeriodListURL(landDef, vid);
+
+        Charset charset = landDef.getEncoding();
+        DecodedContent content;
+        try(InputStream istream = url.openStream()){
+            content = Builder.contentFromStream(charset, istream);
+        }
+
+        HtmlParser parser = new HtmlParser();
+        PeriodListHandler handler = new PeriodListHandler(landDef, vid);
+        parser.setBasicHandler(handler);
+        parser.setTalkHandler(handler);
+        parser.setSysEventHandler(handler);
+        parser.parseAutomatic(content);
+
+        List<PeriodResource> result = handler.getResourceList();
+
+        return result;
+    }
+
+    /**
+     * 日一覧パース用ハンドラ。
+     */
+    public static class PeriodListHandler extends HtmlAdapter{
+
+        private final LandDef landDef;
+        private final int vid;
+
+        private List<PeriodResource> resourceList = null;
+
+        private int progressDays;
+        private boolean hasDone;
+
+        /**
+         * コンストラクタ。
+         * @param landDef 国指定
+         * @param vid 村番号
+         */
+        public PeriodListHandler(LandDef landDef, int vid){
+            super();
+            this.landDef = landDef;
+            this.vid = vid;
+            return;
+        }
+
+        /**
+         * 日ページのURL文字列を生成する。
+         * @param type 日種類
+         * @param day 日にち
+         * @return URL文字列
+         */
+        public String getURL(PeriodType type, int day){
+            String base = this.landDef.getCgiURI().toASCIIString();
+            base += "?vid=" + this.vid + "&meslog=";
+
+            String landId = this.landDef.getLandId();
+            if("wolfg".equals(landId)){
+                String dnum = "000" + (day - 1);
+                dnum = dnum.substring(dnum.length() - 3);
+                switch(type){
+                case PROLOGUE:
+                    base += "000_ready";
+                    break;
+                case PROGRESS:
+                    base += dnum;
+                    base += "_progress";
+                    break;
+                case EPILOGUE:
+                    base += dnum;
+                    base += "_party";
+                    break;
+                default:
+                    assert false;
+                    return null;
+                }
+            }else{
+                base += this.vid + "_";
+                switch(type){
+                case PROLOGUE:
+                    base += "ready_0";
+                    break;
+                case PROGRESS:
+                    base += "progress_" + (day - 1);
+                    break;
+                case EPILOGUE:
+                    base += "party_" + (day - 1);
+                    break;
+                default:
+                    return null;
+                }
+            }
+
+            base += "&mes=all";
+
+            return base;
+        }
+
+        /**
+         * PeriodResource一覧を得る。
+         * @return PeriodResource一覧
+         */
+        public List<PeriodResource> getResourceList(){
+            return this.resourceList;
+        }
+
+        /**
+         * {@inheritDoc}
+         * @param content {@inheritDoc}
+         * @throws HtmlParseException {@inheritDoc}
+         */
+        @Override
+        public void startParse(DecodedContent content)
+                throws HtmlParseException{
+            this.resourceList = new LinkedList<>();
+            this.progressDays = 0;
+            this.hasDone = false;
+            return;
+        }
+
+        /**
+         * {@inheritDoc}
+         * @param type {@inheritDoc}
+         * @throws HtmlParseException {@inheritDoc}
+         */
+        @Override
+        public void pageType(PageType type) throws HtmlParseException{
+            if(type != PageType.PERIOD_PAGE) throw new HtmlParseException();
+            return;
+        }
+
+        /**
+         * {@inheritDoc}
+         * @param content {@inheritDoc}
+         * @param anchorRange {@inheritDoc}
+         * @param periodType {@inheritDoc}
+         * @param day {@inheritDoc}
+         * @throws HtmlParseException {@inheritDoc}
+         */
+        @Override
+        public void periodLink(DecodedContent content,
+                                SeqRange anchorRange,
+                                PeriodType periodType,
+                                int day )
+                throws HtmlParseException{
+            if(periodType == null){
+                this.hasDone = true;
+            }else if(periodType == PeriodType.PROGRESS){
+                this.progressDays = day;
+            }
+            return;
+        }
+
+        /**
+         * {@inheritDoc}
+         * @throws HtmlParseException {@inheritDoc}
+         */
+        @Override
+        public void endParse() throws HtmlParseException{
+            if( ! this.hasDone ) throw new HtmlParseException();
+
+            PeriodResource resource;
+
+            String prologueURI = getURL(PeriodType.PROLOGUE, 0);
+            resource = new PeriodResource(this.landDef,
+                                          this.vid,
+                                          PeriodType.PROLOGUE,
+                                          0,
+                                          prologueURI,
+                                          0L,
+                                          null);
+            this.resourceList.add(resource);
+
+            for(int day = 1; day <= this.progressDays; day++){
+                String progressURI = getURL(PeriodType.PROGRESS, day);
+                resource = new PeriodResource(this.landDef,
+                                              this.vid,
+                                              PeriodType.PROGRESS,
+                                              day,
+                                              progressURI,
+                                              0L,
+                                              null);
+                this.resourceList.add(resource);
+            }
+
+            String epilogueURI = getURL(PeriodType.EPILOGUE,
+                                        this.progressDays + 1);
+            resource = new PeriodResource(this.landDef,
+                                          this.vid,
+                                          PeriodType.EPILOGUE,
+                                          this.progressDays + 1,
+                                          epilogueURI,
+                                          0L,
+                                          null);
+            this.resourceList.add(resource);
+
+            return;
+        }
+
+    }
+
+}