6e0262b43b1c78ac58406f65807cb2399a027031
1 package be
.nikiroo
.fanfix
;
4 import java
.io
.IOException
;
6 import java
.util
.ArrayList
;
7 import java
.util
.Collections
;
8 import java
.util
.HashMap
;
11 import java
.util
.Map
.Entry
;
13 import be
.nikiroo
.fanfix
.bundles
.Config
;
14 import be
.nikiroo
.fanfix
.data
.MetaData
;
15 import be
.nikiroo
.fanfix
.data
.Story
;
16 import be
.nikiroo
.fanfix
.output
.BasicOutput
;
17 import be
.nikiroo
.fanfix
.output
.BasicOutput
.OutputType
;
18 import be
.nikiroo
.fanfix
.supported
.BasicSupport
;
19 import be
.nikiroo
.fanfix
.supported
.BasicSupport
.SupportType
;
20 import be
.nikiroo
.fanfix
.supported
.InfoReader
;
21 import be
.nikiroo
.utils
.IOUtils
;
22 import be
.nikiroo
.utils
.Progress
;
25 * Manage a library of Stories: import, export, list.
27 * Each {@link Story} object will be associated with a (local to the library)
28 * unique ID, the LUID, which will be used to identify the {@link Story}.
32 public class Library
{
34 private Map
<MetaData
, File
> stories
;
36 private OutputType text
;
37 private OutputType image
;
40 * Create a new {@link Library} with the given backend directory.
43 * the directory where to find the {@link Story} objects
45 * the {@link OutputType} to save the text-focused stories into
47 * the {@link OutputType} to save the images-focused stories into
49 public Library(File dir
, OutputType text
, OutputType image
) {
51 this.stories
= new HashMap
<MetaData
, File
>();
60 * List all the known types of stories.
64 public synchronized List
<String
> getTypes() {
65 List
<String
> list
= new ArrayList
<String
>();
66 for (Entry
<MetaData
, File
> entry
: getStories().entrySet()) {
67 String storyType
= entry
.getValue().getParentFile().getName();
68 if (!list
.contains(storyType
)) {
77 * List all the stories of the given source type in the {@link Library}, or
78 * all the stories if NULL is passed as a type.
81 * the type of story to retrieve, or NULL for all
85 public synchronized List
<MetaData
> getList(String type
) {
86 List
<MetaData
> list
= new ArrayList
<MetaData
>();
87 for (Entry
<MetaData
, File
> entry
: getStories().entrySet()) {
88 String storyType
= entry
.getValue().getParentFile().getName();
89 if (type
== null || type
.equalsIgnoreCase(storyType
)) {
90 list
.add(entry
.getKey());
94 Collections
.sort(list
);
99 * Retrieve a {@link File} corresponding to the given {@link Story}.
102 * the Library UID of the story
104 * @return the corresponding {@link Story}
106 public synchronized MetaData
getInfo(String luid
) {
108 for (Entry
<MetaData
, File
> entry
: getStories().entrySet()) {
109 if (luid
.equals(entry
.getKey().getLuid())) {
110 return entry
.getKey();
119 * Retrieve a {@link File} corresponding to the given {@link Story}.
122 * the Library UID of the story
124 * @return the corresponding {@link Story}
126 public synchronized File
getFile(String luid
) {
128 for (Entry
<MetaData
, File
> entry
: getStories().entrySet()) {
129 if (luid
.equals(entry
.getKey().getLuid())) {
130 return entry
.getValue();
139 * Retrieve a specific {@link Story}.
142 * the Library UID of the story
144 * the optional progress reporter
146 * @return the corresponding {@link Story} or NULL if not found
148 public synchronized Story
getStory(String luid
, Progress pg
) {
150 for (Entry
<MetaData
, File
> entry
: getStories().entrySet()) {
151 if (luid
.equals(entry
.getKey().getLuid())) {
153 SupportType type
= SupportType
.valueOfAllOkUC(entry
154 .getKey().getType());
155 URL url
= entry
.getValue().toURI().toURL();
157 return BasicSupport
.getSupport(type
).process(url
,
160 throw new IOException("Unknown type: "
161 + entry
.getKey().getType());
163 } catch (IOException e
) {
164 // We should not have not-supported files in the
166 Instance
.syserr(new IOException(
167 "Cannot load file from library: "
168 + entry
.getValue().getPath(), e
));
183 * Import the {@link Story} at the given {@link URL} into the
187 * the {@link URL} to import
189 * the optional progress reporter
191 * @return the imported {@link Story}
193 * @throws IOException
194 * in case of I/O error
196 public Story
imprt(URL url
, Progress pg
) throws IOException
{
197 BasicSupport support
= BasicSupport
.getSupport(url
);
198 if (support
== null) {
199 throw new IOException("URL not supported: " + url
.toString());
202 return save(support
.process(url
, pg
), null);
206 * Export the {@link Story} to the given target in the given format.
209 * the {@link Story} ID
211 * the {@link OutputType} to transform it to
213 * the target to save to
215 * the optional progress reporter
217 * @return the saved resource (the main saved {@link File})
219 * @throws IOException
220 * in case of I/O error
222 public File
export(String luid
, OutputType type
, String target
, Progress pg
)
224 Progress pgGetStory
= new Progress();
225 Progress pgOut
= new Progress();
228 pg
.addProgress(pgGetStory
, 1);
229 pg
.addProgress(pgOut
, 1);
232 BasicOutput out
= BasicOutput
.getOutput(type
, true);
234 throw new IOException("Output type not supported: " + type
);
237 Story story
= getStory(luid
, pgGetStory
);
239 throw new IOException("Cannot find story to export: " + luid
);
242 return out
.process(story
, target
, pgOut
);
246 * Save a {@link Story} to the {@link Library}.
249 * the {@link Story} to save
251 * the optional progress reporter
253 * @return the same {@link Story}, whose LUID may have changed
255 * @throws IOException
256 * in case of I/O error
258 public Story
save(Story story
, Progress pg
) throws IOException
{
259 return save(story
, null, pg
);
263 * Save a {@link Story} to the {@link Library} -- the LUID <b>must</b> be
264 * correct, or NULL to get the next free one.
267 * the {@link Story} to save
269 * the <b>correct</b> LUID or NULL to get the next free one
271 * the optional progress reporter
273 * @return the same {@link Story}, whose LUID may have changed
275 * @throws IOException
276 * in case of I/O error
278 public synchronized Story
save(Story story
, String luid
, Progress pg
)
280 // Do not change the original metadata, but change the original story
281 MetaData key
= story
.getMeta().clone();
284 if (luid
== null || luid
.isEmpty()) {
285 getStories(); // refresh lastId if needed
286 key
.setLuid(String
.format("%03d", (++lastId
)));
291 getDir(key
).mkdirs();
292 if (!getDir(key
).exists()) {
293 throw new IOException("Cannot create library dir");
297 if (key
!= null && key
.isImageDocument()) {
303 BasicOutput it
= BasicOutput
.getOutput(out
, true);
304 it
.process(story
, getFile(key
).getPath(), pg
);
313 * Delete the given {@link Story} from this {@link Library}.
316 * the LUID of the target {@link Story}
318 * @return TRUE if it was deleted
320 public synchronized boolean delete(String luid
) {
323 MetaData meta
= getInfo(luid
);
324 File file
= getStories().get(meta
);
328 String readerExt
= getOutputType(meta
)
329 .getDefaultExtension(true);
330 String fileExt
= getOutputType(meta
).getDefaultExtension(false);
332 String path
= file
.getAbsolutePath();
333 if (readerExt
!= null && !readerExt
.equals(fileExt
)) {
335 .substring(0, path
.length() - readerExt
.length())
337 file
= new File(path
);
338 IOUtils
.deltree(file
);
341 File infoFile
= new File(path
+ ".info");
342 if (!infoFile
.exists()) {
343 infoFile
= new File(path
.substring(0, path
.length()
349 String coverExt
= "."
350 + Instance
.getConfig().getString(
351 Config
.IMAGE_FORMAT_COVER
);
352 File coverFile
= new File(path
+ coverExt
);
353 if (!coverFile
.exists()) {
354 coverFile
= new File(path
.substring(0, path
.length()
355 - fileExt
.length()));
370 * The directory (full path) where the {@link Story} related to this
371 * {@link MetaData} should be located on disk.
374 * the {@link Story} {@link MetaData}
376 * @return the target directory
378 private File
getDir(MetaData key
) {
379 String source
= key
.getSource().replaceAll("[^a-zA-Z0-9._+-]", "_");
380 return new File(baseDir
, source
);
384 * The target (full path) where the {@link Story} related to this
385 * {@link MetaData} should be located on disk.
388 * the {@link Story} {@link MetaData}
392 private File
getFile(MetaData key
) {
393 String title
= key
.getTitle();
397 title
= title
.replaceAll("[^a-zA-Z0-9._+-]", "_");
398 return new File(getDir(key
), key
.getLuid() + "_" + title
);
402 * Return all the known stories in this {@link Library} object.
404 * @return the stories
406 private synchronized Map
<MetaData
, File
> getStories() {
407 if (stories
.isEmpty()) {
410 String ext
= ".info";
411 for (File dir
: baseDir
.listFiles()) {
412 if (dir
.isDirectory()) {
413 for (File file
: dir
.listFiles()) {
415 if (file
.getPath().toLowerCase().endsWith(ext
)) {
416 MetaData meta
= InfoReader
.readMeta(file
);
418 int id
= Integer
.parseInt(meta
.getLuid());
423 // Replace .info with whatever is needed:
424 String path
= file
.getPath();
425 path
= path
.substring(0, path
.length()
428 String newExt
= getOutputType(meta
)
429 .getDefaultExtension(true);
431 file
= new File(path
+ newExt
);
434 stories
.put(meta
, file
);
436 } catch (Exception e
) {
438 Instance
.syserr(new IOException(
439 "Cannot understand the LUID of "
440 + file
.getPath() + ": "
441 + meta
.getLuid(), e
));
444 } catch (IOException e
) {
445 // We should not have not-supported files in the
447 Instance
.syserr(new IOException(
448 "Cannot load file from library: "
449 + file
.getPath(), e
));
460 * Return the {@link OutputType} for this {@link Story}.
463 * the {@link Story} {@link MetaData}
467 private OutputType
getOutputType(MetaData meta
) {
468 if (meta
!= null && meta
.isImageDocument()) {