View Javadoc
1   package org.apache.maven.doxia.module.xhtml;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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   * @author Jason van Zyl
34   * @since 1.0
35   */
36  public class XhtmlSinkTest
37      extends AbstractSinkTest
38  {
39      /** {@inheritDoc} */
40      protected String outputExtension()
41      {
42          return "xhtml";
43      }
44  
45      /** {@inheritDoc} */
46      protected Sink createSink( Writer writer )
47      {
48          return new XhtmlSink( writer, "UTF-8" );
49      }
50  
51      /** {@inheritDoc} */
52      protected boolean isXmlSink()
53      {
54          return true;
55      }
56  
57      /**
58       * Test link generation.
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      /** {@inheritDoc} */
93      protected String getTitleBlock( String title )
94      {
95          return "<title>" + title + "</title>";
96      }
97  
98      /** {@inheritDoc} */
99      protected String getAuthorBlock( String author )
100     {
101         return author;
102     }
103 
104     /** {@inheritDoc} */
105     protected String getDateBlock( String date )
106     {
107         return date;
108     }
109 
110     /** {@inheritDoc} */
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     /** {@inheritDoc} */
118     protected String getBodyBlock()
119     {
120         return "<body></body></html>";
121     }
122 
123     /** {@inheritDoc} */
124     protected String getArticleBlock()
125     {
126         return "";
127     }
128 
129     /** {@inheritDoc} */
130     protected String getNavigationBlock()
131     {
132         return "";
133     }
134 
135     /** {@inheritDoc} */
136     protected String getSidebarBlock()
137     {
138         return "";
139     }
140 
141     /** {@inheritDoc} */
142     protected String getSectionTitleBlock( String title )
143     {
144         return title;
145     }
146 
147     /** {@inheritDoc} */
148     protected String getSection1Block( String title )
149     {
150         return "<div class=\"section\">\n<h2>" + title + "</h2></div>";
151     }
152 
153     /** {@inheritDoc} */
154     protected String getSection2Block( String title )
155     {
156         return "<div class=\"section\">\n<h3>" + title + "</h3></div>";
157     }
158 
159     /** {@inheritDoc} */
160     protected String getSection3Block( String title )
161     {
162         return "<div class=\"section\">\n<h4>" + title + "</h4></div>";
163     }
164 
165     /** {@inheritDoc} */
166     protected String getSection4Block( String title )
167     {
168         return "<div class=\"section\">\n<h5>" + title + "</h5></div>";
169     }
170 
171     /** {@inheritDoc} */
172     protected String getSection5Block( String title )
173     {
174         return "<div class=\"section\">\n<h6>" + title + "</h6></div>";
175     }
176 
177     /** {@inheritDoc} */
178     protected String getHeaderBlock()
179     {
180         return "";
181     }
182 
183     /** {@inheritDoc} */
184     protected String getContentBlock()
185     {
186         return "";
187     }
188 
189     /** {@inheritDoc} */
190     protected String getFooterBlock()
191     {
192         return "";
193     }
194 
195     /** {@inheritDoc} */
196     protected String getListBlock( String item )
197     {
198         return "<ul>\n<li>" + item + "</li></ul>";
199     }
200 
201     /** {@inheritDoc} */
202     protected String getNumberedListBlock( String item )
203     {
204         return "<ol style=\"list-style-type: lower-roman\">\n<li>" + item + "</li></ol>";
205     }
206 
207     /** {@inheritDoc} */
208     protected String getDefinitionListBlock( String definum, String definition )
209     {
210         return "<dl>\n<dt>" + definum + "</dt>\n<dd>" + definition + "</dd></dl>";
211     }
212 
213     /** {@inheritDoc} */
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 //@todo fix DOXIA-361
222         {
223             figureBlock += " alt=\"\"";
224         }
225         figureBlock += " />";
226         return figureBlock;
227     }
228 
229     /** {@inheritDoc} */
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     // Disable testTable until the order of attributes issue is clarified
238     // TODO: remove
239     /** {@inheritDoc} */
240     public void testTable()
241     {
242         assertEquals( "Dummy!", "", "" );
243     }
244 
245     /** {@inheritDoc} */
246     protected String getParagraphBlock( String text )
247     {
248         return "<p>" + text + "</p>";
249     }
250 
251     /** {@inheritDoc} */
252     protected String getDataBlock( String value, String text )
253     {
254         return text;
255     }
256 
257     /** {@inheritDoc} */
258     protected String getTimeBlock( String datetime, String text )
259     {
260         return text;
261     }
262 
263     /** {@inheritDoc} */
264     protected String getAddressBlock( String text )
265     {
266         return "<address>" + text + "</address>";
267     }
268 
269     /** {@inheritDoc} */
270     protected String getBlockquoteBlock( String text )
271     {
272         return "<blockquote>" + text + "</blockquote>";
273     }
274 
275     /** {@inheritDoc} */
276     protected String getDivisionBlock( String text )
277     {
278         return "<div>" + text + "</div>";
279     }
280 
281     /** {@inheritDoc} */
282     protected String getVerbatimBlock( String text )
283     {
284         return "<div class=\"source\">\n<pre>" + text + "</pre></div>";
285     }
286 
287     /** {@inheritDoc} */
288     protected String getHorizontalRuleBlock()
289     {
290         return "<hr />";
291     }
292 
293     /** {@inheritDoc} */
294     protected String getPageBreakBlock()
295     {
296         return "<!-- PB -->";
297     }
298 
299     /** {@inheritDoc} */
300     protected String getAnchorBlock( String anchor )
301     {
302         return "<a name=\"" + anchor + "\">" + anchor + "</a>";
303     }
304 
305     /** {@inheritDoc} */
306     protected String getLinkBlock( String link, String text )
307     {
308         return "<a href=\"" + link + "\">" + text + "</a>";
309     }
310 
311     /** {@inheritDoc} */
312     protected String getInlineBlock( String text )
313     {
314         return text;
315     }
316 
317     /** {@inheritDoc} */
318     protected String getInlineItalicBlock( String text )
319     {
320         return "<i>" + text + "</i>";
321     }
322 
323     /** {@inheritDoc} */
324     protected String getInlineBoldBlock( String text )
325     {
326         return "<b>" + text + "</b>";
327     }
328 
329     /** {@inheritDoc} */
330     protected String getInlineCodeBlock( String text )
331     {
332         return "<code>" + text + "</code>";
333     }
334 
335     /** {@inheritDoc} */
336     protected String getItalicBlock( String text )
337     {
338         return "<i>" + text + "</i>";
339     }
340 
341     /** {@inheritDoc} */
342     protected String getBoldBlock( String text )
343     {
344         return "<b>" + text + "</b>";
345     }
346 
347     /** {@inheritDoc} */
348     protected String getMonospacedBlock( String text )
349     {
350         return "<tt>" + text + "</tt>";
351     }
352 
353     /** {@inheritDoc} */
354     protected String getLineBreakBlock()
355     {
356         return "<br />";
357     }
358 
359     /** {@inheritDoc} */
360     protected String getLineBreakOpportunityBlock()
361     {
362         return "";
363     }
364 
365     /** {@inheritDoc} */
366     protected String getNonBreakingSpaceBlock()
367     {
368         return "&#160;";
369     }
370 
371     /** {@inheritDoc} */
372     protected String getTextBlock( String text )
373     {
374         // TODO: need to be able to retreive those from outside the sink
375         return "~,_=,_-,_+,_*,_[,_],_&lt;,_&gt;,_{,_},_\\";
376     }
377 
378     /** {@inheritDoc} */
379     protected String getRawTextBlock( String text )
380     {
381         return text;
382     }
383 
384     /**
385      * Test entities is section titles and paragraphs.
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>&amp;</h2>\n<p>&amp;</p></div>", writer.toString() );
410     }
411 
412     /**
413      * Test head events.
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             // note: this is really illegal, there should be no un-resolved entities emitted into text()
430             sink.text( "&#x123;&" );
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=\"&#x123;&amp;\" />"
444                 + "<base href=\"http://maven.apache.org/\" /></head>";
445         String actual = writer.toString();
446         assertTrue( actual, actual.contains( expected ) );
447     }
448 
449     /** {@inheritDoc} */
450     protected String getCommentBlock( String text )
451     {
452         return "<!--" + toXmlComment( text ) + "-->";
453     }
454 }