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  
24  import java.util.Locale;
25  
26  import javax.swing.text.MutableAttributeSet;
27  import javax.swing.text.SimpleAttributeSet;
28  
29  import org.apache.maven.doxia.sink.AbstractSinkTest;
30  import org.apache.maven.doxia.sink.Sink;
31  import org.apache.maven.doxia.sink.SinkUtils;
32  import org.apache.maven.doxia.util.DoxiaUtils;
33  
34  import org.codehaus.plexus.util.FileUtils;
35  
36  /**
37   * @author <a href="mailto:jason@maven.org">Jason van Zyl</a>
38   * @version $Id: DocBookSinkTest.java 1467294 2013-04-12 14:51:42Z rfscholte $
39   */
40  public class DocBookSinkTest extends AbstractSinkTest
41  {
42      /** {@inheritDoc} */
43      protected String outputExtension()
44      {
45          return "docbook";
46      }
47  
48      /** {@inheritDoc} */
49      protected Sink createSink( Writer writer )
50      {
51          return new DocBookSink( writer, "UTF-8" );
52      }
53  
54      /** {@inheritDoc} */
55      protected boolean isXmlSink()
56      {
57          return true;
58      }
59  
60      /** {@inheritDoc} */
61      protected String getTitleBlock( String title )
62      {
63          return "<articleinfo><title>" + title + "</title>";
64      }
65  
66      /** {@inheritDoc} */
67      protected String getAuthorBlock( String author )
68      {
69          return "<corpauthor>" + author + "</corpauthor>";
70      }
71  
72      /** {@inheritDoc} */
73      protected String getDateBlock( String date )
74      {
75          return "<date>" + date + "</date>";
76      }
77  
78      /** {@inheritDoc} */
79      protected String getHeadBlock()
80      {
81          return "<?xml version=\"1.0\" encoding=\"UTF-8\"?><!DOCTYPE article PUBLIC \""
82                  + SimplifiedDocbookMarkup.DEFAULT_XML_PUBLIC_ID + "\" "
83                  + "\"" + SimplifiedDocbookMarkup.DEFAULT_XML_SYSTEM_ID + "\"><article>";
84      }
85  
86      /** {@inheritDoc} */
87      protected String getBodyBlock()
88      {
89          return "</article>";
90      }
91  
92      /** {@inheritDoc} */
93      protected String getSectionTitleBlock( String title )
94      {
95          return "<title>" + title + "</title>";
96      }
97  
98      /** {@inheritDoc} */
99      protected String getSection1Block( String title )
100     {
101         return "<section><title>" + title + "</title>" + "</section>";
102     }
103 
104     /** {@inheritDoc} */
105     protected String getSection2Block( String title )
106     {
107         return "<section><title>" + title + "</title>" + "</section>";
108     }
109 
110     /** {@inheritDoc} */
111     protected String getSection3Block( String title )
112     {
113         return "<section><title>" + title + "</title>" + "</section>";
114     }
115 
116     /** {@inheritDoc} */
117     protected String getSection4Block( String title )
118     {
119         return "<section><title>" + title + "</title>" + "</section>";
120     }
121 
122     /** {@inheritDoc} */
123     protected String getSection5Block( String title )
124     {
125         return "<section><title>" + title + "</title>" + "</section>";
126     }
127 
128     /** {@inheritDoc} */
129     protected String getListBlock( String item )
130     {
131         return "<itemizedlist><listitem><para>" + item  + "</para></listitem>" + "</itemizedlist>";
132     }
133 
134     /** {@inheritDoc} */
135     protected String getNumberedListBlock( String item )
136     {
137         return "<orderedlist numeration=\"lowerroman\"><listitem><para>"
138             + item  + "</para></listitem>" + "</orderedlist>";
139     }
140 
141     /** {@inheritDoc} */
142     protected String getDefinitionListBlock( String definum, String definition )
143     {
144         return "<variablelist><varlistentry><term>" + definum
145             + "</term>" + "<listitem><para>" + definition
146             + "</para></listitem>" + "</varlistentry>" + "</variablelist>";
147     }
148 
149     /** {@inheritDoc} */
150     protected String getFigureBlock( String source, String caption )
151     {
152         String format = FileUtils.extension( source ).toUpperCase( Locale.ENGLISH );
153         String figureBlock = "<mediaobject><imageobject>"
154                 + "<imagedata fileref=\"" + source + "\" format=\"" + format + "\" />"
155                 + "</imageobject>";
156         if ( caption != null )
157         {
158             figureBlock += "<caption><para>" + caption + "</para></caption>";
159         }
160         figureBlock += "</mediaobject>";
161         
162         return figureBlock;
163     }
164 
165     /** {@inheritDoc} */
166     protected String getTableBlock( String cell, String caption )
167     {
168         // Using the same set ordering than the JVM
169         MutableAttributeSet att = new SimpleAttributeSet();
170         att.addAttribute( "frame", "none" );
171         att.addAttribute( "rowsep", "0" );
172         att.addAttribute( "colsep", "0" );
173 
174         return "<table" + SinkUtils.getAttributeString( att ) + "><title>" + caption
175             + "</title>" + "<tgroup cols=\"1\"><colspec align=\"center\" />" + "<tbody><row><entry>"
176             + cell  + "</entry>" + "</row>" + "</tbody></tgroup>" + "</table>";
177     }
178 
179     /** {@inheritDoc} */
180     protected String getParagraphBlock( String text )
181     {
182         return "<para>" + text + "</para>";
183     }
184 
185     /** {@inheritDoc} */
186     protected String getVerbatimBlock( String text )
187     {
188         return "<programlisting>" + text + "</programlisting>";
189     }
190 
191     /** {@inheritDoc} */
192     protected String getHorizontalRuleBlock()
193     {
194         return "<!-- HR -->";
195     }
196 
197     /** {@inheritDoc} */
198     protected String getPageBreakBlock()
199     {
200         return "<!-- PB -->";
201     }
202 
203     /** {@inheritDoc} */
204     protected String getAnchorBlock( String anchor )
205     {
206         return "<anchor id=\"" + anchor + "\" />" + anchor + "<!-- anchor_end -->";
207     }
208 
209     /** {@inheritDoc} */
210     protected String getLinkBlock( String link, String text )
211     {
212         String linkend = DoxiaUtils.isInternalLink( link ) ? link.substring( 1 ) : link;
213         return "<link linkend=\"" + linkend + "\">" + text + "</link>";
214     }
215 
216     /** {@inheritDoc} */
217     protected String getItalicBlock( String text )
218     {
219         return "<emphasis>" + text + "</emphasis>";
220     }
221 
222     /** {@inheritDoc} */
223     protected String getBoldBlock( String text )
224     {
225         return "<emphasis role=\"bold\">" + text + "</emphasis>";
226     }
227 
228     /** {@inheritDoc} */
229     protected String getMonospacedBlock( String text )
230     {
231         return "<literal>" + text + "</literal>";
232     }
233 
234     /** {@inheritDoc} */
235     protected String getLineBreakBlock()
236     {
237         return "<!-- LB -->";
238     }
239 
240     /** {@inheritDoc} */
241     protected String getNonBreakingSpaceBlock()
242     {
243         return "&#x00A0;";
244     }
245 
246     /** {@inheritDoc} */
247     protected String getTextBlock( String text )
248     {
249         // TODO: retreive those from the sink
250         return "~,_=,_-,_+,_*,_[,_],_&lt;,_&gt;,_{,_},_\\";
251     }
252 
253     /** {@inheritDoc} */
254     protected String getRawTextBlock( String text )
255     {
256         // TODO
257         return "";
258     }
259 
260     /** {@inheritDoc} */
261     protected String getCommentBlock( String text )
262     {
263         return "<!-- Simple comment with - - - - -->";
264     }
265 }