| 1 | package be.nikiroo.fanfix.reader; |
| 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 | |
| 13 | /** |
| 14 | * Command line {@link Story} reader. |
| 15 | * <p> |
| 16 | * Will output stories to the console. |
| 17 | * |
| 18 | * @author niki |
| 19 | */ |
| 20 | class CliReader extends BasicReader { |
| 21 | @Override |
| 22 | public void read() throws IOException { |
| 23 | MetaData meta = getMeta(); |
| 24 | |
| 25 | if (meta == null) { |
| 26 | throw new IOException("No story to read"); |
| 27 | } |
| 28 | |
| 29 | String title = ""; |
| 30 | String author = ""; |
| 31 | |
| 32 | if (meta.getTitle() != null) { |
| 33 | title = meta.getTitle(); |
| 34 | } |
| 35 | |
| 36 | if (meta.getAuthor() != null) { |
| 37 | author = "©" + meta.getAuthor(); |
| 38 | if (meta.getDate() != null && !meta.getDate().isEmpty()) { |
| 39 | author = author + " (" + meta.getDate() + ")"; |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | System.out.println(title); |
| 44 | System.out.println(author); |
| 45 | System.out.println(""); |
| 46 | |
| 47 | // TODO: progress? |
| 48 | for (Chapter chap : getStory(null)) { |
| 49 | if (chap.getName() != null && !chap.getName().isEmpty()) { |
| 50 | System.out.println(Instance.getTrans().getString( |
| 51 | StringId.CHAPTER_NAMED, chap.getNumber(), |
| 52 | chap.getName())); |
| 53 | } else { |
| 54 | System.out.println(Instance.getTrans().getString( |
| 55 | StringId.CHAPTER_UNNAMED, chap.getNumber())); |
| 56 | } |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | public void read(int chapter) throws IOException { |
| 61 | MetaData meta = getMeta(); |
| 62 | |
| 63 | if (meta == null) { |
| 64 | throw new IOException("No story to read"); |
| 65 | } |
| 66 | |
| 67 | // TODO: progress? |
| 68 | if (chapter > getStory(null).getChapters().size()) { |
| 69 | System.err.println("Chapter " + chapter + ": no such chapter"); |
| 70 | } else { |
| 71 | Chapter chap = getStory(null).getChapters().get(chapter - 1); |
| 72 | System.out.println("Chapter " + chap.getNumber() + ": " |
| 73 | + chap.getName()); |
| 74 | |
| 75 | for (Paragraph para : chap) { |
| 76 | System.out.println(para.getContent()); |
| 77 | System.out.println(""); |
| 78 | } |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | @Override |
| 83 | public void browse(String source) { |
| 84 | List<MetaData> stories; |
| 85 | stories = getLibrary().getListBySource(source); |
| 86 | |
| 87 | for (MetaData story : stories) { |
| 88 | String author = ""; |
| 89 | if (story.getAuthor() != null && !story.getAuthor().isEmpty()) { |
| 90 | author = " (" + story.getAuthor() + ")"; |
| 91 | } |
| 92 | |
| 93 | System.out.println(story.getLuid() + ": " + story.getTitle() |
| 94 | + author); |
| 95 | } |
| 96 | } |
| 97 | } |