OSDN Git Service

コメントファイルパース時, 通常コメントと投稿者コメントを分けてパース出来るようにする
authoryukihane <yukihane.feather@gmail.com>
Mon, 12 Sep 2011 08:28:08 +0000 (17:28 +0900)
committeryukihane <yukihane.feather@gmail.com>
Mon, 12 Sep 2011 08:53:04 +0000 (17:53 +0900)
frontend/src/saccubus/conv/ConvertToVideoHook.java
frontend/src/saccubus/conv/NicoXMLReader.java

index 4991c29..3937f9d 100644 (file)
@@ -1,6 +1,8 @@
 package saccubus.conv;
 
 import java.io.*;
+import java.util.EnumSet;
+import java.util.Set;
 import org.xml.sax.*;
 import javax.xml.parsers.*;
 
@@ -29,7 +31,13 @@ public final class ConvertToVideoHook {
     private ConvertToVideoHook() {
     }
 
+    @Deprecated
     public static void convert(File file, File out, String ng_id, String ng_word) throws IOException {
+        convert(EnumSet.allOf(NicoXMLReader.ProcessType.class), file, out, ng_id, ng_word);
+    }
+
+    public static void convert(Set<NicoXMLReader.ProcessType> types, File file, File out, String ng_id, String ng_word)
+            throws IOException {
         try {
             final Packet packet = new Packet();
             // SAXパーサーファクトリを生成
@@ -37,7 +45,7 @@ public final class ConvertToVideoHook {
             // SAXパーサーを生成
             final SAXParser parser = spfactory.newSAXParser();
             // XMLファイルを指定されたデフォルトハンドラーで処理します
-            final NicoXMLReader nico_reader = new NicoXMLReader(packet, ng_id, ng_word);
+            final NicoXMLReader nico_reader = new NicoXMLReader(types, packet, ng_id, ng_word);
 
             parser.parse(file, nico_reader);
 
@@ -47,5 +55,5 @@ public final class ConvertToVideoHook {
         } catch (IOException | SAXException | ParserConfigurationException ex) {
             throw new IOException("コメント変換に失敗。ファイル名に使用できない文字が含まれているか正規表現の間違い?", ex);
         }
-       }
+    }
 }
index 2b75a70..8fb5425 100644 (file)
@@ -1,5 +1,7 @@
 package saccubus.conv;
 
+import java.util.EnumSet;
+import java.util.Set;
 import java.util.regex.Pattern;
 
 import org.xml.sax.helpers.DefaultHandler;
@@ -27,13 +29,20 @@ import org.xml.sax.Attributes;
  */
 public class NicoXMLReader extends DefaultHandler {
 
+    private final Set<ProcessType> processTypes;
     private final Packet packet;
     private final Pattern ngWord;
     private final Pattern ngId;
     private Chat item;
     private boolean itemKicked;
 
-    public NicoXMLReader(Packet packet, String ng_id, String ng_word) {
+    public enum ProcessType {
+
+        NORMAL, OWNER;
+    }
+
+    public NicoXMLReader(Set<ProcessType> types, Packet packet, String ng_id, String ng_word) {
+        this.processTypes = EnumSet.copyOf(types);
         this.packet = packet;
         this.ngWord = makePattern(ng_word);
         this.ngId = makePattern(ng_id);
@@ -128,6 +137,20 @@ public class NicoXMLReader extends DefaultHandler {
             // System.out.println("----------");
             item = new Chat();
             itemKicked = false;
+
+            // 通常コメントを処理するか, 投稿者コメントを処理するか
+            final String fork = attributes.getValue("fork");
+            final boolean isOwner = "1".equals(fork);
+            if (isOwner) {
+                if (!processTypes.contains(ProcessType.OWNER)) {
+                    itemKicked = true;
+                }
+            } else {
+                if (!processTypes.contains(ProcessType.NORMAL)) {
+                    itemKicked = true;
+                }
+            }
+
             //マイメモリ削除対象
             final String deleted = attributes.getValue("deleted");
             if (deleted != null && deleted.equalsIgnoreCase("1")) {