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