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
, START
,
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] ([chapter number]): convert on the fly and read the
50 * story, without saving it</li>
51 * <li>--list ([type]): 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 MainAction action
= MainAction
.START
;
66 Boolean plusInfo
= null;
68 boolean noMoreActions
= false;
71 for (int i
= 0; exitCode
== 0 && i
< args
.length
; i
++) {
72 // Action (--) handling:
73 if (!noMoreActions
&& args
[i
].startsWith("--")) {
74 if (args
[i
].equals("--")) {
78 action
= MainAction
.valueOf(args
[i
].substring(2)
79 .toUpperCase().replace("-", "_"));
80 } catch (Exception e
) {
81 Instance
.syserr(new IllegalArgumentException(
82 "Unknown action: " + args
[i
], e
));
92 if (urlString
== null) {
101 } else if (typeString
== null) {
102 typeString
= args
[i
];
103 } else if (target
== null) {
110 if (urlString
== null) {
112 } else if (typeString
== null) {
113 typeString
= args
[i
];
114 } else if (target
== null) {
116 } else if (plusInfo
== null) {
117 if ("+info".equals(args
[i
])) {
127 if (typeString
== null) {
128 typeString
= args
[i
];
136 } else if (chapString
== null) {
137 chapString
= args
[i
];
143 if (urlString
== null) {
145 } else if (chapString
== null) {
146 chapString
= args
[i
];
155 exitCode
= setReaderType(args
[i
]);
158 exitCode
= 255; // not supposed to be selected by user
163 if (exitCode
!= 255) {
166 exitCode
= imprt(urlString
);
169 exitCode
= export(luid
, typeString
, target
);
172 exitCode
= convert(urlString
, typeString
, target
,
173 plusInfo
== null ?
false : plusInfo
);
176 exitCode
= list(typeString
);
179 exitCode
= read(luid
, chapString
, true);
182 exitCode
= read(urlString
, chapString
, false);
191 BasicReader
.setDefaultReaderType(ReaderType
.LOCAL
);
192 BasicReader
.getReader().start(null);
197 if (exitCode
== 255) {
202 System
.exit(exitCode
);
207 * Return an {@link URL} from this {@link String}, be it a file path or an
208 * actual {@link URL}.
210 * @param sourceString
213 * @return the corresponding {@link URL}
215 * @throws MalformedURLException
216 * if this is neither a file nor a conventional {@link URL}
218 private static URL
getUrl(String sourceString
) throws MalformedURLException
{
219 if (sourceString
== null || sourceString
.isEmpty()) {
220 throw new MalformedURLException("Empty url");
225 source
= new URL(sourceString
);
226 } catch (MalformedURLException e
) {
227 File sourceFile
= new File(sourceString
);
228 source
= sourceFile
.toURI().toURL();
235 * Import the given resource into the {@link Library}.
238 * the resource to import
240 * @return the exit return code (0 = success)
242 public static int imprt(String urlString
) {
244 Story story
= Instance
.getLibrary().imprt(getUrl(urlString
));
245 System
.out
.println(story
.getMeta().getLuid() + ": \""
246 + story
.getMeta().getTitle() + "\" imported.");
247 } catch (IOException e
) {
256 * Export the {@link Story} from the {@link Library} to the given target.
261 * the {@link OutputType} to use
265 * @return the exit return code (0 = success)
267 public static int export(String luid
, String typeString
, String target
) {
268 OutputType type
= OutputType
.valueOfNullOkUC(typeString
);
270 Instance
.syserr(new Exception(trans(StringId
.OUTPUT_DESC
,
276 Instance
.getLibrary().export(luid
, type
, target
);
277 } catch (IOException e
) {
286 * List the stories of the given type from the {@link Library} (unless NULL
287 * is passed, in which case all stories will be listed).
290 * the type to list the known stories of, or NULL to list all
293 * @return the exit return code (0 = success)
295 private static int list(String type
) {
296 BasicReader
.getReader().start(type
);
301 * Start the CLI reader for this {@link Story}.
304 * the LUID of the {@link Story} in the {@link Library} <b>or</b>
305 * the {@link Story} {@link URL}
307 * which {@link Chapter} to read (starting at 1), or NULL to get
308 * the {@link Story} description
310 * TRUE if the source is the {@link Story} LUID, FALSE if it is a
313 * @return the exit return code (0 = success)
315 private static int read(String story
, String chapString
, boolean library
) {
317 BasicReader reader
= BasicReader
.getReader();
319 reader
.setStory(story
);
321 reader
.setStory(getUrl(story
));
324 if (chapString
!= null) {
326 reader
.read(Integer
.parseInt(chapString
));
327 } catch (NumberFormatException e
) {
328 Instance
.syserr(new IOException(
329 "Chapter number cannot be parsed: " + chapString
, e
));
335 } catch (IOException e
) {
344 * Convert the {@link Story} into another format.
347 * the source {@link Story} to convert
349 * the {@link OutputType} to convert to
353 * TRUE to also export the cover and info file, even if the given
354 * {@link OutputType} does not usually save them
356 * @return the exit return code (0 = success)
358 private static int convert(String urlString
, String typeString
,
359 String target
, boolean infoCover
) {
362 String sourceName
= urlString
;
364 URL source
= getUrl(urlString
);
365 sourceName
= source
.toString();
366 if (source
.toString().startsWith("file://")) {
367 sourceName
= sourceName
.substring("file://".length());
370 OutputType type
= OutputType
.valueOfAllOkUC(typeString
);
372 Instance
.syserr(new IOException(trans(
373 StringId
.ERR_BAD_OUTPUT_TYPE
, typeString
)));
378 BasicSupport support
= BasicSupport
.getSupport(source
);
380 if (support
!= null) {
381 Story story
= support
.process(source
);
384 target
= new File(target
).getAbsolutePath();
385 BasicOutput
.getOutput(type
, infoCover
).process(
387 } catch (IOException e
) {
388 Instance
.syserr(new IOException(trans(
389 StringId
.ERR_SAVING
, target
), e
));
393 Instance
.syserr(new IOException(trans(
394 StringId
.ERR_NOT_SUPPORTED
, source
)));
398 } catch (IOException e
) {
399 Instance
.syserr(new IOException(trans(StringId
.ERR_LOADING
,
404 } catch (MalformedURLException e
) {
405 Instance
.syserr(new IOException(trans(StringId
.ERR_BAD_URL
,
414 * Simple shortcut method to call {link Instance#getTrans()#getString()}.
417 * the ID to translate
419 * @return the translated result
421 private static String
trans(StringId id
, Object
... params
) {
422 return Instance
.getTrans().getString(id
, params
);
426 * Display the correct syntax of the program to the user to stdout, or an
427 * error message if the syntax used was wrong on stderr.
430 * TRUE to show the syntax help, FALSE to show "syntax error"
432 private static void syntax(boolean showHelp
) {
434 StringBuilder builder
= new StringBuilder();
435 for (SupportType type
: SupportType
.values()) {
436 builder
.append(trans(StringId
.ERR_SYNTAX_TYPE
, type
.toString(),
438 builder
.append('\n');
441 String typesIn
= builder
.toString();
442 builder
.setLength(0);
444 for (OutputType type
: OutputType
.values()) {
445 builder
.append(trans(StringId
.ERR_SYNTAX_TYPE
, type
.toString(),
447 builder
.append('\n');
450 String typesOut
= builder
.toString();
452 System
.out
.println(trans(StringId
.HELP_SYNTAX
, typesIn
, typesOut
));
454 System
.err
.println(trans(StringId
.ERR_SYNTAX
));
459 * Set the default reader type for this session only (it can be changed in
460 * the configuration file, too, but this value will override it).
462 * @param readerTypeString
465 private static int setReaderType(String readerTypeString
) {
467 ReaderType readerType
= ReaderType
.valueOf(readerTypeString
469 BasicReader
.setDefaultReaderType(readerType
);
471 } catch (IllegalArgumentException e
) {
472 Instance
.syserr(new IOException("Unknown reader type: "
473 + readerTypeString
, e
));