001package org.apache.maven.doxia.module.xdoc;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 *   http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import org.apache.maven.doxia.sink.Sink;
023import org.apache.maven.doxia.sink.AbstractSinkTest;
024import org.apache.maven.doxia.util.HtmlTools;
025
026import java.io.StringWriter;
027import java.io.Writer;
028import org.apache.maven.doxia.sink.SinkEventAttributeSet;
029
030/**
031 * @author <a href="mailto:jason@maven.org">Jason van Zyl</a>
032 * @version $Id$
033 * @since 1.0
034 */
035public class XdocSinkTest
036    extends AbstractSinkTest
037{
038    /** {@inheritDoc} */
039    protected String outputExtension()
040    {
041        return "xml";
042    }
043
044    /** {@inheritDoc} */
045    protected Sink createSink( Writer writer )
046    {
047        return new XdocSink( writer, "UTF-8" );
048    }
049
050    /** {@inheritDoc} */
051    protected boolean isXmlSink()
052    {
053        return true;
054    }
055
056    /** {@inheritDoc} */
057    protected String getTitleBlock( String title )
058    {
059        return "<title>" + title + "</title>";
060    }
061
062    /** {@inheritDoc} */
063    protected String getAuthorBlock( String author )
064    {
065        return author;
066    }
067
068    /** {@inheritDoc} */
069    protected String getDateBlock( String date )
070    {
071        return date;
072    }
073
074    /** {@inheritDoc} */
075    protected String getHeadBlock()
076    {
077        return "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
078            + "<document xmlns=\"http://maven.apache.org/XDOC/2.0\" "
079            + "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" "
080            + "xsi:schemaLocation=\"http://maven.apache.org/XDOC/2.0 http://maven.apache.org/xsd/xdoc-2.0.xsd\">"
081            + "<properties></properties>";
082    }
083
084    /** {@inheritDoc} */
085    protected String getBodyBlock()
086    {
087        return "<body></body></document>";
088    }
089
090    /** {@inheritDoc} */
091    protected String getSectionTitleBlock( String title )
092    {
093        return title;
094    }
095
096    /** {@inheritDoc} */
097    protected String getSection1Block( String title )
098    {
099        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}