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
.ui
.UIUtils
;
21 * Main program entry point.
26 private enum MainAction
{
27 IMPORT
, EXPORT
, CONVERT
, READ
, READ_URL
, LIST
, HELP
, SET_READER
, START
,
31 * Main program entry point.
33 * Known environment variables:
35 * <li>NOUTF: if set to 1 or 'true', the program will prefer non-unicode
36 * {@link String}s when possible</li>
37 * <li>CONFIG_DIR: a path where to look for the <tt>.properties</tt> files
38 * before taking the usual ones; they will also be saved/updated into this
39 * path when the program starts</li>
40 * <li>DEBUG: if set to 1 or 'true', the program will override the DEBUG_ERR
41 * configuration value with 'true'</li>
45 * <li>--import [URL]: import into library</li>
46 * <li>--export [id] [output_type] [target]: export story to target</li>
47 * <li>--convert [URL] [output_type] [target] (+info): convert URL into
49 * <li>--read [id] ([chapter number]): read the given story from the library
51 * <li>--read-url [URL] ([chapter number]): convert on the fly and read the
52 * story, without saving it</li>
53 * <li>--list ([type]): list the stories present in the library</li>
54 * <li>--set-reader [reader type]: set the reader type to CLI or LOCAL for
59 * see method description
61 public static void main(String
[] args
) {
62 String urlString
= null;
64 String typeString
= null;
65 String chapString
= null;
67 MainAction action
= MainAction
.START
;
68 Boolean plusInfo
= null;
70 boolean noMoreActions
= false;
73 for (int i
= 0; exitCode
== 0 && i
< args
.length
; i
++) {
74 // Action (--) handling:
75 if (!noMoreActions
&& args
[i
].startsWith("--")) {
76 if (args
[i
].equals("--")) {
80 action
= MainAction
.valueOf(args
[i
].substring(2)
81 .toUpperCase().replace("-", "_"));
82 } catch (Exception e
) {
83 Instance
.syserr(new IllegalArgumentException(
84 "Unknown action: " + args
[i
], e
));
94 if (urlString
== null) {
103 } else if (typeString
== null) {
104 typeString
= args
[i
];
105 } else if (target
== null) {
112 if (urlString
== null) {
114 } else if (typeString
== null) {
115 typeString
= args
[i
];
116 } else if (target
== null) {
118 } else if (plusInfo
== null) {
119 if ("+info".equals(args
[i
])) {
129 if (typeString
== null) {
130 typeString
= args
[i
];
138 } else if (chapString
== null) {
139 chapString
= args
[i
];
145 if (urlString
== null) {
147 } else if (chapString
== null) {
148 chapString
= args
[i
];
157 exitCode
= setReaderType(args
[i
]);
160 exitCode
= 255; // not supposed to be selected by user
165 final Progress mainProgress
= new Progress(0, 80);
166 mainProgress
.addProgressListener(new Progress
.ProgressListener() {
167 private int current
= mainProgress
.getMin();
169 public void progress(Progress progress
, String name
) {
170 int diff
= progress
.getProgress() - current
;
173 StringBuilder builder
= new StringBuilder();
174 for (int i
= 0; i
< diff
; i
++) {
178 System
.err
.print(builder
.toString());
180 if (progress
.isDone()) {
181 System
.err
.println("");
185 Progress pg
= new Progress();
186 mainProgress
.addProgress(pg
, mainProgress
.getMax());
188 if (exitCode
!= 255) {
191 exitCode
= imprt(urlString
, pg
);
194 exitCode
= export(luid
, typeString
, target
, pg
);
197 exitCode
= convert(urlString
, typeString
, target
,
198 plusInfo
== null ?
false : plusInfo
, pg
);
201 exitCode
= list(typeString
);
204 exitCode
= read(luid
, chapString
, true);
207 exitCode
= read(urlString
, chapString
, false);
216 UIUtils
.setLookAndFeel();
217 BasicReader
.setDefaultReaderType(ReaderType
.LOCAL
);
218 BasicReader
.getReader().start(null);
223 if (exitCode
== 255) {
228 System
.exit(exitCode
);
233 * Import the given resource into the {@link Library}.
236 * the resource to import
238 * the optional progress reporter
240 * @return the exit return code (0 = success)
242 public static int imprt(String urlString
, Progress pg
) {
244 Story story
= Instance
.getLibrary().imprt(
245 BasicReader
.getUrl(urlString
), pg
);
246 System
.out
.println(story
.getMeta().getLuid() + ": \""
247 + story
.getMeta().getTitle() + "\" imported.");
248 } catch (IOException e
) {
257 * Export the {@link Story} from the {@link Library} to the given target.
262 * the {@link OutputType} to use
266 * the optional progress reporter
268 * @return the exit return code (0 = success)
270 public static int export(String luid
, String typeString
, String target
,
272 OutputType type
= OutputType
.valueOfNullOkUC(typeString
);
274 Instance
.syserr(new Exception(trans(StringId
.OUTPUT_DESC
,
280 Instance
.getLibrary().export(luid
, type
, target
, pg
);
281 } catch (IOException e
) {
290 * List the stories of the given type from the {@link Library} (unless NULL
291 * is passed, in which case all stories will be listed).
294 * the type to list the known stories of, or NULL to list all
297 * @return the exit return code (0 = success)
299 private static int list(String type
) {
300 BasicReader
.getReader().start(type
);
305 * Start the CLI reader for this {@link Story}.
308 * the LUID of the {@link Story} in the {@link Library} <b>or</b>
309 * the {@link Story} {@link URL}
311 * which {@link Chapter} to read (starting at 1), or NULL to get
312 * the {@link Story} description
314 * TRUE if the source is the {@link Story} LUID, FALSE if it is a
317 * @return the exit return code (0 = success)
319 private static int read(String story
, String chapString
, boolean library
) {
321 BasicReader reader
= BasicReader
.getReader();
323 reader
.setStory(story
, null);
325 reader
.setStory(BasicReader
.getUrl(story
), null);
328 if (chapString
!= null) {
330 reader
.read(Integer
.parseInt(chapString
));
331 } catch (NumberFormatException e
) {
332 Instance
.syserr(new IOException(
333 "Chapter number cannot be parsed: " + chapString
, e
));
339 } catch (IOException e
) {
348 * Convert the {@link Story} into another format.
351 * the source {@link Story} to convert
353 * the {@link OutputType} to convert to
357 * TRUE to also export the cover and info file, even if the given
358 * {@link OutputType} does not usually save them
360 * the optional progress reporter
362 * @return the exit return code (0 = success)
364 private static int convert(String urlString
, String typeString
,
365 String target
, boolean infoCover
, Progress pg
) {
368 String sourceName
= urlString
;
370 URL source
= BasicReader
.getUrl(urlString
);
371 sourceName
= source
.toString();
372 if (source
.toString().startsWith("file://")) {
373 sourceName
= sourceName
.substring("file://".length());
376 OutputType type
= OutputType
.valueOfAllOkUC(typeString
);
378 Instance
.syserr(new IOException(trans(
379 StringId
.ERR_BAD_OUTPUT_TYPE
, typeString
)));
384 BasicSupport support
= BasicSupport
.getSupport(source
);
386 if (support
!= null) {
387 Progress pgIn
= new Progress();
388 Progress pgOut
= new Progress();
391 pg
.addProgress(pgIn
, 1);
392 pg
.addProgress(pgOut
, 1);
395 Story story
= support
.process(source
, pgIn
);
397 target
= new File(target
).getAbsolutePath();
398 BasicOutput
.getOutput(type
, infoCover
).process(
399 story
, target
, pgOut
);
400 } catch (IOException e
) {
401 Instance
.syserr(new IOException(trans(
402 StringId
.ERR_SAVING
, target
), e
));
406 Instance
.syserr(new IOException(trans(
407 StringId
.ERR_NOT_SUPPORTED
, source
)));
411 } catch (IOException e
) {
412 Instance
.syserr(new IOException(trans(StringId
.ERR_LOADING
,
417 } catch (MalformedURLException e
) {
418 Instance
.syserr(new IOException(trans(StringId
.ERR_BAD_URL
,
427 * Simple shortcut method to call {link Instance#getTrans()#getString()}.
430 * the ID to translate
432 * @return the translated result
434 private static String
trans(StringId id
, Object
... params
) {
435 return Instance
.getTrans().getString(id
, params
);
439 * Display the correct syntax of the program to the user to stdout, or an
440 * error message if the syntax used was wrong on stderr.
443 * TRUE to show the syntax help, FALSE to show "syntax error"
445 private static void syntax(boolean showHelp
) {
447 StringBuilder builder
= new StringBuilder();
448 for (SupportType type
: SupportType
.values()) {
449 builder
.append(trans(StringId
.ERR_SYNTAX_TYPE
, type
.toString(),
451 builder
.append('\n');
454 String typesIn
= builder
.toString();
455 builder
.setLength(0);
457 for (OutputType type
: OutputType
.values()) {
458 builder
.append(trans(StringId
.ERR_SYNTAX_TYPE
, type
.toString(),
459 type
.getDesc(true)));
460 builder
.append('\n');
463 String typesOut
= builder
.toString();
465 System
.out
.println(trans(StringId
.HELP_SYNTAX
, typesIn
, typesOut
));
467 System
.err
.println(trans(StringId
.ERR_SYNTAX
));
472 * Set the default reader type for this session only (it can be changed in
473 * the configuration file, too, but this value will override it).
475 * @param readerTypeString
478 private static int setReaderType(String readerTypeString
) {
480 ReaderType readerType
= ReaderType
.valueOf(readerTypeString
482 BasicReader
.setDefaultReaderType(readerType
);
484 } catch (IllegalArgumentException e
) {
485 Instance
.syserr(new IOException("Unknown reader type: "
486 + readerTypeString
, e
));