update nikiroo-utils
[fanfix.git] / src / be / nikiroo / fanfix / reader / ui / GuiReaderViewerTextOutput.java
1 package be.nikiroo.fanfix.reader.ui;
2
3 import java.io.IOException;
4 import java.util.Arrays;
5
6 import be.nikiroo.fanfix.Instance;
7 import be.nikiroo.fanfix.data.Chapter;
8 import be.nikiroo.fanfix.data.Paragraph;
9 import be.nikiroo.fanfix.data.Paragraph.ParagraphType;
10 import be.nikiroo.fanfix.data.Story;
11 import be.nikiroo.fanfix.output.BasicOutput;
12
13 /**
14 * This class can export a chapter into HTML3 code ready for Java Swing support.
15 *
16 * @author niki
17 */
18 public class GuiReaderViewerTextOutput {
19 private StringBuilder builder;
20 private BasicOutput output;
21 private Story fakeStory;
22
23 /**
24 * Create a new {@link GuiReaderViewerTextOutput} that will convert a
25 * {@link Chapter} into HTML3 suited for Java Swing.
26 */
27 public GuiReaderViewerTextOutput() {
28 builder = new StringBuilder();
29 fakeStory = new Story();
30
31 output = new BasicOutput() {
32 private boolean paraInQuote;
33
34 @Override
35 protected void writeChapterHeader(Chapter chap) throws IOException {
36 builder.append("<HTML>");
37
38 builder.append("<H1>");
39 builder.append("Chapter ");
40 builder.append(chap.getNumber());
41 builder.append(": ");
42 builder.append(chap.getName());
43 builder.append("</H1>");
44
45 builder.append("<DIV align='justify'>");
46 }
47
48 @Override
49 protected void writeChapterFooter(Chapter chap) throws IOException {
50 if (paraInQuote) {
51 builder.append("</DIV>");
52 }
53 paraInQuote = false;
54
55 builder.append("</DIV>");
56 builder.append("</HTML>");
57 }
58
59 @Override
60 protected void writeParagraph(Paragraph para) throws IOException {
61 if ((para.getType() == ParagraphType.QUOTE) == !paraInQuote) {
62 paraInQuote = !paraInQuote;
63 if (paraInQuote) {
64 builder.append("<BR>");
65 builder.append("<DIV>");
66 } else {
67 builder.append("</DIV>");
68 builder.append("<BR>");
69 }
70 }
71
72 switch (para.getType()) {
73 case NORMAL:
74 builder.append("&nbsp;&nbsp;&nbsp;&nbsp;");
75 builder.append(decorateText(para.getContent()));
76 builder.append("<BR>");
77 break;
78 case BLANK:
79 builder.append("<BR><BR>");
80 break;
81 case BREAK:
82 builder.append("<BR><P COLOR='#7777DD' ALIGN='CENTER'><B>");
83 builder.append("* * *");
84 builder.append("</B></P><BR><BR>");
85 break;
86 case QUOTE:
87 builder.append("<DIV>");
88 builder.append("&nbsp;&nbsp;&nbsp;&nbsp;");
89 builder.append("&mdash;&nbsp;");
90 builder.append(decorateText(para.getContent()));
91 builder.append("</DIV>");
92
93 break;
94 case IMAGE:
95 }
96 }
97
98 @Override
99 protected String enbold(String word) {
100 return "<B COLOR='#7777DD'>" + word + "</B>";
101 }
102
103 @Override
104 protected String italize(String word) {
105 return "<I COLOR='GRAY'>" + word + "</I>";
106 }
107 };
108 }
109
110 /**
111 * Convert the chapter into HTML3 code.
112 *
113 * @param chap
114 * the {@link Chapter} to convert.
115 *
116 * @return HTML3 code tested with Java Swing
117 */
118 public String convert(Chapter chap) {
119 builder.setLength(0);
120 try {
121 fakeStory.setChapters(Arrays.asList(chap));
122 output.process(fakeStory, null, null);
123 } catch (IOException e) {
124 Instance.getTraceHandler().error(e);
125 }
126 return builder.toString();
127 }
128 }