manage remote and io exception in fanfix
[fanfix.git] / src / be / nikiroo / fanfix / Main.java
1 package be.nikiroo.fanfix;
2
3 import java.io.File;
4 import java.io.IOException;
5 import java.net.MalformedURLException;
6 import java.net.URL;
7 import java.util.ArrayList;
8 import java.util.List;
9
10 import javax.net.ssl.SSLException;
11
12 import be.nikiroo.fanfix.bundles.Config;
13 import be.nikiroo.fanfix.bundles.StringId;
14 import be.nikiroo.fanfix.data.Chapter;
15 import be.nikiroo.fanfix.data.Story;
16 import be.nikiroo.fanfix.library.BasicLibrary;
17 import be.nikiroo.fanfix.library.CacheLibrary;
18 import be.nikiroo.fanfix.library.LocalLibrary;
19 import be.nikiroo.fanfix.library.RemoteLibrary;
20 import be.nikiroo.fanfix.library.RemoteLibraryServer;
21 import be.nikiroo.fanfix.output.BasicOutput;
22 import be.nikiroo.fanfix.output.BasicOutput.OutputType;
23 import be.nikiroo.fanfix.reader.BasicReader;
24 import be.nikiroo.fanfix.reader.Reader;
25 import be.nikiroo.fanfix.reader.Reader.ReaderType;
26 import be.nikiroo.fanfix.searchable.BasicSearchable;
27 import be.nikiroo.fanfix.supported.BasicSupport;
28 import be.nikiroo.fanfix.supported.SupportType;
29 import be.nikiroo.utils.Progress;
30 import be.nikiroo.utils.Version;
31 import be.nikiroo.utils.serial.server.ServerObject;
32
33 /**
34 * Main program entry point.
35 *
36 * @author niki
37 */
38 public class Main {
39 private enum MainAction {
40 IMPORT, EXPORT, CONVERT, READ, READ_URL, LIST, HELP, SET_READER, START, VERSION, SERVER, STOP_SERVER, REMOTE, SET_SOURCE, SET_TITLE, SET_AUTHOR, SEARCH, SEARCH_TAG
41 }
42
43 /**
44 * Main program entry point.
45 * <p>
46 * Known environment variables:
47 * <ul>
48 * <li>NOUTF: if set to 1 or 'true', the program will prefer non-unicode
49 * {@link String}s when possible</li>
50 * <li>CONFIG_DIR: a path where to look for the <tt>.properties</tt> files
51 * before taking the usual ones; they will also be saved/updated into this
52 * path when the program starts</li>
53 * <li>DEBUG: if set to 1 or 'true', the program will override the DEBUG_ERR
54 * configuration value with 'true'</li>
55 * </ul>
56 * <p>
57 * <ul>
58 * <li>--import [URL]: import into library</li>
59 * <li>--export [id] [output_type] [target]: export story to target</li>
60 * <li>--convert [URL] [output_type] [target] (+info): convert URL into
61 * target</li>
62 * <li>--read [id] ([chapter number]): read the given story from the library
63 * </li>
64 * <li>--read-url [URL] ([chapter number]): convert on the fly and read the
65 * story, without saving it</li>
66 * <li>--search: list the supported websites (where)</li>
67 * <li>--search [where] [keywords] (page [page]) (item [item]): search on
68 * the supported website and display the given results page of stories it
69 * found, or the story details if asked</li>
70 * <li>--search-tag [where]: list all the tags supported by this website</li>
71 * <li>--search-tag [index 1]... (page [page]) (item [item]): search for the
72 * given stories or subtags, tag by tag, and display information about a
73 * specific page of results or about a specific item if requested</li>
74 * <li>--list ([type]): list the stories present in the library</li>
75 * <li>--set-source [id] [new source]: change the source of the given story</li>
76 * <li>--set-title [id] [new title]: change the title of the given story</li>
77 * <li>--set-author [id] [new author]: change the author of the given story</li>
78 * <li>--set-reader [reader type]: set the reader type to CLI, TUI or LOCAL
79 * for this command</li>
80 * <li>--version: get the version of the program</li>
81 * <li>--server: start the server mode (see config file for parameters)</li>
82 * <li>--stop-server: stop the running server on this port if any</li>
83 * <li>--remote [key] [host] [port]: use a the given remote library</li>
84 * </ul>
85 *
86 * @param args
87 * see method description
88 */
89 public static void main(String[] args) {
90 String urlString = null;
91 String luid = null;
92 String sourceString = null;
93 String titleString = null;
94 String authorString = null;
95 String chapString = null;
96 String target = null;
97 String key = null;
98 MainAction action = MainAction.START;
99 Boolean plusInfo = null;
100 String host = null;
101 Integer port = null;
102 SupportType searchOn = null;
103 String search = null;
104 List<Integer> tags = new ArrayList<Integer>();
105 Integer page = null;
106 Integer item = null;
107
108 boolean noMoreActions = false;
109
110 int exitCode = 0;
111 for (int i = 0; exitCode == 0 && i < args.length; i++) {
112 // Action (--) handling:
113 if (!noMoreActions && args[i].startsWith("--")) {
114 if (args[i].equals("--")) {
115 noMoreActions = true;
116 } else {
117 try {
118 action = MainAction.valueOf(args[i].substring(2)
119 .toUpperCase().replace("-", "_"));
120 } catch (Exception e) {
121 Instance.getTraceHandler().error(
122 new IllegalArgumentException("Unknown action: "
123 + args[i], e));
124 exitCode = 255;
125 }
126 }
127
128 continue;
129 }
130
131 switch (action) {
132 case IMPORT:
133 if (urlString == null) {
134 urlString = args[i];
135 } else {
136 exitCode = 255;
137 }
138 break;
139 case EXPORT:
140 if (luid == null) {
141 luid = args[i];
142 } else if (sourceString == null) {
143 sourceString = args[i];
144 } else if (target == null) {
145 target = args[i];
146 } else {
147 exitCode = 255;
148 }
149 break;
150 case CONVERT:
151 if (urlString == null) {
152 urlString = args[i];
153 } else if (sourceString == null) {
154 sourceString = args[i];
155 } else if (target == null) {
156 target = args[i];
157 } else if (plusInfo == null) {
158 if ("+info".equals(args[i])) {
159 plusInfo = true;
160 } else {
161 exitCode = 255;
162 }
163 } else {
164 exitCode = 255;
165 }
166 break;
167 case LIST:
168 if (sourceString == null) {
169 sourceString = args[i];
170 } else {
171 exitCode = 255;
172 }
173 break;
174 case SET_SOURCE:
175 if (luid == null) {
176 luid = args[i];
177 } else if (sourceString == null) {
178 sourceString = args[i];
179 } else {
180 exitCode = 255;
181 }
182 break;
183 case SET_TITLE:
184 if (luid == null) {
185 luid = args[i];
186 } else if (sourceString == null) {
187 titleString = args[i];
188 } else {
189 exitCode = 255;
190 }
191 break;
192 case SET_AUTHOR:
193 if (luid == null) {
194 luid = args[i];
195 } else if (sourceString == null) {
196 authorString = args[i];
197 } else {
198 exitCode = 255;
199 }
200 break;
201 case READ:
202 if (luid == null) {
203 luid = args[i];
204 } else if (chapString == null) {
205 chapString = args[i];
206 } else {
207 exitCode = 255;
208 }
209 break;
210 case READ_URL:
211 if (urlString == null) {
212 urlString = args[i];
213 } else if (chapString == null) {
214 chapString = args[i];
215 } else {
216 exitCode = 255;
217 }
218 break;
219 case SEARCH:
220 if (searchOn == null) {
221 searchOn = SupportType.valueOfAllOkUC(args[i]);
222
223 if (searchOn == null) {
224 Instance.getTraceHandler().error(
225 "Website not known: <" + args[i] + ">");
226 exitCode = 41;
227 break;
228 }
229
230 if (BasicSearchable.getSearchable(searchOn) == null) {
231 Instance.getTraceHandler().error(
232 "Website not supported: " + searchOn);
233 exitCode = 42;
234 break;
235 }
236 } else if (search == null) {
237 search = args[i];
238 } else if (page != null && page == -1) {
239 try {
240 page = Integer.parseInt(args[i]);
241 } catch (Exception e) {
242 page = -2;
243 }
244 } else if (item != null && item == -1) {
245 try {
246 item = Integer.parseInt(args[i]);
247 } catch (Exception e) {
248 item = -2;
249 }
250 } else if (page == null || item == null) {
251 if (page == null && "page".equals(args[i])) {
252 page = -1;
253 } else if (item == null && "item".equals(args[i])) {
254 item = -1;
255 } else {
256 exitCode = 255;
257 }
258 } else {
259 exitCode = 255;
260 }
261 break;
262 case SEARCH_TAG:
263 if (searchOn == null) {
264 searchOn = SupportType.valueOfAllOkUC(args[i]);
265
266 if (searchOn == null) {
267 Instance.getTraceHandler().error(
268 "Website not known: <" + args[i] + ">");
269 exitCode = 255;
270 }
271
272 if (BasicSearchable.getSearchable(searchOn) == null) {
273 Instance.getTraceHandler().error(
274 "Website not supported: " + searchOn);
275 exitCode = 255;
276 }
277 } else if (page == null && item == null) {
278 if ("page".equals(args[i])) {
279 page = -1;
280 } else if ("item".equals(args[i])) {
281 item = -1;
282 } else {
283 try {
284 int index = Integer.parseInt(args[i]);
285 tags.add(index);
286 } catch (NumberFormatException e) {
287 Instance.getTraceHandler().error(
288 "Invalid tag index: " + args[i]);
289 exitCode = 255;
290 }
291 }
292 } else if (page != null && page == -1) {
293 try {
294 page = Integer.parseInt(args[i]);
295 } catch (Exception e) {
296 page = -2;
297 }
298 } else if (item != null && item == -1) {
299 try {
300 item = Integer.parseInt(args[i]);
301 } catch (Exception e) {
302 item = -2;
303 }
304 } else if (page == null || item == null) {
305 if (page == null && "page".equals(args[i])) {
306 page = -1;
307 } else if (item == null && "item".equals(args[i])) {
308 item = -1;
309 } else {
310 exitCode = 255;
311 }
312 } else {
313 exitCode = 255;
314 }
315 break;
316 case HELP:
317 exitCode = 255;
318 break;
319 case SET_READER:
320 exitCode = setReaderType(args[i]);
321 action = MainAction.START;
322 break;
323 case START:
324 exitCode = 255; // not supposed to be selected by user
325 break;
326 case VERSION:
327 exitCode = 255; // no arguments for this option
328 break;
329 case SERVER:
330 exitCode = 255; // no arguments for this option
331 break;
332 case STOP_SERVER:
333 exitCode = 255; // no arguments for this option
334 break;
335 case REMOTE:
336 if (key == null) {
337 key = args[i];
338 } else if (host == null) {
339 host = args[i];
340 } else if (port == null) {
341 port = Integer.parseInt(args[i]);
342
343 BasicLibrary lib = new RemoteLibrary(key, host, port);
344 lib = new CacheLibrary(Instance.getRemoteDir(host), lib);
345
346 BasicReader.setDefaultLibrary(lib);
347
348 action = MainAction.START;
349 } else {
350 exitCode = 255;
351 }
352 break;
353 }
354 }
355
356 final Progress mainProgress = new Progress(0, 80);
357 mainProgress.addProgressListener(new Progress.ProgressListener() {
358 private int current = mainProgress.getMin();
359
360 @Override
361 public void progress(Progress progress, String name) {
362 int diff = progress.getProgress() - current;
363 current += diff;
364
365 if (diff <= 0)
366 return;
367
368 StringBuilder builder = new StringBuilder();
369 for (int i = 0; i < diff; i++) {
370 builder.append('.');
371 }
372
373 System.err.print(builder.toString());
374
375 if (progress.isDone()) {
376 System.err.println("");
377 }
378 }
379 });
380 Progress pg = new Progress();
381 mainProgress.addProgress(pg, mainProgress.getMax());
382
383 VersionCheck updates = VersionCheck.check();
384 if (updates.isNewVersionAvailable()) {
385 // Sent to syserr so not to cause problem if one tries to capture a
386 // story content in text mode
387 System.err
388 .println("A new version of the program is available at https://github.com/nikiroo/fanfix/releases");
389 System.err.println("");
390 for (Version v : updates.getNewer()) {
391 System.err.println("\tVersion " + v);
392 System.err.println("\t-------------");
393 System.err.println("");
394 for (String it : updates.getChanges().get(v)) {
395 System.err.println("\t- " + it);
396 }
397 System.err.println("");
398 }
399 }
400
401 if (exitCode == 0) {
402 switch (action) {
403 case IMPORT:
404 exitCode = imprt(urlString, pg);
405 updates.ok(); // we consider it read
406 break;
407 case EXPORT:
408 exitCode = export(luid, sourceString, target, pg);
409 updates.ok(); // we consider it read
410 break;
411 case CONVERT:
412 exitCode = convert(urlString, sourceString, target,
413 plusInfo == null ? false : plusInfo, pg);
414 updates.ok(); // we consider it read
415 break;
416 case LIST:
417 if (BasicReader.getReader() == null) {
418 Instance.getTraceHandler()
419 .error(new Exception(
420 "No reader type has been configured"));
421 exitCode = 10;
422 break;
423 }
424 exitCode = list(sourceString);
425 break;
426 case SET_SOURCE:
427 try {
428 Instance.getLibrary().changeSource(luid, sourceString, pg);
429 } catch (IOException e1) {
430 Instance.getTraceHandler().error(e1);
431 exitCode = 21;
432 }
433 break;
434 case SET_TITLE:
435 try {
436 Instance.getLibrary().changeTitle(luid, titleString, pg);
437 } catch (IOException e1) {
438 Instance.getTraceHandler().error(e1);
439 exitCode = 22;
440 }
441 break;
442 case SET_AUTHOR:
443 try {
444 Instance.getLibrary().changeAuthor(luid, authorString, pg);
445 } catch (IOException e1) {
446 Instance.getTraceHandler().error(e1);
447 exitCode = 23;
448 }
449 break;
450 case READ:
451 if (BasicReader.getReader() == null) {
452 Instance.getTraceHandler()
453 .error(new Exception(
454 "No reader type has been configured"));
455 exitCode = 10;
456 break;
457 }
458 exitCode = read(luid, chapString, true);
459 break;
460 case READ_URL:
461 if (BasicReader.getReader() == null) {
462 Instance.getTraceHandler()
463 .error(new Exception(
464 "No reader type has been configured"));
465 exitCode = 10;
466 break;
467 }
468 exitCode = read(urlString, chapString, false);
469 break;
470 case SEARCH:
471 page = page == null ? 1 : page;
472 if (page < 0) {
473 Instance.getTraceHandler().error("Incorrect page number");
474 exitCode = 255;
475 break;
476 }
477
478 item = item == null ? 0 : item;
479 if (item < 0) {
480 Instance.getTraceHandler().error("Incorrect item number");
481 exitCode = 255;
482 break;
483 }
484
485 if (BasicReader.getReader() == null) {
486 Instance.getTraceHandler()
487 .error(new Exception(
488 "No reader type has been configured"));
489 exitCode = 10;
490 break;
491 }
492
493 try {
494 if (searchOn == null) {
495 BasicReader.getReader().search(true);
496 } else if (search != null) {
497
498 BasicReader.getReader().search(searchOn, search, page,
499 item, true);
500 } else {
501 exitCode = 255;
502 }
503 } catch (IOException e1) {
504 Instance.getTraceHandler().error(e1);
505 exitCode = 20;
506 }
507
508 break;
509 case SEARCH_TAG:
510 if (searchOn == null) {
511 exitCode = 255;
512 break;
513 }
514
515 page = page == null ? 1 : page;
516 if (page < 0) {
517 Instance.getTraceHandler().error("Incorrect page number");
518 exitCode = 255;
519 break;
520 }
521
522 item = item == null ? 0 : item;
523 if (item < 0) {
524 Instance.getTraceHandler().error("Incorrect item number");
525 exitCode = 255;
526 break;
527 }
528
529 if (BasicReader.getReader() == null) {
530 Instance.getTraceHandler()
531 .error(new Exception(
532 "No reader type has been configured"));
533 exitCode = 10;
534 break;
535 }
536
537 try {
538 BasicReader.getReader().searchTag(searchOn, page, item,
539 true, tags.toArray(new Integer[] {}));
540 } catch (IOException e1) {
541 Instance.getTraceHandler().error(e1);
542 }
543
544 break;
545 case HELP:
546 syntax(true);
547 exitCode = 0;
548 break;
549 case SET_READER:
550 exitCode = 255;
551 break;
552 case VERSION:
553 System.out
554 .println(String.format("Fanfix version %s"
555 + "%nhttps://github.com/nikiroo/fanfix/"
556 + "%n\tWritten by Nikiroo",
557 Version.getCurrentVersion()));
558 updates.ok(); // we consider it read
559 break;
560 case START:
561 if (BasicReader.getReader() == null) {
562 Instance.getTraceHandler()
563 .error(new Exception(
564 "No reader type has been configured"));
565 exitCode = 10;
566 break;
567 }
568 try {
569 BasicReader.getReader().browse(null);
570 } catch (IOException e) {
571 Instance.getTraceHandler().error(e);
572 exitCode = 66;
573 }
574 break;
575 case SERVER:
576 key = Instance.getConfig().getString(Config.SERVER_KEY);
577 port = Instance.getConfig().getInteger(Config.SERVER_PORT);
578 if (port == null) {
579 System.err.println("No port configured in the config file");
580 exitCode = 15;
581 break;
582 }
583 try {
584 ServerObject server = new RemoteLibraryServer(key, port);
585 server.setTraceHandler(Instance.getTraceHandler());
586 server.run();
587 } catch (IOException e) {
588 Instance.getTraceHandler().error(e);
589 }
590 return;
591 case STOP_SERVER:
592 key = Instance.getConfig().getString(Config.SERVER_KEY);
593 port = Instance.getConfig().getInteger(Config.SERVER_PORT);
594 if (port == null) {
595 System.err.println("No port configured in the config file");
596 exitCode = 15;
597 break;
598 }
599 try {
600 new RemoteLibrary(key, host, port).exit();
601 } catch (SSLException e) {
602 Instance.getTraceHandler().error(
603 "Bad access key for remote library");
604 exitCode = 43;
605 } catch (IOException e) {
606 Instance.getTraceHandler().error(e);
607 exitCode = 44;
608 }
609
610 break;
611 case REMOTE:
612 exitCode = 255; // should not be reachable (REMOTE -> START)
613 break;
614 }
615 }
616
617 try {
618 Instance.getTempFiles().close();
619 } catch (IOException e) {
620 Instance.getTraceHandler()
621 .error(new IOException(
622 "Cannot dispose of the temporary files", e));
623 }
624
625 if (exitCode == 255) {
626 syntax(false);
627 }
628
629 System.exit(exitCode);
630 }
631
632 /**
633 * Import the given resource into the {@link LocalLibrary}.
634 *
635 * @param urlString
636 * the resource to import
637 * @param pg
638 * the optional progress reporter
639 *
640 * @return the exit return code (0 = success)
641 */
642 public static int imprt(String urlString, Progress pg) {
643 try {
644 Story story = Instance.getLibrary().imprt(
645 BasicReader.getUrl(urlString), pg);
646 System.out.println(story.getMeta().getLuid() + ": \""
647 + story.getMeta().getTitle() + "\" imported.");
648 } catch (IOException e) {
649 Instance.getTraceHandler().error(e);
650 return 1;
651 }
652
653 return 0;
654 }
655
656 /**
657 * Export the {@link Story} from the {@link LocalLibrary} to the given
658 * target.
659 *
660 * @param luid
661 * the story LUID
662 * @param typeString
663 * the {@link OutputType} to use
664 * @param target
665 * the target
666 * @param pg
667 * the optional progress reporter
668 *
669 * @return the exit return code (0 = success)
670 */
671 public static int export(String luid, String typeString, String target,
672 Progress pg) {
673 OutputType type = OutputType.valueOfNullOkUC(typeString, null);
674 if (type == null) {
675 Instance.getTraceHandler().error(
676 new Exception(trans(StringId.OUTPUT_DESC, typeString)));
677 return 1;
678 }
679
680 try {
681 Instance.getLibrary().export(luid, type, target, pg);
682 } catch (IOException e) {
683 Instance.getTraceHandler().error(e);
684 return 4;
685 }
686
687 return 0;
688 }
689
690 /**
691 * List the stories of the given source from the {@link LocalLibrary}
692 * (unless NULL is passed, in which case all stories will be listed).
693 *
694 * @param source
695 * the source to list the known stories of, or NULL to list all
696 * stories
697 *
698 * @return the exit return code (0 = success)
699 */
700 private static int list(String source) {
701 BasicReader.setDefaultReaderType(ReaderType.CLI);
702 try {
703 BasicReader.getReader().browse(source);
704 } catch (IOException e) {
705 Instance.getTraceHandler().error(e);
706 return 66;
707 }
708
709 return 0;
710 }
711
712 /**
713 * Start the current reader for this {@link Story}.
714 *
715 * @param story
716 * the LUID of the {@link Story} in the {@link LocalLibrary}
717 * <b>or</b> the {@link Story} {@link URL}
718 * @param chapString
719 * which {@link Chapter} to read (starting at 1), or NULL to get
720 * the {@link Story} description
721 * @param library
722 * TRUE if the source is the {@link Story} LUID, FALSE if it is a
723 * {@link URL}
724 *
725 * @return the exit return code (0 = success)
726 */
727 private static int read(String story, String chapString, boolean library) {
728 try {
729 Reader reader = BasicReader.getReader();
730 if (library) {
731 reader.setMeta(story);
732 } else {
733 reader.setMeta(BasicReader.getUrl(story), null);
734 }
735
736 if (chapString != null) {
737 try {
738 reader.setChapter(Integer.parseInt(chapString));
739 reader.read(true);
740 } catch (NumberFormatException e) {
741 Instance.getTraceHandler().error(
742 new IOException("Chapter number cannot be parsed: "
743 + chapString, e));
744 return 2;
745 }
746 } else {
747 reader.read(true);
748 }
749 } catch (IOException e) {
750 Instance.getTraceHandler().error(e);
751 return 1;
752 }
753
754 return 0;
755 }
756
757 /**
758 * Convert the {@link Story} into another format.
759 *
760 * @param urlString
761 * the source {@link Story} to convert
762 * @param typeString
763 * the {@link OutputType} to convert to
764 * @param target
765 * the target file
766 * @param infoCover
767 * TRUE to also export the cover and info file, even if the given
768 * {@link OutputType} does not usually save them
769 * @param pg
770 * the optional progress reporter
771 *
772 * @return the exit return code (0 = success)
773 */
774 public static int convert(String urlString, String typeString,
775 String target, boolean infoCover, Progress pg) {
776 int exitCode = 0;
777
778 Instance.getTraceHandler().trace("Convert: " + urlString);
779 String sourceName = urlString;
780 try {
781 URL source = BasicReader.getUrl(urlString);
782 sourceName = source.toString();
783 if (source.toString().startsWith("file://")) {
784 sourceName = sourceName.substring("file://".length());
785 }
786
787 OutputType type = OutputType.valueOfAllOkUC(typeString, null);
788 if (type == null) {
789 Instance.getTraceHandler().error(
790 new IOException(trans(StringId.ERR_BAD_OUTPUT_TYPE,
791 typeString)));
792
793 exitCode = 2;
794 } else {
795 try {
796 BasicSupport support = BasicSupport.getSupport(source);
797
798 if (support != null) {
799 Instance.getTraceHandler().trace(
800 "Support found: " + support.getClass());
801 Progress pgIn = new Progress();
802 Progress pgOut = new Progress();
803 if (pg != null) {
804 pg.setMax(2);
805 pg.addProgress(pgIn, 1);
806 pg.addProgress(pgOut, 1);
807 }
808
809 Story story = support.process(pgIn);
810 try {
811 target = new File(target).getAbsolutePath();
812 BasicOutput.getOutput(type, infoCover, infoCover)
813 .process(story, target, pgOut);
814 } catch (IOException e) {
815 Instance.getTraceHandler().error(
816 new IOException(trans(StringId.ERR_SAVING,
817 target), e));
818 exitCode = 5;
819 }
820 } else {
821 Instance.getTraceHandler().error(
822 new IOException(trans(
823 StringId.ERR_NOT_SUPPORTED, source)));
824
825 exitCode = 4;
826 }
827 } catch (IOException e) {
828 Instance.getTraceHandler().error(
829 new IOException(trans(StringId.ERR_LOADING,
830 sourceName), e));
831 exitCode = 3;
832 }
833 }
834 } catch (MalformedURLException e) {
835 Instance.getTraceHandler()
836 .error(new IOException(trans(StringId.ERR_BAD_URL,
837 sourceName), e));
838 exitCode = 1;
839 }
840
841 return exitCode;
842 }
843
844 /**
845 * Simple shortcut method to call {link Instance#getTrans()#getString()}.
846 *
847 * @param id
848 * the ID to translate
849 *
850 * @return the translated result
851 */
852 private static String trans(StringId id, Object... params) {
853 return Instance.getTrans().getString(id, params);
854 }
855
856 /**
857 * Display the correct syntax of the program to the user to stdout, or an
858 * error message if the syntax used was wrong on stderr.
859 *
860 * @param showHelp
861 * TRUE to show the syntax help, FALSE to show "syntax error"
862 */
863 private static void syntax(boolean showHelp) {
864 if (showHelp) {
865 StringBuilder builder = new StringBuilder();
866 for (SupportType type : SupportType.values()) {
867 builder.append(trans(StringId.ERR_SYNTAX_TYPE, type.toString(),
868 type.getDesc()));
869 builder.append('\n');
870 }
871
872 String typesIn = builder.toString();
873 builder.setLength(0);
874
875 for (OutputType type : OutputType.values()) {
876 builder.append(trans(StringId.ERR_SYNTAX_TYPE, type.toString(),
877 type.getDesc(true)));
878 builder.append('\n');
879 }
880
881 String typesOut = builder.toString();
882
883 System.out.println(trans(StringId.HELP_SYNTAX, typesIn, typesOut));
884 } else {
885 System.err.println(trans(StringId.ERR_SYNTAX));
886 }
887 }
888
889 /**
890 * Set the default reader type for this session only (it can be changed in
891 * the configuration file, too, but this value will override it).
892 *
893 * @param readerTypeString
894 * the type
895 */
896 private static int setReaderType(String readerTypeString) {
897 try {
898 ReaderType readerType = ReaderType.valueOf(readerTypeString
899 .toUpperCase());
900 BasicReader.setDefaultReaderType(readerType);
901 return 0;
902 } catch (IllegalArgumentException e) {
903 Instance.getTraceHandler().error(
904 new IOException("Unknown reader type: " + readerTypeString,
905 e));
906 return 1;
907 }
908 }
909 }