1 package be
.nikiroo
.fanfix
;
4 import java
.io
.IOException
;
5 import java
.net
.MalformedURLException
;
8 import be
.nikiroo
.fanfix
.bundles
.StringId
;
9 import be
.nikiroo
.fanfix
.data
.Chapter
;
10 import be
.nikiroo
.fanfix
.data
.Story
;
11 import be
.nikiroo
.fanfix
.output
.BasicOutput
;
12 import be
.nikiroo
.fanfix
.output
.BasicOutput
.OutputType
;
13 import be
.nikiroo
.fanfix
.reader
.BasicReader
;
14 import be
.nikiroo
.fanfix
.reader
.BasicReader
.ReaderType
;
15 import be
.nikiroo
.fanfix
.supported
.BasicSupport
;
16 import be
.nikiroo
.fanfix
.supported
.BasicSupport
.SupportType
;
19 * Main program entry point.
24 private enum MainAction
{
25 IMPORT
, EXPORT
, CONVERT
, READ
, READ_URL
, LIST
, HELP
, SET_READER
29 * Main program entry point.
31 * Known environment variables:
33 * <li>NOUTF: if set to 1 or 'true', the program will prefer non-unicode
34 * {@link String}s when possible</li>
35 * <li>CONFIG_DIR: a path where to look for the <tt>.properties</tt> files
36 * before taking the included ones; they will also be saved/updated into
37 * this path when the program starts</li>
38 * <li>DEBUG: if set to 1 or 'true', the program will override the DEBUG_ERR
39 * configuration value with 'true'</li>
43 * <li>--import [URL]: import into library</li>
44 * <li>--export [id] [output_type] [target]: export story to target</li>
45 * <li>--convert [URL] [output_type] [target] (+info): convert URL into
47 * <li>--read [id] ([chapter number]): read the given story from the library
49 * <li>--read-url [URL] ([cahpter number]): convert on the fly and read the
50 * story, without saving it</li>
51 * <li>--list: list the stories present in the library</li>
52 * <li>--set-reader [reader type]: set the reader type to CLI or LOCAL for
57 * see method description
59 public static void main(String
[] args
) {
60 String urlString
= null;
62 String typeString
= null;
63 String chapString
= null;
65 String readerTypeString
= null;
66 MainAction action
= null;
67 Boolean plusInfo
= null;
69 boolean noMoreActions
= false;
72 for (int i
= 0; exitCode
== 0 && i
< args
.length
; i
++) {
73 // Action (--) handling:
74 if (!noMoreActions
&& args
[i
].startsWith("--")) {
75 if (args
[i
].equals("--")) {
79 action
= MainAction
.valueOf(args
[i
].substring(2)
80 .toUpperCase().replace("-", "_"));
81 } catch (Exception e
) {
82 Instance
.syserr(new IllegalArgumentException(
83 "Unknown action: " + args
[i
], e
));
93 if (urlString
== null) {
102 } else if (typeString
== null) {
103 typeString
= args
[i
];
104 } else if (target
== null) {
111 if (urlString
== null) {
113 } else if (typeString
== null) {
114 typeString
= args
[i
];
115 } else if (target
== null) {
117 } else if (plusInfo
== null) {
118 if ("+info".equals(args
[i
])) {
128 if (typeString
== null) {
129 typeString
= args
[i
];
137 } else if (chapString
== null) {
138 chapString
= args
[i
];
144 if (urlString
== null) {
146 } else if (chapString
== null) {
147 chapString
= args
[i
];
156 if (readerTypeString
== null) {
157 readerTypeString
= args
[i
];
165 if (exitCode
!= 255) {
168 exitCode
= imprt(urlString
);
171 exitCode
= export(urlString
, typeString
, target
);
174 exitCode
= convert(urlString
, typeString
, target
,
175 plusInfo
== null ?
false : plusInfo
);
178 exitCode
= list(typeString
);
181 exitCode
= read(luid
, chapString
, true);
184 exitCode
= read(urlString
, chapString
, false);
191 exitCode
= setReaderType(readerTypeString
);
196 if (exitCode
== 255) {
201 System
.exit(exitCode
);
206 * Return an {@link URL} from this {@link String}, be it a file path or an
207 * actual {@link URL}.
209 * @param sourceString
212 * @return the corresponding {@link URL}
214 * @throws MalformedURLException
215 * if this is neither a file nor a conventional {@link URL}
217 private static URL
getUrl(String sourceString
) throws MalformedURLException
{
218 if (sourceString
== null || sourceString
.isEmpty()) {
219 throw new MalformedURLException("Empty url");
224 source
= new URL(sourceString
);
225 } catch (MalformedURLException e
) {
226 File sourceFile
= new File(sourceString
);
227 source
= sourceFile
.toURI().toURL();
234 * Import the given resource into the {@link Library}.
237 * the resource to import
239 * @return the exit return code (0 = success)
241 private static int imprt(String urlString
) {
243 Story story
= Instance
.getLibrary().imprt(getUrl(urlString
));
244 System
.out
.println(story
.getMeta().getLuid() + ": \""
245 + story
.getMeta().getTitle() + "\" imported.");
246 } catch (IOException e
) {
255 * Export the {@link Story} from the {@link Library} to the given target.
260 * the {@link OutputType} to use
264 * @return the exit return code (0 = success)
266 private static int export(String urlString
, String typeString
, String target
) {
267 OutputType type
= OutputType
.valueOfNullOkUC(typeString
);
269 Instance
.syserr(new Exception(trans(StringId
.OUTPUT_DESC
,
275 Story story
= Instance
.getLibrary().imprt(new URL(urlString
));
276 Instance
.getLibrary().export(story
.getMeta().getLuid(), type
,
278 } catch (IOException e
) {
287 * List the stories of the given type from the {@link Library} (unless NULL
288 * is passed, in which case all stories will be listed).
291 * the {@link SupportType} to list the known stories of, or NULL
292 * to list all stories
294 * @return the exit return code (0 = success)
296 private static int list(String typeString
) {
297 SupportType type
= null;
299 type
= SupportType
.valueOfNullOkUC(typeString
);
300 } catch (Exception e
) {
301 Instance
.syserr(new Exception(
302 trans(StringId
.INPUT_DESC
, typeString
), e
));
306 BasicReader
.getReader().start(type
);
312 * Start the CLI reader for this {@link Story}.
315 * the LUID of the {@link Story} in the {@link Library} <b>or</b>
316 * the {@link Story} {@link URL}
318 * which {@link Chapter} to read (starting at 1), or NULL to get
319 * the {@link Story} description
321 * TRUE if the source is the {@link Story} LUID, FALSE if it is a
324 * @return the exit return code (0 = success)
326 private static int read(String story
, String chapString
, boolean library
) {
328 BasicReader reader
= BasicReader
.getReader();
330 reader
.setStory(story
);
332 reader
.setStory(getUrl(story
));
335 if (chapString
!= null) {
337 reader
.read(Integer
.parseInt(chapString
));
338 } catch (NumberFormatException e
) {
339 Instance
.syserr(new IOException(
340 "Chapter number cannot be parsed: " + chapString
, e
));
346 } catch (IOException e
) {
355 * Convert the {@link Story} into another format.
358 * the source {@link Story} to convert
360 * the {@link OutputType} to convert to
364 * TRUE to also export the cover and info file, even if the given
365 * {@link OutputType} does not usually save them
367 * @return the exit return code (0 = success)
369 private static int convert(String urlString
, String typeString
,
370 String target
, boolean infoCover
) {
373 String sourceName
= urlString
;
375 URL source
= getUrl(urlString
);
376 sourceName
= source
.toString();
377 if (source
.toString().startsWith("file://")) {
378 sourceName
= sourceName
.substring("file://".length());
381 OutputType type
= OutputType
.valueOfAllOkUC(typeString
);
383 Instance
.syserr(new IOException(trans(
384 StringId
.ERR_BAD_OUTPUT_TYPE
, typeString
)));
389 BasicSupport support
= BasicSupport
.getSupport(source
);
390 if (support
!= null) {
391 Story story
= support
.process(source
);
394 target
= new File(target
).getAbsolutePath();
395 BasicOutput
.getOutput(type
, infoCover
).process(
397 } catch (IOException e
) {
398 Instance
.syserr(new IOException(trans(
399 StringId
.ERR_SAVING
, target
), e
));
403 Instance
.syserr(new IOException(trans(
404 StringId
.ERR_NOT_SUPPORTED
, source
)));
408 } catch (IOException e
) {
409 Instance
.syserr(new IOException(trans(StringId
.ERR_LOADING
,
414 } catch (MalformedURLException e
) {
415 Instance
.syserr(new IOException(trans(StringId
.ERR_BAD_URL
,
424 * Simple shortcut method to call {link Instance#getTrans()#getString()}.
427 * the ID to translate
429 * @return the translated result
431 private static String
trans(StringId id
, Object
... params
) {
432 return Instance
.getTrans().getString(id
, params
);
436 * Display the correct syntax of the program to the user to stdout, or an
437 * error message if the syntax used was wrong on stderr.
440 * TRUE to show the syntax help, FALSE to show "syntax error"
442 private static void syntax(boolean showHelp
) {
444 StringBuilder builder
= new StringBuilder();
445 for (SupportType type
: SupportType
.values()) {
446 builder
.append(trans(StringId
.ERR_SYNTAX_TYPE
, type
.toString(),
448 builder
.append('\n');
451 String typesIn
= builder
.toString();
452 builder
.setLength(0);
454 for (OutputType type
: OutputType
.values()) {
455 builder
.append(trans(StringId
.ERR_SYNTAX_TYPE
, type
.toString(),
457 builder
.append('\n');
460 String typesOut
= builder
.toString();
462 System
.out
.println(trans(StringId
.HELP_SYNTAX
, typesIn
, typesOut
));
464 System
.err
.println(trans(StringId
.ERR_SYNTAX
));
469 * Set the default reader type for this session only (it can be changed in
470 * the configuration file, too, but this value will override it).
472 * @param readerTypeString
475 private static int setReaderType(String readerTypeString
) {
477 ReaderType readerType
= ReaderType
.valueOf(readerTypeString
);
478 BasicReader
.setDefaultReaderType(readerType
);
480 } catch (IllegalArgumentException e
) {
481 Instance
.syserr(new IOException("Unknown reader type: "
482 + readerTypeString
, e
));