1 package org.apache.maven.doxia.module.xhtml;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.StringWriter;
23 import java.io.Writer;
24
25 import org.apache.maven.doxia.markup.HtmlMarkup;
26 import org.apache.maven.doxia.sink.Sink;
27 import org.apache.maven.doxia.sink.impl.AbstractSinkTest;
28 import org.apache.maven.doxia.sink.impl.SinkEventAttributeSet;
29 import org.junit.jupiter.api.Test;
30
31 import static org.apache.maven.doxia.util.HtmlTools.escapeHTML;
32 import static org.junit.jupiter.api.Assertions.assertEquals;
33 import static org.junit.jupiter.api.Assertions.assertTrue;
34
35
36
37
38
39 public class XhtmlSinkTest
40 extends AbstractSinkTest
41 {
42
43 protected String outputExtension()
44 {
45 return "xhtml";
46 }
47
48
49 protected Sink createSink( Writer writer )
50 {
51 return new XhtmlSink( writer, "UTF-8" );
52 }
53
54
55 protected boolean isXmlSink()
56 {
57 return true;
58 }
59
60
61
62
63
64 @Test
65 public void testLinks()
66 {
67 XhtmlSink sink = null;
68 Writer writer = new StringWriter();
69 try
70 {
71 sink = (XhtmlSink) createSink( writer );
72 sink.link( "http:/www.xdoc.com" );
73 sink.link_();
74 sink.link( "./index.html#anchor" );
75 sink.link_();
76 sink.link( "../index.html#anchor" );
77 sink.link_();
78 sink.link( "index.html" );
79 sink.link_();
80 }
81 finally
82 {
83 if ( sink != null )
84 {
85 sink.close();
86 }
87 }
88
89 String actual = writer.toString();
90 assertTrue( actual.contains( "<a class=\"externalLink\" href=\"http:/www.xdoc.com\"></a>" ) );
91 assertTrue( actual.contains( "<a href=\"./index.html#anchor\"></a>" ) );
92 assertTrue( actual.contains( "<a href=\"../index.html#anchor\"></a>" ) );
93 assertTrue( actual.contains( "<a href=\"index.html\"></a>" ) );
94 }
95
96
97 protected String getTitleBlock( String title )
98 {
99 return "<title>" + title + "</title>";
100 }
101
102
103 protected String getAuthorBlock( String author )
104 {
105 return author;
106 }
107
108
109 protected String getDateBlock( String date )
110 {
111 return date;
112 }
113
114
115 protected String getHeadBlock()
116 {
117 return "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">" +
118 "<html xmlns=\"http://www.w3.org/1999/xhtml\">\n<head>\n<title></title>\n<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\"/></head>";
119 }
120
121
122 protected String getBodyBlock()
123 {
124 return "<body></body></html>";
125 }
126
127
128 protected String getArticleBlock()
129 {
130 return "";
131 }
132
133
134 protected String getNavigationBlock()
135 {
136 return "";
137 }
138
139
140 protected String getSidebarBlock()
141 {
142 return "";
143 }
144
145
146 protected String getSectionTitleBlock( String title )
147 {
148 return title;
149 }
150
151
152 protected String getSection1Block( String title )
153 {
154 return "<div class=\"section\">\n<h2>" + title + "</h2></div>";
155 }
156
157
158 protected String getSection2Block( String title )
159 {
160 return "<div class=\"section\">\n<h3>" + title + "</h3></div>";
161 }
162
163
164 protected String getSection3Block( String title )
165 {
166 return "<div class=\"section\">\n<h4>" + title + "</h4></div>";
167 }
168
169
170 protected String getSection4Block( String title )
171 {
172 return "<div class=\"section\">\n<h5>" + title + "</h5></div>";
173 }
174
175
176 protected String getSection5Block( String title )
177 {
178 return "<div class=\"section\">\n<h6>" + title + "</h6></div>";
179 }
180
181
182 protected String getHeaderBlock()
183 {
184 return "";
185 }
186
187
188 protected String getContentBlock()
189 {
190 return "";
191 }
192
193
194 protected String getFooterBlock()
195 {
196 return "";
197 }
198
199
200 protected String getListBlock( String item )
201 {
202 return "<ul>\n<li>" + item + "</li></ul>";
203 }
204
205
206 protected String getNumberedListBlock( String item )
207 {
208 return "<ol style=\"list-style-type: lower-roman\">\n<li>" + item + "</li></ol>";
209 }
210
211
212 protected String getDefinitionListBlock( String definum, String definition )
213 {
214 return "<dl>\n<dt>" + definum + "</dt>\n<dd>" + definition + "</dd></dl>";
215 }
216
217
218 protected String getFigureBlock( String source, String caption )
219 {
220 String figureBlock = "<img src=\"" + escapeHTML( source, true ) + "\"";
221 if( caption != null )
222 {
223 figureBlock += " alt=\"" + caption + "\"";
224 }
225 else
226 {
227 figureBlock += " alt=\"\"";
228 }
229 figureBlock += " />";
230 return figureBlock;
231 }
232
233
234 protected String getTableBlock( String cell, String caption )
235 {
236 return "<table border=\"0\" class=\"bodyTable\">"
237 + "<caption>Table caption</caption><tr class=\"a\">\n<td>cell</td></tr>"
238 + "</table>";
239 }
240
241
242
243
244 @Test
245 public void testTable()
246 {
247 assertEquals( "", "", "Dummy!" );
248 }
249
250
251 protected String getParagraphBlock( String text )
252 {
253 return "<p>" + text + "</p>";
254 }
255
256
257 protected String getDataBlock( String value, String text )
258 {
259 return text;
260 }
261
262
263 protected String getTimeBlock( String datetime, String text )
264 {
265 return text;
266 }
267
268
269 protected String getAddressBlock( String text )
270 {
271 return "<address>" + text + "</address>";
272 }
273
274
275 protected String getBlockquoteBlock( String text )
276 {
277 return "<blockquote>" + text + "</blockquote>";
278 }
279
280
281 protected String getDivisionBlock( String text )
282 {
283 return "<div>" + text + "</div>";
284 }
285
286
287 protected String getVerbatimBlock( String text )
288 {
289 return "<div class=\"source\">\n<pre>" + text + "</pre></div>";
290 }
291
292
293 protected String getHorizontalRuleBlock()
294 {
295 return "<hr />";
296 }
297
298
299 protected String getPageBreakBlock()
300 {
301 return "<!-- PB -->";
302 }
303
304
305 protected String getAnchorBlock( String anchor )
306 {
307 return "<a name=\"" + anchor + "\">" + anchor + "</a>";
308 }
309
310
311 protected String getLinkBlock( String link, String text )
312 {
313 return "<a href=\"" + link + "\">" + text + "</a>";
314 }
315
316
317 protected String getInlineBlock( String text )
318 {
319 return text;
320 }
321
322
323 protected String getInlineItalicBlock( String text )
324 {
325 return "<i>" + text + "</i>";
326 }
327
328
329 protected String getInlineBoldBlock( String text )
330 {
331 return "<b>" + text + "</b>";
332 }
333
334
335 protected String getInlineCodeBlock( String text )
336 {
337 return "<code>" + text + "</code>";
338 }
339
340
341 protected String getItalicBlock( String text )
342 {
343 return "<i>" + text + "</i>";
344 }
345
346
347 protected String getBoldBlock( String text )
348 {
349 return "<b>" + text + "</b>";
350 }
351
352
353 protected String getMonospacedBlock( String text )
354 {
355 return "<tt>" + text + "</tt>";
356 }
357
358
359 protected String getLineBreakBlock()
360 {
361 return "<br />";
362 }
363
364
365 protected String getLineBreakOpportunityBlock()
366 {
367 return "";
368 }
369
370
371 protected String getNonBreakingSpaceBlock()
372 {
373 return " ";
374 }
375
376
377 protected String getTextBlock( String text )
378 {
379
380 return "~,_=,_-,_+,_*,_[,_],_<,_>,_{,_},_\\";
381 }
382
383
384 protected String getRawTextBlock( String text )
385 {
386 return text;
387 }
388
389
390
391
392 @Test
393 public void testEntities()
394 {
395 XhtmlSink sink = null;
396 Writer writer = new StringWriter();
397
398 try
399 {
400 sink = new XhtmlSink( writer );
401 sink.section( Sink.SECTION_LEVEL_1, null );
402 sink.sectionTitle( Sink.SECTION_LEVEL_1, null );
403 sink.text( "&", null );
404 sink.sectionTitle_( Sink.SECTION_LEVEL_1 );
405 sink.paragraph( null );
406 sink.text( "&", null );
407 sink.paragraph_();
408 sink.section_( Sink.SECTION_LEVEL_1 );
409 }
410 finally
411 {
412 sink.close();
413 }
414
415 assertEquals( "<div class=\"section\">\n<h2>&</h2>\n<p>&</p></div>", writer.toString() );
416 }
417
418
419
420
421 @Test
422 public void testHead()
423 {
424 XhtmlSink sink = null;
425 Writer writer = new StringWriter();
426
427 try
428 {
429 sink = new XhtmlSink( writer );
430 sink.head();
431 sink.title();
432 sink.text( "Title" );
433 sink.title_();
434 sink.comment( "A comment" );
435 sink.author();
436
437 sink.text( "ģ&" );
438 sink.author_();
439 SinkEventAttributeSet atts = new SinkEventAttributeSet( 1 );
440 atts.addAttribute( "href", "http://maven.apache.org/" );
441 sink.unknown( "base", new Object[] { HtmlMarkup.TAG_TYPE_SIMPLE }, atts );
442 sink.head_();
443 }
444 finally
445 {
446 sink.close();
447 }
448
449 String expected =
450 "<head>\n<title>Title</title><!--A comment--><meta name=\"author\" content=\"ģ&\" />"
451 + "<base href=\"http://maven.apache.org/\" /></head>";
452 String actual = writer.toString();
453 assertTrue( actual.contains( expected ), actual );
454 }
455
456
457 protected String getCommentBlock( String text )
458 {
459 return "<!--" + toXmlComment( text ) + "-->";
460 }
461 }