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