OSDN Git Service

3c92cb68bc200254565c21911b42648b0ebc7f1d
[coroid/inqubus.git] / frontend / src / saccubus / converter / filegetter / FileInstanciator.java
1 /* $Id$ */
2 package saccubus.converter.filegetter;
3
4 import java.io.File;
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;
11
12 /**
13  * ダウンロード処理を全く必要としない場合のファイルインスタンス化クラス.
14  * @author yuki
15  */
16 public class FileInstanciator {
17
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;
25
26     protected void setVideoFileGetter(FileGetter getter) {
27         this.videoFileGetter = getter;
28     }
29
30     protected void setCommentFileGetter(FileGetter getter) {
31         this.commentFileGetter = getter;
32     }
33
34     protected void setTcommFileGetter(FileGetter getter) {
35         this.tcommFileGetter = getter;
36     }
37
38     public static FileInstanciator create(
39             ConvertStopFlag stopFlag,
40             InstanciationType videoType,
41             CommentInstanciationType commentType,
42             InstanciationType tcommType,
43             LoginInfo li,
44             String tag, String time) throws
45             IOException {
46         FileInstanciator getter;
47         if (videoType.isDoanload() || commentType.isDoanload() || tcommType.isDoanload()) {
48             getter = new WebFileInstanciator(stopFlag, videoType, commentType, tcommType, li, tag, time);
49         } else {
50             getter = new FileInstanciator(videoType, commentType, tcommType, tag);
51         }
52         return getter;
53     }
54
55     protected FileInstanciator(
56             InstanciationType videoType,
57             InstanciationType commentType,
58             InstanciationType tcommType,
59             String videoId) {
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);
68     }
69
70     /**
71      * @return 動画のタイトル.
72      * @throws FileNotFoundException ビデオタイトルの自動命名時、参考となるファイルが見つからなかった。
73      */
74     public String getVideoTitle() throws FileNotFoundException {
75         String fileName = null;
76         if (!videoType.isAutoFileName()) {
77             // 動画ファイル名を直接指定している場合は、そのファイル名を基にタイトルを取得する.
78             fileName = videoType.getInitFile().toString();
79         } else {
80             // 自動命名の場合は、ディレクトリ内にあるファイルからタイトル名を類推。
81             String[] files = videoType.getInitFile().list();
82             if (files != null) {
83                 for (String file : files) {
84                     if (file.startsWith(getVideoIdWithBracket())) {
85                         fileName = FilenameUtils.getBaseName(file);
86                         break;
87                     }
88                 }
89             }
90             if (fileName == null) {
91                 throw new FileNotFoundException(getVideoIdWithBracket() + "のファイルを特定できませんでした。");
92             }
93         }
94
95         String baseName = FilenameUtils.getBaseName(fileName);
96         int s = baseName.indexOf(getVideoIdWithBracket());
97         return baseName.replace(getVideoIdWithBracket(), "");
98     }
99
100     /**
101      * 動画ファイルを取得します.
102      * @param listener 進捗通知を受け取るためのリスナ.
103      * @return 動画ファイル.
104      * @throws IOException 動画ファイルが存在しない, 取得に失敗した.
105      */
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);
110         return file;
111     }
112
113     /**
114      * コメントファイルを取得します.
115      * @param listener 進捗通知を受け取るためのリスナ.
116      * @return コメントファイル.
117      * @throws IOException コメントファイルが存在しない, 取得に失敗した.
118      */
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);
123         return file;
124     }
125
126     /**
127      * 投稿者コメントファイルを取得します.
128      * @param listener 進捗通知を受け取るためのリスナ.
129      * @return 投稿者コメントファイル.
130      * @throws IOException コメントファイルが存在しない, 取得に失敗した.
131      */
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);
136         return file;
137     }
138
139     private String getVideoIdWithBracket() {
140         return "[" + videoId + "]";
141     }
142
143     public static class InstanciationType {
144
145         private final boolean download;
146         private final boolean autoFileName;
147         private final File initFile;
148
149         /**
150          * ファイルをインスタンス化する方法を指定するクラス.
151          */
152         public InstanciationType(InputFileSetting fileSetting) {
153             this.download = fileSetting.isDownload();
154             this.autoFileName = !fileSetting.getFile().isFile();
155             this.initFile = fileSetting.getFile().getFile();
156         }
157
158         public boolean isDoanload() {
159             return download;
160         }
161
162         public File getInitFile() {
163             return initFile;
164         }
165
166         public boolean isAutoFileName() {
167             return autoFileName;
168         }
169     }
170
171     public static class CommentInstanciationType extends InstanciationType {
172
173         private final boolean autoCommentNum;
174         private final int backComment;
175
176         public CommentInstanciationType(InputFileSetting fileSetting, boolean autoCommentNum, int backComment) {
177             super(fileSetting);
178             this.autoCommentNum = autoCommentNum;
179             this.backComment = backComment;
180         }
181
182         public boolean isAutoCommentNum() {
183             return autoCommentNum;
184         }
185
186         public int getBackComment() {
187             return backComment;
188         }
189     }
190 }