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
.UIUtils
;
20 * Main program entry point.
25 private enum MainAction
{
26 IMPORT
, EXPORT
, CONVERT
, READ
, READ_URL
, LIST
, HELP
, SET_READER
, START
,
30 * Main program entry point.
32 * Known environment variables:
34 * <li>NOUTF: if set to 1 or 'true', the program will prefer non-unicode
35 * {@link String}s when possible</li>
36 * <li>CONFIG_DIR: a path where to look for the <tt>.properties</tt> files
37 * before taking the included ones; they will also be saved/updated into
38 * this path when the program starts</li>
39 * <li>DEBUG: if set to 1 or 'true', the program will override the DEBUG_ERR
40 * configuration value with 'true'</li>
44 * <li>--import [URL]: import into library</li>
45 * <li>--export [id] [output_type] [target]: export story to target</li>
46 * <li>--convert [URL] [output_type] [target] (+info): convert URL into
48 * <li>--read [id] ([chapter number]): read the given story from the library
50 * <li>--read-url [URL] ([chapter number]): convert on the fly and read the
51 * story, without saving it</li>
52 * <li>--list ([type]): list the stories present in the library</li>
53 * <li>--set-reader [reader type]: set the reader type to CLI or LOCAL for
58 * see method description
60 public static void main(String
[] args
) {
61 String urlString
= null;
63 String typeString
= null;
64 String chapString
= null;
66 MainAction action
= MainAction
.START
;
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 exitCode
= setReaderType(args
[i
]);
159 exitCode
= 255; // not supposed to be selected by user
164 if (exitCode
!= 255) {
167 exitCode
= imprt(urlString
);
170 exitCode
= export(luid
, typeString
, target
);
173 exitCode
= convert(urlString
, typeString
, target
,
174 plusInfo
== null ?
false : plusInfo
);
177 exitCode
= list(typeString
);
180 exitCode
= read(luid
, chapString
, true);
183 exitCode
= read(urlString
, chapString
, false);
192 UIUtils
.setLookAndFeel();
193 BasicReader
.setDefaultReaderType(ReaderType
.LOCAL
);
194 BasicReader
.getReader().start(null);
199 if (exitCode
== 255) {
204 System
.exit(exitCode
);
209 * Return an {@link URL} from this {@link String}, be it a file path or an
210 * actual {@link URL}.
212 * @param sourceString
215 * @return the corresponding {@link URL}
217 * @throws MalformedURLException
218 * if this is neither a file nor a conventional {@link URL}
220 private static URL
getUrl(String sourceString
) throws MalformedURLException
{
221 if (sourceString
== null || sourceString
.isEmpty()) {
222 throw new MalformedURLException("Empty url");
227 source
= new URL(sourceString
);
228 } catch (MalformedURLException e
) {
229 File sourceFile
= new File(sourceString
);
230 source
= sourceFile
.toURI().toURL();
237 * Import the given resource into the {@link Library}.
240 * the resource to import
242 * @return the exit return code (0 = success)
244 public static int imprt(String urlString
) {
246 Story story
= Instance
.getLibrary().imprt(getUrl(urlString
));
247 System
.out
.println(story
.getMeta().getLuid() + ": \""
248 + story
.getMeta().getTitle() + "\" imported.");
249 } catch (IOException e
) {
258 * Export the {@link Story} from the {@link Library} to the given target.
263 * the {@link OutputType} to use
267 * @return the exit return code (0 = success)
269 public static int export(String luid
, String typeString
, String target
) {
270 OutputType type
= OutputType
.valueOfNullOkUC(typeString
);
272 Instance
.syserr(new Exception(trans(StringId
.OUTPUT_DESC
,
278 Instance
.getLibrary().export(luid
, type
, target
);
279 } catch (IOException e
) {
288 * List the stories of the given type from the {@link Library} (unless NULL
289 * is passed, in which case all stories will be listed).
292 * the type to list the known stories of, or NULL to list all
295 * @return the exit return code (0 = success)
297 private static int list(String type
) {
298 BasicReader
.getReader().start(type
);
303 * Start the CLI reader for this {@link Story}.
306 * the LUID of the {@link Story} in the {@link Library} <b>or</b>
307 * the {@link Story} {@link URL}
309 * which {@link Chapter} to read (starting at 1), or NULL to get
310 * the {@link Story} description
312 * TRUE if the source is the {@link Story} LUID, FALSE if it is a
315 * @return the exit return code (0 = success)
317 private static int read(String story
, String chapString
, boolean library
) {
319 BasicReader reader
= BasicReader
.getReader();
321 reader
.setStory(story
);
323 reader
.setStory(getUrl(story
));
326 if (chapString
!= null) {
328 reader
.read(Integer
.parseInt(chapString
));
329 } catch (NumberFormatException e
) {
330 Instance
.syserr(new IOException(
331 "Chapter number cannot be parsed: " + chapString
, e
));
337 } catch (IOException e
) {
346 * Convert the {@link Story} into another format.
349 * the source {@link Story} to convert
351 * the {@link OutputType} to convert to
355 * TRUE to also export the cover and info file, even if the given
356 * {@link OutputType} does not usually save them
358 * @return the exit return code (0 = success)
360 private static int convert(String urlString
, String typeString
,
361 String target
, boolean infoCover
) {
364 String sourceName
= urlString
;
366 URL source
= getUrl(urlString
);
367 sourceName
= source
.toString();
368 if (source
.toString().startsWith("file://")) {
369 sourceName
= sourceName
.substring("file://".length());
372 OutputType type
= OutputType
.valueOfAllOkUC(typeString
);
374 Instance
.syserr(new IOException(trans(
375 StringId
.ERR_BAD_OUTPUT_TYPE
, typeString
)));
380 BasicSupport support
= BasicSupport
.getSupport(source
);
382 if (support
!= null) {
383 Story story
= support
.process(source
);
386 target
= new File(target
).getAbsolutePath();
387 BasicOutput
.getOutput(type
, infoCover
).process(
389 } catch (IOException e
) {
390 Instance
.syserr(new IOException(trans(
391 StringId
.ERR_SAVING
, target
), e
));
395 Instance
.syserr(new IOException(trans(
396 StringId
.ERR_NOT_SUPPORTED
, source
)));
400 } catch (IOException e
) {
401 Instance
.syserr(new IOException(trans(StringId
.ERR_LOADING
,
406 } catch (MalformedURLException e
) {
407 Instance
.syserr(new IOException(trans(StringId
.ERR_BAD_URL
,
416 * Simple shortcut method to call {link Instance#getTrans()#getString()}.
419 * the ID to translate
421 * @return the translated result
423 private static String
trans(StringId id
, Object
... params
) {
424 return Instance
.getTrans().getString(id
, params
);
428 * Display the correct syntax of the program to the user to stdout, or an
429 * error message if the syntax used was wrong on stderr.
432 * TRUE to show the syntax help, FALSE to show "syntax error"
434 private static void syntax(boolean showHelp
) {
436 StringBuilder builder
= new StringBuilder();
437 for (SupportType type
: SupportType
.values()) {
438 builder
.append(trans(StringId
.ERR_SYNTAX_TYPE
, type
.toString(),
440 builder
.append('\n');
443 String typesIn
= builder
.toString();
444 builder
.setLength(0);
446 for (OutputType type
: OutputType
.values()) {
447 builder
.append(trans(StringId
.ERR_SYNTAX_TYPE
, type
.toString(),
449 builder
.append('\n');
452 String typesOut
= builder
.toString();
454 System
.out
.println(trans(StringId
.HELP_SYNTAX
, typesIn
, typesOut
));
456 System
.err
.println(trans(StringId
.ERR_SYNTAX
));
461 * Set the default reader type for this session only (it can be changed in
462 * the configuration file, too, but this value will override it).
464 * @param readerTypeString
467 private static int setReaderType(String readerTypeString
) {
469 ReaderType readerType
= ReaderType
.valueOf(readerTypeString
471 BasicReader
.setDefaultReaderType(readerType
);
473 } catch (IllegalArgumentException e
) {
474 Instance
.syserr(new IOException("Unknown reader type: "
475 + readerTypeString
, e
));