OSDN Git Service

merge.
[coroid/inqubus.git] / frontend / src / saccubus / filegetter / FileInstanciator.java
1 /* $Id$ */
2 package saccubus.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.VideoSaveKind;
9 import saccubus.net.TextProgressListener;
10
11 /**
12  * \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.
13  * @author yuki
14  */
15 public class FileInstanciator {
16
17     private final InstanciationType<VideoSaveKind> videoType;
18     private final InstanciationType<Boolean> commentType;
19     private final InstanciationType<Boolean> tcommType;
20     private final String videoId;
21     private FileGetter videoFileGetter;
22     private FileGetter commentFileGetter;
23     private FileGetter tcommFileGetter;
24
25     protected void setVideoFileGetter(FileGetter getter) {
26         this.videoFileGetter = getter;
27     }
28
29     protected void setCommentFileGetter(FileGetter getter) {
30         this.commentFileGetter = getter;
31     }
32
33     protected void setTcommFileGetter(FileGetter getter) {
34         this.tcommFileGetter = getter;
35     }
36
37     public static FileInstanciator create(
38             ConvertStopFlag stopFlag,
39             InstanciationType<VideoSaveKind> videoType,
40             CommentInstanciationType commentType,
41             InstanciationType<Boolean> tcommType,
42             LoginInfo li,
43             String tag, String time) throws
44             IOException {
45         FileInstanciator getter;
46         if (videoType.getFileType() == VideoSaveKind.SAVE || commentType.getFileType().booleanValue() || tcommType.
47                 getFileType().booleanValue()) {
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<VideoSaveKind> videoType,
57             InstanciationType<Boolean> commentType,
58             InstanciationType<Boolean> 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() || videoType.getFileType() == VideoSaveKind.NICOBROWSER) {
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.substring(s + 1);
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 VideoFileLocator(videoType.getFileType(), videoType.isAutoFileName(), videoType.getInitFile(),
105                 getVideoIdWithBracket(), 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<T> {
141
142         private final T fileType;
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          * @param fileType \83t\83@\83C\83\8b\82Ì\8eí\97Þ\82ð\8ew\92è\82·\82é.
149          * @param autoFileName \83t\83@\83C\83\8b\96¼\82ð\8e©\93®\96½\96¼\82·\82é\82È\82çtrue, \92¼\90Ú\8ew\92è\82·\82é\82È\82çfalse.
150          * @param initFileName \83t\83@\83C\83\8b\8fî\95ñ. autoFileName\82ªtrue\82Å\82 \82ê\82Î\95Û\91\82·\82é\83f\83B\83\8c\83N\83g\83\8a\96¼\82ð, false\82È\82ç\95Û\91\82·\82é\83t\83@\83C\83\8b\96¼\82ð\8ew\92è\82·\82é.
151          */
152         public InstanciationType(T fileType, boolean autoFileName, File initFileName) {
153             this.fileType = fileType;
154             this.autoFileName = autoFileName;
155             this.initFile = initFileName;
156         }
157
158         public T getFileType() {
159             return fileType;
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<Boolean> {
172
173         private final boolean autoCommentNum;
174         private final String backComment;
175
176         public CommentInstanciationType(Boolean fileType, boolean autoFileName, File initFileName,
177                 boolean autoCommentNum, String backComment) {
178             super(fileType, autoFileName, initFileName);
179             this.autoCommentNum = autoCommentNum;
180             this.backComment = backComment;
181         }
182
183         public boolean isAutoCommentNum() {
184             return autoCommentNum;
185         }
186
187         public String getBackComment() {
188             return backComment;
189         }
190     }
191 }