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
;
17 import be
.nikiroo
.utils
.Progress
;
18 import be
.nikiroo
.utils
.Version
;
19 import be
.nikiroo
.utils
.ui
.UIUtils
;
22 * Main program entry point.
27 private enum MainAction
{
28 IMPORT
, EXPORT
, CONVERT
, READ
, READ_URL
, LIST
, HELP
, SET_READER
, START
, VERSION
,
32 * Main program entry point.
34 * Known environment variables:
36 * <li>NOUTF: if set to 1 or 'true', the program will prefer non-unicode
37 * {@link String}s when possible</li>
38 * <li>CONFIG_DIR: a path where to look for the <tt>.properties</tt> files
39 * before taking the usual ones; they will also be saved/updated into this
40 * path when the program starts</li>
41 * <li>DEBUG: if set to 1 or 'true', the program will override the DEBUG_ERR
42 * configuration value with 'true'</li>
46 * <li>--import [URL]: import into library</li>
47 * <li>--export [id] [output_type] [target]: export story to target</li>
48 * <li>--convert [URL] [output_type] [target] (+info): convert URL into
50 * <li>--read [id] ([chapter number]): read the given story from the library
52 * <li>--read-url [URL] ([chapter number]): convert on the fly and read the
53 * story, without saving it</li>
54 * <li>--list ([type]): list the stories present in the library</li>
55 * <li>--set-reader [reader type]: set the reader type to CLI or LOCAL for
57 * <li>--version: get the version of the program</li>
61 * see method description
63 public static void main(String
[] args
) {
64 String urlString
= null;
66 String typeString
= null;
67 String chapString
= null;
69 MainAction action
= MainAction
.START
;
70 Boolean plusInfo
= null;
72 boolean noMoreActions
= false;
75 for (int i
= 0; exitCode
== 0 && i
< args
.length
; i
++) {
76 // Action (--) handling:
77 if (!noMoreActions
&& args
[i
].startsWith("--")) {
78 if (args
[i
].equals("--")) {
82 action
= MainAction
.valueOf(args
[i
].substring(2)
83 .toUpperCase().replace("-", "_"));
84 } catch (Exception e
) {
85 Instance
.syserr(new IllegalArgumentException(
86 "Unknown action: " + args
[i
], e
));
96 if (urlString
== null) {
105 } else if (typeString
== null) {
106 typeString
= args
[i
];
107 } else if (target
== null) {
114 if (urlString
== null) {
116 } else if (typeString
== null) {
117 typeString
= args
[i
];
118 } else if (target
== null) {
120 } else if (plusInfo
== null) {
121 if ("+info".equals(args
[i
])) {
131 if (typeString
== null) {
132 typeString
= args
[i
];
140 } else if (chapString
== null) {
141 chapString
= args
[i
];
147 if (urlString
== null) {
149 } else if (chapString
== null) {
150 chapString
= args
[i
];
159 exitCode
= setReaderType(args
[i
]);
162 exitCode
= 255; // not supposed to be selected by user
165 exitCode
= 255; // no arguments for this option
169 final Progress mainProgress
= new Progress(0, 80);
170 mainProgress
.addProgressListener(new Progress
.ProgressListener() {
171 private int current
= mainProgress
.getMin();
173 public void progress(Progress progress
, String name
) {
174 int diff
= progress
.getProgress() - current
;
177 StringBuilder builder
= new StringBuilder();
178 for (int i
= 0; i
< diff
; i
++) {
182 System
.err
.print(builder
.toString());
184 if (progress
.isDone()) {
185 System
.err
.println("");
189 Progress pg
= new Progress();
190 mainProgress
.addProgress(pg
, mainProgress
.getMax());
192 VersionCheck updates
= VersionCheck
.check();
193 if (updates
.isNewVersionAvailable()) {
194 // Sent to syserr so not to cause problem if one tries to capture a
195 // story content in text mode
197 .println("A new version of the program is available at https://github.com/nikiroo/fanfix/releases");
198 System
.err
.println("");
199 for (Version v
: updates
.getNewer()) {
200 System
.err
.println("\tVersion " + v
);
201 System
.err
.println("\t-------------");
202 System
.err
.println("");
203 for (String item
: updates
.getChanges().get(v
)) {
204 System
.err
.println("\t- " + item
);
206 System
.err
.println("");
210 if (exitCode
!= 255) {
213 exitCode
= imprt(urlString
, pg
);
214 updates
.ok(); // we consider it read
217 exitCode
= export(luid
, typeString
, target
, pg
);
218 updates
.ok(); // we consider it read
221 exitCode
= convert(urlString
, typeString
, target
,
222 plusInfo
== null ?
false : plusInfo
, pg
);
223 updates
.ok(); // we consider it read
226 exitCode
= list(typeString
);
229 exitCode
= read(luid
, chapString
, true);
232 exitCode
= read(urlString
, chapString
, false);
242 .println(String
.format("Fanfix version %s"
243 + "\nhttps://github.com/nikiroo/fanfix/"
244 + "\n\tWritten by Nikiroo",
245 Version
.getCurrentVersion()));
246 updates
.ok(); // we consider it read
249 UIUtils
.setLookAndFeel();
250 BasicReader
.setDefaultReaderType(ReaderType
.LOCAL
);
251 BasicReader
.getReader().start(null);
256 if (exitCode
== 255) {
261 System
.exit(exitCode
);
266 * Import the given resource into the {@link Library}.
269 * the resource to import
271 * the optional progress reporter
273 * @return the exit return code (0 = success)
275 public static int imprt(String urlString
, Progress pg
) {
277 Story story
= Instance
.getLibrary().imprt(
278 BasicReader
.getUrl(urlString
), pg
);
279 System
.out
.println(story
.getMeta().getLuid() + ": \""
280 + story
.getMeta().getTitle() + "\" imported.");
281 } catch (IOException e
) {
290 * Export the {@link Story} from the {@link Library} to the given target.
295 * the {@link OutputType} to use
299 * the optional progress reporter
301 * @return the exit return code (0 = success)
303 public static int export(String luid
, String typeString
, String target
,
305 OutputType type
= OutputType
.valueOfNullOkUC(typeString
);
307 Instance
.syserr(new Exception(trans(StringId
.OUTPUT_DESC
,
313 Instance
.getLibrary().export(luid
, type
, target
, pg
);
314 } catch (IOException e
) {
323 * List the stories of the given type from the {@link Library} (unless NULL
324 * is passed, in which case all stories will be listed).
327 * the type to list the known stories of, or NULL to list all
330 * @return the exit return code (0 = success)
332 private static int list(String type
) {
333 BasicReader
.getReader().start(type
);
338 * Start the CLI reader for this {@link Story}.
341 * the LUID of the {@link Story} in the {@link Library} <b>or</b>
342 * the {@link Story} {@link URL}
344 * which {@link Chapter} to read (starting at 1), or NULL to get
345 * the {@link Story} description
347 * TRUE if the source is the {@link Story} LUID, FALSE if it is a
350 * @return the exit return code (0 = success)
352 private static int read(String story
, String chapString
, boolean library
) {
354 BasicReader reader
= BasicReader
.getReader();
356 reader
.setStory(story
, null);
358 reader
.setStory(BasicReader
.getUrl(story
), null);
361 if (chapString
!= null) {
363 reader
.read(Integer
.parseInt(chapString
));
364 } catch (NumberFormatException e
) {
365 Instance
.syserr(new IOException(
366 "Chapter number cannot be parsed: " + chapString
, e
));
372 } catch (IOException e
) {
381 * Convert the {@link Story} into another format.
384 * the source {@link Story} to convert
386 * the {@link OutputType} to convert to
390 * TRUE to also export the cover and info file, even if the given
391 * {@link OutputType} does not usually save them
393 * the optional progress reporter
395 * @return the exit return code (0 = success)
397 private static int convert(String urlString
, String typeString
,
398 String target
, boolean infoCover
, Progress pg
) {
401 String sourceName
= urlString
;
403 URL source
= BasicReader
.getUrl(urlString
);
404 sourceName
= source
.toString();
405 if (source
.toString().startsWith("file://")) {
406 sourceName
= sourceName
.substring("file://".length());
409 OutputType type
= OutputType
.valueOfAllOkUC(typeString
);
411 Instance
.syserr(new IOException(trans(
412 StringId
.ERR_BAD_OUTPUT_TYPE
, typeString
)));
417 BasicSupport support
= BasicSupport
.getSupport(source
);
419 if (support
!= null) {
420 Progress pgIn
= new Progress();
421 Progress pgOut
= new Progress();
424 pg
.addProgress(pgIn
, 1);
425 pg
.addProgress(pgOut
, 1);
428 Story story
= support
.process(source
, pgIn
);
430 target
= new File(target
).getAbsolutePath();
431 BasicOutput
.getOutput(type
, infoCover
).process(
432 story
, target
, pgOut
);
433 } catch (IOException e
) {
434 Instance
.syserr(new IOException(trans(
435 StringId
.ERR_SAVING
, target
), e
));
439 Instance
.syserr(new IOException(trans(
440 StringId
.ERR_NOT_SUPPORTED
, source
)));
444 } catch (IOException e
) {
445 Instance
.syserr(new IOException(trans(StringId
.ERR_LOADING
,
450 } catch (MalformedURLException e
) {
451 Instance
.syserr(new IOException(trans(StringId
.ERR_BAD_URL
,
460 * Simple shortcut method to call {link Instance#getTrans()#getString()}.
463 * the ID to translate
465 * @return the translated result
467 private static String
trans(StringId id
, Object
... params
) {
468 return Instance
.getTrans().getString(id
, params
);
472 * Display the correct syntax of the program to the user to stdout, or an
473 * error message if the syntax used was wrong on stderr.
476 * TRUE to show the syntax help, FALSE to show "syntax error"
478 private static void syntax(boolean showHelp
) {
480 StringBuilder builder
= new StringBuilder();
481 for (SupportType type
: SupportType
.values()) {
482 builder
.append(trans(StringId
.ERR_SYNTAX_TYPE
, type
.toString(),
484 builder
.append('\n');
487 String typesIn
= builder
.toString();
488 builder
.setLength(0);
490 for (OutputType type
: OutputType
.values()) {
491 builder
.append(trans(StringId
.ERR_SYNTAX_TYPE
, type
.toString(),
492 type
.getDesc(true)));
493 builder
.append('\n');
496 String typesOut
= builder
.toString();
498 System
.out
.println(trans(StringId
.HELP_SYNTAX
, typesIn
, typesOut
));
500 System
.err
.println(trans(StringId
.ERR_SYNTAX
));
505 * Set the default reader type for this session only (it can be changed in
506 * the configuration file, too, but this value will override it).
508 * @param readerTypeString
511 private static int setReaderType(String readerTypeString
) {
513 ReaderType readerType
= ReaderType
.valueOf(readerTypeString
515 BasicReader
.setDefaultReaderType(readerType
);
517 } catch (IllegalArgumentException e
) {
518 Instance
.syserr(new IOException("Unknown reader type: "
519 + readerTypeString
, e
));