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
.bundles
.Config
;
13 import be
.nikiroo
.fanfix
.data
.MetaData
;
14 import be
.nikiroo
.fanfix
.data
.Story
;
15 import be
.nikiroo
.fanfix
.output
.BasicOutput
;
16 import be
.nikiroo
.fanfix
.output
.BasicOutput
.OutputType
;
17 import be
.nikiroo
.fanfix
.supported
.BasicSupport
;
18 import be
.nikiroo
.fanfix
.supported
.BasicSupport
.SupportType
;
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
;
31 private BasicSupport itSupport
= BasicSupport
32 .getSupport(SupportType
.INFO_TEXT
);
36 * Create a new {@link Library} with the given backend directory.
39 * the directoy where to find the {@link Story} objects
41 public Library(File dir
) {
43 this.stories
= new HashMap
<MetaData
, File
>();
50 * List all the stories of the given source type in the {@link Library}, or
51 * all the stories if NULL is passed as a type.
54 * the type of story to retrieve, or NULL for all
58 public List
<MetaData
> getList(SupportType type
) {
59 String typeString
= type
== null ?
null : type
.getSourceName();
61 List
<MetaData
> list
= new ArrayList
<MetaData
>();
62 for (Entry
<MetaData
, File
> entry
: getStories().entrySet()) {
63 String storyType
= entry
.getValue().getParentFile().getName();
64 if (typeString
== null || typeString
.equalsIgnoreCase(storyType
)) {
65 list
.add(entry
.getKey());
73 * Retrieve a specific {@link Story}.
76 * the Library UID of the story
78 * @return the corresponding {@link Story}
80 public Story
getStory(String luid
) {
82 for (Entry
<MetaData
, File
> entry
: getStories().entrySet()) {
83 if (luid
.equals(entry
.getKey().getLuid())) {
85 return itSupport
.process(entry
.getValue().toURI()
87 } catch (IOException e
) {
88 // We should not have not-supported files in the
90 Instance
.syserr(new IOException(
91 "Cannot load file from library: "
92 + entry
.getValue().getPath(), e
));
102 * Import the {@link Story} at the given {@link URL} into the
106 * the {@link URL} to import
108 * @return the imported {@link Story}
110 * @throws IOException
111 * in case of I/O error
113 public Story
imprt(URL url
) throws IOException
{
114 BasicSupport support
= BasicSupport
.getSupport(url
);
115 if (support
== null) {
116 throw new IOException("URL not supported: " + url
.toString());
119 getStories(); // refresh lastId
120 Story story
= support
.process(url
);
121 story
.getMeta().setLuid(String
.format("%03d", (++lastId
)));
128 * Export the {@link Story} to the given target in the given format.
131 * the {@link Story} ID
133 * the {@link OutputType} to transform it to
135 * the target to save to
137 * @return the saved resource (the main saved {@link File})
139 * @throws IOException
140 * in case of I/O error
142 public File
export(String luid
, OutputType type
, String target
)
144 BasicOutput out
= BasicOutput
.getOutput(type
, true);
146 throw new IOException("Output type not supported: " + type
);
149 return out
.process(getStory(luid
), target
);
153 * Save a story as-is to the {@link Library} -- the LUID <b>must</b> be
157 * the {@link Story} to save
159 * @throws IOException
160 * in case of I/O error
162 private void save(Story story
) throws IOException
{
163 MetaData key
= story
.getMeta();
165 getDir(key
).mkdirs();
166 if (!getDir(key
).exists()) {
167 throw new IOException("Cannot create library dir");
172 if (key
!= null && key
.isImageDocument()) {
173 in
= SupportType
.CBZ
;
174 out
= OutputType
.CBZ
;
176 in
= SupportType
.INFO_TEXT
;
177 out
= OutputType
.INFO_TEXT
;
179 BasicOutput it
= BasicOutput
.getOutput(out
, true);
180 File file
= it
.process(story
, getFile(key
).getPath());
182 BasicSupport
.getSupport(in
).processMeta(file
.toURI().toURL())
187 * The directory (full path) where the {@link Story} related to this
188 * {@link MetaData} should be located on disk.
191 * the {@link Story} {@link MetaData}
193 * @return the target directory
195 private File
getDir(MetaData key
) {
196 String source
= key
.getSource().replaceAll("[^a-zA-Z0-9._+-]", "_");
197 return new File(baseDir
, source
);
201 * The target (full path) where the {@link Story} related to this
202 * {@link MetaData} should be located on disk.
205 * the {@link Story} {@link MetaData}
209 private File
getFile(MetaData key
) {
210 String title
= key
.getTitle().replaceAll("[^a-zA-Z0-9._+-]", "_");
211 return new File(getDir(key
), key
.getLuid() + "_" + title
);
215 * Return all the known stories in this {@link Library} object.
217 * @return the stories
219 private Map
<MetaData
, File
> getStories() {
220 if (stories
.isEmpty()) {
222 String format
= Instance
.getConfig()
223 .getString(Config
.IMAGE_FORMAT_COVER
).toLowerCase();
224 for (File dir
: baseDir
.listFiles()) {
225 if (dir
.isDirectory()) {
226 for (File file
: dir
.listFiles()) {
228 String path
= file
.getPath().toLowerCase();
229 if (!path
.endsWith(".info")
230 && !path
.endsWith(format
)) {
231 MetaData meta
= itSupport
.processMeta(
232 file
.toURI().toURL()).getMeta();
234 stories
.put(meta
, file
);
236 int id
= Integer
.parseInt(meta
241 } catch (Exception e
) {
243 Instance
.syserr(new IOException(
244 "Cannot understand the LUID of "
245 + file
.getPath() + ": "
246 + meta
.getLuid(), e
));
250 Instance
.syserr(new IOException(
251 "Cannot get metadata for: "
255 } catch (IOException e
) {
256 // We should not have not-supported files in the
258 Instance
.syserr(new IOException(
259 "Cannot load file from library: "
260 + file
.getPath(), e
));