OSDN Git Service

refactoring Sample demo
authorOlyutorskii <olyutorskii@users.osdn.me>
Sat, 31 Mar 2018 01:51:25 +0000 (10:51 +0900)
committerOlyutorskii <olyutorskii@users.osdn.me>
Sat, 31 Mar 2018 01:51:25 +0000 (10:51 +0900)
src/test/java/sample/SampleParser.java

index 92a247c..cef3297 100644 (file)
@@ -14,10 +14,7 @@ import java.io.IOException;
 import java.io.InputStream;
 import java.nio.charset.Charset;
 import java.nio.charset.CharsetDecoder;
-import java.util.Collections;
 import java.util.Enumeration;
-import java.util.SortedMap;
-import java.util.TreeMap;
 import java.util.zip.ZipEntry;
 import java.util.zip.ZipFile;
 import jp.osdn.jindolf.parser.HtmlHandler;
@@ -27,37 +24,25 @@ import jp.osdn.jindolf.parser.content.ContentBuilder;
 import jp.osdn.jindolf.parser.content.DecodedContent;
 
 /**
- * サンプルのパーサ
+ * サンプルのパーサ。
+ *
+ * <p>F国以前(Shift_JIS)用
  */
 public class SampleParser{
 
-    private static final CharsetDecoder CHD_UTF8;
+    private static final Charset CS = Charset.forName("Shift_JIS");
 
-    static{
-        CHD_UTF8 = Charset.forName("UTF-8").newDecoder();
-    }
 
     private SampleParser(){
         super();
         return;
     }
 
-    public static SortedMap<String, ZipEntry> createEntryMap(ZipFile file){
-        TreeMap<String, ZipEntry> result = new TreeMap<>();
-
-        Enumeration<? extends ZipEntry> list = file.entries();
-        while(list.hasMoreElements()){
-            ZipEntry entry = list.nextElement();
-            String name = entry.getName();
-            result.put(name, entry);
-        }
-
-        return Collections.unmodifiableSortedMap(result);
-    }
 
-    public static DecodedContent contentFromStream(InputStream istream)
+    private static DecodedContent contentFromStream(InputStream istream)
             throws IOException, DecodeBreakException{
-        DecodeNotifier decoder = new DecodeNotifier(CHD_UTF8);
+        CharsetDecoder cd = CS.newDecoder();
+        DecodeNotifier decoder = new DecodeNotifier(cd);
         ContentBuilder builder = new ContentBuilder();
 
         decoder.setCharDecodeListener(builder);
@@ -69,7 +54,7 @@ public class SampleParser{
         return content;
     }
 
-    public static void parseContent(DecodedContent content)
+    private static void parseContent(DecodedContent content)
             throws HtmlParseException{
         HtmlParser parser = new HtmlParser();
         HtmlHandler handler = new SampleHandler();
@@ -83,67 +68,81 @@ public class SampleParser{
         return;
     }
 
-    public static void parseStream(InputStream istream)
+    private static void parseStream(InputStream istream)
             throws IOException,
                    DecodeBreakException,
                    HtmlParseException{
         DecodedContent content = contentFromStream(istream);
-
         parseContent(content);
-
         return;
     }
 
-    public static void main(String[] args)
-            throws IOException,
-                   DecodeBreakException,
-                   HtmlParseException {
-        if(args.length == 0){
-            System.out.println(
-                     "標準入力から人狼BBSのXHTML文書の読み取りを"
-                    +"開始します...");
+    private static void modeStdin()
+            throws IOException,DecodeBreakException,HtmlParseException{
+        System.out.println(
+                 "標準入力から人狼BBSのXHTML文書の読み取りを"
+                +"開始します...");
 
-            parseStream(System.in);
+        parseStream(System.in);
 
-            System.exit(0);
-
-            return;
-        }else if(args[0].endsWith(".zip")){
-            System.out.println(
-                     "ZIPアーカイブ内の*.htmlファイルから"
-                    +"人狼BBSのXHTML文書の読み取りを開始します...");
-
-            ZipFile zipfile = new ZipFile(args[0]);
+        return;
+    }
 
-            SortedMap<String, ZipEntry> map = createEntryMap(zipfile);
+    private static void modeZip(String zipFileName)
+            throws IOException,DecodeBreakException,HtmlParseException{
+        System.out.println(
+                 "ZIPアーカイブ内の*.htmlファイルから"
+                +"人狼BBSのXHTML文書の読み取りを開始します...");
 
-            for(ZipEntry entry : map.values()){
+        try(ZipFile zipFile = new ZipFile(zipFileName)){
+            Enumeration<? extends ZipEntry> list = zipFile.entries();
+            while(list.hasMoreElements()){
+                ZipEntry entry = list.nextElement();
                 String name = entry.getName();
                 if( ! name.endsWith(".html") ) continue;
 
                 System.out.println(name + "のパースを開始...");
 
-                InputStream istream = zipfile.getInputStream(entry);
-                parseStream(istream);
-
-                istream.close();
+                try(InputStream istream = zipFile.getInputStream(entry)){
+                    parseStream(istream);
+                }
             }
+        }
+
+        return;
+    }
+
+    private static void modeFile(String fileName)
+            throws IOException,DecodeBreakException,HtmlParseException{
+        System.out.println(fileName + "のパースを開始...");
 
-            zipfile.close();
+        try(InputStream istream = new FileInputStream(fileName)){
+            parseStream(istream);
+        }
 
-            System.exit(0);
+        return;
+    }
 
+    public static void main(String[] args)
+            throws IOException,
+                   DecodeBreakException,
+                   HtmlParseException {
+        if(args.length == 0){
+            modeStdin();
             return;
-        }else{
-            System.out.println(args[0] + "のパースを開始...");
+        }
 
-            InputStream istream = new FileInputStream(args[0]);
-            parseStream(istream);
-            istream.close();
+        String fileName;
+        fileName = args[0];
 
-            System.exit(0);
+        if(fileName.endsWith(".zip")){
+            modeZip(fileName);
+        }else{
+            modeFile(fileName);
         }
 
+        System.exit(0);
+
         return;
     }