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
.ui
.UIUtils
;
18 import be
.nikiroo
.utils
.ui
.Progress
;
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 included ones; they will also be saved/updated into
39 * this 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 * Return an {@link URL} from this {@link String}, be it a file path or an
234 * actual {@link URL}.
236 * @param sourceString
239 * @return the corresponding {@link URL}
241 * @throws MalformedURLException
242 * if this is neither a file nor a conventional {@link URL}
244 private static URL
getUrl(String sourceString
) throws MalformedURLException
{
245 if (sourceString
== null || sourceString
.isEmpty()) {
246 throw new MalformedURLException("Empty url");
251 source
= new URL(sourceString
);
252 } catch (MalformedURLException e
) {
253 File sourceFile
= new File(sourceString
);
254 source
= sourceFile
.toURI().toURL();
261 * Import the given resource into the {@link Library}.
264 * the resource to import
266 * the optional progress reporter
268 * @return the exit return code (0 = success)
270 public static int imprt(String urlString
, Progress pg
) {
272 Story story
= Instance
.getLibrary().imprt(getUrl(urlString
), pg
);
273 System
.out
.println(story
.getMeta().getLuid() + ": \""
274 + story
.getMeta().getTitle() + "\" imported.");
275 } catch (IOException e
) {
284 * Export the {@link Story} from the {@link Library} to the given target.
289 * the {@link OutputType} to use
293 * the optional progress reporter
295 * @return the exit return code (0 = success)
297 public static int export(String luid
, String typeString
, String target
,
299 OutputType type
= OutputType
.valueOfNullOkUC(typeString
);
301 Instance
.syserr(new Exception(trans(StringId
.OUTPUT_DESC
,
307 Instance
.getLibrary().export(luid
, type
, target
, pg
);
308 } catch (IOException e
) {
317 * List the stories of the given type from the {@link Library} (unless NULL
318 * is passed, in which case all stories will be listed).
321 * the type to list the known stories of, or NULL to list all
324 * @return the exit return code (0 = success)
326 private static int list(String type
) {
327 BasicReader
.getReader().start(type
);
332 * Start the CLI reader for this {@link Story}.
335 * the LUID of the {@link Story} in the {@link Library} <b>or</b>
336 * the {@link Story} {@link URL}
338 * which {@link Chapter} to read (starting at 1), or NULL to get
339 * the {@link Story} description
341 * TRUE if the source is the {@link Story} LUID, FALSE if it is a
344 * @return the exit return code (0 = success)
346 private static int read(String story
, String chapString
, boolean library
) {
348 BasicReader reader
= BasicReader
.getReader();
350 reader
.setStory(story
, null);
352 reader
.setStory(getUrl(story
), null);
355 if (chapString
!= null) {
357 reader
.read(Integer
.parseInt(chapString
));
358 } catch (NumberFormatException e
) {
359 Instance
.syserr(new IOException(
360 "Chapter number cannot be parsed: " + chapString
, e
));
366 } catch (IOException e
) {
375 * Convert the {@link Story} into another format.
378 * the source {@link Story} to convert
380 * the {@link OutputType} to convert to
384 * TRUE to also export the cover and info file, even if the given
385 * {@link OutputType} does not usually save them
387 * the optional progress reporter
389 * @return the exit return code (0 = success)
391 private static int convert(String urlString
, String typeString
,
392 String target
, boolean infoCover
, Progress pg
) {
395 String sourceName
= urlString
;
397 URL source
= getUrl(urlString
);
398 sourceName
= source
.toString();
399 if (source
.toString().startsWith("file://")) {
400 sourceName
= sourceName
.substring("file://".length());
403 OutputType type
= OutputType
.valueOfAllOkUC(typeString
);
405 Instance
.syserr(new IOException(trans(
406 StringId
.ERR_BAD_OUTPUT_TYPE
, typeString
)));
411 BasicSupport support
= BasicSupport
.getSupport(source
);
413 if (support
!= null) {
414 Story story
= support
.process(source
, pg
);
417 target
= new File(target
).getAbsolutePath();
418 BasicOutput
.getOutput(type
, infoCover
).process(
420 } catch (IOException e
) {
421 Instance
.syserr(new IOException(trans(
422 StringId
.ERR_SAVING
, target
), e
));
426 Instance
.syserr(new IOException(trans(
427 StringId
.ERR_NOT_SUPPORTED
, source
)));
431 } catch (IOException e
) {
432 Instance
.syserr(new IOException(trans(StringId
.ERR_LOADING
,
437 } catch (MalformedURLException e
) {
438 Instance
.syserr(new IOException(trans(StringId
.ERR_BAD_URL
,
447 * Simple shortcut method to call {link Instance#getTrans()#getString()}.
450 * the ID to translate
452 * @return the translated result
454 private static String
trans(StringId id
, Object
... params
) {
455 return Instance
.getTrans().getString(id
, params
);
459 * Display the correct syntax of the program to the user to stdout, or an
460 * error message if the syntax used was wrong on stderr.
463 * TRUE to show the syntax help, FALSE to show "syntax error"
465 private static void syntax(boolean showHelp
) {
467 StringBuilder builder
= new StringBuilder();
468 for (SupportType type
: SupportType
.values()) {
469 builder
.append(trans(StringId
.ERR_SYNTAX_TYPE
, type
.toString(),
471 builder
.append('\n');
474 String typesIn
= builder
.toString();
475 builder
.setLength(0);
477 for (OutputType type
: OutputType
.values()) {
478 builder
.append(trans(StringId
.ERR_SYNTAX_TYPE
, type
.toString(),
480 builder
.append('\n');
483 String typesOut
= builder
.toString();
485 System
.out
.println(trans(StringId
.HELP_SYNTAX
, typesIn
, typesOut
));
487 System
.err
.println(trans(StringId
.ERR_SYNTAX
));
492 * Set the default reader type for this session only (it can be changed in
493 * the configuration file, too, but this value will override it).
495 * @param readerTypeString
498 private static int setReaderType(String readerTypeString
) {
500 ReaderType readerType
= ReaderType
.valueOf(readerTypeString
502 BasicReader
.setDefaultReaderType(readerType
);
504 } catch (IllegalArgumentException e
) {
505 Instance
.syserr(new IOException("Unknown reader type: "
506 + readerTypeString
, e
));