OSDN Git Service

merge.
[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.IOException;
6 import org.apache.commons.io.FilenameUtils;
7 import saccubus.ConvertStopFlag;
8 import saccubus.converter.profile.InputFileSetting;
9 import saccubus.converter.profile.Profile;
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     /** @return \93®\89æ\82Ì\83^\83C\83g\83\8b\95ª\82©\82ç\82È\82¢\8fê\8d\87\82Ínull. */
71     public String getVideoTitle() {
72         String fileName = null;
73         if (!videoType.isAutoFileName()) {
74             // \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é.
75             fileName = videoType.getInitFile().toString();
76         } else {
77             // \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
78             String[] files = videoType.getInitFile().list();
79             if (files != null) {
80                 for (String file : files) {
81                     if (file.startsWith(getVideoIdWithBracket())) {
82                         fileName = FilenameUtils.getBaseName(file);
83                         break;
84                     }
85                 }
86             }
87         }
88
89         if (fileName == null) {
90             return null;
91         }
92         String baseName = FilenameUtils.getBaseName(fileName);
93         int s = baseName.indexOf(getVideoIdWithBracket());
94         return baseName.replace(getVideoIdWithBracket(), "");
95     }
96
97     /**
98      * \93®\89æ\83t\83@\83C\83\8b\82ð\8eæ\93¾\82µ\82Ü\82·.
99      * @param listener \90i\92»\92Ê\92m\82ð\8eó\82¯\8eæ\82é\82½\82ß\82Ì\83\8a\83X\83i.
100      * @return \93®\89æ\83t\83@\83C\83\8b.
101      * @throws IOException \93®\89æ\83t\83@\83C\83\8b\82ª\91\8dÝ\82µ\82È\82¢, \8eæ\93¾\82É\8e¸\94s\82µ\82½.
102      */
103     public final File getVideoFile(TextProgressListener listener) {
104         File file = new FileLocator(videoType.isAutoFileName(), videoType.getInitFile(), getVideoIdWithBracket(),
105                 getVideoTitle(), ".flv").getFile();
106         file = videoFileGetter.get(file, listener);
107         return file;
108     }
109
110     /**
111      * \83R\83\81\83\93\83g\83t\83@\83C\83\8b\82ð\8eæ\93¾\82µ\82Ü\82·.
112      * @param listener \90i\92»\92Ê\92m\82ð\8eó\82¯\8eæ\82é\82½\82ß\82Ì\83\8a\83X\83i.
113      * @return \83R\83\81\83\93\83g\83t\83@\83C\83\8b.
114      * @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½.
115      */
116     public final File getCommentFile(TextProgressListener listener) {
117         File file = new FileLocator(commentType.isAutoFileName(), commentType.getInitFile(), getVideoIdWithBracket(),
118                 getVideoTitle(), ".xml").getFile();
119         file = commentFileGetter.get(file, listener);
120         return file;
121     }
122
123     /**
124      * \93\8a\8de\8eÒ\83R\83\81\83\93\83g\83t\83@\83C\83\8b\82ð\8eæ\93¾\82µ\82Ü\82·.
125      * @param listener \90i\92»\92Ê\92m\82ð\8eó\82¯\8eæ\82é\82½\82ß\82Ì\83\8a\83X\83i.
126      * @return \93\8a\8de\8eÒ\83R\83\81\83\93\83g\83t\83@\83C\83\8b.
127      * @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½.
128      */
129     public final File getTcommFile(TextProgressListener listener) {
130         File file = new FileLocator(tcommType.isAutoFileName(), tcommType.getInitFile(), getVideoIdWithBracket(),
131                 getVideoTitle(), ".xml").getFile();
132         file = tcommFileGetter.get(file, listener);
133         return file;
134     }
135
136     private String getVideoIdWithBracket() {
137         return "[" + videoId + "]";
138     }
139
140     public static class InstanciationType {
141
142         private final boolean download;
143         private final boolean autoFileName;
144         private final File initFile;
145
146         /**
147          * \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.
148          */
149         public InstanciationType(InputFileSetting fileSetting) {
150             this.download = fileSetting.isDownload();
151             this.autoFileName = !fileSetting.getFile().isFile();
152             this.initFile = fileSetting.getFile().getFile();
153         }
154
155         public boolean isDoanload() {
156             return download;
157         }
158
159         public File getInitFile() {
160             return initFile;
161         }
162
163         public boolean isAutoFileName() {
164             return autoFileName;
165         }
166     }
167
168     public static class CommentInstanciationType extends InstanciationType {
169
170         private final boolean autoCommentNum;
171         private final int backComment;
172
173         public CommentInstanciationType(InputFileSetting fileSetting, boolean autoCommentNum, int backComment) {
174             super(fileSetting);
175             this.autoCommentNum = autoCommentNum;
176             this.backComment = backComment;
177         }
178
179         public boolean isAutoCommentNum() {
180             return autoCommentNum;
181         }
182
183         public int getBackComment() {
184             return backComment;
185         }
186     }
187 }