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