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