1 package be
.nikiroo
.fanfix
;
4 import java
.io
.IOException
;
6 import java
.util
.ArrayList
;
7 import java
.util
.HashMap
;
10 import java
.util
.Map
.Entry
;
12 import be
.nikiroo
.fanfix
.data
.MetaData
;
13 import be
.nikiroo
.fanfix
.data
.Story
;
14 import be
.nikiroo
.fanfix
.output
.BasicOutput
;
15 import be
.nikiroo
.fanfix
.output
.BasicOutput
.OutputType
;
16 import be
.nikiroo
.fanfix
.supported
.BasicSupport
;
17 import be
.nikiroo
.fanfix
.supported
.BasicSupport
.SupportType
;
18 import be
.nikiroo
.fanfix
.supported
.InfoReader
;
21 * Manage a library of Stories: import, export, list.
23 * Each {@link Story} object will be associated with a (local to the library)
24 * unique ID, the LUID, which will be used to identify the {@link Story}.
28 public class Library
{
30 private Map
<MetaData
, File
> stories
;
32 private OutputType text
;
33 private OutputType image
;
36 * Create a new {@link Library} with the given backend directory.
39 * the directory where to find the {@link Story} objects
41 * the {@link OutputType} to save the text-focused stories into
43 * the {@link OutputType} to save the images-focused stories into
45 public Library(File dir
, OutputType text
, OutputType image
) {
47 this.stories
= new HashMap
<MetaData
, File
>();
56 * List all the known types of stories.
60 public List
<String
> getTypes() {
61 List
<String
> list
= new ArrayList
<String
>();
62 for (Entry
<MetaData
, File
> entry
: getStories().entrySet()) {
63 String storyType
= entry
.getValue().getParentFile().getName();
64 if (!list
.contains(storyType
)) {
73 * List all the stories of the given source type in the {@link Library}, or
74 * all the stories if NULL is passed as a type.
77 * the type of story to retrieve, or NULL for all
81 public List
<MetaData
> getList(String type
) {
82 List
<MetaData
> list
= new ArrayList
<MetaData
>();
83 for (Entry
<MetaData
, File
> entry
: getStories().entrySet()) {
84 String storyType
= entry
.getValue().getParentFile().getName();
85 if (type
== null || type
.equalsIgnoreCase(storyType
)) {
86 list
.add(entry
.getKey());
94 * Retrieve a {@link File} corresponding to the given {@link Story}.
97 * the Library UID of the story
99 * @return the corresponding {@link Story}
101 public MetaData
getInfo(String luid
) {
103 for (Entry
<MetaData
, File
> entry
: getStories().entrySet()) {
104 if (luid
.equals(entry
.getKey().getLuid())) {
105 return entry
.getKey();
114 * Retrieve a {@link File} corresponding to the given {@link Story}.
117 * the Library UID of the story
119 * @return the corresponding {@link Story}
121 public File
getFile(String luid
) {
123 for (Entry
<MetaData
, File
> entry
: getStories().entrySet()) {
124 if (luid
.equals(entry
.getKey().getLuid())) {
125 return entry
.getValue();
134 * Retrieve a specific {@link Story}.
137 * the Library UID of the story
139 * @return the corresponding {@link Story} or NULL if not found
141 public Story
getStory(String luid
) {
143 for (Entry
<MetaData
, File
> entry
: getStories().entrySet()) {
144 if (luid
.equals(entry
.getKey().getLuid())) {
146 SupportType type
= SupportType
.valueOfAllOkUC(entry
147 .getKey().getType());
148 URL url
= entry
.getValue().toURI().toURL();
150 return BasicSupport
.getSupport(type
).process(url
);
152 throw new IOException("Unknown type: "
153 + entry
.getKey().getType());
155 } catch (IOException e
) {
156 // We should not have not-supported files in the
158 Instance
.syserr(new IOException(
159 "Cannot load file from library: "
160 + entry
.getValue().getPath(), e
));
170 * Import the {@link Story} at the given {@link URL} into the
174 * the {@link URL} to import
176 * @return the imported {@link Story}
178 * @throws IOException
179 * in case of I/O error
181 public Story
imprt(URL url
) throws IOException
{
182 BasicSupport support
= BasicSupport
.getSupport(url
);
183 if (support
== null) {
184 throw new IOException("URL not supported: " + url
.toString());
187 return save(support
.process(url
), null);
191 * Export the {@link Story} to the given target in the given format.
194 * the {@link Story} ID
196 * the {@link OutputType} to transform it to
198 * the target to save to
200 * @return the saved resource (the main saved {@link File})
202 * @throws IOException
203 * in case of I/O error
205 public File
export(String luid
, OutputType type
, String target
)
207 BasicOutput out
= BasicOutput
.getOutput(type
, true);
209 throw new IOException("Output type not supported: " + type
);
212 Story story
= getStory(luid
);
214 throw new IOException("Cannot find story to export: " + luid
);
217 return out
.process(getStory(luid
), target
);
221 * Save a {@link Story} to the {@link Library}.
224 * the {@link Story} to save
226 * @return the same {@link Story}, whose LUID may have changed
228 * @throws IOException
229 * in case of I/O error
231 public Story
save(Story story
) throws IOException
{
232 return save(story
, null);
236 * Save a {@link Story} to the {@link Library} -- the LUID <b>must</b> be
237 * correct, or NULL to get the next free one.
240 * the {@link Story} to save
242 * the <b>correct</b> LUID or NULL to get the next free one
244 * @return the same {@link Story}, whose LUID may have changed
246 * @throws IOException
247 * in case of I/O error
249 public Story
save(Story story
, String luid
) throws IOException
{
250 // Do not change the original metadata, but change the original story
251 MetaData key
= story
.getMeta().clone();
254 if (luid
== null || luid
.isEmpty()) {
255 getStories(); // refresh lastId if needed
256 key
.setLuid(String
.format("%03d", (++lastId
)));
261 getDir(key
).mkdirs();
262 if (!getDir(key
).exists()) {
263 throw new IOException("Cannot create library dir");
267 if (key
!= null && key
.isImageDocument()) {
273 BasicOutput it
= BasicOutput
.getOutput(out
, true);
274 File file
= it
.process(story
, getFile(key
).getPath());
275 getStories().put(story
.getMeta(), file
);
281 * The directory (full path) where the {@link Story} related to this
282 * {@link MetaData} should be located on disk.
285 * the {@link Story} {@link MetaData}
287 * @return the target directory
289 private File
getDir(MetaData key
) {
290 String source
= key
.getSource().replaceAll("[^a-zA-Z0-9._+-]", "_");
291 return new File(baseDir
, source
);
295 * The target (full path) where the {@link Story} related to this
296 * {@link MetaData} should be located on disk.
299 * the {@link Story} {@link MetaData}
303 private File
getFile(MetaData key
) {
304 String title
= key
.getTitle().replaceAll("[^a-zA-Z0-9._+-]", "_");
305 return new File(getDir(key
), key
.getLuid() + "_" + title
);
309 * Return all the known stories in this {@link Library} object.
311 * @return the stories
313 private Map
<MetaData
, File
> getStories() {
314 if (stories
.isEmpty()) {
317 String ext
= ".info";
318 for (File dir
: baseDir
.listFiles()) {
319 if (dir
.isDirectory()) {
320 for (File file
: dir
.listFiles()) {
322 if (file
.getPath().toLowerCase().endsWith(ext
)) {
323 MetaData meta
= InfoReader
.readMeta(file
);
325 int id
= Integer
.parseInt(meta
.getLuid());
330 // Replace .info with whatever is needed:
331 String path
= file
.getPath();
332 path
= path
.substring(0, path
.length()
335 String newExt
= getOutputType(meta
)
336 .getDefaultExtension();
338 file
= new File(path
+ newExt
);
341 stories
.put(meta
, file
);
343 } catch (Exception e
) {
345 Instance
.syserr(new IOException(
346 "Cannot understand the LUID of "
347 + file
.getPath() + ": "
348 + meta
.getLuid(), e
));
351 } catch (IOException e
) {
352 // We should not have not-supported files in the
354 Instance
.syserr(new IOException(
355 "Cannot load file from library: "
356 + file
.getPath(), e
));
367 * Return the {@link OutputType} for this {@link Story}.
370 * the {@link Story} {@link MetaData}
374 private OutputType
getOutputType(MetaData meta
) {
375 if (meta
!= null && meta
.isImageDocument()) {