2437cae567310700509c7c9a08a0a59212c7b1ec
[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 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 || search == 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 {
502 try {
503 BasicReader.getReader().search(searchOn, search, page,
504 item, true);
505 } catch (IOException e1) {
506 Instance.getTraceHandler().error(e1);
507 }
508 }
509
510 break;
511 case TAG:
512 if (searchOn == null) {
513 exitCode = 255;
514 break;
515 }
516
517 page = page == null ? 1 : page;
518 if (page < 0) {
519 Instance.getTraceHandler().error("Incorrect page number");
520 exitCode = 255;
521 break;
522 }
523
524 item = item == null ? 0 : item;
525 if (item < 0) {
526 Instance.getTraceHandler().error("Incorrect item number");
527 exitCode = 255;
528 break;
529 }
530
531 if (BasicReader.getReader() == null) {
532 Instance.getTraceHandler()
533 .error(new Exception(
534 "No reader type has been configured"));
535 exitCode = 10;
536 break;
537 }
538
539 try {
540 BasicReader.getReader().searchTag(searchOn, page, item,
541 true, tags.toArray(new Integer[] {}));
542 } catch (IOException e1) {
543 Instance.getTraceHandler().error(e1);
544 }
545
546 break;
547 case HELP:
548 syntax(true);
549 exitCode = 0;
550 break;
551 case SET_READER:
552 exitCode = 255;
553 break;
554 case VERSION:
555 System.out
556 .println(String.format("Fanfix version %s"
557 + "%nhttps://github.com/nikiroo/fanfix/"
558 + "%n\tWritten by Nikiroo",
559 Version.getCurrentVersion()));
560 updates.ok(); // we consider it read
561 break;
562 case START:
563 if (BasicReader.getReader() == null) {
564 Instance.getTraceHandler()
565 .error(new Exception(
566 "No reader type has been configured"));
567 exitCode = 10;
568 break;
569 }
570 BasicReader.getReader().browse(null);
571 break;
572 case SERVER:
573 if (port == null) {
574 exitCode = 255;
575 break;
576 }
577 try {
578 ServerObject server = new RemoteLibraryServer(key, port);
579 server.setTraceHandler(Instance.getTraceHandler());
580 server.run();
581 } catch (IOException e) {
582 Instance.getTraceHandler().error(e);
583 }
584 return;
585 case STOP_SERVER:
586 if (port == null) {
587 exitCode = 255;
588 break;
589 }
590
591 new RemoteLibrary(key, host, port).exit();
592 break;
593 case REMOTE:
594 exitCode = 255; // should not be reachable (REMOTE -> START)
595 break;
596 }
597 }
598
599 try {
600 Instance.getTempFiles().close();
601 } catch (IOException e) {
602 Instance.getTraceHandler()
603 .error(new IOException(
604 "Cannot dispose of the temporary files", e));
605 }
606
607 if (exitCode == 255) {
608 syntax(false);
609 }
610
611 System.exit(exitCode);
612 }
613
614 /**
615 * Import the given resource into the {@link LocalLibrary}.
616 *
617 * @param urlString
618 * the resource to import
619 * @param pg
620 * the optional progress reporter
621 *
622 * @return the exit return code (0 = success)
623 */
624 public static int imprt(String urlString, Progress pg) {
625 try {
626 Story story = Instance.getLibrary().imprt(
627 BasicReader.getUrl(urlString), pg);
628 System.out.println(story.getMeta().getLuid() + ": \""
629 + story.getMeta().getTitle() + "\" imported.");
630 } catch (IOException e) {
631 Instance.getTraceHandler().error(e);
632 return 1;
633 }
634
635 return 0;
636 }
637
638 /**
639 * Export the {@link Story} from the {@link LocalLibrary} to the given
640 * target.
641 *
642 * @param luid
643 * the story LUID
644 * @param typeString
645 * the {@link OutputType} to use
646 * @param target
647 * the target
648 * @param pg
649 * the optional progress reporter
650 *
651 * @return the exit return code (0 = success)
652 */
653 public static int export(String luid, String typeString, String target,
654 Progress pg) {
655 OutputType type = OutputType.valueOfNullOkUC(typeString, null);
656 if (type == null) {
657 Instance.getTraceHandler().error(
658 new Exception(trans(StringId.OUTPUT_DESC, typeString)));
659 return 1;
660 }
661
662 try {
663 Instance.getLibrary().export(luid, type, target, pg);
664 } catch (IOException e) {
665 Instance.getTraceHandler().error(e);
666 return 4;
667 }
668
669 return 0;
670 }
671
672 /**
673 * List the stories of the given source from the {@link LocalLibrary}
674 * (unless NULL is passed, in which case all stories will be listed).
675 *
676 * @param source
677 * the source to list the known stories of, or NULL to list all
678 * stories
679 *
680 * @return the exit return code (0 = success)
681 */
682 private static int list(String source) {
683 List<MetaData> stories;
684 stories = BasicReader.getReader().getLibrary().getListBySource(source);
685
686 for (MetaData story : stories) {
687 String author = "";
688 if (story.getAuthor() != null && !story.getAuthor().isEmpty()) {
689 author = " (" + story.getAuthor() + ")";
690 }
691
692 System.out.println(story.getLuid() + ": " + story.getTitle()
693 + author);
694 }
695 return 0;
696 }
697
698 /**
699 * Start the current reader for this {@link Story}.
700 *
701 * @param story
702 * the LUID of the {@link Story} in the {@link LocalLibrary}
703 * <b>or</b> the {@link Story} {@link URL}
704 * @param chapString
705 * which {@link Chapter} to read (starting at 1), or NULL to get
706 * the {@link Story} description
707 * @param library
708 * TRUE if the source is the {@link Story} LUID, FALSE if it is a
709 * {@link URL}
710 *
711 * @return the exit return code (0 = success)
712 */
713 private static int read(String story, String chapString, boolean library) {
714 try {
715 Reader reader = BasicReader.getReader();
716 if (library) {
717 reader.setMeta(story);
718 } else {
719 reader.setMeta(BasicReader.getUrl(story), null);
720 }
721
722 if (chapString != null) {
723 try {
724 reader.setChapter(Integer.parseInt(chapString));
725 reader.read(true);
726 } catch (NumberFormatException e) {
727 Instance.getTraceHandler().error(
728 new IOException("Chapter number cannot be parsed: "
729 + chapString, e));
730 return 2;
731 }
732 } else {
733 reader.read(true);
734 }
735 } catch (IOException e) {
736 Instance.getTraceHandler().error(e);
737 return 1;
738 }
739
740 return 0;
741 }
742
743 /**
744 * Convert the {@link Story} into another format.
745 *
746 * @param urlString
747 * the source {@link Story} to convert
748 * @param typeString
749 * the {@link OutputType} to convert to
750 * @param target
751 * the target file
752 * @param infoCover
753 * TRUE to also export the cover and info file, even if the given
754 * {@link OutputType} does not usually save them
755 * @param pg
756 * the optional progress reporter
757 *
758 * @return the exit return code (0 = success)
759 */
760 public static int convert(String urlString, String typeString,
761 String target, boolean infoCover, Progress pg) {
762 int exitCode = 0;
763
764 Instance.getTraceHandler().trace("Convert: " + urlString);
765 String sourceName = urlString;
766 try {
767 URL source = BasicReader.getUrl(urlString);
768 sourceName = source.toString();
769 if (source.toString().startsWith("file://")) {
770 sourceName = sourceName.substring("file://".length());
771 }
772
773 OutputType type = OutputType.valueOfAllOkUC(typeString, null);
774 if (type == null) {
775 Instance.getTraceHandler().error(
776 new IOException(trans(StringId.ERR_BAD_OUTPUT_TYPE,
777 typeString)));
778
779 exitCode = 2;
780 } else {
781 try {
782 BasicSupport support = BasicSupport.getSupport(source);
783
784 if (support != null) {
785 Instance.getTraceHandler().trace(
786 "Support found: " + support.getClass());
787 Progress pgIn = new Progress();
788 Progress pgOut = new Progress();
789 if (pg != null) {
790 pg.setMax(2);
791 pg.addProgress(pgIn, 1);
792 pg.addProgress(pgOut, 1);
793 }
794
795 Story story = support.process(pgIn);
796 try {
797 target = new File(target).getAbsolutePath();
798 BasicOutput.getOutput(type, infoCover, infoCover)
799 .process(story, target, pgOut);
800 } catch (IOException e) {
801 Instance.getTraceHandler().error(
802 new IOException(trans(StringId.ERR_SAVING,
803 target), e));
804 exitCode = 5;
805 }
806 } else {
807 Instance.getTraceHandler().error(
808 new IOException(trans(
809 StringId.ERR_NOT_SUPPORTED, source)));
810
811 exitCode = 4;
812 }
813 } catch (IOException e) {
814 Instance.getTraceHandler().error(
815 new IOException(trans(StringId.ERR_LOADING,
816 sourceName), e));
817 exitCode = 3;
818 }
819 }
820 } catch (MalformedURLException e) {
821 Instance.getTraceHandler()
822 .error(new IOException(trans(StringId.ERR_BAD_URL,
823 sourceName), e));
824 exitCode = 1;
825 }
826
827 return exitCode;
828 }
829
830 /**
831 * Simple shortcut method to call {link Instance#getTrans()#getString()}.
832 *
833 * @param id
834 * the ID to translate
835 *
836 * @return the translated result
837 */
838 private static String trans(StringId id, Object... params) {
839 return Instance.getTrans().getString(id, params);
840 }
841
842 /**
843 * Display the correct syntax of the program to the user to stdout, or an
844 * error message if the syntax used was wrong on stderr.
845 *
846 * @param showHelp
847 * TRUE to show the syntax help, FALSE to show "syntax error"
848 */
849 private static void syntax(boolean showHelp) {
850 if (showHelp) {
851 StringBuilder builder = new StringBuilder();
852 for (SupportType type : SupportType.values()) {
853 builder.append(trans(StringId.ERR_SYNTAX_TYPE, type.toString(),
854 type.getDesc()));
855 builder.append('\n');
856 }
857
858 String typesIn = builder.toString();
859 builder.setLength(0);
860
861 for (OutputType type : OutputType.values()) {
862 builder.append(trans(StringId.ERR_SYNTAX_TYPE, type.toString(),
863 type.getDesc(true)));
864 builder.append('\n');
865 }
866
867 String typesOut = builder.toString();
868
869 System.out.println(trans(StringId.HELP_SYNTAX, typesIn, typesOut));
870 } else {
871 System.err.println(trans(StringId.ERR_SYNTAX));
872 }
873 }
874
875 /**
876 * Set the default reader type for this session only (it can be changed in
877 * the configuration file, too, but this value will override it).
878 *
879 * @param readerTypeString
880 * the type
881 */
882 private static int setReaderType(String readerTypeString) {
883 try {
884 ReaderType readerType = ReaderType.valueOf(readerTypeString
885 .toUpperCase());
886 BasicReader.setDefaultReaderType(readerType);
887 return 0;
888 } catch (IllegalArgumentException e) {
889 Instance.getTraceHandler().error(
890 new IOException("Unknown reader type: " + readerTypeString,
891 e));
892 return 1;
893 }
894 }
895 }