OSDN Git Service

ed33d2119d1ed73ab187fc64d5cd3df74acd84fd
[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.profile.InputFileSetting;
10 import saccubus.net.TextProgressListener;
11
12 /**
13  * \83_\83E\83\93\83\8d\81[\83h\8f\88\97\9d\82ð\91S\82­\95K\97v\82Æ\82µ\82È\82¢\8fê\8d\87\82Ì\83t\83@\83C\83\8b\83C\83\93\83X\83^\83\93\83X\89»\83N\83\89\83X.
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 \93®\89æ\82Ì\83^\83C\83g\83\8b.
72      * @throws FileNotFoundException \83r\83f\83I\83^\83C\83g\83\8b\82Ì\8e©\93®\96½\96¼\8e\9e\81A\8eQ\8dl\82Æ\82È\82é\83t\83@\83C\83\8b\82ª\8c©\82Â\82©\82ç\82È\82©\82Á\82½\81B
73      */
74     public String getVideoTitle() throws FileNotFoundException {
75         String fileName = null;
76         if (!videoType.isAutoFileName()) {
77             // \93®\89æ\83t\83@\83C\83\8b\96¼\82ð\92¼\90Ú\8ew\92è\82µ\82Ä\82¢\82é\8fê\8d\87\82Í\81A\82»\82Ì\83t\83@\83C\83\8b\96¼\82ð\8aî\82É\83^\83C\83g\83\8b\82ð\8eæ\93¾\82·\82é.
78             fileName = videoType.getInitFile().toString();
79         } else {
80             // \8e©\93®\96½\96¼\82Ì\8fê\8d\87\82Í\81A\83f\83B\83\8c\83N\83g\83\8a\93à\82É\82 \82é\83t\83@\83C\83\8b\82©\82ç\83^\83C\83g\83\8b\96¼\82ð\97Þ\90\84\81B
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() + "\82Ì\83t\83@\83C\83\8b\82ð\93Á\92è\82Å\82«\82Ü\82¹\82ñ\82Å\82µ\82½\81B");
92             }
93         }
94
95         String baseName = FilenameUtils.getBaseName(fileName);
96         int s = baseName.indexOf(getVideoIdWithBracket());
97         return baseName.replace(getVideoIdWithBracket(), "");
98     }
99
100     /**
101      * \93®\89æ\83t\83@\83C\83\8b\82ð\8eæ\93¾\82µ\82Ü\82·.
102      * @param listener \90i\92»\92Ê\92m\82ð\8eó\82¯\8eæ\82é\82½\82ß\82Ì\83\8a\83X\83i.
103      * @return \93®\89æ\83t\83@\83C\83\8b.
104      * @throws IOException \93®\89æ\83t\83@\83C\83\8b\82ª\91\8dÝ\82µ\82È\82¢, \8eæ\93¾\82É\8e¸\94s\82µ\82½.
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      * \83R\83\81\83\93\83g\83t\83@\83C\83\8b\82ð\8eæ\93¾\82µ\82Ü\82·.
115      * @param listener \90i\92»\92Ê\92m\82ð\8eó\82¯\8eæ\82é\82½\82ß\82Ì\83\8a\83X\83i.
116      * @return \83R\83\81\83\93\83g\83t\83@\83C\83\8b.
117      * @throws IOException \83R\83\81\83\93\83g\83t\83@\83C\83\8b\82ª\91\8dÝ\82µ\82È\82¢, \8eæ\93¾\82É\8e¸\94s\82µ\82½.
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      * \93\8a\8de\8eÒ\83R\83\81\83\93\83g\83t\83@\83C\83\8b\82ð\8eæ\93¾\82µ\82Ü\82·.
128      * @param listener \90i\92»\92Ê\92m\82ð\8eó\82¯\8eæ\82é\82½\82ß\82Ì\83\8a\83X\83i.
129      * @return \93\8a\8de\8eÒ\83R\83\81\83\93\83g\83t\83@\83C\83\8b.
130      * @throws IOException \83R\83\81\83\93\83g\83t\83@\83C\83\8b\82ª\91\8dÝ\82µ\82È\82¢, \8eæ\93¾\82É\8e¸\94s\82µ\82½.
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          * \83t\83@\83C\83\8b\82ð\83C\83\93\83X\83^\83\93\83X\89»\82·\82é\95û\96@\82ð\8ew\92è\82·\82é\83N\83\89\83X.
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 }