Merge branch 'subtree'
[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 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.MetaData;
16 import be.nikiroo.fanfix.data.Story;
17 import be.nikiroo.fanfix.library.BasicLibrary;
18 import be.nikiroo.fanfix.library.CacheLibrary;
19 import be.nikiroo.fanfix.library.LocalLibrary;
20 import be.nikiroo.fanfix.library.RemoteLibrary;
21 import be.nikiroo.fanfix.library.RemoteLibraryServer;
22 import be.nikiroo.fanfix.output.BasicOutput;
23 import be.nikiroo.fanfix.output.BasicOutput.OutputType;
24 import be.nikiroo.fanfix.reader.BasicReader;
25 import be.nikiroo.fanfix.reader.CliReader;
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, 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>--version: get the version of the program</li>
79 * <li>--server: start the server mode (see config file for parameters)</li>
80 * <li>--stop-server: stop the running server on this port if any</li>
81 * <li>--remote [key] [host] [port]: use a the given remote library</li>
82 * </ul>
83 *
84 * @param args
85 * see method description
86 */
87 public static void main(String[] args) {
88 // Only one line, but very important:
89 Instance.init();
90
91 String urlString = null;
92 String luid = null;
93 String sourceString = null;
94 String titleString = null;
95 String authorString = null;
96 String chapString = null;
97 String target = null;
98 String key = null;
99 MainAction action = MainAction.START;
100 Boolean plusInfo = null;
101 String host = null;
102 Integer port = null;
103 SupportType searchOn = null;
104 String search = null;
105 List<Integer> tags = new ArrayList<Integer>();
106 Integer page = null;
107 Integer item = null;
108
109 boolean noMoreActions = false;
110
111 int exitCode = 0;
112 for (int i = 0; exitCode == 0 && i < args.length; i++) {
113 // Action (--) handling:
114 if (!noMoreActions && args[i].startsWith("--")) {
115 if (args[i].equals("--")) {
116 noMoreActions = true;
117 } else {
118 try {
119 action = MainAction.valueOf(args[i].substring(2)
120 .toUpperCase().replace("-", "_"));
121 } catch (Exception e) {
122 Instance.getInstance().getTraceHandler()
123 .error(new IllegalArgumentException("Unknown action: " + 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.getInstance().getTraceHandler().error("Website not known: <" + args[i] + ">");
225 exitCode = 41;
226 break;
227 }
228
229 if (BasicSearchable.getSearchable(searchOn) == null) {
230 Instance.getInstance().getTraceHandler().error("Website not supported: " + searchOn);
231 exitCode = 42;
232 break;
233 }
234 } else if (search == null) {
235 search = args[i];
236 } else if (page != null && page == -1) {
237 try {
238 page = Integer.parseInt(args[i]);
239 } catch (Exception e) {
240 page = -2;
241 }
242 } else if (item != null && item == -1) {
243 try {
244 item = Integer.parseInt(args[i]);
245 } catch (Exception e) {
246 item = -2;
247 }
248 } else if (page == null || item == null) {
249 if (page == null && "page".equals(args[i])) {
250 page = -1;
251 } else if (item == null && "item".equals(args[i])) {
252 item = -1;
253 } else {
254 exitCode = 255;
255 }
256 } else {
257 exitCode = 255;
258 }
259 break;
260 case SEARCH_TAG:
261 if (searchOn == null) {
262 searchOn = SupportType.valueOfAllOkUC(args[i]);
263
264 if (searchOn == null) {
265 Instance.getInstance().getTraceHandler().error("Website not known: <" + args[i] + ">");
266 exitCode = 255;
267 }
268
269 if (BasicSearchable.getSearchable(searchOn) == null) {
270 Instance.getInstance().getTraceHandler().error("Website not supported: " + searchOn);
271 exitCode = 255;
272 }
273 } else if (page == null && item == null) {
274 if ("page".equals(args[i])) {
275 page = -1;
276 } else if ("item".equals(args[i])) {
277 item = -1;
278 } else {
279 try {
280 int index = Integer.parseInt(args[i]);
281 tags.add(index);
282 } catch (NumberFormatException e) {
283 Instance.getInstance().getTraceHandler().error("Invalid tag index: " + args[i]);
284 exitCode = 255;
285 }
286 }
287 } else if (page != null && page == -1) {
288 try {
289 page = Integer.parseInt(args[i]);
290 } catch (Exception e) {
291 page = -2;
292 }
293 } else if (item != null && item == -1) {
294 try {
295 item = Integer.parseInt(args[i]);
296 } catch (Exception e) {
297 item = -2;
298 }
299 } else if (page == null || item == null) {
300 if (page == null && "page".equals(args[i])) {
301 page = -1;
302 } else if (item == null && "item".equals(args[i])) {
303 item = -1;
304 } else {
305 exitCode = 255;
306 }
307 } else {
308 exitCode = 255;
309 }
310 break;
311 case HELP:
312 exitCode = 255;
313 break;
314 case START:
315 exitCode = 255; // not supposed to be selected by user
316 break;
317 case VERSION:
318 exitCode = 255; // no arguments for this option
319 break;
320 case SERVER:
321 exitCode = 255; // no arguments for this option
322 break;
323 case STOP_SERVER:
324 exitCode = 255; // no arguments for this option
325 break;
326 case REMOTE:
327 if (key == null) {
328 key = args[i];
329 } else if (host == null) {
330 host = args[i];
331 } else if (port == null) {
332 port = Integer.parseInt(args[i]);
333
334 BasicLibrary lib = new RemoteLibrary(key, host, port);
335 lib = new CacheLibrary(
336 Instance.getInstance().getRemoteDir(host), lib,
337 Instance.getInstance().getUiConfig());
338
339 Instance.getInstance().setLibrary(lib);
340
341 action = MainAction.START;
342 } else {
343 exitCode = 255;
344 }
345 break;
346 }
347 }
348
349 final Progress mainProgress = new Progress(0, 80);
350 mainProgress.addProgressListener(new Progress.ProgressListener() {
351 private int current = mainProgress.getMin();
352
353 @Override
354 public void progress(Progress progress, String name) {
355 int diff = progress.getProgress() - current;
356 current += diff;
357
358 if (diff <= 0)
359 return;
360
361 StringBuilder builder = new StringBuilder();
362 for (int i = 0; i < diff; i++) {
363 builder.append('.');
364 }
365
366 System.err.print(builder.toString());
367
368 if (progress.isDone()) {
369 System.err.println("");
370 }
371 }
372 });
373 Progress pg = new Progress();
374 mainProgress.addProgress(pg, mainProgress.getMax());
375
376 VersionCheck updates = VersionCheck.check();
377 if (updates.isNewVersionAvailable()) {
378 // Sent to syserr so not to cause problem if one tries to capture a
379 // story content in text mode
380 System.err
381 .println("A new version of the program is available at https://github.com/nikiroo/fanfix/releases");
382 System.err.println("");
383 for (Version v : updates.getNewer()) {
384 System.err.println("\tVersion " + v);
385 System.err.println("\t-------------");
386 System.err.println("");
387 for (String it : updates.getChanges().get(v)) {
388 System.err.println("\t- " + it);
389 }
390 System.err.println("");
391 }
392 }
393
394 if (exitCode == 0) {
395 switch (action) {
396 case IMPORT:
397 exitCode = imprt(urlString, pg);
398 updates.ok(); // we consider it read
399 break;
400 case EXPORT:
401 exitCode = export(luid, sourceString, target, pg);
402 updates.ok(); // we consider it read
403 break;
404 case CONVERT:
405 exitCode = convert(urlString, sourceString, target,
406 plusInfo == null ? false : plusInfo, pg);
407 updates.ok(); // we consider it read
408 break;
409 case LIST:
410 exitCode = list(sourceString);
411 break;
412 case SET_SOURCE:
413 try {
414 Instance.getInstance().getLibrary().changeSource(luid, sourceString, pg);
415 } catch (IOException e1) {
416 Instance.getInstance().getTraceHandler().error(e1);
417 exitCode = 21;
418 }
419 break;
420 case SET_TITLE:
421 try {
422 Instance.getInstance().getLibrary().changeTitle(luid, titleString, pg);
423 } catch (IOException e1) {
424 Instance.getInstance().getTraceHandler().error(e1);
425 exitCode = 22;
426 }
427 break;
428 case SET_AUTHOR:
429 try {
430 Instance.getInstance().getLibrary().changeAuthor(luid, authorString, pg);
431 } catch (IOException e1) {
432 Instance.getInstance().getTraceHandler().error(e1);
433 exitCode = 23;
434 }
435 break;
436 case READ:
437 if (luid == null || luid.isEmpty()) {
438 syntax(false);
439 exitCode = 255;
440 break;
441 }
442
443 try {
444 BasicLibrary lib = Instance.getInstance().getLibrary();
445 exitCode = read(lib.getStory(luid, null), chapString);
446 } catch (IOException e) {
447 Instance.getInstance().getTraceHandler()
448 .error(new IOException("Failed to read book", e));
449 exitCode = 2;
450 }
451
452 break;
453 case READ_URL:
454 if (urlString == null || urlString.isEmpty()) {
455 syntax(false);
456 exitCode = 255;
457 break;
458 }
459
460 try {
461 BasicSupport support = BasicSupport
462 .getSupport(BasicReader.getUrl(urlString));
463 if (support == null) {
464 Instance.getInstance().getTraceHandler()
465 .error("URL not supported: " + urlString);
466 exitCode = 2;
467 break;
468 }
469
470 exitCode = read(support.process(null), chapString);
471 } catch (IOException e) {
472 Instance.getInstance().getTraceHandler()
473 .error(new IOException("Failed to read book", e));
474 exitCode = 2;
475 }
476
477 break;
478 case SEARCH:
479 page = page == null ? 1 : page;
480 if (page < 0) {
481 Instance.getInstance().getTraceHandler().error("Incorrect page number");
482 exitCode = 255;
483 break;
484 }
485
486 item = item == null ? 0 : item;
487 if (item < 0) {
488 Instance.getInstance().getTraceHandler().error("Incorrect item number");
489 exitCode = 255;
490 break;
491 }
492
493 try {
494 if (searchOn == null) {
495 new CliReader().listSearchables();
496 } else if (search != null) {
497
498 new CliReader().searchBooksByKeyword(searchOn, search, page,
499 item);
500 } else {
501 exitCode = 255;
502 }
503 } catch (IOException e1) {
504 Instance.getInstance().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.getInstance().getTraceHandler().error("Incorrect page number");
518 exitCode = 255;
519 break;
520 }
521
522 item = item == null ? 0 : item;
523 if (item < 0) {
524 Instance.getInstance().getTraceHandler().error("Incorrect item number");
525 exitCode = 255;
526 break;
527 }
528
529 try {
530 new CliReader().searchBooksByTag(searchOn, page, item,
531 tags.toArray(new Integer[] {}));
532 } catch (IOException e1) {
533 Instance.getInstance().getTraceHandler().error(e1);
534 }
535
536 break;
537 case HELP:
538 syntax(true);
539 exitCode = 0;
540 break;
541 case VERSION:
542 System.out
543 .println(String.format("Fanfix version %s"
544 + "%nhttps://github.com/nikiroo/fanfix/"
545 + "%n\tWritten by Nikiroo",
546 Version.getCurrentVersion()));
547 updates.ok(); // we consider it read
548 break;
549 case START:
550 try {
551 new CliReader().listBooks(null);
552 } catch (IOException e) {
553 Instance.getInstance().getTraceHandler().error(e);
554 exitCode = 66;
555 }
556 break;
557 case SERVER:
558 key = Instance.getInstance().getConfig().getString(Config.SERVER_KEY);
559 port = Instance.getInstance().getConfig().getInteger(Config.SERVER_PORT);
560 if (port == null) {
561 System.err.println("No port configured in the config file");
562 exitCode = 15;
563 break;
564 }
565 try {
566 ServerObject server = new RemoteLibraryServer(key, port);
567 server.setTraceHandler(Instance.getInstance().getTraceHandler());
568 server.run();
569 } catch (IOException e) {
570 Instance.getInstance().getTraceHandler().error(e);
571 }
572 return;
573 case STOP_SERVER:
574 // Can be given via "--remote XX XX XX"
575 if (key == null)
576 key = Instance.getInstance().getConfig().getString(Config.SERVER_KEY);
577 if (port == null)
578 port = Instance.getInstance().getConfig().getInteger(Config.SERVER_PORT);
579
580 if (port == null) {
581 System.err.println("No port given nor configured in the config file");
582 exitCode = 15;
583 break;
584 }
585 try {
586 new RemoteLibrary(key, host, port).exit();
587 } catch (SSLException e) {
588 Instance.getInstance().getTraceHandler().error(
589 "Bad access key for remote library");
590 exitCode = 43;
591 } catch (IOException e) {
592 Instance.getInstance().getTraceHandler().error(e);
593 exitCode = 44;
594 }
595
596 break;
597 case REMOTE:
598 exitCode = 255; // should not be reachable (REMOTE -> START)
599 break;
600 }
601 }
602
603 try {
604 Instance.getInstance().getTempFiles().close();
605 } catch (IOException e) {
606 Instance.getInstance().getTraceHandler().error(new IOException("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 MetaData meta = Instance.getInstance().getLibrary().imprt(BasicReader.getUrl(urlString), pg);
629 System.out.println(meta.getLuid() + ": \"" + meta.getTitle() + "\" imported.");
630 } catch (IOException e) {
631 Instance.getInstance().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.getInstance().getTraceHandler().error(new Exception(trans(StringId.OUTPUT_DESC, typeString)));
658 return 1;
659 }
660
661 try {
662 Instance.getInstance().getLibrary().export(luid, type, target, pg);
663 } catch (IOException e) {
664 Instance.getInstance().getTraceHandler().error(e);
665 return 4;
666 }
667
668 return 0;
669 }
670
671 /**
672 * List the stories of the given source from the {@link LocalLibrary}
673 * (unless NULL is passed, in which case all stories will be listed).
674 *
675 * @param source
676 * the source to list the known stories of, or NULL to list all
677 * stories
678 *
679 * @return the exit return code (0 = success)
680 */
681 private static int list(String source) {
682 try {
683 new CliReader().listBooks(source);
684 } catch (IOException e) {
685 Instance.getInstance().getTraceHandler().error(e);
686 return 66;
687 }
688
689 return 0;
690 }
691
692 /**
693 * Start the current reader for this {@link Story}.
694 *
695 * @param story
696 * the story to read
697 * @param chapString
698 * which {@link Chapter} to read (starting at 1), or NULL to get
699 * the {@link Story} description
700 *
701 * @return the exit return code (0 = success)
702 */
703 private static int read(Story story, String chapString) {
704 Integer chap = null;
705 if (chapString != null) {
706 try {
707 chap = Integer.parseInt(chapString);
708 } catch (NumberFormatException e) {
709 Instance.getInstance().getTraceHandler().error(new IOException(
710 "Chapter number cannot be parsed: " + chapString, e));
711 return 2;
712 }
713 }
714
715 if (story != null) {
716 try {
717 if (chap == null) {
718 new CliReader().listChapters(story);
719 } else {
720 new CliReader().printChapter(story, chap);
721 }
722 } catch (IOException e) {
723 Instance.getInstance().getTraceHandler()
724 .error(new IOException("Failed to read book", e));
725 return 2;
726 }
727 } else {
728 Instance.getInstance().getTraceHandler()
729 .error("Cannot find book: " + chapString);
730 return 2;
731 }
732
733 return 0;
734 }
735
736 /**
737 * Convert the {@link Story} into another format.
738 *
739 * @param urlString
740 * the source {@link Story} to convert
741 * @param typeString
742 * the {@link OutputType} to convert to
743 * @param target
744 * the target file
745 * @param infoCover
746 * TRUE to also export the cover and info file, even if the given
747 * {@link OutputType} does not usually save them
748 * @param pg
749 * the optional progress reporter
750 *
751 * @return the exit return code (0 = success)
752 */
753 public static int convert(String urlString, String typeString,
754 String target, boolean infoCover, Progress pg) {
755 int exitCode = 0;
756
757 Instance.getInstance().getTraceHandler().trace("Convert: " + urlString);
758 String sourceName = urlString;
759 try {
760 URL source = BasicReader.getUrl(urlString);
761 sourceName = source.toString();
762 if (sourceName.startsWith("file://")) {
763 sourceName = sourceName.substring("file://".length());
764 }
765
766 OutputType type = OutputType.valueOfAllOkUC(typeString, null);
767 if (type == null) {
768 Instance.getInstance().getTraceHandler()
769 .error(new IOException(trans(StringId.ERR_BAD_OUTPUT_TYPE, typeString)));
770
771 exitCode = 2;
772 } else {
773 try {
774 BasicSupport support = BasicSupport.getSupport(source);
775
776 if (support != null) {
777 Instance.getInstance().getTraceHandler().trace("Support found: " + support.getClass());
778 Progress pgIn = new Progress();
779 Progress pgOut = new Progress();
780 if (pg != null) {
781 pg.setMax(2);
782 pg.addProgress(pgIn, 1);
783 pg.addProgress(pgOut, 1);
784 }
785
786 Story story = support.process(pgIn);
787 try {
788 target = new File(target).getAbsolutePath();
789 BasicOutput.getOutput(type, infoCover, infoCover).process(story, target, pgOut);
790 } catch (IOException e) {
791 Instance.getInstance().getTraceHandler()
792 .error(new IOException(trans(StringId.ERR_SAVING, target), e));
793 exitCode = 5;
794 }
795 } else {
796 Instance.getInstance().getTraceHandler()
797 .error(new IOException(trans( StringId.ERR_NOT_SUPPORTED, source)));
798
799 exitCode = 4;
800 }
801 } catch (IOException e) {
802 Instance.getInstance().getTraceHandler()
803 .error(new IOException(trans(StringId.ERR_LOADING, sourceName), e));
804 exitCode = 3;
805 }
806 }
807 } catch (MalformedURLException e) {
808 Instance.getInstance().getTraceHandler().error(new IOException(trans(StringId.ERR_BAD_URL, sourceName), e));
809 exitCode = 1;
810 }
811
812 return exitCode;
813 }
814
815 /**
816 * Simple shortcut method to call {link Instance#getTrans()#getString()}.
817 *
818 * @param id
819 * the ID to translate
820 *
821 * @return the translated result
822 */
823 private static String trans(StringId id, Object... params) {
824 return Instance.getInstance().getTrans().getString(id, params);
825 }
826
827 /**
828 * Display the correct syntax of the program to the user to stdout, or an
829 * error message if the syntax used was wrong on stderr.
830 *
831 * @param showHelp
832 * TRUE to show the syntax help, FALSE to show "syntax error"
833 */
834 private static void syntax(boolean showHelp) {
835 if (showHelp) {
836 StringBuilder builder = new StringBuilder();
837 for (SupportType type : SupportType.values()) {
838 builder.append(trans(StringId.ERR_SYNTAX_TYPE, type.toString(),
839 type.getDesc()));
840 builder.append('\n');
841 }
842
843 String typesIn = builder.toString();
844 builder.setLength(0);
845
846 for (OutputType type : OutputType.values()) {
847 builder.append(trans(StringId.ERR_SYNTAX_TYPE, type.toString(),
848 type.getDesc(true)));
849 builder.append('\n');
850 }
851
852 String typesOut = builder.toString();
853
854 System.out.println(trans(StringId.HELP_SYNTAX, typesIn, typesOut));
855 } else {
856 System.err.println(trans(StringId.ERR_SYNTAX));
857 }
858 }
859 }