2 package saccubus.converter.filegetter;
5 import java.io.FileNotFoundException;
6 import java.io.IOException;
7 import org.apache.commons.io.FilenameUtils;
8 import saccubus.ConvertStopFlag;
9 import saccubus.converter.classic.profile.InputFileSetting;
10 import saccubus.net.TextProgressListener;
13 * ダウンロード処理を全く必要としない場合のファイルインスタンス化クラス.
16 public class FileInstanciator {
18 private final InstanciationType videoType;
19 private final InstanciationType commentType;
20 private final InstanciationType tcommType;
21 private final String videoId;
22 private FileGetter videoFileGetter;
23 private FileGetter commentFileGetter;
24 private FileGetter tcommFileGetter;
26 protected void setVideoFileGetter(FileGetter getter) {
27 this.videoFileGetter = getter;
30 protected void setCommentFileGetter(FileGetter getter) {
31 this.commentFileGetter = getter;
34 protected void setTcommFileGetter(FileGetter getter) {
35 this.tcommFileGetter = getter;
38 public static FileInstanciator create(
39 ConvertStopFlag stopFlag,
40 InstanciationType videoType,
41 CommentInstanciationType commentType,
42 InstanciationType tcommType,
44 String tag, String time) throws
46 FileInstanciator getter;
47 if (videoType.isDoanload() || commentType.isDoanload() || tcommType.isDoanload()) {
48 getter = new WebFileInstanciator(stopFlag, videoType, commentType, tcommType, li, tag, time);
50 getter = new FileInstanciator(videoType, commentType, tcommType, tag);
55 protected FileInstanciator(
56 InstanciationType videoType,
57 InstanciationType commentType,
58 InstanciationType tcommType,
60 this.videoType = videoType;
61 this.commentType = commentType;
62 this.tcommType = tcommType;
63 this.videoId = videoId;
64 FileGetter getter = new FileGetter();
65 setVideoFileGetter(getter);
66 setCommentFileGetter(getter);
67 setTcommFileGetter(getter);
72 * @throws FileNotFoundException ビデオタイトルの自動命名時、参考となるファイルが見つからなかった。
74 public String getVideoTitle() throws FileNotFoundException {
75 String fileName = null;
76 if (!videoType.isAutoFileName()) {
77 // 動画ファイル名を直接指定している場合は、そのファイル名を基にタイトルを取得する.
78 fileName = videoType.getInitFile().toString();
80 // 自動命名の場合は、ディレクトリ内にあるファイルからタイトル名を類推。
81 String[] files = videoType.getInitFile().list();
83 for (String file : files) {
84 if (file.startsWith(getVideoIdWithBracket())) {
85 fileName = FilenameUtils.getBaseName(file);
90 if (fileName == null) {
91 throw new FileNotFoundException(getVideoIdWithBracket() + "のファイルを特定できませんでした。");
95 String baseName = FilenameUtils.getBaseName(fileName);
96 int s = baseName.indexOf(getVideoIdWithBracket());
97 return baseName.replace(getVideoIdWithBracket(), "");
102 * @param listener 進捗通知を受け取るためのリスナ.
104 * @throws IOException 動画ファイルが存在しない, 取得に失敗した.
106 public final File getVideoFile(TextProgressListener listener) throws IOException {
107 File file = new FileLocator(videoType.isAutoFileName(), videoType.getInitFile(), getVideoIdWithBracket(),
108 getVideoTitle(), ".flv", ".mp4", ".swf").getFile();
109 file = videoFileGetter.get(file, listener);
115 * @param listener 進捗通知を受け取るためのリスナ.
117 * @throws IOException コメントファイルが存在しない, 取得に失敗した.
119 public final File getCommentFile(TextProgressListener listener) throws IOException {
120 File file = new FileLocator(commentType.isAutoFileName(), commentType.getInitFile(), getVideoIdWithBracket(),
121 getVideoTitle(), ".xml").getFile();
122 file = commentFileGetter.get(file, listener);
128 * @param listener 進捗通知を受け取るためのリスナ.
129 * @return 投稿者コメントファイル.
130 * @throws IOException コメントファイルが存在しない, 取得に失敗した.
132 public final File getTcommFile(TextProgressListener listener) throws IOException {
133 File file = new FileLocator(tcommType.isAutoFileName(), tcommType.getInitFile(), getVideoIdWithBracket(),
134 getVideoTitle(), ".txml").getFile();
135 file = tcommFileGetter.get(file, listener);
139 private String getVideoIdWithBracket() {
140 return "[" + videoId + "]";
143 public static class InstanciationType {
145 private final boolean download;
146 private final boolean autoFileName;
147 private final File initFile;
150 * ファイルをインスタンス化する方法を指定するクラス.
152 public InstanciationType(InputFileSetting fileSetting) {
153 this.download = fileSetting.isDownload();
154 this.autoFileName = !fileSetting.getFile().isFile();
155 this.initFile = fileSetting.getFile().getFile();
158 public boolean isDoanload() {
162 public File getInitFile() {
166 public boolean isAutoFileName() {
171 public static class CommentInstanciationType extends InstanciationType {
173 private final boolean autoCommentNum;
174 private final int backComment;
176 public CommentInstanciationType(InputFileSetting fileSetting, boolean autoCommentNum, int backComment) {
178 this.autoCommentNum = autoCommentNum;
179 this.backComment = backComment;
182 public boolean isAutoCommentNum() {
183 return autoCommentNum;
186 public int getBackComment() {