1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.doxia.module.xdoc;
20
21 import javax.swing.text.html.HTML.Attribute;
22
23 import java.io.StringWriter;
24 import java.io.Writer;
25
26 import org.apache.maven.doxia.sink.Sink;
27 import org.apache.maven.doxia.sink.SinkEventAttributes;
28 import org.apache.maven.doxia.sink.impl.AbstractSinkTest;
29 import org.apache.maven.doxia.sink.impl.SinkEventAttributeSet;
30 import org.junit.jupiter.api.Test;
31
32 import static org.apache.maven.doxia.util.HtmlTools.escapeHTML;
33 import static org.junit.jupiter.api.Assertions.assertEquals;
34
35
36
37
38
39 public class XdocSinkTest extends AbstractSinkTest {
40
41 @Test
42 public void testSection1() {
43 String title = "Title1";
44 getSink().section1();
45 getSink().sectionTitle1();
46 getSink().text(title);
47 getSink().sectionTitle1_();
48 getSink().section1_();
49 getSink().flush();
50 getSink().close();
51
52 String actual = getSinkContent();
53 String expected = getSection1Block(title);
54
55 assertEquals(expected, actual, "Wrong section1 block!");
56 }
57
58
59 @Test
60 public void testSection2() {
61 String title = "Title2";
62 getSink().section2();
63 getSink().sectionTitle2();
64 getSink().text(title);
65 getSink().sectionTitle2_();
66 getSink().section2_();
67 getSink().flush();
68 getSink().close();
69
70 String actual = getSinkContent();
71 String expected = getSection2Block(title);
72
73 assertEquals(expected, actual, "Wrong section2 block!");
74 }
75
76
77 protected String outputExtension() {
78 return "xml";
79 }
80
81
82 protected Sink createSink(Writer writer) {
83 return new XdocSink(writer, "UTF-8");
84 }
85
86
87 protected boolean isXmlSink() {
88 return true;
89 }
90
91
92 protected String getTitleBlock(String title) {
93 return "<title>" + title + "</title>";
94 }
95
96
97 protected String getAuthorBlock(String author) {
98 return author;
99 }
100
101
102 protected String getDateBlock(String date) {
103 return date;
104 }
105
106
107 protected String getHeadBlock() {
108 return "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
109 + "<document xmlns=\"http://maven.apache.org/XDOC/2.0\" "
110 + "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" "
111 + "xsi:schemaLocation=\"http://maven.apache.org/XDOC/2.0 https://maven.apache.org/xsd/xdoc-2.0.xsd\">"
112 + "<properties></properties>";
113 }
114
115
116 protected String getBodyBlock() {
117 return "<body></body></document>";
118 }
119
120
121 protected String getArticleBlock() {
122 return "<article></article>";
123 }
124
125
126 protected String getNavigationBlock() {
127 return "<nav></nav>";
128 }
129
130
131 protected String getSidebarBlock() {
132 return "<aside></aside>";
133 }
134
135
136 protected String getSectionTitleBlock(String title) {
137 return title;
138 }
139
140
141 protected String getSection1Block(String title) {
142 return "<section name=\"" + title + "\"></section>";
143 }
144
145
146 protected String getSection2Block(String title) {
147 return "<subsection name=\"" + title + "\"></subsection>";
148 }
149
150
151 protected String getSection3Block(String title) {
152 return "<header>" + EOL + "<h3>" + title + "</h3></header>";
153 }
154
155
156 protected String getSection4Block(String title) {
157 return "<header>" + EOL + "<h4>" + title + "</h4></header>";
158 }
159
160
161 protected String getSection5Block(String title) {
162 return "<header>" + EOL + "<h5>" + title + "</h5></header>";
163 }
164
165
166 protected String getSection6Block(String title) {
167 return "<header>" + EOL + "<h6>" + title + "</h6></header>";
168 }
169
170
171 protected String getHeaderBlock() {
172 return "<header></header>";
173 }
174
175
176 protected String getContentBlock() {
177 return "<main>" + EOL + "<div class=\"content\"></div></main>";
178 }
179
180
181 protected String getFooterBlock() {
182 return "<footer></footer>";
183 }
184
185
186 protected String getListBlock(String item) {
187 return "<ul>\n<li>" + item + "</li></ul>";
188 }
189
190
191 protected String getNumberedListBlock(String item) {
192 return "<ol style=\"list-style-type: lower-roman;\">\n<li>" + item + "</li></ol>";
193 }
194
195
196 protected String getDefinitionListBlock(String definum, String definition) {
197 return "<dl>\n<dt>" + definum + "</dt>\n<dd>" + definition + "</dd></dl>";
198 }
199
200
201 protected String getFigureBlock(String source, String caption) {
202 String figureBlock = "<figure><img src=\"" + escapeHTML(source) + "\" />";
203 if (caption != null) {
204 figureBlock += "<figcaption>" + caption + "</figcaption>";
205 }
206 figureBlock += "</figure>";
207 return figureBlock;
208 }
209
210
211 protected String getTableBlock(String cell, String caption) {
212 return "<table border=\"0\"><caption>" + caption + "</caption>\n<tr>\n<td style=\"text-align: center;\">" + cell
213 + "</td></tr></table>";
214 }
215
216 @Override
217 protected String getTableWithHeaderBlock(String... rowPrefixes) {
218 return "<table border=\"0\">\n<tr>\n<th>" + rowPrefixes[0] + "0</th>\n<th>" + rowPrefixes[0] + "1</th>\n<th>"
219 + rowPrefixes[0] + "2</th></tr>\n"
220 + "<tr>\n<td style=\"text-align: left;\">" + rowPrefixes[1]
221 + "0</td>\n<td style=\"text-align: right;\">" + rowPrefixes[1]
222 + "1</td>\n<td style=\"text-align: center;\">" + rowPrefixes[1] + "2</td></tr>\n"
223 + "<tr>\n<td style=\"text-align: left;\">" + rowPrefixes[2]
224 + "0</td>\n<td style=\"text-align: right;\">" + rowPrefixes[2]
225 + "1</td>\n<td style=\"text-align: center;\">" + rowPrefixes[2] + "2</td></tr>"
226 + "</table>";
227 }
228
229
230 protected String getParagraphBlock(String text) {
231 return "<p>" + text + "</p>";
232 }
233
234
235 protected String getDataBlock(String value, String text) {
236 return "<data value=\"" + value + "\">" + text + "</data>";
237 }
238
239
240 protected String getTimeBlock(String datetime, String text) {
241 return "<time datetime=\"" + datetime + "\">" + text + "</time>";
242 }
243
244
245 protected String getAddressBlock(String text) {
246 return "<address>" + text + "</address>";
247 }
248
249
250 protected String getBlockquoteBlock(String text) {
251 return "<blockquote>" + text + "</blockquote>";
252 }
253
254
255 protected String getDivisionBlock(String text) {
256 return "<div>" + text + "</div>";
257 }
258
259
260 protected String getVerbatimSourceBlock(String text) {
261 return "<source>" + text + "</source>";
262 }
263
264
265 protected String getHorizontalRuleBlock() {
266 return "<hr />";
267 }
268
269
270 protected String getPageBreakBlock() {
271 return "<!-- PB -->";
272 }
273
274
275 protected String getAnchorBlock(String anchor) {
276 return "<a id=\"" + anchor + "\">" + anchor + "</a>";
277 }
278
279
280 protected String getLinkBlock(String link, String text) {
281 return "<a href=\"" + link + "\">" + text + "</a>";
282 }
283
284
285 protected String getInlineBlock(String text) {
286 return text;
287 }
288
289
290 protected String getInlineItalicBlock(String text) {
291 return "<i>" + text + "</i>";
292 }
293
294
295 protected String getInlineBoldBlock(String text) {
296 return "<b>" + text + "</b>";
297 }
298
299
300 protected String getInlineCodeBlock(String text) {
301 return "<code>" + text + "</code>";
302 }
303
304
305 protected String getLineBreakBlock() {
306 return "<br />";
307 }
308
309
310 protected String getLineBreakOpportunityBlock() {
311 return "<wbr />";
312 }
313
314
315 protected String getNonBreakingSpaceBlock() {
316 return " ";
317 }
318
319
320 protected String getTextBlock(String text) {
321
322 return escapeHTML(text);
323 }
324
325
326 protected String getRawTextBlock(String text) {
327 return "~,_=,_-,_+,_*,_[,_],_<,_>,_{,_},_\\";
328 }
329
330
331
332
333 @Test
334 public void testVerbatimSource() {
335 Writer writer = new StringWriter();
336 XdocSink sink = null;
337
338 try {
339 sink = new XdocSink(writer);
340
341 sink.verbatim(null);
342 sink.verbatim_();
343 sink.verbatim(SinkEventAttributeSet.SOURCE);
344 sink.verbatim_();
345 } finally {
346 sink.close();
347 }
348
349 assertEquals("<pre></pre><source></source>", writer.toString());
350 }
351
352
353
354
355 @Test
356 public void testLinkWithTarget() {
357 Writer writer = new StringWriter();
358 XdocSink sink = null;
359
360 try {
361 sink = new XdocSink(writer);
362
363 sink.link("name");
364 sink.link_();
365 SinkEventAttributes attrs = new SinkEventAttributeSet();
366 attrs.addAttribute(Attribute.TARGET, "nirvana");
367 sink.link("name", attrs);
368 sink.link_();
369 } finally {
370 sink.close();
371 }
372
373 assertEquals("<a href=\"name\"></a><a target=\"nirvana\" href=\"name\"></a>", writer.toString());
374 }
375
376
377 protected String getCommentBlock(String text) {
378 return "<!--" + toXmlComment(text) + "-->";
379 }
380 }