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 File
getFile(String luid
) {
88 for (Entry
<MetaData
, File
> entry
: getStories().entrySet()) {
89 if (luid
.equals(entry
.getKey().getLuid())) {
90 return entry
.getValue();
99 * Retrieve a specific {@link Story}.
102 * the Library UID of the story
104 * @return the corresponding {@link Story}
106 public Story
getStory(String luid
) {
108 for (Entry
<MetaData
, File
> entry
: getStories().entrySet()) {
109 if (luid
.equals(entry
.getKey().getLuid())) {
111 SupportType type
= SupportType
.valueOfAllOkUC(entry
112 .getKey().getType());
113 URL url
= entry
.getValue().toURI().toURL();
115 return BasicSupport
.getSupport(type
).process(url
);
117 throw new IOException("Unknown type: "
118 + entry
.getKey().getType());
120 } catch (IOException e
) {
121 // We should not have not-supported files in the
123 Instance
.syserr(new IOException(
124 "Cannot load file from library: "
125 + entry
.getValue().getPath(), e
));
135 * Import the {@link Story} at the given {@link URL} into the
139 * the {@link URL} to import
141 * @return the imported {@link Story}
143 * @throws IOException
144 * in case of I/O error
146 public Story
imprt(URL url
) throws IOException
{
147 BasicSupport support
= BasicSupport
.getSupport(url
);
148 if (support
== null) {
149 throw new IOException("URL not supported: " + url
.toString());
152 return save(support
.process(url
), null);
156 * Export the {@link Story} to the given target in the given format.
159 * the {@link Story} ID
161 * the {@link OutputType} to transform it to
163 * the target to save to
165 * @return the saved resource (the main saved {@link File})
167 * @throws IOException
168 * in case of I/O error
170 public File
export(String luid
, OutputType type
, String target
)
172 BasicOutput out
= BasicOutput
.getOutput(type
, true);
174 throw new IOException("Output type not supported: " + type
);
177 return out
.process(getStory(luid
), target
);
181 * Save a {@link Story} to the {@link Library}.
184 * the {@link Story} to save
186 * @return the same {@link Story}, whose LUID may have changed
188 * @throws IOException
189 * in case of I/O error
191 public Story
save(Story story
) throws IOException
{
192 return save(story
, null);
196 * Save a {@link Story} to the {@link Library} -- the LUID <b>must</b> be
197 * correct, or NULL to get the next free one.
200 * the {@link Story} to save
202 * the <b>correct</b> LUID or NULL to get the next free one
204 * @return the same {@link Story}, whose LUID may have changed
206 * @throws IOException
207 * in case of I/O error
209 private Story
save(Story story
, String luid
) throws IOException
{
210 MetaData key
= story
.getMeta();
212 if (luid
== null || luid
.isEmpty()) {
213 getStories(); // refresh lastId if needed
214 key
.setLuid(String
.format("%03d", (++lastId
)));
219 getDir(key
).mkdirs();
220 if (!getDir(key
).exists()) {
221 throw new IOException("Cannot create library dir");
225 if (key
!= null && key
.isImageDocument()) {
231 BasicOutput it
= BasicOutput
.getOutput(out
, true);
232 File file
= it
.process(story
, getFile(key
).getPath());
233 getStories().put(story
.getMeta(), file
);
239 * The directory (full path) where the {@link Story} related to this
240 * {@link MetaData} should be located on disk.
243 * the {@link Story} {@link MetaData}
245 * @return the target directory
247 private File
getDir(MetaData key
) {
248 String source
= key
.getSource().replaceAll("[^a-zA-Z0-9._+-]", "_");
249 return new File(baseDir
, source
);
253 * The target (full path) where the {@link Story} related to this
254 * {@link MetaData} should be located on disk.
257 * the {@link Story} {@link MetaData}
261 private File
getFile(MetaData key
) {
262 String title
= key
.getTitle().replaceAll("[^a-zA-Z0-9._+-]", "_");
263 return new File(getDir(key
), key
.getLuid() + "_" + title
);
267 * Return all the known stories in this {@link Library} object.
269 * @return the stories
271 private Map
<MetaData
, File
> getStories() {
272 if (stories
.isEmpty()) {
275 String ext
= ".info";
276 for (File dir
: baseDir
.listFiles()) {
277 if (dir
.isDirectory()) {
278 for (File file
: dir
.listFiles()) {
280 if (file
.getPath().toLowerCase().endsWith(ext
)) {
281 MetaData meta
= InfoReader
.readMeta(file
);
283 int id
= Integer
.parseInt(meta
.getLuid());
288 // Replace .info with whatever is needed:
289 String path
= file
.getPath();
290 path
= path
.substring(0, path
.length()
293 String newExt
= getOutputType(meta
)
294 .getDefaultExtension();
296 file
= new File(path
+ newExt
);
299 stories
.put(meta
, file
);
301 } catch (Exception e
) {
303 Instance
.syserr(new IOException(
304 "Cannot understand the LUID of "
305 + file
.getPath() + ": "
306 + meta
.getLuid(), e
));
309 } catch (IOException e
) {
310 // We should not have not-supported files in the
312 Instance
.syserr(new IOException(
313 "Cannot load file from library: "
314 + file
.getPath(), e
));
325 * Return the {@link OutputType} for this {@link Story}.
328 * the {@link Story} {@link MetaData}
332 private OutputType
getOutputType(MetaData meta
) {
333 if (meta
!= null && meta
.isImageDocument()) {