View Javadoc
1   package org.apache.maven.doxia.module.docbook;
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.Writer;
23  import java.util.Locale;
24  
25  import javax.swing.text.MutableAttributeSet;
26  import javax.swing.text.SimpleAttributeSet;
27  
28  import org.apache.maven.doxia.sink.Sink;
29  import org.apache.maven.doxia.sink.impl.AbstractSinkTest;
30  import org.apache.maven.doxia.sink.impl.SinkUtils;
31  import org.apache.maven.doxia.util.DoxiaUtils;
32  import org.codehaus.plexus.util.FileUtils;
33  
34  import static org.apache.maven.doxia.util.HtmlTools.escapeHTML;
35  
36  /**
37   * @author <a href="mailto:jason@maven.org">Jason van Zyl</a>
38   */
39  public class DocBookSinkTest extends AbstractSinkTest
40  {
41      /** {@inheritDoc} */
42      protected String outputExtension()
43      {
44          return "docbook";
45      }
46  
47      /** {@inheritDoc} */
48      protected Sink createSink( Writer writer )
49      {
50          return new DocBookSink( writer, "UTF-8" );
51      }
52  
53      /** {@inheritDoc} */
54      protected boolean isXmlSink()
55      {
56          return true;
57      }
58  
59      /** {@inheritDoc} */
60      protected String getTitleBlock( String title )
61      {
62          return "<articleinfo><title>" + title + "</title>";
63      }
64  
65      /** {@inheritDoc} */
66      protected String getAuthorBlock( String author )
67      {
68          return "<corpauthor>" + author + "</corpauthor>";
69      }
70  
71      /** {@inheritDoc} */
72      protected String getDateBlock( String date )
73      {
74          return "<date>" + date + "</date>";
75      }
76  
77      /** {@inheritDoc} */
78      protected String getHeadBlock()
79      {
80          return "<?xml version=\"1.0\" encoding=\"UTF-8\"?><!DOCTYPE article PUBLIC \""
81                  + SimplifiedDocbookMarkup.DEFAULT_XML_PUBLIC_ID + "\" "
82                  + "\"" + SimplifiedDocbookMarkup.DEFAULT_XML_SYSTEM_ID + "\"><article>";
83      }
84  
85      /** {@inheritDoc} */
86      protected String getBodyBlock()
87      {
88          return "</article>";
89      }
90  
91      /** {@inheritDoc} */
92      protected String getArticleBlock()
93      {
94          return "";
95      }
96  
97      /** {@inheritDoc} */
98      protected String getNavigationBlock()
99      {
100         return "";
101     }
102 
103     /** {@inheritDoc} */
104     protected String getSidebarBlock()
105     {
106         return "<sidebar></sidebar>";
107     }
108 
109     /** {@inheritDoc} */
110     protected String getSectionTitleBlock( String title )
111     {
112         return "<title>" + title + "</title>";
113     }
114 
115     /** {@inheritDoc} */
116     protected String getSection1Block( String title )
117     {
118         return "<section><sectioninfo><title>" + title + "</title></sectioninfo></section>";
119     }
120 
121     /** {@inheritDoc} */
122     protected String getSection2Block( String title )
123     {
124         return "<section><sectioninfo><title>" + title + "</title></sectioninfo></section>";
125     }
126 
127     /** {@inheritDoc} */
128     protected String getSection3Block( String title )
129     {
130         return "<section><sectioninfo><title>" + title + "</title></sectioninfo></section>";
131     }
132 
133     /** {@inheritDoc} */
134     protected String getSection4Block( String title )
135     {
136         return "<section><sectioninfo><title>" + title + "</title></sectioninfo></section>";
137     }
138 
139     /** {@inheritDoc} */
140     protected String getSection5Block( String title )
141     {
142         return "<section><sectioninfo><title>" + title + "</title></sectioninfo></section>";
143     }
144 
145     /** {@inheritDoc} */
146     protected String getHeaderBlock()
147     {
148         return "<sectioninfo></sectioninfo>";
149     }
150 
151     /** {@inheritDoc} */
152     protected String getContentBlock()
153     {
154         return "";
155     }
156 
157     /** {@inheritDoc} */
158     protected String getFooterBlock()
159     {
160         return "";
161     }
162 
163     /** {@inheritDoc} */
164     protected String getListBlock( String item )
165     {
166         return "<itemizedlist><listitem><para>" + item  + "</para></listitem>" + "</itemizedlist>";
167     }
168 
169     /** {@inheritDoc} */
170     protected String getNumberedListBlock( String item )
171     {
172         return "<orderedlist numeration=\"lowerroman\"><listitem><para>"
173             + item  + "</para></listitem>" + "</orderedlist>";
174     }
175 
176     /** {@inheritDoc} */
177     protected String getDefinitionListBlock( String definum, String definition )
178     {
179         return "<variablelist><varlistentry><term>" + definum
180             + "</term>" + "<listitem><para>" + definition
181             + "</para></listitem>" + "</varlistentry>" + "</variablelist>";
182     }
183 
184     /** {@inheritDoc} */
185     protected String getFigureBlock( String source, String caption )
186     {
187         String format = FileUtils.extension( source ).toUpperCase( Locale.ENGLISH );
188         String figureBlock = "<mediaobject><imageobject>"
189                 + "<imagedata fileref=\"" + escapeHTML( source ) + "\" format=\"" + escapeHTML( format ) + "\" />"
190                 + "</imageobject>";
191         if ( caption != null )
192         {
193             figureBlock += "<caption><para>" + caption + "</para></caption>";
194         }
195         figureBlock += "</mediaobject>";
196         
197         return figureBlock;
198     }
199 
200     /** {@inheritDoc} */
201     protected String getTableBlock( String cell, String caption )
202     {
203         // Using the same set ordering than the JVM
204         MutableAttributeSet att = new SimpleAttributeSet();
205         att.addAttribute( "frame", "none" );
206         att.addAttribute( "rowsep", "0" );
207         att.addAttribute( "colsep", "0" );
208 
209         return "<table" + SinkUtils.getAttributeString( att ) + "><title>" + caption
210             + "</title>" + "<tgroup cols=\"1\"><colspec align=\"center\" />" + "<tbody><row><entry>"
211             + cell  + "</entry>" + "</row>" + "</tbody></tgroup>" + "</table>";
212     }
213 
214     /** {@inheritDoc} */
215     protected String getParagraphBlock( String text )
216     {
217         return "<para>" + text + "</para>";
218     }
219 
220     /** {@inheritDoc} */
221     protected String getDataBlock( String value, String text )
222     {
223         return text;
224     }
225 
226     /** {@inheritDoc} */
227     protected String getTimeBlock( String datetime, String text )
228     {
229         return text;
230     }
231 
232     /** {@inheritDoc} */
233     protected String getAddressBlock( String text )
234     {
235         return text;
236     }
237 
238     /** {@inheritDoc} */
239     protected String getBlockquoteBlock( String text )
240     {
241         return text;
242     }
243 
244     /** {@inheritDoc} */
245     protected String getDivisionBlock( String text )
246     {
247         return text;
248     }
249 
250     /** {@inheritDoc} */
251     protected String getVerbatimBlock( String text )
252     {
253         return "<programlisting>" + text + "</programlisting>";
254     }
255 
256     /** {@inheritDoc} */
257     protected String getHorizontalRuleBlock()
258     {
259         return "<!-- HR -->";
260     }
261 
262     /** {@inheritDoc} */
263     protected String getPageBreakBlock()
264     {
265         return "<!-- PB -->";
266     }
267 
268     /** {@inheritDoc} */
269     protected String getAnchorBlock( String anchor )
270     {
271         return "<anchor id=\"" + anchor + "\" />" + anchor + "<!-- anchor_end -->";
272     }
273 
274     /** {@inheritDoc} */
275     protected String getLinkBlock( String link, String text )
276     {
277         String linkend = DoxiaUtils.isInternalLink( link ) ? link.substring( 1 ) : link;
278         return "<link linkend=\"" + linkend + "\">" + text + "</link>";
279     }
280 
281     /** {@inheritDoc} */
282     protected String getInlineBlock( String text )
283     {
284         return text;
285     }
286 
287     /** {@inheritDoc} */
288     protected String getInlineItalicBlock( String text )
289     {
290         return "<emphasis>" + text + "</emphasis>";
291     }
292 
293     /** {@inheritDoc} */
294     protected String getInlineBoldBlock( String text )
295     {
296         return "<emphasis role=\"bold\">" + text + "</emphasis>";
297     }
298 
299     /** {@inheritDoc} */
300     protected String getInlineCodeBlock( String text )
301     {
302         return "<literal>" + text + "</literal>";
303     }
304 
305     /** {@inheritDoc} */
306     protected String getItalicBlock( String text )
307     {
308         return "<emphasis>" + text + "</emphasis>";
309     }
310 
311     /** {@inheritDoc} */
312     protected String getBoldBlock( String text )
313     {
314         return "<emphasis role=\"bold\">" + text + "</emphasis>";
315     }
316 
317     /** {@inheritDoc} */
318     protected String getMonospacedBlock( String text )
319     {
320         return "<literal>" + text + "</literal>";
321     }
322 
323     /** {@inheritDoc} */
324     protected String getLineBreakBlock()
325     {
326         return "<!-- LB -->";
327     }
328 
329     /** {@inheritDoc} */
330     protected String getLineBreakOpportunityBlock()
331     {
332         return "";
333     }
334 
335     /** {@inheritDoc} */
336     protected String getNonBreakingSpaceBlock()
337     {
338         return "&#x00A0;";
339     }
340 
341     /** {@inheritDoc} */
342     protected String getTextBlock( String text )
343     {
344         // TODO: retreive those from the sink
345         return "~,_=,_-,_+,_*,_[,_],_&lt;,_&gt;,_{,_},_\\";
346     }
347 
348     /** {@inheritDoc} */
349     protected String getRawTextBlock( String text )
350     {
351         // TODO
352         return "";
353     }
354 
355     /** {@inheritDoc} */
356     protected String getCommentBlock( String text )
357     {
358         return "<!--" + toXmlComment( text ) + "-->";
359     }
360 }