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 yukihane.saccubus.converter.profile.InputFileSetting;
10 import saccubus.net.TextProgressListener;
11 import yukihane.saccubus.converter.profile.LoinProfile;
12 import yukihane.saccubus.converter.profile.ProxyProfile;
15 * ダウンロード処理を全く必要としない場合のファイルインスタンス化クラス.
18 public class FileInstanciator {
20 private final InstanciationType videoType;
21 private final InstanciationType commentType;
22 private final InstanciationType tcommType;
23 private final String videoId;
24 private FileGetter videoFileGetter;
25 private FileGetter commentFileGetter;
26 private FileGetter tcommFileGetter;
28 protected void setVideoFileGetter(FileGetter getter) {
29 this.videoFileGetter = getter;
32 protected void setCommentFileGetter(FileGetter getter) {
33 this.commentFileGetter = getter;
36 protected void setTcommFileGetter(FileGetter getter) {
37 this.tcommFileGetter = getter;
40 public static FileInstanciator create(
41 ConvertStopFlag stopFlag,
42 InstanciationType videoType,
43 CommentInstanciationType commentType,
44 InstanciationType tcommType,
50 FileInstanciator getter;
51 if (videoType.isDownload() || commentType.isDownload() || tcommType.isDownload()) {
52 getter = new WebFileInstanciator(stopFlag, videoType, commentType, tcommType, li, proxy, tag, time);
54 getter = new FileInstanciator(videoType, commentType, tcommType, tag);
59 protected FileInstanciator(
60 InstanciationType videoType,
61 InstanciationType commentType,
62 InstanciationType tcommType,
64 this.videoType = videoType;
65 this.commentType = commentType;
66 this.tcommType = tcommType;
67 this.videoId = videoId;
68 FileGetter getter = new FileGetter();
69 setVideoFileGetter(getter);
70 setCommentFileGetter(getter);
71 setTcommFileGetter(getter);
76 * @throws FileNotFoundException ビデオタイトルの自動命名時、参考となるファイルが見つからなかった。
78 public String getVideoTitle() throws FileNotFoundException {
79 String fileName = null;
80 if (!videoType.isAutoFileName()) {
81 // 動画ファイル名を直接指定している場合は、そのファイル名を基にタイトルを取得する.
82 fileName = videoType.getInitFile().toString();
84 // 自動命名の場合は、ディレクトリ内にあるファイルからタイトル名を類推。
85 String[] files = videoType.getInitFile().list();
87 for (String file : files) {
88 if (file.startsWith(getVideoIdWithBracket())) {
89 fileName = FilenameUtils.getBaseName(file);
94 if (fileName == null) {
95 throw new FileNotFoundException(getVideoIdWithBracket() + "のファイルを特定できませんでした。");
99 String baseName = FilenameUtils.getBaseName(fileName);
100 int s = baseName.indexOf(getVideoIdWithBracket());
101 return baseName.replace(getVideoIdWithBracket(), "");
106 * @param listener 進捗通知を受け取るためのリスナ.
108 * @throws IOException 動画ファイルが存在しない, 取得に失敗した.
110 public final File getVideoFile(TextProgressListener listener) throws IOException {
111 File file = new FileLocator(videoType.isAutoFileName(), videoType.getInitFile(), getVideoIdWithBracket(),
112 getVideoTitle(), ".flv", ".mp4", ".swf").getFile();
113 file = videoFileGetter.get(file, listener);
119 * @param listener 進捗通知を受け取るためのリスナ.
121 * @throws IOException コメントファイルが存在しない, 取得に失敗した.
123 public final File getCommentFile(TextProgressListener listener) throws IOException {
124 File file = new FileLocator(commentType.isAutoFileName(), commentType.getInitFile(), getVideoIdWithBracket(),
125 getVideoTitle(), ".xml").getFile();
126 file = commentFileGetter.get(file, listener);
132 * @param listener 進捗通知を受け取るためのリスナ.
133 * @return 投稿者コメントファイル.
134 * @throws IOException コメントファイルが存在しない, 取得に失敗した.
136 public final File getTcommFile(TextProgressListener listener) throws IOException {
137 File file = new FileLocator(tcommType.isAutoFileName(), tcommType.getInitFile(), getVideoIdWithBracket(),
138 getVideoTitle(), ".xml").getFile();
139 file = tcommFileGetter.get(file, listener);
143 private String getVideoIdWithBracket() {
144 return "[" + videoId + "]";
147 public static class InstanciationType {
149 private final boolean download;
150 private final boolean autoFileName;
151 private final File initFile;
154 * ファイルをインスタンス化する方法を指定するクラス.
156 public InstanciationType(InputFileSetting fileSetting) {
157 this.download = fileSetting.isDownload();
158 this.autoFileName = !fileSetting.getFile().isFile();
159 this.initFile = fileSetting.getFile().getFile();
162 public boolean isDownload() {
166 public File getInitFile() {
170 public boolean isAutoFileName() {
175 public static class CommentInstanciationType extends InstanciationType {
177 private final boolean autoCommentNum;
178 private final int backComment;
179 private final boolean reduceComment;
181 public CommentInstanciationType(InputFileSetting fileSetting, boolean autoCommentNum, int backComment,
182 boolean reduceComment) {
184 this.autoCommentNum = autoCommentNum;
185 this.backComment = backComment;
186 this.reduceComment = reduceComment;
189 public boolean isAutoCommentNum() {
190 return autoCommentNum;
193 public int getBackComment() {
197 boolean isReduceComment() {
198 return reduceComment;