package saccubus.conv;
import java.io.*;
-import java.util.logging.Level;
-import java.util.logging.Logger;
+import java.util.EnumSet;
+import java.util.Set;
import org.xml.sax.*;
import javax.xml.parsers.*;
* @author 未入力
* @version 1.0
*/
-public class ConvertToVideoHook {
+public final class ConvertToVideoHook {
- private static final Logger logger = Logger.getLogger(ConvertToVideoHook.class.getName());
+ private ConvertToVideoHook() {
+ }
- public static boolean convert(File file, File out, String ng_id,
- String ng_word) {
- try {
- Packet packet = new Packet();
- // SAXパーサーファクトリを生成
- SAXParserFactory spfactory = SAXParserFactory.newInstance();
- // SAXパーサーを生成
- SAXParser parser = spfactory.newSAXParser();
- // XMLファイルを指定されたデフォルトハンドラーで処理します
- NicoXMLReader nico_reader = null;
- try {
- nico_reader = new NicoXMLReader(packet, ng_id, ng_word);
- } catch (java.util.regex.PatternSyntaxException e) {
- logger.log(Level.SEVERE, null, e);
- return false;
- }
- if (nico_reader != null) {
- parser.parse(file, nico_reader);
- }
- // 変換結果の書き込み
- FileOutputStream fos = new FileOutputStream(out);
- packet.write(fos);
- fos.close();
- return true;
- } catch (IOException ex) {
- logger.log(Level.SEVERE, null, ex);
- } catch (SAXException ex) {
- logger.log(Level.SEVERE, null, ex);
- } catch (ParserConfigurationException ex) {
- logger.log(Level.SEVERE, null, ex);
- }
- return false;
- }
+ @Deprecated
+ public static void convert(File file, File out, String ng_id, String ng_word) throws IOException {
+ convert(EnumSet.allOf(CommentType.class), file, out, ng_id, ng_word);
+ }
+
+ public static void convert(Set<CommentType> types, File file, File out, String ng_id, String ng_word)
+ throws IOException {
+ try {
+ final Packet packet = new Packet();
+ // SAXパーサーファクトリを生成
+ final SAXParserFactory spfactory = SAXParserFactory.newInstance();
+ // SAXパーサーを生成
+ final SAXParser parser = spfactory.newSAXParser();
+ // XMLファイルを指定されたデフォルトハンドラーで処理します
+ final NicoXMLReader nico_reader = new NicoXMLReader(types, packet, ng_id, ng_word);
+
+ parser.parse(file, nico_reader);
+
+ try (final FileOutputStream fos = new FileOutputStream(out)) {
+ packet.write(fos);
+ }
+ } catch (IOException | SAXException | ParserConfigurationException ex) {
+ throw new IOException("コメント変換に失敗。ファイル名に使用できない文字が含まれているか正規表現の間違い?", ex);
+ }
+ }
}