| 1 | package be.nikiroo.fanfix.reader.cli; |
| 2 | |
| 3 | import java.io.IOException; |
| 4 | import java.util.List; |
| 5 | |
| 6 | import be.nikiroo.fanfix.Instance; |
| 7 | import be.nikiroo.fanfix.bundles.StringId; |
| 8 | import be.nikiroo.fanfix.data.Chapter; |
| 9 | import be.nikiroo.fanfix.data.MetaData; |
| 10 | import be.nikiroo.fanfix.data.Paragraph; |
| 11 | import be.nikiroo.fanfix.data.Story; |
| 12 | import be.nikiroo.fanfix.reader.FanfixReader; |
| 13 | import be.nikiroo.fanfix.supported.BasicSupport.SupportType; |
| 14 | |
| 15 | /** |
| 16 | * Command line {@link Story} reader. |
| 17 | * <p> |
| 18 | * Will output stories to the console. |
| 19 | * |
| 20 | * @author niki |
| 21 | */ |
| 22 | public class CliReader extends FanfixReader { |
| 23 | /** |
| 24 | * Start the {@link Story} Reading. |
| 25 | * |
| 26 | * @throws IOException |
| 27 | * in case of I/O error or if the {@link Story} was not |
| 28 | * previously set |
| 29 | */ |
| 30 | public void read() throws IOException { |
| 31 | if (getStory() == null) { |
| 32 | throw new IOException("No story to read"); |
| 33 | } |
| 34 | |
| 35 | String title = ""; |
| 36 | String author = ""; |
| 37 | |
| 38 | MetaData meta = getStory().getMeta(); |
| 39 | if (meta != null) { |
| 40 | if (meta.getTitle() != null) { |
| 41 | title = meta.getTitle(); |
| 42 | } |
| 43 | |
| 44 | if (meta.getAuthor() != null) { |
| 45 | author = "©" + meta.getAuthor(); |
| 46 | if (meta.getDate() != null && !meta.getDate().isEmpty()) { |
| 47 | author = author + " (" + meta.getDate() + ")"; |
| 48 | } |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | System.out.println(title); |
| 53 | System.out.println(author); |
| 54 | System.out.println(""); |
| 55 | |
| 56 | for (Chapter chap : getStory()) { |
| 57 | if (chap.getName() != null && !chap.getName().isEmpty()) { |
| 58 | System.out.println(Instance.getTrans().getString( |
| 59 | StringId.CHAPTER_NAMED, chap.getNumber(), |
| 60 | chap.getName())); |
| 61 | } else { |
| 62 | System.out.println(Instance.getTrans().getString( |
| 63 | StringId.CHAPTER_UNNAMED, chap.getNumber())); |
| 64 | } |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Read the selected chapter (starting at 1). |
| 70 | * |
| 71 | * @param chapter |
| 72 | * the chapter |
| 73 | */ |
| 74 | public void read(int chapter) { |
| 75 | if (chapter > getStory().getChapters().size()) { |
| 76 | System.err.println("Chapter " + chapter + ": no such chapter"); |
| 77 | } else { |
| 78 | Chapter chap = getStory().getChapters().get(chapter - 1); |
| 79 | System.out.println("Chapter " + chap.getNumber() + ": " |
| 80 | + chap.getName()); |
| 81 | |
| 82 | for (Paragraph para : chap) { |
| 83 | System.out.println(para.getContent()); |
| 84 | System.out.println(""); |
| 85 | } |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | @Override |
| 90 | public void start(SupportType type) { |
| 91 | List<MetaData> stories; |
| 92 | stories = Instance.getLibrary().getList(type); |
| 93 | |
| 94 | for (MetaData story : stories) { |
| 95 | String author = ""; |
| 96 | if (story.getAuthor() != null && !story.getAuthor().isEmpty()) { |
| 97 | author = " (" + story.getAuthor() + ")"; |
| 98 | } |
| 99 | |
| 100 | System.out.println(story.getLuid() + ": " + story.getTitle() |
| 101 | + author); |
| 102 | } |
| 103 | } |
| 104 | } |