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.AbstractSinkTest;
24  import org.apache.maven.doxia.util.HtmlTools;
25  
26  import java.io.StringWriter;
27  import java.io.Writer;
28  import org.apache.maven.doxia.sink.SinkEventAttributeSet;
29  
30  /**
31   * @author <a href="mailto:jason@maven.org">Jason van Zyl</a>
32   * @version $Id: XdocSinkTest.java 1467294 2013-04-12 14:51:42Z rfscholte $
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 http://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 getSectionTitleBlock( String title )
92      {
93          return title;
94      }
95  
96      /** {@inheritDoc} */
97      protected String getSection1Block( String title )
98      {
99          return "<section name=\"" + title + "\"></section>";
100     }
101 
102     /** {@inheritDoc} */
103     protected String getSection2Block( String title )
104     {
105         return "<subsection name=\"" + title + "\"></subsection>";
106     }
107 
108     /** {@inheritDoc} */
109     protected String getSection3Block( String title )
110     {
111         return "<h4>" + title + "</h4>";
112     }
113 
114     /** {@inheritDoc} */
115     protected String getSection4Block( String title )
116     {
117         return "<h5>" + title + "</h5>";
118     }
119 
120     /** {@inheritDoc} */
121     protected String getSection5Block( String title )
122     {
123         return "<h6>" + title + "</h6>";
124     }
125 
126     /** {@inheritDoc} */
127     protected String getListBlock( String item )
128     {
129         return "<ul>\n<li>" + item + "</li></ul>";
130     }
131 
132     /** {@inheritDoc} */
133     protected String getNumberedListBlock( String item )
134     {
135         return "<ol style=\"list-style-type: lower-roman\">\n<li>" + item + "</li></ol>";
136     }
137 
138     /** {@inheritDoc} */
139     protected String getDefinitionListBlock( String definum, String definition )
140     {
141         return "<dl>\n<dt>" + definum + "</dt>\n<dd>" + definition + "</dd></dl>";
142     }
143 
144     /** {@inheritDoc} */
145     protected String getFigureBlock( String source, String caption )
146     {
147         String figureBlock = "<img src=\"" + source + "\"";
148         if ( caption != null )
149         {
150             figureBlock += " alt=\"" + caption + "\"";
151         }
152         else //@todo fix DOXIA-361
153         {
154             figureBlock += " alt=\"\"";
155         }
156         figureBlock += " />";
157         return figureBlock;
158     }
159 
160     /** {@inheritDoc} */
161     protected String getTableBlock( String cell, String caption )
162     {
163         return "<table border=\"0\"><caption>" + caption
164                 + "</caption>\n<tr valign=\"top\">\n<td>" + cell + "</td></tr></table>";
165     }
166 
167     /** {@inheritDoc} */
168     protected String getParagraphBlock( String text )
169     {
170         return "<p>" + text + "</p>";
171     }
172 
173     /** {@inheritDoc} */
174     protected String getVerbatimBlock( String text )
175     {
176         return "<source>" + text + "</source>";
177     }
178 
179     /** {@inheritDoc} */
180     protected String getHorizontalRuleBlock()
181     {
182         return "<hr />";
183     }
184 
185     /** {@inheritDoc} */
186     protected String getPageBreakBlock()
187     {
188         return "<!-- PB -->";
189     }
190 
191     /** {@inheritDoc} */
192     protected String getAnchorBlock( String anchor )
193     {
194         return "<a name=\"" + anchor + "\">" + anchor + "</a>";
195     }
196 
197     /** {@inheritDoc} */
198     protected String getLinkBlock( String link, String text )
199     {
200         return "<a href=\"" + link + "\">" + text + "</a>";
201     }
202 
203     /** {@inheritDoc} */
204     protected String getItalicBlock( String text )
205     {
206         return "<i>" + text + "</i>";
207     }
208 
209     /** {@inheritDoc} */
210     protected String getBoldBlock( String text )
211     {
212         return "<b>" + text + "</b>";
213     }
214 
215     /** {@inheritDoc} */
216     protected String getMonospacedBlock( String text )
217     {
218         return "<tt>" + text + "</tt>";
219     }
220 
221     /** {@inheritDoc} */
222     protected String getLineBreakBlock()
223     {
224         return "<br />";
225     }
226 
227     /** {@inheritDoc} */
228     protected String getNonBreakingSpaceBlock()
229     {
230         return "&#160;";
231     }
232 
233     /** {@inheritDoc} */
234     protected String getTextBlock( String text )
235     {
236         // TODO: need to be able to retreive those from outside the sink
237         return HtmlTools.escapeHTML( text );
238     }
239 
240     /** {@inheritDoc} */
241     protected String getRawTextBlock( String text )
242     {
243         return "~,_=,_-,_+,_*,_[,_],_<,_>,_{,_},_\\";
244     }
245 
246     /**
247      * Test verbatim.
248      */
249     public void testBoxedVerbatim()
250     {
251         Writer writer =  new StringWriter();
252         XdocSink sink = null;
253 
254         try
255         {
256             sink = new XdocSink( writer );
257 
258             sink.verbatim( null );
259             sink.verbatim_();
260             sink.verbatim( SinkEventAttributeSet.BOXED );
261             sink.verbatim_();
262             sink.verbatim( new SinkEventAttributeSet( new String[] {SinkEventAttributeSet.WIDTH, "20%"} ) );
263             sink.verbatim_();
264         }
265         finally
266         {
267             sink.close();
268         }
269 
270         assertEquals( "<pre></pre><source></source>\n<pre width=\"20%\"></pre>", writer.toString() );
271     }
272 
273     /**
274      * Test link.
275      */
276     public void testLinkWithTarget()
277     {
278         Writer writer =  new StringWriter();
279         XdocSink sink = null;
280 
281         try
282         {
283             sink = new XdocSink( writer );
284 
285             sink.link( "name", (String) null );
286             sink.link_();
287             sink.link( "name", "nirvana" );
288             sink.link_();
289         }
290         finally
291         {
292             sink.close();
293         }
294 
295         assertEquals( "<a href=\"name\"></a><a href=\"name\" target=\"nirvana\"></a>", writer.toString() );
296     }
297 
298     /** {@inheritDoc} */
299     protected String getCommentBlock( String text )
300     {
301         return "<!-- Simple comment with - - - - -->";
302     }
303 }