OSDN Git Service

Merge commit 'c273eaeb2e7f84eb5e002982d62bcbff90f15a2d'
[jindolf/JinParser.git] / src / test / java / sample / SampleParser.java
index 43ab984..cef3297 100644 (file)
@@ -7,54 +7,45 @@
 
 package sample;
 
+import io.bitbucket.olyutorskii.jiocema.DecodeBreakException;
+import io.bitbucket.olyutorskii.jiocema.DecodeNotifier;
 import java.io.FileInputStream;
-import jp.sourceforge.jindolf.parser.*;
 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;
+import jp.osdn.jindolf.parser.HtmlParseException;
+import jp.osdn.jindolf.parser.HtmlParser;
+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 ud;
+    private static final Charset CS = Charset.forName("Shift_JIS");
 
-    static{
-        ud = Charset.forName("UTF-8").newDecoder();
-    }
 
     private SampleParser(){
         super();
         return;
     }
 
-    public static SortedMap<String, ZipEntry> createEntryMap(ZipFile file){
-        TreeMap<String, ZipEntry> result = new TreeMap<String, ZipEntry>();
 
-        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);
-    }
+    private static DecodedContent contentFromStream(InputStream istream)
+            throws IOException, DecodeBreakException{
+        CharsetDecoder cd = CS.newDecoder();
+        DecodeNotifier decoder = new DecodeNotifier(cd);
+        ContentBuilder builder = new ContentBuilder();
 
-    public static DecodedContent contentFromStream(InputStream istream)
-            throws IOException, DecodeException{
-        StreamDecoder decoder = new StreamDecoder(ud);
-        ContentBuilderUCS2 builder = new ContentBuilderUCS2();
-
-        decoder.setDecodeHandler(builder);
+        decoder.setCharDecodeListener(builder);
 
         decoder.decode(istream);
 
@@ -63,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();
@@ -77,67 +68,81 @@ public class SampleParser{
         return;
     }
 
-    public static void parseStream(InputStream istream)
+    private static void parseStream(InputStream istream)
             throws IOException,
-                   DecodeException,
+                   DecodeBreakException,
                    HtmlParseException{
         DecodedContent content = contentFromStream(istream);
-
         parseContent(content);
-
         return;
     }
 
-    public static void main(String[] args)
-            throws IOException,
-                   DecodeException,
-                   HtmlParseException {
-        if(args.length == 0){
-            System.out.println(
-                     "標準入力から人狼BBSのXHTML文書の読み取りを"
-                    +"開始します...");
-
-            parseStream(System.in);
+    private static void modeStdin()
+            throws IOException,DecodeBreakException,HtmlParseException{
+        System.out.println(
+                 "標準入力から人狼BBSのXHTML文書の読み取りを"
+                +"開始します...");
 
-            System.exit(0);
+        parseStream(System.in);
 
-            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);
+                }
             }
+        }
 
-            zipfile.close();
+        return;
+    }
 
-            System.exit(0);
+    private static void modeFile(String fileName)
+            throws IOException,DecodeBreakException,HtmlParseException{
+        System.out.println(fileName + "のパースを開始...");
+
+        try(InputStream istream = new FileInputStream(fileName)){
+            parseStream(istream);
+        }
+
+        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;
     }