15b81e44db7aca3caf8869f8f0b53539e1ddb8aa
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 stories of the given source type in the {@link Library}, or
57 * all the stories if NULL is passed as a type.
60 * the type of story to retrieve, or NULL for all
64 public List
<MetaData
> getList(SupportType type
) {
65 String typeString
= type
== null ?
null : type
.getSourceName();
67 List
<MetaData
> list
= new ArrayList
<MetaData
>();
68 for (Entry
<MetaData
, File
> entry
: getStories().entrySet()) {
69 String storyType
= entry
.getValue().getParentFile().getName();
70 if (typeString
== null || typeString
.equalsIgnoreCase(storyType
)) {
71 list
.add(entry
.getKey());
79 * Retrieve a {@link File} corresponding to the given {@link Story}.
82 * the Library UID of the story
84 * @return the corresponding {@link Story}
86 public MetaData
getInfo(String luid
) {
88 for (Entry
<MetaData
, File
> entry
: getStories().entrySet()) {
89 if (luid
.equals(entry
.getKey().getLuid())) {
90 return entry
.getKey();
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 File
getFile(String luid
) {
108 for (Entry
<MetaData
, File
> entry
: getStories().entrySet()) {
109 if (luid
.equals(entry
.getKey().getLuid())) {
110 return entry
.getValue();
119 * Retrieve a specific {@link Story}.
122 * the Library UID of the story
124 * @return the corresponding {@link Story}
126 public Story
getStory(String luid
) {
128 for (Entry
<MetaData
, File
> entry
: getStories().entrySet()) {
129 if (luid
.equals(entry
.getKey().getLuid())) {
131 SupportType type
= SupportType
.valueOfAllOkUC(entry
132 .getKey().getType());
133 URL url
= entry
.getValue().toURI().toURL();
135 return BasicSupport
.getSupport(type
).process(url
);
137 throw new IOException("Unknown type: "
138 + entry
.getKey().getType());
140 } catch (IOException e
) {
141 // We should not have not-supported files in the
143 Instance
.syserr(new IOException(
144 "Cannot load file from library: "
145 + entry
.getValue().getPath(), e
));
155 * Import the {@link Story} at the given {@link URL} into the
159 * the {@link URL} to import
161 * @return the imported {@link Story}
163 * @throws IOException
164 * in case of I/O error
166 public Story
imprt(URL url
) throws IOException
{
167 BasicSupport support
= BasicSupport
.getSupport(url
);
168 if (support
== null) {
169 throw new IOException("URL not supported: " + url
.toString());
172 return save(support
.process(url
), null);
176 * Export the {@link Story} to the given target in the given format.
179 * the {@link Story} ID
181 * the {@link OutputType} to transform it to
183 * the target to save to
185 * @return the saved resource (the main saved {@link File})
187 * @throws IOException
188 * in case of I/O error
190 public File
export(String luid
, OutputType type
, String target
)
192 BasicOutput out
= BasicOutput
.getOutput(type
, true);
194 throw new IOException("Output type not supported: " + type
);
197 Story story
= getStory(luid
);
199 throw new IOException("Cannot find story to export: " + luid
);
202 return out
.process(getStory(luid
), target
);
206 * Save a {@link Story} to the {@link Library}.
209 * the {@link Story} to save
211 * @return the same {@link Story}, whose LUID may have changed
213 * @throws IOException
214 * in case of I/O error
216 public Story
save(Story story
) throws IOException
{
217 return save(story
, null);
221 * Save a {@link Story} to the {@link Library} -- the LUID <b>must</b> be
222 * correct, or NULL to get the next free one.
225 * the {@link Story} to save
227 * the <b>correct</b> LUID or NULL to get the next free one
229 * @return the same {@link Story}, whose LUID may have changed
231 * @throws IOException
232 * in case of I/O error
234 private Story
save(Story story
, String luid
) throws IOException
{
235 // Do not change the original metadata, but change the original story
236 MetaData key
= story
.getMeta().clone();
239 if (luid
== null || luid
.isEmpty()) {
240 getStories(); // refresh lastId if needed
241 key
.setLuid(String
.format("%03d", (++lastId
)));
246 getDir(key
).mkdirs();
247 if (!getDir(key
).exists()) {
248 throw new IOException("Cannot create library dir");
252 if (key
!= null && key
.isImageDocument()) {
258 BasicOutput it
= BasicOutput
.getOutput(out
, true);
259 File file
= it
.process(story
, getFile(key
).getPath());
260 getStories().put(story
.getMeta(), file
);
266 * The directory (full path) where the {@link Story} related to this
267 * {@link MetaData} should be located on disk.
270 * the {@link Story} {@link MetaData}
272 * @return the target directory
274 private File
getDir(MetaData key
) {
275 String source
= key
.getSource().replaceAll("[^a-zA-Z0-9._+-]", "_");
276 return new File(baseDir
, source
);
280 * The target (full path) where the {@link Story} related to this
281 * {@link MetaData} should be located on disk.
284 * the {@link Story} {@link MetaData}
288 private File
getFile(MetaData key
) {
289 String title
= key
.getTitle().replaceAll("[^a-zA-Z0-9._+-]", "_");
290 return new File(getDir(key
), key
.getLuid() + "_" + title
);
294 * Return all the known stories in this {@link Library} object.
296 * @return the stories
298 private Map
<MetaData
, File
> getStories() {
299 if (stories
.isEmpty()) {
302 String ext
= ".info";
303 for (File dir
: baseDir
.listFiles()) {
304 if (dir
.isDirectory()) {
305 for (File file
: dir
.listFiles()) {
307 if (file
.getPath().toLowerCase().endsWith(ext
)) {
308 MetaData meta
= InfoReader
.readMeta(file
);
310 int id
= Integer
.parseInt(meta
.getLuid());
315 // Replace .info with whatever is needed:
316 String path
= file
.getPath();
317 path
= path
.substring(0, path
.length()
320 String newExt
= getOutputType(meta
)
321 .getDefaultExtension();
323 file
= new File(path
+ newExt
);
326 stories
.put(meta
, file
);
328 } catch (Exception e
) {
330 Instance
.syserr(new IOException(
331 "Cannot understand the LUID of "
332 + file
.getPath() + ": "
333 + meta
.getLuid(), e
));
336 } catch (IOException e
) {
337 // We should not have not-supported files in the
339 Instance
.syserr(new IOException(
340 "Cannot load file from library: "
341 + file
.getPath(), e
));
352 * Return the {@link OutputType} for this {@link Story}.
355 * the {@link Story} {@link MetaData}
359 private OutputType
getOutputType(MetaData meta
) {
360 if (meta
!= null && meta
.isImageDocument()) {