OSDN Git Service

Merge commit '2234b50cfbe7c86237086a3bf4e62397814a390e'
[jindolf/JinParser.git] / src / test / java / sample / SampleParser.java
index e7e2b3b..43ab984 100644 (file)
-/*\r
- * sample parser\r
- *\r
- * Copyright(c) 2009 olyutorskii\r
- * $Id: SampleParser.java 1000 2010-03-15 12:08:42Z olyutorskii $\r
- */\r
-\r
-package sample;\r
-\r
-import java.io.FileInputStream;\r
-import jp.sourceforge.jindolf.parser.*;\r
-import java.io.IOException;\r
-import java.io.InputStream;\r
-import java.nio.charset.Charset;\r
-import java.nio.charset.CharsetDecoder;\r
-import java.util.Collections;\r
-import java.util.Enumeration;\r
-import java.util.SortedMap;\r
-import java.util.TreeMap;\r
-import java.util.zip.ZipEntry;\r
-import java.util.zip.ZipFile;\r
-\r
-/**\r
- * サンプルのパーサ\r
- */\r
-public class SampleParser{\r
-\r
-    private static final CharsetDecoder ud;\r
-\r
-    static{\r
-        ud = Charset.forName("UTF-8").newDecoder();\r
-    }\r
-\r
-    private SampleParser(){\r
-        super();\r
-        return;\r
-    }\r
-\r
-    public static SortedMap<String, ZipEntry> createEntryMap(ZipFile file){\r
-        TreeMap<String, ZipEntry> result = new TreeMap<String, ZipEntry>();\r
-\r
-        Enumeration<? extends ZipEntry> list = file.entries();\r
-        while(list.hasMoreElements()){\r
-            ZipEntry entry = list.nextElement();\r
-            String name = entry.getName();\r
-            result.put(name, entry);\r
-        }\r
-\r
-        return Collections.unmodifiableSortedMap(result);\r
-    }\r
-\r
-    public static DecodedContent contentFromStream(InputStream istream)\r
-            throws IOException, DecodeException{\r
-        StreamDecoder decoder = new StreamDecoder(ud);\r
-        ContentBuilderUCS2 builder = new ContentBuilderUCS2();\r
-\r
-        decoder.setDecodeHandler(builder);\r
-\r
-        decoder.decode(istream);\r
-\r
-        DecodedContent content = builder.getContent();\r
-\r
-        return content;\r
-    }\r
-\r
-    public static void parseContent(DecodedContent content)\r
-            throws HtmlParseException{\r
-        HtmlParser parser = new HtmlParser();\r
-        HtmlHandler handler = new SampleHandler();\r
-\r
-        parser.setBasicHandler   (handler);\r
-        parser.setTalkHandler    (handler);\r
-        parser.setSysEventHandler(handler);\r
-\r
-        parser.parseAutomatic(content);\r
-\r
-        return;\r
-    }\r
-\r
-    public static void parseStream(InputStream istream)\r
-            throws IOException,\r
-                   DecodeException,\r
-                   HtmlParseException{\r
-        DecodedContent content = contentFromStream(istream);\r
-\r
-        parseContent(content);\r
-\r
-        return;\r
-    }\r
-\r
-    public static void main(String[] args)\r
-            throws IOException,\r
-                   DecodeException,\r
-                   HtmlParseException {\r
-        if(args.length == 0){\r
-            System.out.println(\r
-                     "標準入力から人狼BBSのXHTML文書の読み取りを"\r
-                    +"開始します...");\r
-\r
-            parseStream(System.in);\r
-\r
-            System.exit(0);\r
-\r
-            return;\r
-        }else if(args[0].endsWith(".zip")){\r
-            System.out.println(\r
-                     "ZIPアーカイブ内の*.htmlファイルから"\r
-                    +"人狼BBSのXHTML文書の読み取りを開始します...");\r
-\r
-            ZipFile zipfile = new ZipFile(args[0]);\r
-\r
-            SortedMap<String, ZipEntry> map = createEntryMap(zipfile);\r
-\r
-            for(ZipEntry entry : map.values()){\r
-                String name = entry.getName();\r
-                if( ! name.endsWith(".html") ) continue;\r
-\r
-                System.out.println(name + "のパースを開始...");\r
-\r
-                InputStream istream = zipfile.getInputStream(entry);\r
-                parseStream(istream);\r
-\r
-                istream.close();\r
-            }\r
-\r
-            zipfile.close();\r
-\r
-            System.exit(0);\r
-\r
-            return;\r
-        }else{\r
-            System.out.println(args[0] + "のパースを開始...");\r
-\r
-            InputStream istream = new FileInputStream(args[0]);\r
-            parseStream(istream);\r
-            istream.close();\r
-\r
-            System.exit(0);\r
-        }\r
-\r
-        return;\r
-    }\r
-\r
-}\r
+/*
+ * sample parser
+ *
+ * License : The MIT License
+ * Copyright(c) 2009 olyutorskii
+ */
+
+package sample;
+
+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;
+
+/**
+ * サンプルのパーサ
+ */
+public class SampleParser{
+
+    private static final CharsetDecoder ud;
+
+    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);
+    }
+
+    public static DecodedContent contentFromStream(InputStream istream)
+            throws IOException, DecodeException{
+        StreamDecoder decoder = new StreamDecoder(ud);
+        ContentBuilderUCS2 builder = new ContentBuilderUCS2();
+
+        decoder.setDecodeHandler(builder);
+
+        decoder.decode(istream);
+
+        DecodedContent content = builder.getContent();
+
+        return content;
+    }
+
+    public static void parseContent(DecodedContent content)
+            throws HtmlParseException{
+        HtmlParser parser = new HtmlParser();
+        HtmlHandler handler = new SampleHandler();
+
+        parser.setBasicHandler   (handler);
+        parser.setTalkHandler    (handler);
+        parser.setSysEventHandler(handler);
+
+        parser.parseAutomatic(content);
+
+        return;
+    }
+
+    public static void parseStream(InputStream istream)
+            throws IOException,
+                   DecodeException,
+                   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);
+
+            System.exit(0);
+
+            return;
+        }else if(args[0].endsWith(".zip")){
+            System.out.println(
+                     "ZIPアーカイブ内の*.htmlファイルから"
+                    +"人狼BBSのXHTML文書の読み取りを開始します...");
+
+            ZipFile zipfile = new ZipFile(args[0]);
+
+            SortedMap<String, ZipEntry> map = createEntryMap(zipfile);
+
+            for(ZipEntry entry : map.values()){
+                String name = entry.getName();
+                if( ! name.endsWith(".html") ) continue;
+
+                System.out.println(name + "のパースを開始...");
+
+                InputStream istream = zipfile.getInputStream(entry);
+                parseStream(istream);
+
+                istream.close();
+            }
+
+            zipfile.close();
+
+            System.exit(0);
+
+            return;
+        }else{
+            System.out.println(args[0] + "のパースを開始...");
+
+            InputStream istream = new FileInputStream(args[0]);
+            parseStream(istream);
+            istream.close();
+
+            System.exit(0);
+        }
+
+        return;
+    }
+
+}